add GetCompatibleDeviceType NDK

Signed-off-by: ming-yue-liu1 <liumingyue12@huawei.com>
Change-Id: I0825528f5022af094ced9d5f5e062559b3f3999e
This commit is contained in:
ming-yue-liu1 2024-10-24 11:11:00 +08:00
parent 1828f432c6
commit 51e423d9ee
20 changed files with 303 additions and 2 deletions

View File

@ -188,7 +188,9 @@ enum class BundleMgrInterfaceCode : uint32_t {
GET_CONTINUE_BUNDLE_NAMES,
GET_LAUNCH_WANT,
UPDATE_APP_ENCRYPTED_KEY_STATUS,
IS_BUNDLE_INSTALLED
IS_BUNDLE_INSTALLED,
GET_COMPATIBLED_DEVICE_TYPE_NATIVE,
GET_COMPATIBLED_DEVICE_TYPE
};
/* SAID: 401-85 Interface No.85 subservice also provides the following interfaces */

View File

@ -839,6 +839,10 @@ private:
ErrCode HandleIsBundleInstalled(MessageParcel &data, MessageParcel &reply);
ErrCode HandleGetCompatibleDeviceTypeNative(MessageParcel &data, MessageParcel &reply);
ErrCode HandleGetCompatibleDeviceType(MessageParcel &data, MessageParcel &reply);
private:
/**
* @brief Write a parcelabe vector objects to the proxy node.

View File

@ -1599,6 +1599,16 @@ public:
{
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
virtual ErrCode GetCompatibleDeviceTypeNative(std::string &deviceType)
{
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
virtual ErrCode GetCompatibleDeviceType(const std::string &bundleName, std::string &deviceType)
{
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
};
#define WRITE_PARCEL(func) \

View File

@ -1115,6 +1115,8 @@ public:
virtual ErrCode IsBundleInstalled(const std::string &bundleName, int32_t userId,
int32_t appIndex, bool &isInstalled) override;
virtual ErrCode GetCompatibleDeviceType(const std::string &bundleName, std::string &deviceType) override;
private:
/**
* @brief Send a command message from the proxy object.

View File

@ -606,6 +606,12 @@ int BundleMgrHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessagePa
case static_cast<uint32_t>(BundleMgrInterfaceCode::IS_BUNDLE_INSTALLED):
errCode = HandleIsBundleInstalled(data, reply);
break;
case static_cast<uint32_t>(BundleMgrInterfaceCode::GET_COMPATIBLED_DEVICE_TYPE_NATIVE):
errCode = HandleGetCompatibleDeviceTypeNative(data, reply);
break;
case static_cast<uint32_t>(BundleMgrInterfaceCode::GET_COMPATIBLED_DEVICE_TYPE):
errCode = HandleGetCompatibleDeviceType(data, reply);
break;
default :
APP_LOGW("bundleMgr host receives unknown code %{public}u", code);
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
@ -4120,5 +4126,38 @@ ErrCode BundleMgrHost::HandleIsBundleInstalled(MessageParcel &data, MessageParce
}
return ERR_OK;
}
ErrCode BundleMgrHost::HandleGetCompatibleDeviceTypeNative(MessageParcel &data, MessageParcel &reply)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
std::string deviceType;
auto ret = GetCompatibleDeviceTypeNative(deviceType);
if (!reply.WriteInt32(ret)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!reply.WriteString(deviceType)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
return ERR_OK;
}
ErrCode BundleMgrHost::HandleGetCompatibleDeviceType(MessageParcel &data, MessageParcel &reply)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
std::string bundleName = data.ReadString();
std::string deviceType;
auto ret = GetCompatibleDeviceType(bundleName, deviceType);
if (!reply.WriteInt32(ret)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!reply.WriteString(deviceType)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
return ERR_OK;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -5337,5 +5337,31 @@ ErrCode BundleMgrProxy::IsBundleInstalled(const std::string &bundleName, int32_t
isInstalled = reply.ReadBool();
return ERR_OK;
}
ErrCode BundleMgrProxy::GetCompatibleDeviceType(const std::string &bundleName, std::string &deviceType)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
MessageParcel data;
if (!data.WriteInterfaceToken(GetDescriptor())) {
APP_LOGE("Write interface token fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!data.WriteString(bundleName)) {
APP_LOGE("Write bundle name fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
MessageParcel reply;
if (!SendTransactCmd(BundleMgrInterfaceCode::GET_COMPATIBLED_DEVICE_TYPE, data, reply)) {
APP_LOGE("Fail to IsBundleInstalled from server");
return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
}
auto ret = reply.ReadInt32();
if (ret == ERR_OK) {
deviceType = reply.ReadString();
}
APP_LOGD("GetCompatibleDeviceType: ret: %{public}d, device type: %{public}s", ret, deviceType.c_str());
return ret;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -46,6 +46,7 @@ ohos_shared_library("bundlemgr_extension") {
"appverify:libhapverify",
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_single",
"relational_store:native_rdb",
]

View File

@ -75,6 +75,7 @@ public:
void CheckBundleNameAndStratAbility(const std::string &bundleName, const std::string &appIdentifier);
bool DetermineCloneNum(const std::string &bundleName, const std::string &appIdentifier, int32_t &cloneNum);
std::string GetCompatibleDeviceType(const std::string &bundleName);
private:
bool OpenHandler();
static BmsExtension bmsExtension_;

View File

@ -153,6 +153,10 @@ public:
{
return false;
}
virtual std::string GetCompatibleDeviceType(const std::string &bundleName)
{
return "";
}
};
} // AppExecFwk

View File

@ -20,6 +20,7 @@
#include "bms_extension_data_mgr.h"
#include "bms_extension_profile.h"
#include "bundle_mgr_ext_register.h"
#include "parameter.h"
namespace OHOS {
namespace AppExecFwk {
@ -553,5 +554,20 @@ bool BmsExtensionDataMgr::DetermineCloneNum(
}
return bundleMgrExtPtr->DetermineCloneNum(bundleName, appIdentifier, cloneNum);
}
std::string BmsExtensionDataMgr::GetCompatibleDeviceType(const std::string &bundleName)
{
if ((Init() == ERR_OK) && handler_) {
auto bundleMgrExtPtr =
BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
if (bundleMgrExtPtr) {
return bundleMgrExtPtr->GetCompatibleDeviceType(bundleName);
}
APP_LOGE("create class: %{public}s failed", bmsExtension_.bmsExtensionBundleMgr.extensionName.c_str());
return "";
}
APP_LOGW("access bms-extension failed");
return GetDeviceType();
}
} // AppExecFwk
} // OHOS

View File

@ -40,8 +40,11 @@ public:
*/
bool GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo);
bool GetCompatibleDeviceTypeNative(std::string &deviceType);
enum {
GET_BUNDLE_INFO_FOR_SELF_NATIVE = 98
GET_BUNDLE_INFO_FOR_SELF_NATIVE = 98,
GET_COMPATIBLED_DEVICE_TYPE_NATIVE = 166
};
private:
sptr<IRemoteObject> GetBmsProxy();

