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_wrapper.h"
20 #include "bundle_mgr_service.h"
21 #include "free_install_params.h"
22 #include "json_util.h"
23 #include "parcel.h"
24 #include "service_center_connection.h"
25 #include "service_center_status_callback.h"
26 #include "string_ex.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string SERVICE_CENTER_BUNDLE_NAME = "com.ohos.hag.famanager";
32 const std::string SERVICE_CENTER_ABILITY_NAME = "HapInstallServiceAbility";
33 const std::string PARAM_FREEINSTALL_APPID = "ohos.freeinstall.params.callingAppId";
34 const std::string PARAM_FREEINSTALL_BUNDLENAMES = "ohos.freeinstall.params.callingBundleNames";
35 const std::string PARAM_FREEINSTALL_UID = "ohos.freeinstall.params.callingUid";
36 const std::string DISCONNECT_DELAY_TASK = "DisconnectDelayTask";
37 const std::string DEFAULT_VERSION = "1";
38 const std::string CONNECT_ABILITY_THREAD = "ConnectAbilityThread";
39 constexpr uint32_t CALLING_TYPE_HARMONY = 2;
40 constexpr uint32_t BIT_ZERO_COMPATIBLE = 0;
41 constexpr uint32_t BIT_ONE_FRONT_MODE = 0;
42 constexpr uint32_t BIT_ONE_BACKGROUND_MODE = 1;
43 constexpr uint32_t BIT_TWO_CUSTOM = 0;
44 constexpr uint32_t BIT_THREE_ZERO = 0;
45 constexpr uint32_t BIT_FOUR_AZ_DEVICE = 0;
46 constexpr uint32_t BIT_FIVE_SAME_BUNDLE_NAME = 0;
47 constexpr uint32_t BIT_SIX_SAME_BUNDLE = 0;
48 constexpr uint32_t BIT_ONE = 2;
49 constexpr uint32_t BIT_TWO = 4;
50 constexpr uint32_t BIT_THREE = 8;
51 constexpr uint32_t BIT_FOUR = 16;
52 constexpr uint32_t BIT_FIVE = 32;
53 constexpr uint32_t BIT_SIX = 64;
54 constexpr uint32_t DISCONNECT_DELAY = 20000;
55 constexpr uint32_t OUT_TIME = 30000;
56
SendSysEvent(int32_t resultCode,const AAFwk::Want & want,int32_t userId)57 void SendSysEvent(int32_t resultCode, const AAFwk::Want &want, int32_t userId)
58 {
59 EventInfo sysEventInfo;
60 ElementName element = want.GetElement();
61 sysEventInfo.bundleName = element.GetBundleName();
62 sysEventInfo.moduleName = element.GetModuleName();
63 sysEventInfo.abilityName = element.GetAbilityName();
64 sysEventInfo.isFreeInstallMode = true;
65 sysEventInfo.userId = userId;
66 sysEventInfo.errCode = resultCode;
67 EventReport::SendSystemEvent(BMSEventType::BUNDLE_INSTALL_EXCEPTION, sysEventInfo);
68 }
69 }
70
Init()71 void BundleConnectAbilityMgr::Init()
72 {
73 runner_ = EventRunner::Create(CONNECT_ABILITY_THREAD);
74 if (runner_ == nullptr) {
75 APP_LOGE("Create runner failed");
76 return;
77 }
78
79 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
80 if (handler_ == nullptr) {
81 APP_LOGE("Create handler failed");
82 }
83 }
84
BundleConnectAbilityMgr()85 BundleConnectAbilityMgr::BundleConnectAbilityMgr()
86 {
87 Init();
88 }
89
~BundleConnectAbilityMgr()90 BundleConnectAbilityMgr::~BundleConnectAbilityMgr()
91 {
92 if (handler_ != nullptr) {
93 handler_.reset();
94 }
95 if (runner_ != nullptr) {
96 runner_.reset();
97 }
98 }
99
ProcessPreloadCheck(const TargetAbilityInfo & targetAbilityInfo)100 bool BundleConnectAbilityMgr::ProcessPreloadCheck(const TargetAbilityInfo &targetAbilityInfo)
101 {
102 APP_LOGD("ProcessPreloadCheck");
103 if (handler_ == nullptr) {
104 APP_LOGE("handler is null");
105 return false;
106 }
107 auto PreloadCheckFunc = [this, targetAbilityInfo]() {
108 int32_t flag = ServiceCenterFunction::CONNECT_PRELOAD_INSTALL;
109 this->ProcessPreloadRequestToServiceCenter(flag, targetAbilityInfo);
110 };
111 handler_->PostTask(PreloadCheckFunc, targetAbilityInfo.targetInfo.transactId.c_str());
112 return true;
113 }
114
ProcessPreloadRequestToServiceCenter(int32_t flag,const TargetAbilityInfo & targetAbilityInfo)115 void BundleConnectAbilityMgr::ProcessPreloadRequestToServiceCenter(int32_t flag,
116 const TargetAbilityInfo &targetAbilityInfo)
117 {
118 APP_LOGD("ProcessPreloadRequestToServiceCenter");
119 Want serviceCenterWant;
120 serviceCenterWant.SetElementName(SERVICE_CENTER_BUNDLE_NAME, SERVICE_CENTER_ABILITY_NAME);
121 bool isConnectSuccess = ConnectAbility(serviceCenterWant, nullptr);
122 if (!isConnectSuccess) {
123 APP_LOGE("Fail to connect ServiceCenter");
124 return;
125 } else {
126 PreloadRequest(flag, targetAbilityInfo);
127 return;
128 }
129 }
130
PreloadRequest(int32_t flag,const TargetAbilityInfo & targetAbilityInfo)131 void BundleConnectAbilityMgr::PreloadRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo)
132 {
133 MessageParcel data;
134 MessageParcel reply;
135 MessageOption option(MessageOption::TF_ASYNC);
136 if (!data.WriteInterfaceToken(SERVICE_CENTER_TOKEN)) {
137 APP_LOGE("failed to WriteInterfaceToken");
138 return;
139 }
140 const std::string dataString = GetJsonStrFromInfo(targetAbilityInfo);
141 APP_LOGI("TargetAbilityInfo to JsonString : %{public}s", dataString.c_str());
142 if (!data.WriteString16(Str8ToStr16(dataString))) {
143 APP_LOGE("%{public}s failed to WriteParcelable targetAbilityInfo", __func__);
144 return;
145 }
146 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
147 if (serviceCenterRemoteObject_ == nullptr) {
148 APP_LOGE("%{public}s failed to get remote object", __func__);
149 return;
150 }
151 int32_t result = serviceCenterRemoteObject_->SendRequest(flag, data, reply, option);
152 if (result != ERR_OK) {
153 APP_LOGE("Failed to sendRequest, result = %{public}d", result);
154 }
155 APP_LOGD("sendRequest to service center success.");
156 }
157
GetPreloadFlag()158 int32_t BundleConnectAbilityMgr::GetPreloadFlag()
159 {
160 int32_t flagZero = BIT_ZERO_COMPATIBLE;
161 int32_t flagOne = BIT_ONE_BACKGROUND_MODE * BIT_ONE;
162 int32_t flagTwo = BIT_TWO_CUSTOM * BIT_TWO;
163 int32_t flagThree = BIT_THREE_ZERO * BIT_THREE;
164 int32_t flagFour = BIT_FOUR_AZ_DEVICE * BIT_FOUR;
165 int32_t flagFive = BIT_FIVE_SAME_BUNDLE_NAME * BIT_FIVE;
166 int32_t flagSix = BIT_SIX_SAME_BUNDLE * BIT_SIX;
167 return flagZero + flagOne + flagTwo + flagThree + flagFour + flagFive + flagSix;
168 }
169
GetPreloadList(const std::string & bundleName,const std::string & moduleName,int32_t userId,sptr<TargetAbilityInfo> & targetAbilityInfo)170 bool BundleConnectAbilityMgr::GetPreloadList(const std::string &bundleName, const std::string &moduleName,
171 int32_t userId, sptr<TargetAbilityInfo> &targetAbilityInfo)
172 {
173 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
174 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
175 if (bundleDataMgr_ == nullptr) {
176 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
177 return false;
178 }
179 InnerBundleInfo innerBundleInfo;
180 int32_t flag = ApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE;
181 auto ret = bundleDataMgr_->GetInnerBundleInfoWithFlags(bundleName, flag, innerBundleInfo, userId);
182 if (!ret) {
183 APP_LOGE("GetInnerBundleInfoWithFlags failed.");
184 return false;
185 }
186 if (innerBundleInfo.GetBaseApplicationInfo().bundleType == BundleType::APP) {
187 return false;
188 }
189 if (moduleName.empty()) {
190 APP_LOGE("moduleName is empty.");
191 return false;
192 }
193 std::set<std::string> preloadModuleNames;
194 auto moduleInfoMap = innerBundleInfo.GetInnerModuleInfos();
195 if (moduleInfoMap.find(moduleName) == moduleInfoMap.end()) {
196 APP_LOGE("get moduleInfo from innerBundleInfo failed.");
197 return false;
198 }
199 auto preloadItems = moduleInfoMap[moduleName].preloads;
200 if (preloadItems.empty()) {
201 return false;
202 }
203 for (const auto &item : preloadItems) {
204 preloadModuleNames.insert(item);
205 }
206 for (const auto &it : moduleInfoMap) {
207 auto iter = preloadModuleNames.find(it.first);
208 if (iter != preloadModuleNames.end()) {
209 preloadModuleNames.erase(iter);
210 }
211 }
212 if (preloadModuleNames.empty()) {
213 APP_LOGD("All preload modules exist locally.");
214 return false;
215 }
216 targetAbilityInfo->targetInfo.callingAppIds.emplace_back(innerBundleInfo.GetBaseBundleInfo().appId);
217 for (const auto &item : preloadModuleNames) {
218 targetAbilityInfo->targetInfo.preloadModuleNames.emplace_back(item);
219 }
220 return true;
221 }
222
ProcessPreload(const Want & want)223 void BundleConnectAbilityMgr::ProcessPreload(const Want &want)
224 {
225 APP_LOGD("BundleConnectAbilityMgr::ProcessPreload is called.");
226 std::string bundleName = want.GetElement().GetBundleName();
227 std::string moduleName = want.GetElement().GetModuleName();
228 std::string abilityName = want.GetElement().GetAbilityName();
229 int32_t uid = want.GetIntParam("uid", 0);
230 int32_t userId = uid / Constants::BASE_USER_RANGE;
231 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
232 if (targetAbilityInfo == nullptr) {
233 APP_LOGE("targetAbilityInfo is nullptr");
234 return;
235 }
236 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
237 if (targetInfo == nullptr) {
238 APP_LOGE("targetInfo is nullptr");
239 return;
240 }
241 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
242 if (targetExtSetting == nullptr) {
243 APP_LOGE("targetExtSetting is nullptr");
244 return;
245 }
246 targetAbilityInfo->targetInfo = *targetInfo;
247 targetAbilityInfo->targetExtSetting = *targetExtSetting;
248 targetAbilityInfo->version = DEFAULT_VERSION;
249
250 if (!GetPreloadList(bundleName, moduleName, userId, targetAbilityInfo)) {
251 APP_LOGI("the module have no preload module.");
252 return;
253 }
254 targetAbilityInfo->targetInfo.transactId = std::to_string(this->GetTransactId());
255 targetAbilityInfo->targetInfo.bundleName = bundleName;
256 targetAbilityInfo->targetInfo.moduleName = moduleName;
257 targetAbilityInfo->targetInfo.abilityName = abilityName;
258 targetAbilityInfo->targetInfo.flags = GetPreloadFlag();
259 targetAbilityInfo->targetInfo.callingUid = uid;
260 targetAbilityInfo->targetInfo.callingAppType = CALLING_TYPE_HARMONY;
261 targetAbilityInfo->targetInfo.callingBundleNames.emplace_back(bundleName);
262 ProcessPreloadCheck(*targetAbilityInfo);
263 }
264
SilentInstall(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)265 bool BundleConnectAbilityMgr::SilentInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
266 const FreeInstallParams &freeInstallParams, int32_t userId)
267 {
268 APP_LOGI("SilentInstall");
269 if (handler_ == nullptr) {
270 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
271 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
272 APP_LOGE("handler is null");
273 return false;
274 }
275 auto silentInstallFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
276 int32_t flag = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
277 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
278 };
279 handler_->PostTask(silentInstallFunc, targetAbilityInfo.targetInfo.transactId.c_str());
280 return true;
281 }
282
UpgradeCheck(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)283 bool BundleConnectAbilityMgr::UpgradeCheck(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
284 const FreeInstallParams &freeInstallParams, int32_t userId)
285 {
286 APP_LOGI("UpgradeCheck");
287 if (handler_ == nullptr) {
288 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
289 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
290 APP_LOGE("handler is null");
291 return false;
292 }
293 auto upgradeCheckFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
294 int32_t flag = ServiceCenterFunction::CONNECT_UPGRADE_CHECK;
295 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
296 };
297 handler_->PostTask(upgradeCheckFunc, targetAbilityInfo.targetInfo.transactId.c_str());
298 return true;
299 }
300
UpgradeInstall(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)301 bool BundleConnectAbilityMgr::UpgradeInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
302 const FreeInstallParams &freeInstallParams, int32_t userId)
303 {
304 APP_LOGI("UpgradeInstall");
305 if (handler_ == nullptr) {
306 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
307 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
308 APP_LOGE("handler is null");
309 return false;
310 }
311 auto upgradeInstallFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
312 int32_t flag = ServiceCenterFunction::CONNECT_UPGRADE_INSTALL;
313 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
314 };
315 handler_->PostTask(upgradeInstallFunc, targetAbilityInfo.targetInfo.transactId.c_str());
316 return true;
317 }
318
SendRequestToServiceCenter(int32_t flag,const TargetAbilityInfo & targetAbilityInfo,const Want & want,int32_t userId,const FreeInstallParams & freeInstallParams)319 bool BundleConnectAbilityMgr::SendRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo,
320 const Want &want, int32_t userId, const FreeInstallParams &freeInstallParams)
321 {
322 APP_LOGI("SendRequestToServiceCenter");
323 Want serviceCenterWant;
324 serviceCenterWant.SetElementName(SERVICE_CENTER_BUNDLE_NAME, SERVICE_CENTER_ABILITY_NAME);
325 bool isConnectSuccess = ConnectAbility(serviceCenterWant, nullptr);
326 if (!isConnectSuccess) {
327 if (freeInstallParams.serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL) {
328 APP_LOGE("Fail to connect ServiceCenter, but freeinstall upgrade return ok");
329 CallAbilityManager(ServiceCenterResultCode::FREE_INSTALL_OK, want, userId, freeInstallParams.callback);
330 } else {
331 APP_LOGE("Fail to connect ServiceCenter");
332 CallAbilityManager(FreeInstallErrorCode::CONNECT_ERROR, want, userId, freeInstallParams.callback);
333 }
334 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
335 return false;
336 } else {
337 SendRequest(flag, targetAbilityInfo, want, userId, freeInstallParams);
338 return true;
339 }
340 }
341
DisconnectAbility()342 void BundleConnectAbilityMgr::DisconnectAbility()
343 {
344 if (serviceCenterConnection_ != nullptr) {
345 APP_LOGI("DisconnectAbility");
346 int result = AbilityManagerClient::GetInstance()->DisconnectAbility(serviceCenterConnection_);
347 if (result != ERR_OK) {
348 APP_LOGE("BundleConnectAbilityMgr::DisconnectAbility fail, resultCode: %{public}d", result);
349 }
350 }
351 }
352
WaitFromConnecting(std::unique_lock<std::mutex> & lock)353 void BundleConnectAbilityMgr::WaitFromConnecting(std::unique_lock<std::mutex> &lock)
354 {
355 APP_LOGI("ConnectAbility await start CONNECTING");
356 while (connectState_ == ServiceCenterConnectState::CONNECTING) {
357 cv_.wait(lock);
358 }
359 APP_LOGI("ConnectAbility await end CONNECTING");
360 }
361
WaitFromConnected(std::unique_lock<std::mutex> & lock)362 void BundleConnectAbilityMgr::WaitFromConnected(std::unique_lock<std::mutex> &lock)
363 {
364 APP_LOGI("ConnectAbility await start CONNECTED");
365 while (connectState_ != ServiceCenterConnectState::CONNECTED) {
366 if (connectState_ == ServiceCenterConnectState::DISCONNECTED) {
367 break;
368 }
369 cv_.wait(lock);
370 }
371 APP_LOGI("ConnectAbility await end CONNECTED");
372 }
373
ConnectAbility(const Want & want,const sptr<IRemoteObject> & callerToken)374 bool BundleConnectAbilityMgr::ConnectAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
375 {
376 APP_LOGI("ConnectAbility start target bundle = %{public}s", want.GetBundle().c_str());
377 std::unique_lock<std::mutex> lock(mutex_);
378 if (handler_ != nullptr) {
379 handler_->RemoveTask(DISCONNECT_DELAY_TASK);
380 }
381 if (connectState_ == ServiceCenterConnectState::CONNECTING) {
382 WaitFromConnecting(lock);
383 } else if (connectState_ == ServiceCenterConnectState::DISCONNECTED) {
384 connectState_ = ServiceCenterConnectState::CONNECTING;
385 serviceCenterConnection_ = new (std::nothrow) ServiceCenterConnection(connectState_,
386 cv_, weak_from_this());
387 if (serviceCenterConnection_ == nullptr) {
388 APP_LOGE("ServiceCenterConnection is nullptr");
389 connectState_ = ServiceCenterConnectState::DISCONNECTED;
390 cv_.notify_all();
391 return false;
392 }
393 APP_LOGI("ConnectAbility start");
394 int result = AbilityManagerClient::GetInstance()->ConnectAbility(want, serviceCenterConnection_, callerToken);
395 if (result == ERR_OK) {
396 if (connectState_ != ServiceCenterConnectState::CONNECTED) {
397 WaitFromConnected(lock);
398 }
399 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
400 } else {
401 APP_LOGE("ConnectAbility fail result = %{public}d", result);
402 }
403 }
404
405 APP_LOGI("ConnectAbility end");
406 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
407 return true;
408 } else {
409 APP_LOGE("ConnectAbility fail");
410 connectState_ = ServiceCenterConnectState::DISCONNECTED;
411 return false;
412 }
413 }
414
DisconnectDelay()415 void BundleConnectAbilityMgr::DisconnectDelay()
416 {
417 if (handler_ == nullptr) {
418 APP_LOGE("DisconnectDelay, handler is nullptr");
419 return;
420 }
421 auto disconnectFunc = [connect = shared_from_this()]() {
422 APP_LOGI("disconnectFunc Disconnect Ability");
423 if (connect) {
424 connect->DisconnectAbility();
425 }
426 };
427 handler_->PostTask(disconnectFunc, DISCONNECT_DELAY_TASK, DISCONNECT_DELAY);
428 }
429
SendCallBack(int32_t resultCode,const AAFwk::Want & want,int32_t userId,const std::string & transactId)430 void BundleConnectAbilityMgr::SendCallBack(
431 int32_t resultCode, const AAFwk::Want &want, int32_t userId, const std::string &transactId)
432 {
433 APP_LOGI("SendCallBack");
434 sptr<IRemoteObject> amsCallBack = GetAbilityManagerServiceCallBack(transactId);
435 if (amsCallBack == nullptr) {
436 APP_LOGE("Abilitity manager callback is null");
437 return;
438 }
439
440 mapMutex_.lock();
441 if (freeInstallParamsMap_[transactId].serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL &&
442 resultCode != ServiceCenterResultCode::FREE_INSTALL_OK) {
443 APP_LOGE("SendCallBack, freeinstall upgrade return ok");
444 resultCode = ServiceCenterResultCode::FREE_INSTALL_OK;
445 }
446 freeInstallParamsMap_.erase(transactId);
447 APP_LOGI("erase map size = %{public}zu, transactId = %{public}s",
448 freeInstallParamsMap_.size(), transactId.c_str());
449 mapMutex_.unlock();
450 if (freeInstallParamsMap_.size() == 0) {
451 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
452 APP_LOGI("DisconnectDelay");
453 DisconnectDelay();
454 }
455 }
456
457 MessageParcel data;
458 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
459 APP_LOGE("Write interface token failed");
460 return;
461 }
462 if (!data.WriteInt32(resultCode)) {
463 APP_LOGE("Write result code failed");
464 return;
465 }
466 if (!data.WriteParcelable(&want)) {
467 APP_LOGE("Write want failed");
468 return;
469 }
470 if (!data.WriteInt32(userId)) {
471 APP_LOGE("Write userId failed");
472 return;
473 }
474 MessageParcel reply;
475 MessageOption option;
476
477 if (amsCallBack->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
478 APP_LOGE("BundleConnectAbilityMgr::SendCallBack SendRequest failed");
479 }
480 }
481
SendCallBack(const std::string & transactId,const FreeInstallParams & freeInstallParams)482 void BundleConnectAbilityMgr::SendCallBack(const std::string &transactId, const FreeInstallParams &freeInstallParams)
483 {
484 if (freeInstallParams.callback == nullptr) {
485 APP_LOGE("freeInstallParams.callback is null");
486 return;
487 }
488
489 MessageParcel data;
490 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
491 APP_LOGE("Write interface token failed");
492 return;
493 }
494 if (!data.WriteInt32(FreeInstallErrorCode::SERVICE_CENTER_CRASH)) {
495 APP_LOGE("Write result code error");
496 return;
497 }
498 if (!data.WriteParcelable(&(freeInstallParams.want))) {
499 APP_LOGE("Write want failed");
500 return;
501 }
502 if (!data.WriteInt32(freeInstallParams.userId)) {
503 APP_LOGE("Write userId error");
504 return;
505 }
506 MessageParcel reply;
507 MessageOption option;
508 if (freeInstallParams.callback->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
509 APP_LOGE("BundleConnectAbilityMgr::SendCallBack SendRequest failed");
510 }
511 }
512
DeathRecipientSendCallback()513 void BundleConnectAbilityMgr::DeathRecipientSendCallback()
514 {
515 APP_LOGI("DeathRecipientSendCallback start");
516 mapMutex_.lock();
517 APP_LOGI("freeInstallParamsMap size = %{public}zu", freeInstallParamsMap_.size());
518 for (auto &it : freeInstallParamsMap_) {
519 SendCallBack(it.first, it.second);
520 }
521 freeInstallParamsMap_.clear();
522 mapMutex_.unlock();
523
524 connectState_ = ServiceCenterConnectState::DISCONNECTED;
525 serviceCenterRemoteObject_ = nullptr;
526 cv_.notify_all();
527
528 APP_LOGI("DeathRecipientSendCallback end");
529 }
530
OnServiceCenterCall(std::string installResultStr)531 void BundleConnectAbilityMgr::OnServiceCenterCall(std::string installResultStr)
532 {
533 APP_LOGI("OnServiceCenterCall start, installResultStr = %{public}s", installResultStr.c_str());
534 InstallResult installResult;
535 if (!ParseInfoFromJsonStr(installResultStr.c_str(), installResult)) {
536 APP_LOGE("Parse info from json fail");
537 return;
538 }
539 APP_LOGI("OnServiceCenterCall, retCode = %{public}d", installResult.result.retCode);
540 FreeInstallParams freeInstallParams;
541 mapMutex_.lock();
542 auto node = freeInstallParamsMap_.find(installResult.result.transactId);
543 mapMutex_.unlock();
544 if (node == freeInstallParamsMap_.end()) {
545 APP_LOGE("Can not find node in %{public}s function", __func__);
546 return;
547 }
548 if (handler_ == nullptr) {
549 APP_LOGE("OnServiceCenterCall, handler is nullptr");
550 return;
551 }
552 handler_->RemoveTask(installResult.result.transactId);
553 freeInstallParams = node->second;
554 if (installResult.result.retCode == ServiceCenterResultCode::FREE_INSTALL_DOWNLOADING) {
555 APP_LOGI("ServiceCenter is downloading, downloadSize = %{public}d, totalSize = %{public}d",
556 installResult.progress.downloadSize, installResult.progress.totalSize);
557 return;
558 }
559 APP_LOGI("serviceCenterFunction = %{public}d", freeInstallParams.serviceCenterFunction);
560 if (freeInstallParams.serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL &&
561 installResult.result.retCode != ServiceCenterResultCode::FREE_INSTALL_OK) {
562 APP_LOGE("freeinstall upgrade return ok");
563 installResult.result.retCode = ServiceCenterResultCode::FREE_INSTALL_OK;
564 }
565 SendCallBack(installResult.result.retCode, freeInstallParams.want, freeInstallParams.userId,
566 installResult.result.transactId);
567 APP_LOGI("OnServiceCenterCall end");
568 }
569
OutTimeMonitor(std::string transactId)570 void BundleConnectAbilityMgr::OutTimeMonitor(std::string transactId)
571 {
572 APP_LOGI("BundleConnectAbilityMgr::OutTimeMonitor");
573 FreeInstallParams freeInstallParams;
574 mapMutex_.lock();
575 auto node = freeInstallParamsMap_.find(transactId);
576 mapMutex_.unlock();
577 if (node == freeInstallParamsMap_.end()) {
578 APP_LOGE("Can not find node in %{public}s function", __func__);
579 return;
580 }
581 freeInstallParams = node->second;
582 if (handler_ == nullptr) {
583 APP_LOGE("OutTimeMonitor, handler is nullptr");
584 return;
585 }
586 auto RegisterEventListenerFunc = [this, freeInstallParams, transactId]() {
587 APP_LOGI("RegisterEventListenerFunc");
588 this->SendCallBack(FreeInstallErrorCode::SERVICE_CENTER_TIMEOUT,
589 freeInstallParams.want, freeInstallParams.userId, transactId);
590 };
591 handler_->PostTask(RegisterEventListenerFunc, transactId, OUT_TIME, AppExecFwk::EventQueue::Priority::LOW);
592 }
593
SendRequest(int32_t flag,const TargetAbilityInfo & targetAbilityInfo,const Want & want,int32_t userId,const FreeInstallParams & freeInstallParams)594 void BundleConnectAbilityMgr::SendRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want,
595 int32_t userId, const FreeInstallParams &freeInstallParams)
596 {
597 MessageParcel data;
598 MessageParcel reply;
599 MessageOption option(MessageOption::TF_ASYNC);
600 if (!data.WriteInterfaceToken(SERVICE_CENTER_TOKEN)) {
601 APP_LOGE("failed to WriteInterfaceToken");
602 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
603 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
604 return;
605 }
606 const std::string dataString = GetJsonStrFromInfo(targetAbilityInfo);
607 APP_LOGI("TargetAbilityInfo to JsonString : %{public}s", dataString.c_str());
608 if (!data.WriteString16(Str8ToStr16(dataString))) {
609 APP_LOGE("%{public}s failed to WriteParcelable targetAbilityInfo", __func__);
610 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
611 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
612 return;
613 }
614 sptr<ServiceCenterStatusCallback> callback = new(std::nothrow) ServiceCenterStatusCallback(weak_from_this());
615 if (callback == nullptr) {
616 APP_LOGE("callback is nullptr");
617 return;
618 }
619 if (!data.WriteRemoteObject(callback)) {
620 APP_LOGE("%{public}s failed to WriteRemoteObject callbcak", __func__);
621 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
622 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
623 return;
624 }
625 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
626 if (serviceCenterRemoteObject_ == nullptr) {
627 APP_LOGE("%{public}s failed to get remote object", __func__);
628 CallAbilityManager(FreeInstallErrorCode::CONNECT_ERROR, want, userId, freeInstallParams.callback);
629 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
630 return;
631 }
632 mapMutex_.lock();
633 auto emplaceResult = freeInstallParamsMap_.emplace(targetAbilityInfo.targetInfo.transactId, freeInstallParams);
634 APP_LOGI("emplace map size = %{public}zu, transactId = %{public}s",
635 freeInstallParamsMap_.size(), targetAbilityInfo.targetInfo.transactId.c_str());
636 mapMutex_.unlock();
637 if (!emplaceResult.second) {
638 APP_LOGE("freeInstallParamsMap emplace error");
639 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
640 return;
641 }
642 int32_t result = serviceCenterRemoteObject_->SendRequest(flag, data, reply, option);
643 if (result != ERR_OK) {
644 APP_LOGE("Failed to sendRequest, result = %{public}d", result);
645 SendCallBack(FreeInstallErrorCode::CONNECT_ERROR, want, userId, targetAbilityInfo.targetInfo.transactId);
646 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
647 return;
648 }
649 OutTimeMonitor(targetAbilityInfo.targetInfo.transactId);
650 }
651
SendRequest(int32_t code,MessageParcel & data,MessageParcel & reply)652 bool BundleConnectAbilityMgr::SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply)
653 {
654 APP_LOGI("BundleConnectAbilityMgr::SendRequest to service center");
655 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
656 if (serviceCenterRemoteObject_ == nullptr) {
657 APP_LOGE("failed to get remote object");
658 return false;
659 }
660 MessageOption option(MessageOption::TF_ASYNC);
661 int32_t result = serviceCenterRemoteObject_->SendRequest(code, data, reply, option);
662 if (result != ERR_OK) {
663 APP_LOGE("failed to send request code:%{public}d", code);
664 return false;
665 }
666 return true;
667 }
668
GetAbilityManagerServiceCallBack(std::string transactId)669 sptr<IRemoteObject> BundleConnectAbilityMgr::GetAbilityManagerServiceCallBack(std::string transactId)
670 {
671 APP_LOGI("GetAbilityManagerServiceCallBack");
672 FreeInstallParams freeInstallParams;
673 mapMutex_.lock();
674 auto node = freeInstallParamsMap_.find(transactId);
675 mapMutex_.unlock();
676 if (node == freeInstallParamsMap_.end()) {
677 APP_LOGE("Can not find node transactId = %{public}s", transactId.c_str());
678 return nullptr;
679 }
680 freeInstallParams = node->second;
681 return freeInstallParams.callback;
682 }
683
GetCallingInfo(int32_t userId,int32_t callingUid,std::vector<std::string> & bundleNames,std::vector<std::string> & callingAppIds)684 void BundleConnectAbilityMgr::GetCallingInfo(int32_t userId, int32_t callingUid,
685 std::vector<std::string> &bundleNames, std::vector<std::string> &callingAppIds)
686 {
687 APP_LOGI("enter");
688 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
689 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
690 if (bundleDataMgr_ == nullptr) {
691 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
692 return;
693 }
694 std::string bundleName;
695 if (bundleDataMgr_->GetBundleNameForUid(callingUid, bundleName)) {
696 bundleNames.emplace_back(bundleName);
697 } else {
698 APP_LOGE("GetBundleNameForUid failed");
699 }
700 BundleInfo bundleInfo;
701 if (bundleDataMgr_->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId)) {
702 callingAppIds.emplace_back(bundleInfo.appId);
703 } else {
704 APP_LOGE("GetBundleInfo failed");
705 }
706 }
707
ExistBundleNameInCallingBundles(const std::string & bundleName,const std::vector<std::string> & callingBundleNames)708 bool ExistBundleNameInCallingBundles(const std::string &bundleName, const std::vector<std::string> &callingBundleNames)
709 {
710 for (const auto &bundleNameItem : callingBundleNames) {
711 if (bundleNameItem == bundleName) {
712 return true;
713 }
714 }
715 return false;
716 }
717
GetTargetInfoFlag(const Want & want,const std::string & deviceId,const std::string & bundleName,const std::vector<std::string> & callingBundleNames)718 int32_t GetTargetInfoFlag(const Want &want, const std::string &deviceId, const std::string &bundleName,
719 const std::vector<std::string> &callingBundleNames)
720 {
721 // make int from bits.
722 int32_t flagZero = BIT_ZERO_COMPATIBLE;
723 int32_t flagOne = 0;
724 if ((want.GetFlags() & Want::FLAG_INSTALL_WITH_BACKGROUND_MODE) == 0) {
725 flagOne = BIT_ONE_FRONT_MODE * BIT_ONE;
726 } else {
727 flagOne = BIT_ONE_BACKGROUND_MODE * BIT_ONE;
728 }
729 int32_t flagTwo = BIT_TWO_CUSTOM * BIT_TWO;
730 int32_t flagThree = !deviceId.empty() * BIT_THREE;
731 int32_t flagFour = BIT_FOUR_AZ_DEVICE * BIT_FOUR;
732 int32_t flagFive = !ExistBundleNameInCallingBundles(bundleName, callingBundleNames) * BIT_FIVE;
733 int32_t flagSix = BIT_SIX_SAME_BUNDLE * BIT_SIX;
734 return flagZero + flagOne + flagTwo + flagThree + flagFour + flagFive + flagSix;
735 }
736
GetTargetAbilityInfo(const Want & want,int32_t userId,const InnerBundleInfo & innerBundleInfo,sptr<TargetAbilityInfo> & targetAbilityInfo)737 void BundleConnectAbilityMgr::GetTargetAbilityInfo(const Want &want, int32_t userId,
738 const InnerBundleInfo &innerBundleInfo, sptr<TargetAbilityInfo> &targetAbilityInfo)
739 {
740 ElementName element = want.GetElement();
741 std::string bundleName = element.GetBundleName();
742 std::string moduleName = element.GetModuleName();
743 std::string abilityName = element.GetAbilityName();
744 std::string deviceId = element.GetDeviceID();
745 std::vector<std::string> callingBundleNames;
746 std::vector<std::string> callingAppids;
747 auto wantParams = want.GetParams();
748 std::map<std::string, std::string> extValues;
749 for (auto it : wantParams.GetParams()) {
750 int typeId = WantParams::GetDataType(it.second);
751 auto info = wantParams.GetParam(it.first);
752 std::string value = wantParams.GetStringByType(info, typeId);
753 extValues.emplace(it.first, value);
754 }
755 auto callingUid = want.GetIntParam(PARAM_FREEINSTALL_UID, IPCSkeleton::GetCallingUid());
756
757 targetAbilityInfo->targetExtSetting.extValues = extValues;
758 targetAbilityInfo->targetInfo.transactId = std::to_string(this->GetTransactId());
759 targetAbilityInfo->targetInfo.bundleName = bundleName;
760 targetAbilityInfo->targetInfo.moduleName = moduleName;
761 targetAbilityInfo->targetInfo.abilityName = abilityName;
762 targetAbilityInfo->targetInfo.callingUid = callingUid;
763 targetAbilityInfo->targetInfo.callingAppType = CALLING_TYPE_HARMONY;
764 std::string callingAppId = want.GetStringParam(PARAM_FREEINSTALL_APPID);
765 if (!callingAppId.empty()) {
766 callingAppids.push_back(callingAppId);
767 }
768 callingBundleNames = want.GetStringArrayParam(PARAM_FREEINSTALL_BUNDLENAMES);
769 if (callingAppids.empty() && callingBundleNames.empty()) {
770 this->GetCallingInfo(userId, callingUid, callingBundleNames, callingAppids);
771 }
772 targetAbilityInfo->targetInfo.callingBundleNames = callingBundleNames;
773 targetAbilityInfo->targetInfo.flags = GetTargetInfoFlag(want, deviceId, bundleName, callingBundleNames);
774 targetAbilityInfo->targetInfo.reasonFlag = static_cast<int32_t>(innerBundleInfo.GetModuleUpgradeFlag(moduleName));
775 targetAbilityInfo->targetInfo.callingAppIds = callingAppids;
776 }
777
CallAbilityManager(int32_t resultCode,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)778 void BundleConnectAbilityMgr::CallAbilityManager(
779 int32_t resultCode, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
780 {
781 if (callBack == nullptr) {
782 APP_LOGE("callBack is nullptr");
783 return;
784 }
785 MessageParcel data;
786 MessageParcel reply;
787 MessageOption option;
788 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
789 APP_LOGE("Write interface token failed");
790 return;
791 }
792 if (!data.WriteInt32(resultCode)) {
793 APP_LOGE("Write result code failed");
794 return;
795 }
796 if (!data.WriteParcelable(&want)) {
797 APP_LOGE("Write want failed");
798 return;
799 }
800 if (!data.WriteInt32(userId)) {
801 APP_LOGE("Write userId failed");
802 return;
803 }
804
805 if (callBack->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
806 APP_LOGE("BundleConnectAbilityMgr::CallAbilityManager SendRequest failed");
807 }
808 }
809
CheckIsModuleNeedUpdate(InnerBundleInfo & innerBundleInfo,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)810 bool BundleConnectAbilityMgr::CheckIsModuleNeedUpdate(
811 InnerBundleInfo &innerBundleInfo, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
812 {
813 APP_LOGI("CheckIsModuleNeedUpdate called");
814 if (innerBundleInfo.GetModuleUpgradeFlag(want.GetModuleName()) != 0) {
815 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
816 if (targetAbilityInfo == nullptr) {
817 APP_LOGE("targetAbilityInfo is nullptr");
818 return false;
819 }
820 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
821 if (targetInfo == nullptr) {
822 APP_LOGE("targetInfo is nullptr");
823 return false;
824 }
825 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
826 if (targetExtSetting == nullptr) {
827 APP_LOGE("targetExtSetting is nullptr");
828 return false;
829 }
830 targetAbilityInfo->targetInfo = *targetInfo;
831 targetAbilityInfo->targetExtSetting = *targetExtSetting;
832 targetAbilityInfo->version = DEFAULT_VERSION;
833 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
834 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
835 if (freeInstallParams == nullptr) {
836 APP_LOGE("freeInstallParams is nullptr");
837 return false;
838 }
839 freeInstallParams->callback = callBack;
840 freeInstallParams->want = want;
841 freeInstallParams->userId = userId;
842 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_UPGRADE_INSTALL;
843 this->UpgradeInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
844 return true;
845 }
846 APP_LOGI("Module is not need update");
847 return false;
848 }
849
CheckDependencies(const std::string & moduleName,const InnerBundleInfo & innerBundleInfo)850 bool BundleConnectAbilityMgr::CheckDependencies(const std::string &moduleName, const InnerBundleInfo &innerBundleInfo)
851 {
852 std::vector<std::string> dependentModuleNames;
853 if (!innerBundleInfo.GetDependentModuleNames(moduleName, dependentModuleNames)) {
854 APP_LOGE("GetDependentModuleNames can not find module %{public}s", moduleName.c_str());
855 return false;
856 }
857 for (const std::string &depend : dependentModuleNames) {
858 if (!innerBundleInfo.FindModule(depend)) {
859 APP_LOGD("%{public}s does not exist locally.", depend.c_str());
860 return false;
861 }
862 }
863 return true;
864 }
865
IsObtainAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack,InnerBundleInfo & innerBundleInfo)866 bool BundleConnectAbilityMgr::IsObtainAbilityInfo(const Want &want, int32_t flags, int32_t userId,
867 AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack, InnerBundleInfo &innerBundleInfo)
868 {
869 APP_LOGD("IsObtainAbilityInfo");
870 std::string bundleName = want.GetElement().GetBundleName();
871 std::string abilityName = want.GetElement().GetAbilityName();
872 std::string moduleName = want.GetElement().GetModuleName();
873 if (bundleName == "" || abilityName == "") {
874 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, callBack);
875 APP_LOGE("bundle name or ability name is null");
876 return false;
877 }
878 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
879 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
880 if (bundleDataMgr_ == nullptr) {
881 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
882 return false;
883 }
884 bool innerBundleInfoResult = bundleDataMgr_->GetInnerBundleInfoWithFlags(bundleName,
885 flags, innerBundleInfo, userId);
886 bool abilityInfoResult = bundleDataMgr_->QueryAbilityInfo(want, flags, userId, abilityInfo);
887 if (!abilityInfoResult) {
888 std::vector<ExtensionAbilityInfo> extensionInfos;
889 abilityInfoResult = bundleDataMgr_->QueryExtensionAbilityInfos(want, flags, userId, extensionInfos);
890 if (abilityInfoResult && moduleName.empty()) {
891 moduleName = extensionInfos[0].moduleName;
892 }
893 } else {
894 if (moduleName.empty()) {
895 moduleName = abilityInfo.moduleName;
896 }
897 }
898 if (innerBundleInfoResult && abilityInfoResult && CheckDependencies(moduleName, innerBundleInfo)) {
899 bool isModuleNeedUpdate = CheckIsModuleNeedUpdate(innerBundleInfo, want, userId, callBack);
900 if (!isModuleNeedUpdate) {
901 CallAbilityManager(ServiceCenterResultCode::FREE_INSTALL_OK, want, userId, callBack);
902 }
903 return true;
904 }
905 return false;
906 }
907
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)908 bool BundleConnectAbilityMgr::QueryAbilityInfo(const Want &want, int32_t flags,
909 int32_t userId, AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack)
910 {
911 APP_LOGD("QueryAbilityInfo");
912 InnerBundleInfo innerBundleInfo;
913 if (IsObtainAbilityInfo(want, flags, userId, abilityInfo, callBack, innerBundleInfo)) {
914 return true;
915 }
916 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
917 if (targetAbilityInfo == nullptr) {
918 APP_LOGE("targetAbilityInfo is nullptr");
919 return false;
920 }
921 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
922 if (targetInfo == nullptr) {
923 APP_LOGE("targetInfo is nullptr");
924 return false;
925 }
926 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
927 if (targetExtSetting == nullptr) {
928 APP_LOGE("targetExtSetting is nullptr");
929 return false;
930 }
931 targetAbilityInfo->targetInfo = *targetInfo;
932 targetAbilityInfo->targetExtSetting = *targetExtSetting;
933 targetAbilityInfo->version = DEFAULT_VERSION;
934 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
935 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
936 if (freeInstallParams == nullptr) {
937 APP_LOGE("freeInstallParams is nullptr");
938 return false;
939 }
940 freeInstallParams->callback = callBack;
941 freeInstallParams->want = want;
942 freeInstallParams->userId = userId;
943 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
944 this->SilentInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
945 return false;
946 }
947
SilentInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)948 bool BundleConnectAbilityMgr::SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
949 {
950 APP_LOGD("SilentInstall");
951 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
952 if (targetAbilityInfo == nullptr) {
953 APP_LOGE("targetAbilityInfo is nullptr");
954 return false;
955 }
956 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
957 if (targetInfo == nullptr) {
958 APP_LOGE("targetInfo is nullptr");
959 return false;
960 }
961 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
962 if (targetExtSetting == nullptr) {
963 APP_LOGE("targetExtSetting is nullptr");
964 return false;
965 }
966
967 targetAbilityInfo->targetInfo = *targetInfo;
968 targetAbilityInfo->targetExtSetting = *targetExtSetting;
969 targetAbilityInfo->version = DEFAULT_VERSION;
970 InnerBundleInfo innerBundleInfo;
971 GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
972 auto callingUid = IPCSkeleton::GetCallingUid();
973 std::vector<std::string> callingBundleNames;
974 std::vector<std::string> callingAppids;
975 GetCallingInfo(userId, callingUid, callingBundleNames, callingAppids);
976 targetAbilityInfo->targetInfo.callingUid = callingUid;
977 targetAbilityInfo->targetInfo.callingBundleNames = callingBundleNames;
978 targetAbilityInfo->targetInfo.callingAppIds = callingAppids;
979 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
980 if (freeInstallParams == nullptr) {
981 APP_LOGE("freeInstallParams is nullptr");
982 return false;
983 }
984
985 freeInstallParams->callback = callBack;
986 freeInstallParams->want = want;
987 freeInstallParams->userId = userId;
988 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
989 SilentInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
990 return true;
991 }
992
UpgradeAtomicService(const Want & want,int32_t userId)993 void BundleConnectAbilityMgr::UpgradeAtomicService(const Want &want, int32_t userId)
994 {
995 APP_LOGI("UpgradeAtomicService");
996 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
997 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
998 if (bundleDataMgr_ == nullptr) {
999 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
1000 return;
1001 }
1002 std::string bundleName = want.GetElement().GetBundleName();
1003 InnerBundleInfo innerBundleInfo;
1004 bundleDataMgr_->GetInnerBundleInfoWithFlags(bundleName, want.GetFlags(), innerBundleInfo, userId);
1005 if (!innerBundleInfo.GetEntryInstallationFree()) {
1006 APP_LOGI("bundleName:%{public}s is atomic application", bundleName.c_str());
1007 return;
1008 }
1009 APP_LOGI("bundleName:%{public}s is atomic service", bundleName.c_str());
1010 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
1011 if (targetAbilityInfo == nullptr) {
1012 APP_LOGE("targetAbilityInfo is nullptr");
1013 return;
1014 }
1015 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
1016 if (targetInfo == nullptr) {
1017 APP_LOGE("targetInfo is nullptr");
1018 return;
1019 }
1020 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
1021 if (targetExtSetting == nullptr) {
1022 APP_LOGE("targetExtSetting is nullptr");
1023 return;
1024 }
1025 targetAbilityInfo->targetInfo = *targetInfo;
1026 targetAbilityInfo->targetExtSetting = *targetExtSetting;
1027 targetAbilityInfo->version = DEFAULT_VERSION;
1028 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
1029 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
1030 if (freeInstallParams == nullptr) {
1031 APP_LOGE("freeInstallParams is nullptr");
1032 return;
1033 }
1034 freeInstallParams->want = want;
1035 freeInstallParams->userId = userId;
1036 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_UPGRADE_CHECK;
1037 this->UpgradeCheck(*targetAbilityInfo, want, *freeInstallParams, userId);
1038 }
1039 } // namespace AppExecFwk
1040 } // namespace OHOS
1041