• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bundle/bundle_manager_callback_stub.h"
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<int32_t> 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 
QueryExtensionAbilityInfo(const AAFwk::Want & want,AppExecFwk::ExtensionAbilityInfo & extensionInfo)114 bool BundleManagerInternal::QueryExtensionAbilityInfo(const AAFwk::Want& want,
115     AppExecFwk::ExtensionAbilityInfo& extensionInfo)
116 {
117     std::vector<int32_t> ids;
118     int32_t ret = OsAccountManager::QueryActiveOsAccountIds(ids);
119     if (ret != ERR_OK || ids.empty()) {
120         return false;
121     }
122     auto bundleMgr = GetBundleManager();
123     if (bundleMgr == nullptr) {
124         HILOGE("failed to get bms");
125         return false;
126     }
127     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
128     bundleMgr->QueryExtensionAbilityInfos(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
129         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, ids[0], extensionInfos);
130     if (extensionInfos.empty()) {
131         HILOGE("QueryExtensionAbilityInfo failed.");
132         return false;
133     }
134     extensionInfo = extensionInfos.front();
135     if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
136         HILOGE("ExtensionAbilityInfo is empty.");
137         return false;
138     }
139     HILOGD("ExtensionAbilityInfo found, name=%{public}s.", extensionInfo.name.c_str());
140     return true;
141 }
142 
InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo & extensionAbilityInfo,AppExecFwk::AbilityInfo & abilityInfo)143 void BundleManagerInternal::InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo &extensionAbilityInfo,
144     AppExecFwk::AbilityInfo &abilityInfo)
145 {
146     abilityInfo.bundleName = extensionAbilityInfo.bundleName;
147     abilityInfo.name = extensionAbilityInfo.name;
148     abilityInfo.permissions = extensionAbilityInfo.permissions;
149     abilityInfo.visible = extensionAbilityInfo.visible;
150 }
151 
IsSameAppId(const std::string & callerAppId,const std::string & targetBundleName)152 bool BundleManagerInternal::IsSameAppId(const std::string& callerAppId, const std::string& targetBundleName)
153 {
154     if (targetBundleName.empty() || callerAppId.empty()) {
155         HILOGE("targetBundleName:%{public}s or callerAppId:%s is empty",
156             targetBundleName.c_str(), callerAppId.c_str());
157         return false;
158     }
159     HILOGD("callerAppId:%s", callerAppId.c_str());
160     std::string calleeAppId;
161     if (!GetCallerAppIdFromBms(targetBundleName, calleeAppId)) {
162         HILOGE("GetCallerAppIdFromBms failed");
163         return false;
164     }
165     HILOGD("calleeAppId:%s", calleeAppId.c_str());
166     return callerAppId == calleeAppId;
167 }
168 
GetLocalBundleInfo(const std::string & bundleName,AppExecFwk::BundleInfo & localBundleInfo)169 int32_t BundleManagerInternal::GetLocalBundleInfo(const std::string& bundleName,
170     AppExecFwk::BundleInfo &localBundleInfo)
171 {
172     auto bms = GetBundleManager();
173     if (bms == nullptr) {
174         HILOGE("get bundle manager failed");
175         return INVALID_PARAMETERS_ERR;
176     }
177 
178     std::vector<int> ids;
179     ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids);
180     if (ret != ERR_OK || ids.empty()) {
181         HILOGE("QueryActiveOsAccountIds failed");
182         return INVALID_PARAMETERS_ERR;
183     }
184     if (!bms->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
185         localBundleInfo, ids[0])) {
186         HILOGE("get local bundle info failed");
187         return INVALID_PARAMETERS_ERR;
188     }
189     return ERR_OK;
190 }
191 
CheckRemoteBundleInfoForContinuation(const std::string & dstDeviceId,const std::string & bundleName,AppExecFwk::DistributedBundleInfo & remoteBundleInfo)192 int32_t BundleManagerInternal::CheckRemoteBundleInfoForContinuation(const std::string& dstDeviceId,
193     const std::string& bundleName, AppExecFwk::DistributedBundleInfo& remoteBundleInfo)
194 {
195     if (bundleName.empty()) {
196         HILOGE("bundle name empty");
197         return INVALID_PARAMETERS_ERR;
198     }
199     HILOGI("bundleName: %{public}s", bundleName.c_str());
200 
201     auto bms = GetBundleManager();
202     if (bms == nullptr) {
203         HILOGE("get bundle manager failed");
204         return INVALID_PARAMETERS_ERR;
205     }
206 
207     bool isInstalled = bms->GetDistributedBundleInfo(dstDeviceId, bundleName, remoteBundleInfo);
208     if (isInstalled) {
209         return ERR_OK;
210     }
211 
212     AppExecFwk::BundleInfo localBundleInfo;
213     if (GetLocalBundleInfo(bundleName, localBundleInfo) != ERR_OK) {
214         HILOGE("get local bundle info failed");
215         return INVALID_PARAMETERS_ERR;
216     }
217     if (localBundleInfo.entryInstallationFree) {
218         return CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL;
219     }
220     return CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL;
221 }
222 
CheckIfRemoteCanInstall(const AAFwk::Want & want,int32_t missionId)223 bool BundleManagerInternal::CheckIfRemoteCanInstall(const AAFwk::Want& want, int32_t missionId)
224 {
225     std::string bundleName = want.GetElement().GetBundleName();
226     std::string moduleName = want.GetElement().GetModuleName();
227     std::string abilityName = want.GetElement().GetAbilityName();
228     std::string deviceId = want.GetElement().GetDeviceID();
229     HILOGD("bundleName = %{public}s, moduleName = %{public}s, abilityName = %{public}s, deviceId = %{public}s",
230         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), deviceId.c_str());
231 
232     if (bundleName.empty() || moduleName.empty() || abilityName.empty() || deviceId.empty()) {
233         HILOGE("deviceId or bundle or module or ability name is empty");
234         return false;
235     }
236     auto bms = GetBundleManager();
237     if (bms == nullptr) {
238         HILOGE("get bundle manager failed");
239         return false;
240     }
241 
242     AAFwk::Want newWant;
243     newWant.SetElementName(deviceId, bundleName, abilityName, moduleName);
244     std::vector<int> ids;
245     ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
246     if (result != ERR_OK || ids.empty()) {
247         return false;
248     }
249     bool ret = bms->CheckAbilityEnableInstall(newWant, missionId, ids[0], new DmsBundleManagerCallbackStub());
250     if (ret != true) {
251         HILOGE("CheckAbilityEnableInstall from bms failed");
252     }
253     return ret;
254 }
255 
GetBundleManager()256 sptr<AppExecFwk::IBundleMgr> BundleManagerInternal::GetBundleManager()
257 {
258     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
259     if (samgrProxy == nullptr) {
260         return nullptr;
261     }
262     sptr<IRemoteObject> bmsProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
263     if (bmsProxy == nullptr) {
264         HILOGE("failed to get bms from samgr");
265         return nullptr;
266     }
267     return iface_cast<AppExecFwk::IBundleMgr>(bmsProxy);
268 }
269 
GetUidFromBms(const std::string & bundleName)270 int32_t BundleManagerInternal::GetUidFromBms(const std::string& bundleName)
271 {
272     auto bundleMgr = GetBundleManager();
273     if (bundleMgr == nullptr) {
274         HILOGE("failed to get bms");
275         return -1;
276     }
277     std::vector<int> ids;
278     ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
279     if (result != ERR_OK || ids.empty()) {
280         return -1;
281     }
282     return bundleMgr->GetUidByBundleName(bundleName, ids[0]);
283 }
284 } // namespace DistributedSchedule
285 } // namespace OHOS
286