View File

@ -146,6 +146,20 @@ char* OH_NativeBundle_GetAppIdentifier();
* @version 1.0
*/
OH_NativeBundle_ElementName OH_NativeBundle_GetMainElementName();
/**
* @brief Obtains the compatible device type of the current application.
* After utilizing this interface, to prevent memory leaks,
* it is necessary to manually release the pointer returned by the interface.
*
* @return Returns the newly created string that indicates the compatible device type,
* if the returned object is NULL, it indicates creation failure.
* The possible cause of failure could be that the application address space is full,
* leading to space allocation failure.
* @since 14
* @version 1.0
*/
char* OH_NativeBundle_GetCompatibleDeviceType();
#ifdef __cplusplus
};
#endif

View File

@ -14,5 +14,9 @@
{
"first_introduced": "13",
"name": "OH_NativeBundle_GetMainElementName"
},
{
"first_introduced": "14",
"name": "OH_NativeBundle_GetCompatibleDeviceType"
}
]

View File

@ -58,6 +58,29 @@ bool BundleMgrProxyNative::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundl
return true;
}
bool BundleMgrProxyNative::GetCompatibleDeviceTypeNative(std::string &deviceType)
{
LOG_I(BMS_TAG_QUERY, "begin to get compatible device type");
MessageParcel data;
if (!data.WriteInterfaceToken(BMS_PROXY_INTERFACE_TOKEN)) {
LOG_E(BMS_TAG_QUERY, "Write interfaceToken failed");
return false;
}
MessageParcel reply;
if (!SendTransactCmd(GET_COMPATIBLED_DEVICE_TYPE_NATIVE, data, reply)) {
return false;
}
int32_t res = reply.ReadInt32();
if (res != NO_ERROR) {
APP_LOGE("reply result failed");
return false;
}
deviceType = reply.ReadString();
APP_LOGD("get compatible device type success");
return true;
}
bool BundleMgrProxyNative::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply)
{
MessageOption option(MessageOption::TF_SYNC);

View File

@ -248,3 +248,29 @@ OH_NativeBundle_ElementName OH_NativeBundle_GetMainElementName()
}
return elementName;
}
char* OH_NativeBundle_GetCompatibleDeviceType()
{
OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
std::string deviceType;
if (!bundleMgrProxyNative.GetCompatibleDeviceTypeNative(deviceType)) {
APP_LOGE("can not get compatible device type");
return nullptr;
}
if (deviceType.size() + 1 > CHAR_MAX_LENGTH) {
APP_LOGE("failed due to the length of device type is too long");
return nullptr;
}
char* deviceTypeC = static_cast<char*>(malloc(deviceType.size() + 1));
if (deviceTypeC == nullptr) {
APP_LOGE("failed due to malloc error");
return nullptr;
}
if (strcpy_s(deviceTypeC, deviceType.size() + 1, deviceType.c_str()) != EOK) {
APP_LOGE("failed due to strcpy_s error");
free(deviceTypeC);
return nullptr;
}
APP_LOGI("OH_NativeBundle_GetCompatibleDeviceType success");
return deviceTypeC;
}

