1 /*
2 * Copyright (c) 2022-2023 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_connect_ability_mgr.h"
17
18 #include "ability_manager_client.h"
19 #include "app_log_tag_wrapper.h"
20 #include "bundle_memory_guard.h"
21 #include "bundle_mgr_service.h"
22 #include "ffrt_inner.h"
23 #include "hitrace_meter.h"
24 #include "service_center_connection.h"
25 #include "service_center_status_callback.h"
26 #include "system_ability_load_callback_stub.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string PARAM_FREEINSTALL_APPID = "ohos.freeinstall.params.callingAppId";
32 const std::string PARAM_FREEINSTALL_BUNDLENAMES = "ohos.freeinstall.params.callingBundleNames";
33 const std::string PARAM_SUB_PACKAGE_NAME = "ohos.param.atomicservice.subpackageName";
34 const std::string PARAM_FREEINSTALL_TARGET_APP_DIST_TYPE = "send_to_erms_targetAppDistType";
35 const std::string PARAM_FREEINSTALL_EMBEDDED = "send_to_erms_embedded";
36 const std::string PARAM_FREEINSTALL_TARGET_APP_PROVISION_TYPE = "send_to_erms_targetAppProvisionType";
37 const std::string PARAM_FREEINSTALL_UID = "ohos.freeinstall.params.callingUid";
38 const std::string DISCONNECT_DELAY_TASK = "DisconnectDelayTask";
39 const std::string DEFAULT_VERSION = "1";
40 const std::string CONNECT_ABILITY_QUEUE = "ConnectAbilityQueue";
41 constexpr uint32_t CALLING_TYPE_HARMONY = 2;
42 constexpr uint32_t BIT_ZERO_COMPATIBLE = 0;
43 constexpr uint32_t BIT_ONE_FRONT_MODE = 0;
44 constexpr uint32_t BIT_ONE_BACKGROUND_MODE = 1;
45 constexpr uint32_t BIT_TWO_CUSTOM = 0;
46 constexpr uint32_t BIT_THREE_ZERO = 0;
47 constexpr uint32_t BIT_FOUR_AZ_DEVICE = 0;
48 constexpr uint32_t BIT_FIVE_SAME_BUNDLE_NAME = 0;
49 constexpr uint32_t BIT_SIX_SAME_BUNDLE = 0;
50 constexpr uint32_t BIT_ONE = 2;
51 constexpr uint32_t BIT_TWO = 4;
52 constexpr uint32_t BIT_THREE = 8;
53 constexpr uint32_t BIT_FOUR = 16;
54 constexpr uint32_t BIT_FIVE = 32;
55 constexpr uint32_t BIT_SIX = 64;
56 constexpr uint32_t DISCONNECT_DELAY = 20000;
57 constexpr uint32_t OUT_TIME = 30000;
58 const std::u16string ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN = u"ohos.IAtomicServiceStatusCallback";
59 const std::u16string SERVICE_CENTER_TOKEN = u"abilitydispatcherhm.openapi.hapinstall.IHapInstall";
60 constexpr uint32_t FREE_INSTALL_DONE = 0;
61 constexpr uint32_t TYPE_HARMONEY_INVALID = 0;
62 constexpr uint32_t TYPE_HARMONEY_APP = 1;
63 constexpr uint32_t TYPE_HARMONEY_SERVICE = 2;
64 constexpr uint32_t DEFAULT_VALUE = -1;
65 constexpr uint32_t DEFAULT_EMBEDDED_VALUE = 0;
66 constexpr int32_t DMS_UID = 5522;
67 // sa id
68 constexpr int32_t DOWNLOAD_SERVICE_SA_ID = 3706;
69 // replace want int ecological rule
70 constexpr const char* PARAM_REPLACE_WANT = "ohos.extra.param.key.replace_want";
71
SendSysEvent(int32_t resultCode,const AAFwk::Want & want,int32_t userId)72 void SendSysEvent(int32_t resultCode, const AAFwk::Want &want, int32_t userId)
73 {
74 EventInfo sysEventInfo;
75 ElementName element = want.GetElement();
76 sysEventInfo.bundleName = element.GetBundleName();
77 sysEventInfo.moduleName = element.GetModuleName();
78 sysEventInfo.abilityName = element.GetAbilityName();
79 sysEventInfo.isFreeInstallMode = true;
80 sysEventInfo.userId = userId;
81 sysEventInfo.errCode = resultCode;
82 EventReport::SendSystemEvent(BMSEventType::BUNDLE_INSTALL_EXCEPTION, sysEventInfo);
83 }
84 }
85
BundleConnectAbilityMgr()86 BundleConnectAbilityMgr::BundleConnectAbilityMgr()
87 {
88 LOG_D(BMS_TAG_DEFAULT, "create BundleConnectAbilityMgr");
89 serialQueue_ = std::make_shared<SerialQueue>(CONNECT_ABILITY_QUEUE);
90 }
91
~BundleConnectAbilityMgr()92 BundleConnectAbilityMgr::~BundleConnectAbilityMgr()
93 {
94 LOG_D(BMS_TAG_DEFAULT, "destroy BundleConnectAbilityMgr");
95 }
96
ProcessPreloadCheck(const TargetAbilityInfo & targetAbilityInfo)97 bool BundleConnectAbilityMgr::ProcessPreloadCheck(const TargetAbilityInfo &targetAbilityInfo)
98 {
99 LOG_D(BMS_TAG_DEFAULT, "ProcessPreloadCheck");
100 auto preloadCheckFunc = [this, targetAbilityInfo]() {
101 BundleMemoryGuard memoryGuard;
102 int32_t flag = ServiceCenterFunction::CONNECT_PRELOAD_INSTALL;
103 this->ProcessPreloadRequestToServiceCenter(flag, targetAbilityInfo);
104 };
105 ffrt::submit(preloadCheckFunc);
106 return true;
107 }
108
ProcessPreloadRequestToServiceCenter(int32_t flag,const TargetAbilityInfo & targetAbilityInfo)109 void BundleConnectAbilityMgr::ProcessPreloadRequestToServiceCenter(int32_t flag,
110 const TargetAbilityInfo &targetAbilityInfo)
111 {
112 LOG_D(BMS_TAG_DEFAULT, "ProcessPreloadRequestToServiceCenter");
113 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
114 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
115 if (bundleDataMgr_ == nullptr) {
116 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
117 return;
118 }
119 std::string bundleName;
120 std::string abilityName;
121 if (!(bundleDataMgr_->QueryAppGalleryAbilityName(bundleName, abilityName))) {
122 LOG_E(BMS_TAG_DEFAULT, "Fail to query ServiceCenter ability and bundle name");
123 return;
124 }
125 auto task = [ owner = weak_from_this() ] {
126 auto mgr = owner.lock();
127 if (mgr == nullptr) {
128 LOG_E(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr is nullptr");
129 return;
130 }
131 mgr->LoadDownloadService();
132 };
133 std::thread loadServiceThread(task);
134 loadServiceThread.detach();
135 Want serviceCenterWant;
136 serviceCenterWant.SetElementName(bundleName, abilityName);
137 bool isConnectSuccess = ConnectAbility(serviceCenterWant, nullptr);
138 if (!isConnectSuccess) {
139 LOG_E(BMS_TAG_DEFAULT, "Fail to connect ServiceCenter");
140 return;
141 } else {
142 PreloadRequest(flag, targetAbilityInfo);
143 return;
144 }
145 }
146
PreloadRequest(int32_t flag,const TargetAbilityInfo & targetAbilityInfo)147 void BundleConnectAbilityMgr::PreloadRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo)
148 {
149 MessageParcel data;
150 MessageParcel reply;
151 MessageOption option(MessageOption::TF_ASYNC);
152 if (!data.WriteInterfaceToken(SERVICE_CENTER_TOKEN)) {
153 LOG_E(BMS_TAG_DEFAULT, "failed to WriteInterfaceToken");
154 return;
155 }
156 const std::string dataString = GetJsonStrFromInfo(targetAbilityInfo);
157 LOG_I(BMS_TAG_DEFAULT, "TargetAbilityInfo to JsonString : %{public}s", dataString.c_str());
158 if (!data.WriteString16(Str8ToStr16(dataString))) {
159 LOG_E(BMS_TAG_DEFAULT, "failed to WriteParcelable targetAbilityInfo");
160 return;
161 }
162 sptr<ServiceCenterStatusCallback> callback = new(std::nothrow) ServiceCenterStatusCallback(weak_from_this());
163 if (callback == nullptr) {
164 LOG_E(BMS_TAG_DEFAULT, "callback is nullptr");
165 return;
166 }
167 if (!data.WriteRemoteObject(callback)) {
168 LOG_E(BMS_TAG_DEFAULT, "failed to WriteRemoteObject callbcak");
169 return;
170 }
171 std::unique_lock<std::mutex> mutexLock(mutex_);
172 sptr<IRemoteObject> serviceCenterRemoteObject = serviceCenterConnection_->GetRemoteObject();
173 if (serviceCenterRemoteObject == nullptr) {
174 LOG_E(BMS_TAG_DEFAULT, "failed to get remote object");
175 return;
176 }
177 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
178 if (freeInstallParams == nullptr) {
179 LOG_E(BMS_TAG_DEFAULT, "freeInstallParams is nullptr");
180 return;
181 }
182 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_PRELOAD_INSTALL;
183 std::unique_lock<std::mutex> lock(mapMutex_);
184 auto emplaceResult = freeInstallParamsMap_.emplace(targetAbilityInfo.targetInfo.transactId, *freeInstallParams);
185 LOG_I(BMS_TAG_DEFAULT, "emplace map size = %{public}zu, transactId = %{public}s",
186 freeInstallParamsMap_.size(), targetAbilityInfo.targetInfo.transactId.c_str());
187 if (!emplaceResult.second) {
188 LOG_E(BMS_TAG_DEFAULT, "freeInstallParamsMap emplace error");
189 return;
190 }
191 lock.unlock();
192 int32_t result = serviceCenterRemoteObject->SendRequest(flag, data, reply, option);
193 if (result != ERR_OK) {
194 LOG_E(BMS_TAG_DEFAULT, "Failed to sendRequest, result = %{public}d", result);
195 }
196 LOG_D(BMS_TAG_DEFAULT, "sendRequest to service center success");
197 }
198
GetPreloadFlag()199 int32_t BundleConnectAbilityMgr::GetPreloadFlag()
200 {
201 int32_t flagZero = BIT_ZERO_COMPATIBLE;
202 int32_t flagOne = BIT_ONE_BACKGROUND_MODE * BIT_ONE;
203 int32_t flagTwo = BIT_TWO_CUSTOM * BIT_TWO;
204 int32_t flagThree = BIT_THREE_ZERO * BIT_THREE;
205 int32_t flagFour = BIT_FOUR_AZ_DEVICE * BIT_FOUR;
206 int32_t flagFive = BIT_FIVE_SAME_BUNDLE_NAME * BIT_FIVE;
207 int32_t flagSix = BIT_SIX_SAME_BUNDLE * BIT_SIX;
208 return flagZero + flagOne + flagTwo + flagThree + flagFour + flagFive + flagSix;
209 }
210
GetPreloadList(const std::string & bundleName,const std::string & moduleName,int32_t userId,sptr<TargetAbilityInfo> & targetAbilityInfo)211 bool BundleConnectAbilityMgr::GetPreloadList(const std::string &bundleName, const std::string &moduleName,
212 int32_t userId, sptr<TargetAbilityInfo> &targetAbilityInfo)
213 {
214 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
215 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
216 if (bundleDataMgr_ == nullptr) {
217 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
218 return false;
219 }
220 InnerBundleInfo innerBundleInfo;
221 int32_t flag = ApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE;
222 auto ret = bundleDataMgr_->GetInnerBundleInfoWithBundleFlagsAndLock(bundleName, flag, innerBundleInfo, userId);
223 if (!ret) {
224 LOG_E(BMS_TAG_DEFAULT, "GetInnerBundleInfoWithBundleFlagsAndLock failed");
225 return false;
226 }
227 if (innerBundleInfo.GetBaseApplicationInfo().bundleType != BundleType::ATOMIC_SERVICE) {
228 return false;
229 }
230 if (moduleName.empty()) {
231 LOG_E(BMS_TAG_DEFAULT, "moduleName is empty");
232 return false;
233 }
234 std::set<std::string> preloadModuleNames;
235 auto moduleInfoMap = innerBundleInfo.GetInnerModuleInfos();
236 if (moduleInfoMap.find(moduleName) == moduleInfoMap.end()) {
237 LOG_E(BMS_TAG_DEFAULT, "get moduleInfo from innerBundleInfo failed");
238 return false;
239 }
240 auto preloadItems = moduleInfoMap[moduleName].preloads;
241 if (preloadItems.empty()) {
242 return false;
243 }
244 for (const auto &item : preloadItems) {
245 preloadModuleNames.insert(item);
246 }
247 for (const auto &it : moduleInfoMap) {
248 auto iter = preloadModuleNames.find(it.first);
249 if (iter != preloadModuleNames.end()) {
250 preloadModuleNames.erase(iter);
251 }
252 }
253 if (preloadModuleNames.empty()) {
254 LOG_D(BMS_TAG_DEFAULT, "All preload modules exist locally");
255 return false;
256 }
257 targetAbilityInfo->targetInfo.callingAppIds.emplace_back(innerBundleInfo.GetBaseBundleInfo().appId);
258 for (const auto &item : preloadModuleNames) {
259 targetAbilityInfo->targetInfo.preloadModuleNames.emplace_back(item);
260 }
261 return true;
262 }
263
ProcessPreload(const Want & want)264 bool BundleConnectAbilityMgr::ProcessPreload(const Want &want)
265 {
266 LOG_D(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::ProcessPreload is called");
267 std::string bundleName = want.GetElement().GetBundleName();
268 std::string moduleName = want.GetElement().GetModuleName();
269 std::string abilityName = want.GetElement().GetAbilityName();
270 int32_t uid = want.GetIntParam("uid", 0);
271 int32_t userId = uid / Constants::BASE_USER_RANGE;
272 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
273 if (targetAbilityInfo == nullptr) {
274 LOG_E(BMS_TAG_DEFAULT, "targetAbilityInfo is nullptr");
275 return false;
276 }
277 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
278 if (targetInfo == nullptr) {
279 LOG_E(BMS_TAG_DEFAULT, "targetInfo is nullptr");
280 return false;
281 }
282 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
283 if (targetExtSetting == nullptr) {
284 LOG_E(BMS_TAG_DEFAULT, "targetExtSetting is nullptr");
285 return false;
286 }
287 targetAbilityInfo->targetInfo = *targetInfo;
288 targetAbilityInfo->targetExtSetting = *targetExtSetting;
289 targetAbilityInfo->version = DEFAULT_VERSION;
290
291 if (!GetPreloadList(bundleName, moduleName, userId, targetAbilityInfo)) {
292 LOG_D(BMS_TAG_DEFAULT, "the module have no preload module");
293 return false;
294 }
295 targetAbilityInfo->targetInfo.transactId = std::to_string(this->GetTransactId());
296 targetAbilityInfo->targetInfo.bundleName = bundleName;
297 targetAbilityInfo->targetInfo.moduleName = moduleName;
298 targetAbilityInfo->targetInfo.abilityName = abilityName;
299 targetAbilityInfo->targetInfo.action = want.GetAction();
300 targetAbilityInfo->targetInfo.uri = want.GetUriString();
301 targetAbilityInfo->targetInfo.type = want.GetType();
302 targetAbilityInfo->targetInfo.flags = GetPreloadFlag();
303 targetAbilityInfo->targetInfo.callingUid = uid;
304 targetAbilityInfo->targetInfo.callingAppType = CALLING_TYPE_HARMONY;
305 targetAbilityInfo->targetInfo.callingBundleNames.emplace_back(bundleName);
306 if (!ProcessPreloadCheck(*targetAbilityInfo)) {
307 LOG_E(BMS_TAG_DEFAULT, "ProcessPreloadCheck failed");
308 return false;
309 }
310 return true;
311 }
312
SilentInstall(TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)313 bool BundleConnectAbilityMgr::SilentInstall(TargetAbilityInfo &targetAbilityInfo, const Want &want,
314 const FreeInstallParams &freeInstallParams, int32_t userId)
315 {
316 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
317 LOG_I(BMS_TAG_DEFAULT, "SilentInstall");
318 if (!CheckIsOnDemandLoad(targetAbilityInfo)) {
319 ErmsCallerInfo callerInfo;
320 GetEcologicalCallerInfo(want, callerInfo, userId);
321 BmsExperienceRule rule;
322 bool ret = CheckEcologicalRule(want, callerInfo, rule);
323 if (!ret) {
324 LOG_E(BMS_TAG_DEFAULT, "check ecological rule failed, skip");
325 } else if (rule.isAllow) {
326 LOG_I(BMS_TAG_DEFAULT, "ecological rule is allow, keep going");
327 } else if (rule.replaceWant != nullptr) {
328 LOG_I(BMS_TAG_DEFAULT, "ecological rule is replace want");
329 targetAbilityInfo.targetExtSetting.extValues.emplace(PARAM_REPLACE_WANT,
330 rule.replaceWant->ToUri());
331 } else {
332 LOG_W(BMS_TAG_DEFAULT, "ecological rule is not allowed, return");
333 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
334 return false;
335 }
336 }
337
338 auto silentInstallFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
339 BundleMemoryGuard memoryGuard;
340 int32_t flag = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
341 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
342 };
343 ffrt::submit(silentInstallFunc, {}, {}, ffrt::task_attr().qos(ffrt::qos_deadline_request));
344 return true;
345 }
346
UpgradeCheck(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)347 bool BundleConnectAbilityMgr::UpgradeCheck(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
348 const FreeInstallParams &freeInstallParams, int32_t userId)
349 {
350 LOG_I(BMS_TAG_DEFAULT, "UpgradeCheck");
351 auto upgradeCheckFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
352 BundleMemoryGuard memoryGuard;
353 int32_t flag = ServiceCenterFunction::CONNECT_UPGRADE_CHECK;
354 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
355 };
356 ffrt::submit(upgradeCheckFunc);
357 return true;
358 }
359
UpgradeInstall(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)360 bool BundleConnectAbilityMgr::UpgradeInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
361 const FreeInstallParams &freeInstallParams, int32_t userId)
362 {
363 LOG_I(BMS_TAG_DEFAULT, "UpgradeInstall");
364 auto upgradeInstallFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
365 BundleMemoryGuard memoryGuard;
366 int32_t flag = ServiceCenterFunction::CONNECT_UPGRADE_INSTALL;
367 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
368 };
369 ffrt::submit(upgradeInstallFunc);
370 return true;
371 }
372
SendRequestToServiceCenter(int32_t flag,const TargetAbilityInfo & targetAbilityInfo,const Want & want,int32_t userId,const FreeInstallParams & freeInstallParams)373 bool BundleConnectAbilityMgr::SendRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo,
374 const Want &want, int32_t userId, const FreeInstallParams &freeInstallParams)
375 {
376 LOG_I(BMS_TAG_DEFAULT, "SendRequestToServiceCenter");
377 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
378 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
379 if (bundleDataMgr_ == nullptr) {
380 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
381 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
382 return false;
383 }
384 std::string bundleName;
385 std::string abilityName;
386 if (!(bundleDataMgr_->QueryAppGalleryAbilityName(bundleName, abilityName))) {
387 LOG_E(BMS_TAG_DEFAULT, "Fail to query ServiceCenter ability and bundle name");
388 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
389 return false;
390 }
391 Want serviceCenterWant;
392 serviceCenterWant.SetElementName(bundleName, abilityName);
393 auto task = [ owner = weak_from_this() ] {
394 auto mgr = owner.lock();
395 if (mgr == nullptr) {
396 LOG_E(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr is nullptr");
397 return;
398 }
399 mgr->LoadDownloadService();
400 };
401 std::thread loadServiceThread(task);
402 loadServiceThread.detach();
403 bool isConnectSuccess = ConnectAbility(serviceCenterWant, nullptr);
404 if (!isConnectSuccess) {
405 if (freeInstallParams.serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL) {
406 LOG_E(BMS_TAG_DEFAULT, "Fail to connect ServiceCenter, but freeinstall upgrade return ok");
407 CallAbilityManager(ServiceCenterResultCode::FREE_INSTALL_OK, want, userId, freeInstallParams.callback);
408 } else {
409 LOG_E(BMS_TAG_DEFAULT, "Fail to connect ServiceCenter");
410 CallAbilityManager(FreeInstallErrorCode::CONNECT_ERROR, want, userId, freeInstallParams.callback);
411 }
412 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
413 return false;
414 } else {
415 SendRequest(flag, targetAbilityInfo, want, userId, freeInstallParams);
416 return true;
417 }
418 }
419
LoadDownloadService() const420 void BundleConnectAbilityMgr::LoadDownloadService() const
421 {
422 LOG_I(BMS_TAG_DEFAULT, "LoadDownloadService start");
423 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
424 if (systemAbilityMgr == nullptr) {
425 LOG_E(BMS_TAG_DEFAULT, "Failed to get SystemAbilityManager");
426 return;
427 }
428 sptr<SystemAbilityLoadCallbackStub> loadCallback = new (std::nothrow) SystemAbilityLoadCallbackStub();
429 if (loadCallback == nullptr) {
430 LOG_E(BMS_TAG_DEFAULT, "Create load callback failed");
431 return;
432 }
433 auto ret = systemAbilityMgr->LoadSystemAbility(DOWNLOAD_SERVICE_SA_ID, loadCallback);
434 if (ret != 0) {
435 LOG_E(BMS_TAG_DEFAULT, "Load system ability %{public}d failed with %{public}d",
436 DOWNLOAD_SERVICE_SA_ID, ret);
437 return;
438 }
439 LOG_I(BMS_TAG_DEFAULT, "LoadDownloadService end");
440 }
441
DisconnectAbility()442 void BundleConnectAbilityMgr::DisconnectAbility()
443 {
444 std::unique_lock<std::mutex> mutexLock(mutex_);
445 if (serviceCenterConnection_ != nullptr) {
446 LOG_I(BMS_TAG_DEFAULT, "DisconnectAbility");
447 int result = AbilityManagerClient::GetInstance()->DisconnectAbility(serviceCenterConnection_);
448 if (result != ERR_OK) {
449 LOG_E(BMS_TAG_DEFAULT, "disconnect fail, resultCode: %{public}d", result);
450 }
451 }
452 }
453
WaitFromConnecting(std::unique_lock<std::mutex> & lock)454 void BundleConnectAbilityMgr::WaitFromConnecting(std::unique_lock<std::mutex> &lock)
455 {
456 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility await start CONNECTING");
457 while (connectState_ == ServiceCenterConnectState::CONNECTING) {
458 cv_.wait(lock);
459 }
460 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility await end CONNECTING");
461 }
462
WaitFromConnected(std::unique_lock<std::mutex> & lock)463 void BundleConnectAbilityMgr::WaitFromConnected(std::unique_lock<std::mutex> &lock)
464 {
465 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility await start CONNECTED");
466 while (connectState_ != ServiceCenterConnectState::CONNECTED) {
467 if (connectState_ == ServiceCenterConnectState::DISCONNECTED) {
468 break;
469 }
470 cv_.wait(lock);
471 }
472 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility await end CONNECTED");
473 }
474
ConnectAbility(const Want & want,const sptr<IRemoteObject> & callerToken)475 bool BundleConnectAbilityMgr::ConnectAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
476 {
477 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility start target bundle = %{public}s", want.GetBundle().c_str());
478 std::unique_lock<std::mutex> lock(mutex_);
479 serialQueue_->CancelDelayTask(DISCONNECT_DELAY_TASK);
480 if (connectState_ == ServiceCenterConnectState::CONNECTING) {
481 WaitFromConnecting(lock);
482 } else if (connectState_ == ServiceCenterConnectState::DISCONNECTED) {
483 connectState_ = ServiceCenterConnectState::CONNECTING;
484 serviceCenterConnection_ = new (std::nothrow) ServiceCenterConnection(connectState_,
485 cv_, weak_from_this());
486 if (serviceCenterConnection_ == nullptr) {
487 LOG_E(BMS_TAG_DEFAULT, "ServiceCenterConnection is nullptr");
488 connectState_ = ServiceCenterConnectState::DISCONNECTED;
489 cv_.notify_all();
490 return false;
491 }
492 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility start");
493 int result = AbilityManagerClient::GetInstance()->ConnectAbility(want, serviceCenterConnection_, callerToken);
494 if (result == ERR_OK) {
495 if (connectState_ != ServiceCenterConnectState::CONNECTED) {
496 WaitFromConnected(lock);
497 }
498 } else {
499 LOG_E(BMS_TAG_DEFAULT, "ConnectAbility fail result = %{public}d", result);
500 }
501 }
502
503 LOG_I(BMS_TAG_DEFAULT, "ConnectAbility end");
504 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
505 return true;
506 } else {
507 LOG_E(BMS_TAG_DEFAULT, "ConnectAbility fail");
508 connectState_ = ServiceCenterConnectState::DISCONNECTED;
509 return false;
510 }
511 }
512
DisconnectDelay()513 void BundleConnectAbilityMgr::DisconnectDelay()
514 {
515 auto disconnectFunc = [connect = shared_from_this()]() {
516 LOG_I(BMS_TAG_DEFAULT, "disconnectFunc Disconnect Ability");
517 BundleMemoryGuard memoryGuard;
518 if (connect) {
519 connect->DisconnectAbility();
520 }
521 };
522 serialQueue_->ScheduleDelayTask(DISCONNECT_DELAY_TASK, DISCONNECT_DELAY, disconnectFunc);
523 }
524
SendCallBack(int32_t resultCode,const AAFwk::Want & want,int32_t userId,const std::string & transactId)525 void BundleConnectAbilityMgr::SendCallBack(
526 int32_t resultCode, const AAFwk::Want &want, int32_t userId, const std::string &transactId)
527 {
528 LOG_I(BMS_TAG_DEFAULT, "SendCallBack");
529 sptr<IRemoteObject> amsCallBack = GetAbilityManagerServiceCallBack(transactId);
530 if (amsCallBack == nullptr) {
531 LOG_E(BMS_TAG_DEFAULT, "Abilitity manager callback is null");
532 return;
533 }
534
535 std::unique_lock<std::mutex> lock(mapMutex_);
536 if (freeInstallParamsMap_[transactId].serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL &&
537 resultCode != ServiceCenterResultCode::FREE_INSTALL_OK) {
538 LOG_E(BMS_TAG_DEFAULT, "SendCallBack, freeinstall upgrade return ok");
539 resultCode = ServiceCenterResultCode::FREE_INSTALL_OK;
540 }
541 freeInstallParamsMap_.erase(transactId);
542 LOG_I(BMS_TAG_DEFAULT, "erase map size = %{public}zu, transactId = %{public}s",
543 freeInstallParamsMap_.size(), transactId.c_str());
544 if (freeInstallParamsMap_.size() == 0) {
545 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
546 LOG_I(BMS_TAG_DEFAULT, "DisconnectDelay");
547 DisconnectDelay();
548 }
549 }
550 lock.unlock();
551
552 MessageParcel data;
553 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
554 LOG_E(BMS_TAG_DEFAULT, "Write interface token failed");
555 return;
556 }
557 if (!data.WriteInt32(resultCode)) {
558 LOG_E(BMS_TAG_DEFAULT, "Write result code failed");
559 return;
560 }
561 if (!data.WriteParcelable(&want)) {
562 LOG_E(BMS_TAG_DEFAULT, "Write want failed");
563 return;
564 }
565 if (!data.WriteInt32(userId)) {
566 LOG_E(BMS_TAG_DEFAULT, "Write userId failed");
567 return;
568 }
569 MessageParcel reply;
570 MessageOption option;
571
572 if (amsCallBack->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
573 LOG_E(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::SendCallBack SendRequest failed");
574 }
575 }
576
SendCallBack(const std::string & transactId,const FreeInstallParams & freeInstallParams)577 void BundleConnectAbilityMgr::SendCallBack(const std::string &transactId, const FreeInstallParams &freeInstallParams)
578 {
579 if (freeInstallParams.callback == nullptr) {
580 LOG_E(BMS_TAG_DEFAULT, "freeInstallParams.callback is null");
581 return;
582 }
583
584 MessageParcel data;
585 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
586 LOG_E(BMS_TAG_DEFAULT, "Write interface token failed");
587 return;
588 }
589 if (!data.WriteInt32(FreeInstallErrorCode::SERVICE_CENTER_CRASH)) {
590 LOG_E(BMS_TAG_DEFAULT, "Write result code error");
591 return;
592 }
593 if (!data.WriteParcelable(&(freeInstallParams.want))) {
594 LOG_E(BMS_TAG_DEFAULT, "Write want failed");
595 return;
596 }
597 if (!data.WriteInt32(freeInstallParams.userId)) {
598 LOG_E(BMS_TAG_DEFAULT, "Write userId error");
599 return;
600 }
601 MessageParcel reply;
602 MessageOption option;
603 if (freeInstallParams.callback->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
604 LOG_E(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::SendCallBack SendRequest failed");
605 }
606 }
607
DeathRecipientSendCallback()608 void BundleConnectAbilityMgr::DeathRecipientSendCallback()
609 {
610 LOG_I(BMS_TAG_DEFAULT, "DeathRecipientSendCallback start");
611 std::unique_lock<std::mutex> mutexLock(mutex_);
612 connectState_ = ServiceCenterConnectState::DISCONNECTED;
613 std::unique_lock<std::mutex> lock(mapMutex_);
614 LOG_I(BMS_TAG_DEFAULT, "freeInstallParamsMap size = %{public}zu", freeInstallParamsMap_.size());
615 for (auto &it : freeInstallParamsMap_) {
616 SendCallBack(it.first, it.second);
617 }
618 freeInstallParamsMap_.clear();
619 lock.unlock();
620 cv_.notify_all();
621
622 LOG_I(BMS_TAG_DEFAULT, "DeathRecipientSendCallback end");
623 }
624
OnServiceCenterCall(std::string installResultStr)625 void BundleConnectAbilityMgr::OnServiceCenterCall(std::string installResultStr)
626 {
627 LOG_I(BMS_TAG_DEFAULT, "OnServiceCenterCall start, installResultStr = %{public}s", installResultStr.c_str());
628 InstallResult installResult;
629 if (!ParseInfoFromJsonStr(installResultStr.c_str(), installResult)) {
630 LOG_E(BMS_TAG_DEFAULT, "Parse info from json fail");
631 return;
632 }
633 LOG_I(BMS_TAG_DEFAULT, "OnServiceCenterCall, retCode = %{public}d", installResult.result.retCode);
634 FreeInstallParams freeInstallParams;
635 std::unique_lock<std::mutex> lock(mapMutex_);
636 auto node = freeInstallParamsMap_.find(installResult.result.transactId);
637 if (node == freeInstallParamsMap_.end()) {
638 LOG_E(BMS_TAG_DEFAULT, "Can not find node");
639 return;
640 }
641 serialQueue_->CancelDelayTask(installResult.result.transactId);
642 freeInstallParams = node->second;
643 lock.unlock();
644 if (installResult.result.retCode == ServiceCenterResultCode::FREE_INSTALL_DOWNLOADING) {
645 LOG_I(BMS_TAG_DEFAULT, "ServiceCenter is downloading, downloadSize = %{public}d, totalSize = %{public}d",
646 installResult.progress.downloadSize, installResult.progress.totalSize);
647 return;
648 }
649 LOG_I(BMS_TAG_DEFAULT, "serviceCenterFunction = %{public}d", freeInstallParams.serviceCenterFunction);
650 if (freeInstallParams.serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL &&
651 installResult.result.retCode != ServiceCenterResultCode::FREE_INSTALL_OK) {
652 LOG_E(BMS_TAG_DEFAULT, "freeinstall upgrade return ok");
653 installResult.result.retCode = ServiceCenterResultCode::FREE_INSTALL_OK;
654 }
655 SendCallBack(installResult.result.retCode, freeInstallParams.want, freeInstallParams.userId,
656 installResult.result.transactId);
657 LOG_I(BMS_TAG_DEFAULT, "OnServiceCenterCall end");
658 }
659
OnDelayedHeartbeat(std::string installResultStr)660 void BundleConnectAbilityMgr::OnDelayedHeartbeat(std::string installResultStr)
661 {
662 LOG_I(BMS_TAG_DEFAULT, "OnDelayedHeartbeat start, installResultStr = %{public}s", installResultStr.c_str());
663 InstallResult installResult;
664 if (!ParseInfoFromJsonStr(installResultStr.c_str(), installResult)) {
665 LOG_E(BMS_TAG_DEFAULT, "Parse info from json fail");
666 return;
667 }
668 LOG_I(BMS_TAG_DEFAULT, "OnDelayedHeartbeat, retCode = %{public}d", installResult.result.retCode);
669 FreeInstallParams freeInstallParams;
670 std::unique_lock<std::mutex> lock(mapMutex_);
671 auto node = freeInstallParamsMap_.find(installResult.result.transactId);
672 if (node == freeInstallParamsMap_.end()) {
673 LOG_E(BMS_TAG_DEFAULT, "Can not find node");
674 return;
675 }
676 serialQueue_->CancelDelayTask(installResult.result.transactId);
677 lock.unlock();
678 OutTimeMonitor(installResult.result.transactId);
679 LOG_I(BMS_TAG_DEFAULT, "OnDelayedHeartbeat end");
680 }
681
OnServiceCenterReceived(std::string installResultStr)682 void BundleConnectAbilityMgr::OnServiceCenterReceived(std::string installResultStr)
683 {
684 LOG_I(BMS_TAG_DEFAULT, "installResultStr = %{public}s", installResultStr.c_str());
685 InstallResult installResult;
686 if (!ParseInfoFromJsonStr(installResultStr.c_str(), installResult)) {
687 LOG_E(BMS_TAG_DEFAULT, "Parse info from json fail");
688 return;
689 }
690 FreeInstallParams freeInstallParams;
691 std::unique_lock<std::mutex> lock(mapMutex_);
692 auto node = freeInstallParamsMap_.find(installResult.result.transactId);
693 if (node == freeInstallParamsMap_.end()) {
694 LOG_E(BMS_TAG_DEFAULT, "Can not find node");
695 return;
696 }
697 if (DISCONNECT_ABILITY_FUNC.find(node->second.serviceCenterFunction) == DISCONNECT_ABILITY_FUNC.end()) {
698 LOG_E(BMS_TAG_DEFAULT, "%{public}d not in DISCONNECT_ABILITY_FUNC", node->second.serviceCenterFunction);
699 return;
700 }
701 freeInstallParamsMap_.erase(installResult.result.transactId);
702 LOG_I(BMS_TAG_DEFAULT, "erase map size = %{public}zu, transactId = %{public}s",
703 freeInstallParamsMap_.size(), installResult.result.transactId.c_str());
704 if (freeInstallParamsMap_.size() == 0) {
705 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
706 LOG_I(BMS_TAG_DEFAULT, "DisconnectDelay");
707 DisconnectDelay();
708 }
709 }
710 }
711
OutTimeMonitor(std::string transactId)712 void BundleConnectAbilityMgr::OutTimeMonitor(std::string transactId)
713 {
714 LOG_I(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::OutTimeMonitor");
715 FreeInstallParams freeInstallParams;
716 std::unique_lock<std::mutex> lock(mapMutex_);
717 auto node = freeInstallParamsMap_.find(transactId);
718 if (node == freeInstallParamsMap_.end()) {
719 LOG_E(BMS_TAG_DEFAULT, "Can not find node");
720 return;
721 }
722 freeInstallParams = node->second;
723 lock.unlock();
724 auto RegisterEventListenerFunc = [this, freeInstallParams, transactId]() {
725 LOG_I(BMS_TAG_DEFAULT, "RegisterEventListenerFunc");
726 BundleMemoryGuard memoryGuard;
727 this->SendCallBack(FreeInstallErrorCode::SERVICE_CENTER_TIMEOUT,
728 freeInstallParams.want, freeInstallParams.userId, transactId);
729 };
730 serialQueue_->ScheduleDelayTask(transactId, OUT_TIME, RegisterEventListenerFunc);
731 }
732
SendRequest(int32_t flag,const TargetAbilityInfo & targetAbilityInfo,const Want & want,int32_t userId,const FreeInstallParams & freeInstallParams)733 void BundleConnectAbilityMgr::SendRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want,
734 int32_t userId, const FreeInstallParams &freeInstallParams)
735 {
736 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
737 MessageParcel data;
738 MessageParcel reply;
739 MessageOption option(MessageOption::TF_ASYNC);
740 if (!data.WriteInterfaceToken(SERVICE_CENTER_TOKEN)) {
741 LOG_E(BMS_TAG_DEFAULT, "failed to WriteInterfaceToken");
742 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
743 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
744 return;
745 }
746 const std::string dataString = GetJsonStrFromInfo(targetAbilityInfo);
747 LOG_I(BMS_TAG_DEFAULT, "TargetAbilityInfo to JsonString : %{public}s", dataString.c_str());
748 if (!data.WriteString16(Str8ToStr16(dataString))) {
749 LOG_E(BMS_TAG_DEFAULT, "failed to WriteParcelable targetAbilityInfo");
750 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
751 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
752 return;
753 }
754 sptr<ServiceCenterStatusCallback> callback = new(std::nothrow) ServiceCenterStatusCallback(weak_from_this());
755 if (callback == nullptr) {
756 LOG_E(BMS_TAG_DEFAULT, "callback is nullptr");
757 return;
758 }
759 if (!data.WriteRemoteObject(callback)) {
760 LOG_E(BMS_TAG_DEFAULT, "failed to WriteRemoteObject callbcak");
761 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
762 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
763 return;
764 }
765 std::unique_lock<std::mutex> mutexLock(mutex_);
766 sptr<IRemoteObject> serviceCenterRemoteObject = serviceCenterConnection_->GetRemoteObject();
767 if (serviceCenterRemoteObject == nullptr) {
768 LOG_E(BMS_TAG_DEFAULT, "failed to get remote object");
769 CallAbilityManager(FreeInstallErrorCode::CONNECT_ERROR, want, userId, freeInstallParams.callback);
770 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
771 return;
772 }
773 std::unique_lock<std::mutex> lock(mapMutex_);
774 auto emplaceResult = freeInstallParamsMap_.emplace(targetAbilityInfo.targetInfo.transactId, freeInstallParams);
775 LOG_I(BMS_TAG_DEFAULT, "emplace map size = %{public}zu, transactId = %{public}s",
776 freeInstallParamsMap_.size(), targetAbilityInfo.targetInfo.transactId.c_str());
777 if (!emplaceResult.second) {
778 LOG_E(BMS_TAG_DEFAULT, "freeInstallParamsMap emplace error");
779 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
780 return;
781 }
782 lock.unlock();
783 int32_t result = serviceCenterRemoteObject->SendRequest(flag, data, reply, option);
784 if (result != ERR_OK) {
785 LOG_E(BMS_TAG_DEFAULT, "Failed to sendRequest, result = %{public}d", result);
786 SendCallBack(FreeInstallErrorCode::CONNECT_ERROR, want, userId, targetAbilityInfo.targetInfo.transactId);
787 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
788 return;
789 }
790 OutTimeMonitor(targetAbilityInfo.targetInfo.transactId);
791 }
792
SendRequest(int32_t code,MessageParcel & data,MessageParcel & reply)793 bool BundleConnectAbilityMgr::SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply)
794 {
795 LOG_I(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::SendRequest to service center");
796 std::unique_lock<std::mutex> mutexLock(mutex_);
797 sptr<IRemoteObject> serviceCenterRemoteObject = serviceCenterConnection_->GetRemoteObject();
798 if (serviceCenterRemoteObject == nullptr) {
799 LOG_E(BMS_TAG_DEFAULT, "failed to get remote object");
800 return false;
801 }
802 MessageOption option(MessageOption::TF_ASYNC);
803 int32_t result = serviceCenterRemoteObject->SendRequest(code, data, reply, option);
804 if (result != ERR_OK) {
805 LOG_E(BMS_TAG_DEFAULT, "failed to send request code:%{public}d", code);
806 return false;
807 }
808 return true;
809 }
810
GetAbilityManagerServiceCallBack(std::string transactId)811 sptr<IRemoteObject> BundleConnectAbilityMgr::GetAbilityManagerServiceCallBack(std::string transactId)
812 {
813 LOG_I(BMS_TAG_DEFAULT, "GetAbilityManagerServiceCallBack");
814 FreeInstallParams freeInstallParams;
815 std::unique_lock<std::mutex> lock(mapMutex_);
816 auto node = freeInstallParamsMap_.find(transactId);
817 if (node == freeInstallParamsMap_.end()) {
818 LOG_E(BMS_TAG_DEFAULT, "Can not find node transactId = %{public}s", transactId.c_str());
819 return nullptr;
820 }
821 freeInstallParams = node->second;
822 lock.unlock();
823 return freeInstallParams.callback;
824 }
825
GetCallingInfo(int32_t userId,int32_t callingUid,std::vector<std::string> & bundleNames,std::vector<std::string> & callingAppIds)826 void BundleConnectAbilityMgr::GetCallingInfo(int32_t userId, int32_t callingUid,
827 std::vector<std::string> &bundleNames, std::vector<std::string> &callingAppIds)
828 {
829 LOG_I(BMS_TAG_DEFAULT, "enter");
830 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
831 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
832 if (bundleDataMgr_ == nullptr) {
833 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
834 return;
835 }
836 std::string bundleName;
837 if (bundleDataMgr_->GetBundleNameForUid(callingUid, bundleName)) {
838 bundleNames.emplace_back(bundleName);
839 } else {
840 LOG_E(BMS_TAG_DEFAULT, "GetBundleNameForUid failed");
841 }
842 BundleInfo bundleInfo;
843 if (bundleDataMgr_->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId)) {
844 callingAppIds.emplace_back(bundleInfo.appId);
845 } else {
846 LOG_E(BMS_TAG_DEFAULT, "GetBundleInfo failed");
847 }
848 }
849
ExistBundleNameInCallingBundles(const std::string & bundleName,const std::vector<std::string> & callingBundleNames)850 bool ExistBundleNameInCallingBundles(const std::string &bundleName, const std::vector<std::string> &callingBundleNames)
851 {
852 for (const auto &bundleNameItem : callingBundleNames) {
853 if (bundleNameItem == bundleName) {
854 return true;
855 }
856 }
857 return false;
858 }
859
GetTargetInfoFlag(const Want & want,const std::string & deviceId,const std::string & bundleName,const std::vector<std::string> & callingBundleNames)860 int32_t GetTargetInfoFlag(const Want &want, const std::string &deviceId, const std::string &bundleName,
861 const std::vector<std::string> &callingBundleNames)
862 {
863 // make int from bits.
864 int32_t flagZero = BIT_ZERO_COMPATIBLE;
865 int32_t flagOne = 0;
866 if ((want.GetFlags() & Want::FLAG_INSTALL_WITH_BACKGROUND_MODE) == 0) {
867 flagOne = BIT_ONE_FRONT_MODE * BIT_ONE;
868 } else {
869 flagOne = BIT_ONE_BACKGROUND_MODE * BIT_ONE;
870 }
871 int32_t flagTwo = BIT_TWO_CUSTOM * BIT_TWO;
872 int32_t flagThree = !deviceId.empty() * BIT_THREE;
873 int32_t flagFour = BIT_FOUR_AZ_DEVICE * BIT_FOUR;
874 int32_t flagFive = !ExistBundleNameInCallingBundles(bundleName, callingBundleNames) * BIT_FIVE;
875 int32_t flagSix = BIT_SIX_SAME_BUNDLE * BIT_SIX;
876 return flagZero + flagOne + flagTwo + flagThree + flagFour + flagFive + flagSix;
877 }
878
GetTargetAbilityInfo(const Want & want,int32_t userId,const InnerBundleInfo & innerBundleInfo,sptr<TargetAbilityInfo> & targetAbilityInfo)879 void BundleConnectAbilityMgr::GetTargetAbilityInfo(const Want &want, int32_t userId,
880 const InnerBundleInfo &innerBundleInfo, sptr<TargetAbilityInfo> &targetAbilityInfo)
881 {
882 if (targetAbilityInfo == nullptr) {
883 LOG_E(BMS_TAG_DEFAULT, "targetability info is nullptr");
884 return;
885 }
886 ElementName element = want.GetElement();
887 std::string bundleName = element.GetBundleName();
888 std::string moduleName = element.GetModuleName();
889 if (!GetModuleName(innerBundleInfo, want, moduleName)) {
890 LOG_W(BMS_TAG_DEFAULT, "GetModuleName failed");
891 }
892 std::string abilityName = element.GetAbilityName();
893 std::string deviceId = element.GetDeviceID();
894 std::vector<std::string> callingBundleNames;
895 std::vector<std::string> callingAppids;
896 auto wantParams = want.GetParams();
897 std::map<std::string, std::string> extValues;
898 for (auto it : wantParams.GetParams()) {
899 int typeId = WantParams::GetDataType(it.second);
900 auto info = wantParams.GetParam(it.first);
901 std::string value = wantParams.GetStringByType(info, typeId);
902 extValues.emplace(it.first, value);
903 }
904 auto callingUid = want.GetIntParam(PARAM_FREEINSTALL_UID, IPCSkeleton::GetCallingUid());
905
906 targetAbilityInfo->targetExtSetting.extValues = extValues;
907 targetAbilityInfo->targetInfo.transactId = std::to_string(this->GetTransactId());
908 targetAbilityInfo->targetInfo.bundleName = bundleName;
909 targetAbilityInfo->targetInfo.moduleName = moduleName;
910 targetAbilityInfo->targetInfo.abilityName = abilityName;
911 targetAbilityInfo->targetInfo.action = want.GetAction();
912 targetAbilityInfo->targetInfo.uri = want.GetUriString();
913 targetAbilityInfo->targetInfo.type = want.GetType();
914 targetAbilityInfo->targetInfo.callingUid = callingUid;
915 targetAbilityInfo->targetInfo.callingAppType = CALLING_TYPE_HARMONY;
916 if (callingUid == DMS_UID) {
917 callingAppids.push_back(want.GetStringParam(PARAM_FREEINSTALL_APPID));
918 callingBundleNames = want.GetStringArrayParam(PARAM_FREEINSTALL_BUNDLENAMES);
919 } else {
920 this->GetCallingInfo(userId, callingUid, callingBundleNames, callingAppids);
921 }
922 targetAbilityInfo->targetInfo.callingBundleNames = callingBundleNames;
923 targetAbilityInfo->targetInfo.flags = GetTargetInfoFlag(want, deviceId, bundleName, callingBundleNames);
924 targetAbilityInfo->targetInfo.reasonFlag = static_cast<int32_t>(innerBundleInfo.GetModuleUpgradeFlag(moduleName));
925 targetAbilityInfo->targetInfo.embedded = want.GetIntParam(PARAM_FREEINSTALL_EMBEDDED, DEFAULT_EMBEDDED_VALUE);
926 targetAbilityInfo->targetInfo.callingAppIds = callingAppids;
927 }
928
CheckSubPackageName(const InnerBundleInfo & innerBundleInfo,const Want & want)929 bool BundleConnectAbilityMgr::CheckSubPackageName(const InnerBundleInfo &innerBundleInfo, const Want &want)
930 {
931 std::string subPackageName = want.GetStringParam(PARAM_SUB_PACKAGE_NAME);
932 if (subPackageName.empty()) {
933 return true;
934 }
935 std::vector<std::string> moduleNameList;
936 innerBundleInfo.GetModuleNames(moduleNameList);
937 auto it = std::find(moduleNameList.begin(), moduleNameList.end(), subPackageName);
938 if (it == moduleNameList.end()) {
939 LOG_I(BMS_TAG_DEFAULT, "check subPackageName %{public}s failed", subPackageName.c_str());
940 return false;
941 }
942 LOG_I(BMS_TAG_DEFAULT, "check subPackageName %{public}s success", subPackageName.c_str());
943 return true;
944 }
945
CallAbilityManager(int32_t resultCode,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)946 void BundleConnectAbilityMgr::CallAbilityManager(
947 int32_t resultCode, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
948 {
949 if (callBack == nullptr) {
950 LOG_E(BMS_TAG_DEFAULT, "callBack is nullptr");
951 return;
952 }
953 MessageParcel data;
954 MessageParcel reply;
955 MessageOption option;
956 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
957 LOG_E(BMS_TAG_DEFAULT, "Write interface token failed");
958 return;
959 }
960 if (!data.WriteInt32(resultCode)) {
961 LOG_E(BMS_TAG_DEFAULT, "Write result code failed");
962 return;
963 }
964 if (!data.WriteParcelable(&want)) {
965 LOG_E(BMS_TAG_DEFAULT, "Write want failed");
966 return;
967 }
968 if (!data.WriteInt32(userId)) {
969 LOG_E(BMS_TAG_DEFAULT, "Write userId failed");
970 return;
971 }
972
973 if (callBack->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
974 LOG_E(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::CallAbilityManager SendRequest failed");
975 }
976 }
977
CheckIsModuleNeedUpdate(InnerBundleInfo & innerBundleInfo,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)978 bool BundleConnectAbilityMgr::CheckIsModuleNeedUpdate(
979 InnerBundleInfo &innerBundleInfo, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
980 {
981 LOG_I(BMS_TAG_DEFAULT, "CheckIsModuleNeedUpdate called");
982 std::string moduleName = want.GetModuleName();
983 if (!GetModuleName(innerBundleInfo, want, moduleName)) {
984 LOG_W(BMS_TAG_DEFAULT, "GetModuleName failed");
985 }
986 if (innerBundleInfo.GetModuleUpgradeFlag(moduleName) != 0) {
987 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
988 if (targetAbilityInfo == nullptr) {
989 LOG_E(BMS_TAG_DEFAULT, "targetAbilityInfo is nullptr");
990 return false;
991 }
992 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
993 if (targetInfo == nullptr) {
994 LOG_E(BMS_TAG_DEFAULT, "targetInfo is nullptr");
995 return false;
996 }
997 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
998 if (targetExtSetting == nullptr) {
999 LOG_E(BMS_TAG_DEFAULT, "targetExtSetting is nullptr");
1000 return false;
1001 }
1002 targetAbilityInfo->targetInfo = *targetInfo;
1003 targetAbilityInfo->targetExtSetting = *targetExtSetting;
1004 targetAbilityInfo->version = DEFAULT_VERSION;
1005 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
1006 if (targetAbilityInfo->targetInfo.moduleName.empty()) {
1007 targetAbilityInfo->targetInfo.moduleName = moduleName;
1008 }
1009 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
1010 if (freeInstallParams == nullptr) {
1011 LOG_E(BMS_TAG_DEFAULT, "freeInstallParams is nullptr");
1012 return false;
1013 }
1014 freeInstallParams->callback = callBack;
1015 freeInstallParams->want = want;
1016 freeInstallParams->userId = userId;
1017 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_UPGRADE_INSTALL;
1018 this->UpgradeInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
1019 return true;
1020 }
1021 LOG_I(BMS_TAG_DEFAULT, "Module is not need update");
1022 return false;
1023 }
1024
IsObtainAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack,InnerBundleInfo & innerBundleInfo)1025 bool BundleConnectAbilityMgr::IsObtainAbilityInfo(const Want &want, int32_t flags, int32_t userId,
1026 AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack, InnerBundleInfo &innerBundleInfo)
1027 {
1028 LOG_D(BMS_TAG_DEFAULT, "IsObtainAbilityInfo");
1029 std::string bundleName = want.GetElement().GetBundleName();
1030 std::string abilityName = want.GetElement().GetAbilityName();
1031 std::string moduleName = want.GetElement().GetModuleName();
1032 if (bundleName == "") {
1033 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, callBack);
1034 LOG_E(BMS_TAG_DEFAULT, "bundle name or ability name is null");
1035 return false;
1036 }
1037 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
1038 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
1039 if (bundleDataMgr_ == nullptr) {
1040 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
1041 return false;
1042 }
1043 bool innerBundleInfoResult = bundleDataMgr_->GetInnerBundleInfoWithBundleFlagsAndLock(bundleName,
1044 flags, innerBundleInfo, userId);
1045 EventReport::SendFreeInstallEvent(bundleName, abilityName, moduleName, innerBundleInfoResult,
1046 BundleUtil::GetCurrentTime());
1047 if (!innerBundleInfoResult) {
1048 APP_LOGE("GetInnerBundleInfoWithBundleFlagsAndLock failed");
1049 return false;
1050 }
1051 if (abilityName.empty()) {
1052 Want launchWant;
1053 auto launchWantResult = bundleDataMgr_->GetLaunchWantForBundle(bundleName, launchWant, userId);
1054 if (launchWantResult == ERR_OK) {
1055 (const_cast<Want &>(want)).SetModuleName(launchWant.GetModuleName());
1056 }
1057 return CheckIsModuleNeedUpdateWrap(innerBundleInfo, want, userId, callBack);
1058 }
1059 bool abilityInfoResult = bundleDataMgr_->QueryAbilityInfo(want, flags, userId, abilityInfo);
1060 if (!abilityInfoResult) {
1061 std::vector<ExtensionAbilityInfo> extensionInfos;
1062 abilityInfoResult = bundleDataMgr_->QueryExtensionAbilityInfos(want, flags, userId, extensionInfos);
1063 if (abilityInfoResult && moduleName.empty()) {
1064 moduleName = extensionInfos[0].moduleName;
1065 }
1066 } else {
1067 if (moduleName.empty()) {
1068 moduleName = abilityInfo.moduleName;
1069 }
1070 }
1071 if (abilityInfoResult) {
1072 if (!CheckSubPackageName(innerBundleInfo, want)) {
1073 return false;
1074 }
1075 return CheckIsModuleNeedUpdateWrap(innerBundleInfo, want, userId, callBack);
1076 }
1077 return false;
1078 }
1079
CheckIsModuleNeedUpdateWrap(InnerBundleInfo & innerBundleInfo,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)1080 bool BundleConnectAbilityMgr::CheckIsModuleNeedUpdateWrap(InnerBundleInfo &innerBundleInfo, const Want &want,
1081 int32_t userId, const sptr<IRemoteObject> &callBack)
1082 {
1083 bool isModuleNeedUpdate = CheckIsModuleNeedUpdate(innerBundleInfo, want, userId, callBack);
1084 if (!isModuleNeedUpdate) {
1085 CallAbilityManager(ServiceCenterResultCode::FREE_INSTALL_OK, want, userId, callBack);
1086 }
1087 return true;
1088 }
1089
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)1090 bool BundleConnectAbilityMgr::QueryAbilityInfo(const Want &want, int32_t flags,
1091 int32_t userId, AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack)
1092 {
1093 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1094 LOG_D(BMS_TAG_DEFAULT, "QueryAbilityInfo");
1095 InnerBundleInfo innerBundleInfo;
1096 if (IsObtainAbilityInfo(want, flags, userId, abilityInfo, callBack, innerBundleInfo)) {
1097 return true;
1098 }
1099 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
1100 if (targetAbilityInfo == nullptr) {
1101 LOG_E(BMS_TAG_DEFAULT, "targetAbilityInfo is nullptr");
1102 return false;
1103 }
1104 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
1105 if (targetInfo == nullptr) {
1106 LOG_E(BMS_TAG_DEFAULT, "targetInfo is nullptr");
1107 return false;
1108 }
1109 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
1110 if (targetExtSetting == nullptr) {
1111 LOG_E(BMS_TAG_DEFAULT, "targetExtSetting is nullptr");
1112 return false;
1113 }
1114 targetAbilityInfo->targetInfo = *targetInfo;
1115 targetAbilityInfo->targetExtSetting = *targetExtSetting;
1116 targetAbilityInfo->version = DEFAULT_VERSION;
1117 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
1118 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
1119 if (freeInstallParams == nullptr) {
1120 LOG_E(BMS_TAG_DEFAULT, "freeInstallParams is nullptr");
1121 return false;
1122 }
1123 freeInstallParams->callback = callBack;
1124 freeInstallParams->want = want;
1125 freeInstallParams->userId = userId;
1126 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
1127
1128 this->SilentInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
1129 return false;
1130 }
1131
SilentInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)1132 bool BundleConnectAbilityMgr::SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
1133 {
1134 LOG_D(BMS_TAG_DEFAULT, "SilentInstall");
1135 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
1136 if (targetAbilityInfo == nullptr) {
1137 LOG_E(BMS_TAG_DEFAULT, "targetAbilityInfo is nullptr");
1138 return false;
1139 }
1140 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
1141 if (targetInfo == nullptr) {
1142 LOG_E(BMS_TAG_DEFAULT, "targetInfo is nullptr");
1143 return false;
1144 }
1145 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
1146 if (targetExtSetting == nullptr) {
1147 LOG_E(BMS_TAG_DEFAULT, "targetExtSetting is nullptr");
1148 return false;
1149 }
1150
1151 targetAbilityInfo->targetInfo = *targetInfo;
1152 targetAbilityInfo->targetExtSetting = *targetExtSetting;
1153 targetAbilityInfo->version = DEFAULT_VERSION;
1154 InnerBundleInfo innerBundleInfo;
1155 GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
1156 auto callingUid = IPCSkeleton::GetCallingUid();
1157 std::vector<std::string> callingBundleNames;
1158 std::vector<std::string> callingAppids;
1159 GetCallingInfo(userId, callingUid, callingBundleNames, callingAppids);
1160 targetAbilityInfo->targetInfo.callingUid = callingUid;
1161 targetAbilityInfo->targetInfo.callingBundleNames = callingBundleNames;
1162 targetAbilityInfo->targetInfo.callingAppIds = callingAppids;
1163 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
1164 if (freeInstallParams == nullptr) {
1165 LOG_E(BMS_TAG_DEFAULT, "freeInstallParams is nullptr");
1166 return false;
1167 }
1168
1169 freeInstallParams->callback = callBack;
1170 freeInstallParams->want = want;
1171 freeInstallParams->userId = userId;
1172 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
1173 SilentInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
1174 return true;
1175 }
1176
UpgradeAtomicService(const Want & want,int32_t userId)1177 void BundleConnectAbilityMgr::UpgradeAtomicService(const Want &want, int32_t userId)
1178 {
1179 LOG_I(BMS_TAG_DEFAULT, "UpgradeAtomicService");
1180 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
1181 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
1182 if (bundleDataMgr_ == nullptr) {
1183 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
1184 return;
1185 }
1186 std::string bundleName = want.GetElement().GetBundleName();
1187 InnerBundleInfo innerBundleInfo;
1188 bundleDataMgr_->GetInnerBundleInfoWithBundleFlagsAndLock(bundleName, want.GetFlags(), innerBundleInfo, userId);
1189 if (!innerBundleInfo.GetEntryInstallationFree()) {
1190 LOG_I(BMS_TAG_DEFAULT, "bundleName:%{public}s is atomic application", bundleName.c_str());
1191 return;
1192 }
1193 LOG_I(BMS_TAG_DEFAULT, "bundleName:%{public}s is atomic service", bundleName.c_str());
1194 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
1195 if (targetAbilityInfo == nullptr) {
1196 LOG_E(BMS_TAG_DEFAULT, "targetAbilityInfo is nullptr");
1197 return;
1198 }
1199 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
1200 if (targetInfo == nullptr) {
1201 LOG_E(BMS_TAG_DEFAULT, "targetInfo is nullptr");
1202 return;
1203 }
1204 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
1205 if (targetExtSetting == nullptr) {
1206 LOG_E(BMS_TAG_DEFAULT, "targetExtSetting is nullptr");
1207 return;
1208 }
1209 targetAbilityInfo->targetInfo = *targetInfo;
1210 targetAbilityInfo->targetExtSetting = *targetExtSetting;
1211 targetAbilityInfo->version = DEFAULT_VERSION;
1212 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
1213 if (!GetModuleName(innerBundleInfo, want, targetAbilityInfo->targetInfo.moduleName)) {
1214 LOG_W(BMS_TAG_DEFAULT, "GetModuleName failed");
1215 }
1216
1217 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
1218 if (freeInstallParams == nullptr) {
1219 LOG_E(BMS_TAG_DEFAULT, "freeInstallParams is nullptr");
1220 return;
1221 }
1222 freeInstallParams->want = want;
1223 freeInstallParams->userId = userId;
1224 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_UPGRADE_CHECK;
1225 this->UpgradeCheck(*targetAbilityInfo, want, *freeInstallParams, userId);
1226 }
1227
CheckEcologicalRule(const Want & want,ErmsCallerInfo & callerInfo,BmsExperienceRule & rule)1228 bool BundleConnectAbilityMgr::CheckEcologicalRule(const Want &want, ErmsCallerInfo &callerInfo, BmsExperienceRule &rule)
1229 {
1230 sptr<BmsEcologicalRuleMgrServiceClient> instance_ =
1231 BmsEcologicalRuleMgrServiceClient::GetInstance();
1232 if (instance_ == nullptr) {
1233 LOG_E(BMS_TAG_DEFAULT, "Failed to get instance from erms");
1234 return false;
1235 }
1236 int ret = instance_->QueryFreeInstallExperience(want, callerInfo, rule);
1237 if (ret != ERR_OK) {
1238 LOG_E(BMS_TAG_DEFAULT, "Failed to query free install experience from erms");
1239 return false;
1240 }
1241 return true;
1242 }
1243
GetEcologicalCallerInfo(const Want & want,ErmsCallerInfo & callerInfo,int32_t userId)1244 void BundleConnectAbilityMgr::GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId)
1245 {
1246 callerInfo.packageName = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
1247 callerInfo.uid = want.GetIntParam(PARAM_FREEINSTALL_UID, IPCSkeleton::GetCallingUid());
1248 callerInfo.pid = want.GetIntParam(Want::PARAM_RESV_CALLER_PID, DEFAULT_VALUE);
1249 callerInfo.targetAppType = TYPE_HARMONEY_SERVICE;
1250 callerInfo.callerAppType = TYPE_HARMONEY_INVALID;
1251 callerInfo.targetAppDistType = want.GetStringParam(PARAM_FREEINSTALL_TARGET_APP_DIST_TYPE);
1252 callerInfo.embedded = want.GetIntParam(PARAM_FREEINSTALL_EMBEDDED, DEFAULT_EMBEDDED_VALUE);
1253 callerInfo.targetAppProvisionType = want.GetStringParam(PARAM_FREEINSTALL_TARGET_APP_PROVISION_TYPE);
1254 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
1255 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
1256 if (bundleDataMgr_ == nullptr) {
1257 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
1258 return;
1259 }
1260 std::string callerBundleName;
1261 ErrCode err = bundleDataMgr_->GetNameForUid(callerInfo.uid, callerBundleName);
1262 if (err != ERR_OK) {
1263 LOG_E(BMS_TAG_DEFAULT, "Get callerBundleName failed");
1264 return;
1265 }
1266 AppExecFwk::ApplicationInfo callerAppInfo;
1267 bool getCallerResult = bundleDataMgr_->GetApplicationInfo(callerBundleName,
1268 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, callerAppInfo);
1269 if (!getCallerResult) {
1270 LOG_E(BMS_TAG_DEFAULT, "Get callerAppInfo failed");
1271 return;
1272 }
1273 callerInfo.callerAppProvisionType = callerAppInfo.appProvisionType;
1274 InnerBundleInfo info;
1275 bool getInnerBundleInfoRes = bundleDataMgr_->GetInnerBundleInfoWithBundleFlagsAndLock(callerBundleName,
1276 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, info, userId);
1277 if (!getInnerBundleInfoRes) {
1278 APP_LOGW("Get callerInnerBundleInfo failed");
1279 return;
1280 }
1281 callerInfo.callerModelType = BmsCallerInfo::MODEL_FA;
1282 if (info.GetIsNewVersion()) {
1283 callerInfo.callerModelType = BmsCallerInfo::MODEL_STAGE;
1284 }
1285 if (callerAppInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE) {
1286 LOG_D(BMS_TAG_DEFAULT, "the caller type is atomic service");
1287 callerInfo.callerAppType = TYPE_HARMONEY_SERVICE;
1288 } else if (callerAppInfo.bundleType == AppExecFwk::BundleType::APP) {
1289 LOG_D(BMS_TAG_DEFAULT, "the caller type is app");
1290 callerInfo.callerAppType = TYPE_HARMONEY_APP;
1291 } else {
1292 LOG_D(BMS_TAG_DEFAULT, "the caller type is invalid type");
1293 }
1294 }
1295
CheckIsOnDemandLoad(const TargetAbilityInfo & targetAbilityInfo) const1296 bool BundleConnectAbilityMgr::CheckIsOnDemandLoad(const TargetAbilityInfo &targetAbilityInfo) const
1297 {
1298 if (targetAbilityInfo.targetInfo.callingBundleNames.empty()) {
1299 LOG_D(BMS_TAG_DEFAULT, "callingBundleNames in targetAbilityInfo is empty");
1300 return false;
1301 }
1302 if (targetAbilityInfo.targetInfo.callingBundleNames[0] != targetAbilityInfo.targetInfo.bundleName) {
1303 LOG_D(BMS_TAG_DEFAULT, "callingBundleName is different with target bundleName");
1304 return false;
1305 }
1306 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
1307 if (bms == nullptr) {
1308 LOG_E(BMS_TAG_DEFAULT, "BundleMgrService GetInstance failed");
1309 return false;
1310 }
1311 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
1312 if (bundleDataMgr_ == nullptr) {
1313 LOG_E(BMS_TAG_DEFAULT, "GetDataMgr failed, bundleDataMgr_ is nullptr");
1314 return false;
1315 }
1316 BundleInfo bundleInfo;
1317 if (bundleDataMgr_->GetBundleInfo(
1318 targetAbilityInfo.targetInfo.bundleName, GET_BUNDLE_DEFAULT, bundleInfo, Constants::ANY_USERID)) {
1319 return bundleInfo.applicationInfo.bundleType == BundleType::ATOMIC_SERVICE;
1320 }
1321 return false;
1322 }
1323
GetModuleName(const InnerBundleInfo & innerBundleInfo,const Want & want,std::string & moduleName) const1324 bool BundleConnectAbilityMgr::GetModuleName(const InnerBundleInfo &innerBundleInfo,
1325 const Want &want, std::string &moduleName) const
1326 {
1327 if (!moduleName.empty()) {
1328 return true;
1329 }
1330 auto baseAbilitiesInfo = innerBundleInfo.GetInnerAbilityInfos();
1331 ElementName element = want.GetElement();
1332 std::string abilityName = element.GetAbilityName();
1333 for (const auto& info : baseAbilitiesInfo) {
1334 if (info.second.name == abilityName) {
1335 moduleName = info.second.moduleName;
1336 return true;
1337 }
1338 }
1339 std::string wantModuleName = element.GetModuleName();
1340 if (abilityName.empty() && wantModuleName.empty()) {
1341 moduleName = innerBundleInfo.GetEntryModuleName();
1342 LOG_I(BMS_TAG_DEFAULT, "get entry module name %{public}s", moduleName.c_str());
1343 return !moduleName.empty();
1344 }
1345 LOG_E(BMS_TAG_DEFAULT, "GetModuleName failed, ability(%{public}s) is not existed in bundle(%{public}s)",
1346 abilityName.c_str(), innerBundleInfo.GetBundleName().c_str());
1347 return false;
1348 }
1349 } // namespace AppExecFwk
1350 } // namespace OHOS
1351