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