View File

@ -1036,6 +1036,9 @@ public:
virtual ErrCode IsBundleInstalled(const std::string &bundleName, int32_t userId,
int32_t appIndex, bool &isInstalled) override;
virtual ErrCode GetCompatibleDeviceTypeNative(std::string &deviceType) override;
virtual ErrCode GetCompatibleDeviceType(const std::string &bundleName, std::string &deviceType) override;
private:
const std::shared_ptr<BundleDataMgr> GetDataMgrFromService();
#ifdef DISTRIBUTED_BUNDLE_FRAMEWORK

View File

@ -20,6 +20,7 @@
#include "app_mgr_interface.h"
#include "aot/aot_handler.h"
#include "bms_extension_client.h"
#include "bms_extension_data_mgr.h"
#include "bundle_parser.h"
#include "bundle_permission_mgr.h"
#ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
@ -4485,5 +4486,44 @@ ErrCode BundleMgrHostImpl::IsBundleInstalled(const std::string &bundleName, int3
}
return dataMgr->IsBundleInstalled(bundleName, userId, appIndex, isInstalled);
}
ErrCode BundleMgrHostImpl::GetCompatibleDeviceTypeNative(std::string &deviceType)
{
APP_LOGD("start GetCompatibleDeviceTypeNative");
auto dataMgr = GetDataMgrFromService();
if (dataMgr == nullptr) {
APP_LOGE("DataMgr is nullptr");
return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
}
std::string bundleName;
dataMgr->GetBundleNameForUid(IPCSkeleton::GetCallingUid(), bundleName);
BmsExtensionDataMgr bmsExtensionDataMgr;
deviceType = bmsExtensionDataMgr.GetCompatibleDeviceType(bundleName);
APP_LOGI("deviceType : %{public}s", deviceType.c_str());
return ERR_OK;
}
ErrCode BundleMgrHostImpl::GetCompatibleDeviceType(const std::string &bundleName, std::string &deviceType)
{
APP_LOGD("start GetCompatibleDeviceType");
if (!BundlePermissionMgr::IsSystemApp()) {
APP_LOGE("non-system app calling system api");
return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
}
if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
APP_LOGE("Verify permission failed");
return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
}
auto dataMgr = GetDataMgrFromService();
if (dataMgr == nullptr) {
APP_LOGE("DataMgr is nullptr");
return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
}
BmsExtensionDataMgr bmsExtensionDataMgr;
deviceType = bmsExtensionDataMgr.GetCompatibleDeviceType(bundleName);
APP_LOGI("deviceType : %{public}s", deviceType.c_str());
return ERR_OK;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -330,6 +330,8 @@ namespace OHOS {
bundleMgrProxy.GetAllDesktopShortcutInfo(reinterpret_cast<uintptr_t>(data), shortcutInfos);
bundleMgrProxy.GetOdidByBundleName(bundleName, odid);
bundleMgrProxy.GetBundleInfosForContinuation(0, bundleInfos, reinterpret_cast<uintptr_t>(data));
std::string deviceType;
bundleMgrProxy.GetCompatibleDeviceType(bundleName, deviceType);
return true;
}
}

