1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bundle/bundle_manager_internal.h"
17
18 #include "distributed_sched_adapter.h"
19 #include "dtbschedmgr_log.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "os_account_manager.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace DistributedSchedule {
27 using namespace AccountSA;
28 namespace {
29 const std::string TAG = "BundleManagerInternal";
30 }
31 IMPLEMENT_SINGLE_INSTANCE(BundleManagerInternal);
GetCallerAppIdFromBms(int32_t callingUid,std::string & appId)32 bool BundleManagerInternal::GetCallerAppIdFromBms(int32_t callingUid, std::string& appId)
33 {
34 std::vector<std::string> bundleNameList;
35 if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
36 HILOGE("GetBundleNameListFromBms failed");
37 return false;
38 }
39 if (bundleNameList.empty()) {
40 HILOGE("bundleNameList empty");
41 return false;
42 }
43 // getting an arbitrary bundlename for they sharing a same appId, here we get the first one
44 return GetCallerAppIdFromBms(bundleNameList.front(), appId);
45 }
46
GetCallerAppIdFromBms(const std::string & bundleName,std::string & appId)47 bool BundleManagerInternal::GetCallerAppIdFromBms(const std::string& bundleName, std::string& appId)
48 {
49 auto bundleMgr = GetBundleManager();
50 if (bundleMgr == nullptr) {
51 HILOGE("failed to get bms");
52 return false;
53 }
54 std::vector<int> ids;
55 ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
56 if (result != ERR_OK || ids.empty()) {
57 return false;
58 }
59 appId = bundleMgr->GetAppIdByBundleName(bundleName, ids[0]);
60 HILOGD("appId:%s", appId.c_str());
61 return true;
62 }
63
GetBundleNameListFromBms(int32_t callingUid,std::vector<std::string> & bundleNameList)64 bool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid, std::vector<std::string>& bundleNameList)
65 {
66 auto bundleMgr = GetBundleManager();
67 if (bundleMgr == nullptr) {
68 HILOGE("failed to get bms");
69 return false;
70 }
71 bool result = bundleMgr->GetBundlesForUid(callingUid, bundleNameList);
72 if (!result) {
73 HILOGE("GetBundlesForUid failed, result: %{public}d", result);
74 return false;
75 }
76 return result;
77 }
78
GetBundleNameListFromBms(int32_t callingUid,std::vector<std::u16string> & u16BundleNameList)79 bool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid,
80 std::vector<std::u16string>& u16BundleNameList)
81 {
82 std::vector<std::string> bundleNameList;
83 if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
84 HILOGE("GetBundleNameListFromBms failed");
85 return false;
86 }
87 for (const std::string& bundleName : bundleNameList) {
88 u16BundleNameList.emplace_back(Str8ToStr16(bundleName));
89 }
90 return true;
91 }
92
QueryAbilityInfo(const AAFwk::Want & want,AppExecFwk::AbilityInfo & abilityInfo)93 bool BundleManagerInternal::QueryAbilityInfo(const AAFwk::Want& want, AppExecFwk::AbilityInfo& abilityInfo)
94 {
95 std::vector<int> ids;
96 int32_t ret = OsAccountManager::QueryActiveOsAccountIds(ids);
97 if (ret != ERR_OK || ids.empty()) {
98 return false;
99 }
100 auto bundleMgr = GetBundleManager();
101 if (bundleMgr == nullptr) {
102 HILOGE("failed to get bms");
103 return false;
104 }
105 bool result = bundleMgr->QueryAbilityInfo(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
106 | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, ids[0], abilityInfo);
107 if (!result) {
108 HILOGE("QueryAbilityInfo failed");
109 return false;
110 }
111 return true;
112 }
113
IsSameAppId(const std::string & callerAppId,const std::string & targetBundleName)114 bool BundleManagerInternal::IsSameAppId(const std::string& callerAppId, const std::string& targetBundleName)
115 {
116 if (targetBundleName.empty() || callerAppId.empty()) {
117 HILOGE("targetBundleName:%{public}s or callerAppId:%s is empty",
118 targetBundleName.c_str(), callerAppId.c_str());
119 return false;
120 }
121 HILOGD("callerAppId:%s", callerAppId.c_str());
122 std::string calleeAppId;
123 if (!GetCallerAppIdFromBms(targetBundleName, calleeAppId)) {
124 HILOGE("GetCallerAppIdFromBms failed");
125 return false;
126 }
127 HILOGD("calleeAppId:%s", calleeAppId.c_str());
128 return callerAppId == calleeAppId;
129 }
130
GetBundleManager()131 sptr<AppExecFwk::IBundleMgr> BundleManagerInternal::GetBundleManager()
132 {
133 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
134 if (samgrProxy == nullptr) {
135 return nullptr;
136 }
137 sptr<IRemoteObject> bmsProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
138 if (bmsProxy == nullptr) {
139 HILOGE("failed to get bms from samgr");
140 return nullptr;
141 }
142 return iface_cast<AppExecFwk::IBundleMgr>(bmsProxy);
143 }
144 } // namespace DistributedSchedule
145 } // namespace OHOS
146