diff --git a/BUILD.gn b/BUILD.gn index 4b01bbc31..2ed5652f6 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -19,6 +19,7 @@ group("bms_target") { "common:common_target", "interfaces/inner_api/appexecfwk_base:appexecfwk_base", "interfaces/inner_api/appexecfwk_core:appexecfwk_core", + "interfaces/inner_api/appexecfwk_core:bundlemgr_mini", "interfaces/inner_api/bundlemgr_extension:bundlemgr_extension", "interfaces/kits/cj:cj_bundle_manager_ffi", "interfaces/kits/js:napi_packages", diff --git a/bundle.json b/bundle.json index aa564dd39..5ce3ac772 100644 --- a/bundle.json +++ b/bundle.json @@ -130,6 +130,15 @@ }, "name": "//foundation/bundlemanager/bundle_framework/interfaces/inner_api/appexecfwk_core:appexecfwk_core" }, + { + "header": { + "header_base": "//foundation/bundlemanager/bundle_framework/interfaces/inner_api/appexecfwk_core/include", + "header_files": [ + "bundlemgr/bundle_mgr_mini_proxy.h" + ] + }, + "name": "//foundation/bundlemanager/bundle_framework/interfaces/inner_api/appexecfwk_core:bundlemgr_mini" + }, { "header": { "header_base": "//foundation/bundlemanager/bundle_framework/interfaces/inner_api/bundlemgr_extension/include", diff --git a/interfaces/inner_api/appexecfwk_core/BUILD.gn b/interfaces/inner_api/appexecfwk_core/BUILD.gn index 74f8e8560..cb416bc59 100644 --- a/interfaces/inner_api/appexecfwk_core/BUILD.gn +++ b/interfaces/inner_api/appexecfwk_core/BUILD.gn @@ -154,3 +154,54 @@ ohos_shared_library("appexecfwk_core") { ] part_name = "bundle_framework" } + +ohos_shared_library("bundlemgr_mini") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + sources = [ "src/bundlemgr/bundle_mgr_mini_proxy.cpp" ] + + public_configs = [ + ":appexecfwk_core_config", + ":bundlemgr_sdk_config", + ] + + defines = [ + "APP_LOG_TAG = \"BMS\"", + "LOG_DOMAIN = 0xD001120", + ] + cflags = [ "-Os" ] + cflags_cc = [ "-Os" ] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${base_path}:appexecfwk_base", + "${common_path}:libappexecfwk_common", + ] + + external_deps = [ + "c_utils:utils", + "common_event_service:cesfwk_core", + "common_event_service:cesfwk_innerkits", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + public_external_deps = [ + "ability_base:want", + "common_event_service:cesfwk_innerkits", + ] + + subsystem_name = "bundlemanager" + innerapi_tags = [ + "platformsdk", + "sasdk", + ] + part_name = "bundle_framework" +} diff --git a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_mini_proxy.h b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_mini_proxy.h new file mode 100644 index 000000000..8042d465f --- /dev/null +++ b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_mini_proxy.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_MINI_PROXY_H +#define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_MINI_PROXY_H + +#include + +#include "bundle_framework_core_ipc_interface_code.h" +#include "bundle_mgr_interface.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace AppExecFwk { + +class BundleMgrMiniProxy : public IRemoteProxy { +public: + explicit BundleMgrMiniProxy(const sptr &impl); + virtual ~BundleMgrMiniProxy() override; + + /** + * @brief Obtains the formal name associated with the given UID. + * @param uid Indicates the uid. + * @param name Indicates the obtained formal name. + * @return Returns ERR_OK if execute success; returns errCode otherwise. + */ + virtual ErrCode GetNameForUid(const int uid, std::string &name) override; + + /** + * @brief Obtains the application ID based on the given bundle name and user ID. + * @param bundleName Indicates the bundle name of the application. + * @param userId Indicates the user ID. + * @return Returns the application ID if successfully obtained; returns empty string otherwise. + */ + virtual std::string GetAppIdByBundleName(const std::string &bundleName, const int userId) override; + + /** + * @brief Obtains the application UID based on the given bundle name and user ID through the proxy object. + * @param bundleName Indicates the bundle name of the application. + * @param userId Indicates the user ID. + * @return Returns the uid if successfully obtained; returns -1 otherwise. + */ + + virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override; + + /** + * @brief Obtains the application UID based on the given bundle name and user ID through the proxy object. + * @param bundleName Indicates the bundle name of the application. + * @param userId Indicates the user ID. + * @param userId Indicates the app Index. + * @return Returns the uid if successfully obtained; returns -1 otherwise. + */ + virtual int32_t GetUidByBundleName(const std::string &bundleName, const int32_t userId, int32_t appIndex) override; + +private: + /** + * @brief Send a command message from the proxy object. + * @param code Indicates the message code to be sent. + * @param data Indicates the objects to be sent. + * @param reply Indicates the reply to be sent; + * @return Returns true if message send successfully; returns false otherwise. + */ + bool SendTransactCmd(BundleMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply); + + /** + * @brief Send a command message from the proxy object and printf log. + * @param code Indicates the message code to be sent. + * @param data Indicates the objects to be sent. + * @param reply Indicates the reply to be sent; + * @return Returns true if message send successfully; returns false otherwise. + */ + bool SendTransactCmdWithLog(BundleMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply); +}; + +} // namespace AppExecFwk +} // namespace OHOS +#endif // FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_MINI_PROXY_H \ No newline at end of file diff --git a/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_mini_proxy.cpp b/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_mini_proxy.cpp new file mode 100644 index 000000000..146001935 --- /dev/null +++ b/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_mini_proxy.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bundle_mgr_mini_proxy.h" + +#include "app_log_wrapper.h" +#include "appexecfwk_errors.h" +#include "hitrace_meter.h" + +namespace OHOS { +namespace AppExecFwk { +BundleMgrMiniProxy::BundleMgrMiniProxy(const sptr &impl) : IRemoteProxy(impl) +{ + APP_LOGD("create BundleMgrMiniProxy"); +} + +BundleMgrMiniProxy::~BundleMgrMiniProxy() +{ + APP_LOGD("destroy ~BundleMgrMiniProxy"); +} + +ErrCode BundleMgrMiniProxy::GetNameForUid(const int uid, std::string &name) +{ + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + APP_LOGD("begin to GetNameForUid of %{public}d", uid); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + APP_LOGE("fail to GetNameForUid due to write InterfaceToken fail"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + if (!data.WriteInt32(uid)) { + APP_LOGE("fail to GetNameForUid due to write uid fail"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + MessageParcel reply; + if (!SendTransactCmdWithLog(BundleMgrInterfaceCode::GET_NAME_FOR_UID, data, reply)) { + APP_LOGE("fail to GetNameForUid from server"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + ErrCode ret = reply.ReadInt32(); + if (ret != ERR_OK) { + return ret; + } + name = reply.ReadString(); + return ERR_OK; +} + +std::string BundleMgrMiniProxy::GetAppIdByBundleName(const std::string &bundleName, const int userId) +{ + if (bundleName.empty()) { + APP_LOGE("failed to GetAppIdByBundleName due to bundleName empty"); + return Constants::EMPTY_STRING; + } + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + APP_LOGD("begin to get appId of %{public}s", bundleName.c_str()); + + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + APP_LOGE("failed to GetAppIdByBundleName due to write InterfaceToken fail"); + return Constants::EMPTY_STRING; + } + if (!data.WriteString(bundleName)) { + APP_LOGE("failed to GetAppIdByBundleName due to write bundleName fail"); + return Constants::EMPTY_STRING; + } + if (!data.WriteInt32(userId)) { + APP_LOGE("failed to GetAppIdByBundleName due to write uid fail"); + return Constants::EMPTY_STRING; + } + + MessageParcel reply; + if (!SendTransactCmd(BundleMgrInterfaceCode::GET_APPID_BY_BUNDLE_NAME, data, reply)) { + APP_LOGE("failed to GetAppIdByBundleName from server"); + return Constants::EMPTY_STRING; + } + std::string appId = reply.ReadString(); + APP_LOGD("appId is %{private}s", appId.c_str()); + return appId; +} + +int BundleMgrMiniProxy::GetUidByBundleName(const std::string &bundleName, const int userId) +{ + return GetUidByBundleName(bundleName, userId, 0); +} + +int32_t BundleMgrMiniProxy::GetUidByBundleName(const std::string &bundleName, const int32_t userId, int32_t appIndex) +{ + if (bundleName.empty()) { + APP_LOGE("failed to GetUidByBundleName due to bundleName empty"); + return Constants::INVALID_UID; + } + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + APP_LOGD("begin to get uid of %{public}s, userId : %{public}d, appIndex : %{public}d", bundleName.c_str(), + userId, appIndex); + + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + APP_LOGE("failed to GetUidByBundleName due to write InterfaceToken fail"); + return Constants::INVALID_UID; + } + if (!data.WriteString(bundleName)) { + APP_LOGE("failed to GetUidByBundleName due to write bundleName fail"); + return Constants::INVALID_UID; + } + if (!data.WriteInt32(userId)) { + APP_LOGE("failed to GetUidByBundleName due to write uid fail"); + return Constants::INVALID_UID; + } + if (!data.WriteInt32(appIndex)) { + APP_LOGE("failed to GetUidByBundleName due to write uid fail"); + return Constants::INVALID_UID; + } + + MessageParcel reply; + if (!SendTransactCmd(BundleMgrInterfaceCode::GET_UID_BY_BUNDLE_NAME, data, reply)) { + APP_LOGE("failed to GetUidByBundleName from server"); + return Constants::INVALID_UID; + } + int32_t uid = reply.ReadInt32(); + APP_LOGD("uid is %{public}d", uid); + return uid; +} + +bool BundleMgrMiniProxy::SendTransactCmd(BundleMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply) +{ + MessageOption option(MessageOption::TF_SYNC); + + sptr remote = Remote(); + if (remote == nullptr) { + APP_LOGE("fail send transact cmd %{public}d due remote object", code); + return false; + } + int32_t result = remote->SendRequest(static_cast(code), data, reply, option); + if (result != NO_ERROR) { + APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code); + return false; + } + return true; +} + +bool BundleMgrMiniProxy::SendTransactCmdWithLog(BundleMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply) +{ + MessageOption option(MessageOption::TF_SYNC); + + sptr remote = Remote(); + if (remote == nullptr) { + APP_LOGE("fail send transact cmd %{public}d due remote object", code); + return false; + } + int32_t sptrRefCount = remote->GetSptrRefCount(); + int32_t wptrRefCount = remote->GetWptrRefCount(); + if (sptrRefCount <= 0 || wptrRefCount <= 0) { + APP_LOGI("SendRequest before sptrRefCount: %{public}d wptrRefCount: %{public}d", + sptrRefCount, wptrRefCount); + } + int32_t result = remote->SendRequest(static_cast(code), data, reply, option); + if (result != NO_ERROR) { + APP_LOGE("receive error transact code %{public}d in transact cmd %{public}d", result, code); + return false; + } + return true; +} +} // namespace AppExecFwk +} // namespace OHOS