View File

@ -9689,5 +9689,56 @@ HWTEST_F(ActsBmsKitSystemTest, IsBundleInstalled_0002, Function | MediumTest | L
EXPECT_EQ(uninstallResult, "Success") << "uninstall fail!";
std::cout << "END IsBundleInstalled_0002" << std::endl;
}
/**
* @tc.number: GetCompatibleDeviceType_0001
* @tc.name: test GetCompatibleDeviceType interface
* @tc.desc: 1.under '/data/test/bms_bundle',there is a hap
* 2.install the app
* 3.call GetCompatibleDeviceType
*/
HWTEST_F(ActsBmsKitSystemTest, GetCompatibleDeviceType_0001, Function | MediumTest | Level1)
{
std::cout << "START GetCompatibleDeviceType_0001" << std::endl;
std::vector<std::string> resvec;
std::string bundleFilePath = THIRD_BUNDLE_PATH + "bundleClient1.hap";
std::string appName = "com.example.ohosproject.hmservice";
Install(bundleFilePath, InstallFlag::REPLACE_EXISTING, resvec);
CommonTool commonTool;
std::string installResult = commonTool.VectorToStr(resvec);
EXPECT_EQ(installResult, "Success") << "install fail!";
sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
ASSERT_NE(bundleMgrProxy, nullptr);
std::string deviceType;
auto queryResult = bundleMgrProxy->GetCompatibleDeviceType(appName, deviceType);
EXPECT_EQ(queryResult, ERR_OK);
resvec.clear();
Uninstall(appName, resvec);
std::string uninstallResult = commonTool.VectorToStr(resvec);
EXPECT_EQ(uninstallResult, "Success") << "uninstall fail!";
std::cout << "END GetCompatibleDeviceType_0001" << std::endl;
}
/**
* @tc.number: GetCompatibleDeviceType_0002
* @tc.name: test GetCompatibleDeviceType interface
* @tc.desc: GetCompatibleDeviceType failed for calling bundle name is invalid
*/
HWTEST_F(ActsBmsKitSystemTest, GetCompatibleDeviceType_0002, Function | MediumTest | Level1)
{
std::cout << "START GetCompatibleDeviceType_0002" << std::endl;
sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
ASSERT_NE(bundleMgrProxy, nullptr);
std::string deviceType;
auto queryResult = bundleMgrProxy->GetCompatibleDeviceType("", deviceType);
EXPECT_EQ(queryResult, ERR_OK);
std::cout << "END GetCompatibleDeviceType_0002" << std::endl;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -1933,5 +1933,35 @@ HWTEST_F(BmsBundleMgrHostTest, HandleIsBundleInstalled_0001, Function | MediumTe
ErrCode res = bundleMgrHost.HandleIsBundleInstalled(data, reply);
EXPECT_EQ(res, ERR_OK);
}
/**
* @tc.number: HandleGetCompatibleDeviceTypeNative_0001
* @tc.name: test the HandleGetCompatibleDeviceTypeNative
* @tc.desc: 1. system running normally
* 2. test HandleGetCompatibleDeviceTypeNative
*/
HWTEST_F(BmsBundleMgrHostTest, HandleGetCompatibleDeviceTypeNative_0001, Function | MediumTest | Level1)
{
BundleMgrHost bundleMgrHost;
MessageParcel data;
MessageParcel reply;
ErrCode res = bundleMgrHost.HandleGetCompatibleDeviceTypeNative(data, reply);
EXPECT_EQ(res, ERR_OK);
}
/**
* @tc.number: HandleGetCompatibleDeviceType_0001
* @tc.name: test the HandleGetCompatibleDeviceType
* @tc.desc: 1. system running normally
* 2. test HandleGetCompatibleDeviceType
*/
HWTEST_F(BmsBundleMgrHostTest, HandleGetCompatibleDeviceType_0001, Function | MediumTest | Level1)
{
BundleMgrHost bundleMgrHost;
MessageParcel data;
MessageParcel reply;
ErrCode res = bundleMgrHost.HandleGetCompatibleDeviceType(data, reply);
EXPECT_EQ(res, ERR_OK);
}
} // AppExecFwk
} // OHOS