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 bool 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 false;
235 }
236 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
237 if (targetInfo == nullptr) {
238 APP_LOGE("targetInfo is nullptr");
239 return false;
240 }
241 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
242 if (targetExtSetting == nullptr) {
243 APP_LOGE("targetExtSetting is nullptr");
244 return false;
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 false;
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 if (!ProcessPreloadCheck(*targetAbilityInfo)) {
263 APP_LOGE("ProcessPreloadCheck failed.");
264 return false;
265 }
266 return true;
267 }
268
SilentInstall(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)269 bool BundleConnectAbilityMgr::SilentInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
270 const FreeInstallParams &freeInstallParams, int32_t userId)
271 {
272 APP_LOGI("SilentInstall");
273 if (handler_ == nullptr) {
274 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
275 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
276 APP_LOGE("handler is null");
277 return false;
278 }
279 auto silentInstallFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
280 int32_t flag = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
281 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
282 };
283 handler_->PostTask(silentInstallFunc, targetAbilityInfo.targetInfo.transactId.c_str());
284 return true;
285 }
286
UpgradeCheck(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)287 bool BundleConnectAbilityMgr::UpgradeCheck(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
288 const FreeInstallParams &freeInstallParams, int32_t userId)
289 {
290 APP_LOGI("UpgradeCheck");
291 if (handler_ == nullptr) {
292 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
293 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
294 APP_LOGE("handler is null");
295 return false;
296 }
297 auto upgradeCheckFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
298 int32_t flag = ServiceCenterFunction::CONNECT_UPGRADE_CHECK;
299 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
300 };
301 handler_->PostTask(upgradeCheckFunc, targetAbilityInfo.targetInfo.transactId.c_str());
302 return true;
303 }
304
UpgradeInstall(const TargetAbilityInfo & targetAbilityInfo,const Want & want,const FreeInstallParams & freeInstallParams,int32_t userId)305 bool BundleConnectAbilityMgr::UpgradeInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
306 const FreeInstallParams &freeInstallParams, int32_t userId)
307 {
308 APP_LOGI("UpgradeInstall");
309 if (handler_ == nullptr) {
310 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
311 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
312 APP_LOGE("handler is null");
313 return false;
314 }
315 auto upgradeInstallFunc = [this, targetAbilityInfo, want, userId, freeInstallParams]() {
316 int32_t flag = ServiceCenterFunction::CONNECT_UPGRADE_INSTALL;
317 this->SendRequestToServiceCenter(flag, targetAbilityInfo, want, userId, freeInstallParams);
318 };
319 handler_->PostTask(upgradeInstallFunc, targetAbilityInfo.targetInfo.transactId.c_str());
320 return true;
321 }
322
SendRequestToServiceCenter(int32_t flag,const TargetAbilityInfo & targetAbilityInfo,const Want & want,int32_t userId,const FreeInstallParams & freeInstallParams)323 bool BundleConnectAbilityMgr::SendRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo,
324 const Want &want, int32_t userId, const FreeInstallParams &freeInstallParams)
325 {
326 APP_LOGI("SendRequestToServiceCenter");
327 Want serviceCenterWant;
328 serviceCenterWant.SetElementName(SERVICE_CENTER_BUNDLE_NAME, SERVICE_CENTER_ABILITY_NAME);
329 bool isConnectSuccess = ConnectAbility(serviceCenterWant, nullptr);
330 if (!isConnectSuccess) {
331 if (freeInstallParams.serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL) {
332 APP_LOGE("Fail to connect ServiceCenter, but freeinstall upgrade return ok");
333 CallAbilityManager(ServiceCenterResultCode::FREE_INSTALL_OK, want, userId, freeInstallParams.callback);
334 } else {
335 APP_LOGE("Fail to connect ServiceCenter");
336 CallAbilityManager(FreeInstallErrorCode::CONNECT_ERROR, want, userId, freeInstallParams.callback);
337 }
338 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
339 return false;
340 } else {
341 SendRequest(flag, targetAbilityInfo, want, userId, freeInstallParams);
342 return true;
343 }
344 }
345
DisconnectAbility()346 void BundleConnectAbilityMgr::DisconnectAbility()
347 {
348 if (serviceCenterConnection_ != nullptr) {
349 APP_LOGI("DisconnectAbility");
350 int result = AbilityManagerClient::GetInstance()->DisconnectAbility(serviceCenterConnection_);
351 if (result != ERR_OK) {
352 APP_LOGE("BundleConnectAbilityMgr::DisconnectAbility fail, resultCode: %{public}d", result);
353 }
354 }
355 }
356
WaitFromConnecting(std::unique_lock<std::mutex> & lock)357 void BundleConnectAbilityMgr::WaitFromConnecting(std::unique_lock<std::mutex> &lock)
358 {
359 APP_LOGI("ConnectAbility await start CONNECTING");
360 while (connectState_ == ServiceCenterConnectState::CONNECTING) {
361 cv_.wait(lock);
362 }
363 APP_LOGI("ConnectAbility await end CONNECTING");
364 }
365
WaitFromConnected(std::unique_lock<std::mutex> & lock)366 void BundleConnectAbilityMgr::WaitFromConnected(std::unique_lock<std::mutex> &lock)
367 {
368 APP_LOGI("ConnectAbility await start CONNECTED");
369 while (connectState_ != ServiceCenterConnectState::CONNECTED) {
370 if (connectState_ == ServiceCenterConnectState::DISCONNECTED) {
371 break;
372 }
373 cv_.wait(lock);
374 }
375 APP_LOGI("ConnectAbility await end CONNECTED");
376 }
377
ConnectAbility(const Want & want,const sptr<IRemoteObject> & callerToken)378 bool BundleConnectAbilityMgr::ConnectAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
379 {
380 APP_LOGI("ConnectAbility start target bundle = %{public}s", want.GetBundle().c_str());
381 std::unique_lock<std::mutex> lock(mutex_);
382 if (handler_ != nullptr) {
383 handler_->RemoveTask(DISCONNECT_DELAY_TASK);
384 }
385 if (connectState_ == ServiceCenterConnectState::CONNECTING) {
386 WaitFromConnecting(lock);
387 } else if (connectState_ == ServiceCenterConnectState::DISCONNECTED) {
388 connectState_ = ServiceCenterConnectState::CONNECTING;
389 serviceCenterConnection_ = new (std::nothrow) ServiceCenterConnection(connectState_,
390 cv_, weak_from_this());
391 if (serviceCenterConnection_ == nullptr) {
392 APP_LOGE("ServiceCenterConnection is nullptr");
393 connectState_ = ServiceCenterConnectState::DISCONNECTED;
394 cv_.notify_all();
395 return false;
396 }
397 APP_LOGI("ConnectAbility start");
398 int result = AbilityManagerClient::GetInstance()->ConnectAbility(want, serviceCenterConnection_, callerToken);
399 if (result == ERR_OK) {
400 if (connectState_ != ServiceCenterConnectState::CONNECTED) {
401 WaitFromConnected(lock);
402 }
403 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
404 } else {
405 APP_LOGE("ConnectAbility fail result = %{public}d", result);
406 }
407 }
408
409 APP_LOGI("ConnectAbility end");
410 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
411 return true;
412 } else {
413 APP_LOGE("ConnectAbility fail");
414 connectState_ = ServiceCenterConnectState::DISCONNECTED;
415 return false;
416 }
417 }
418
DisconnectDelay()419 void BundleConnectAbilityMgr::DisconnectDelay()
420 {
421 if (handler_ == nullptr) {
422 APP_LOGE("DisconnectDelay, handler is nullptr");
423 return;
424 }
425 auto disconnectFunc = [connect = shared_from_this()]() {
426 APP_LOGI("disconnectFunc Disconnect Ability");
427 if (connect) {
428 connect->DisconnectAbility();
429 }
430 };
431 handler_->PostTask(disconnectFunc, DISCONNECT_DELAY_TASK, DISCONNECT_DELAY);
432 }
433
SendCallBack(int32_t resultCode,const AAFwk::Want & want,int32_t userId,const std::string & transactId)434 void BundleConnectAbilityMgr::SendCallBack(
435 int32_t resultCode, const AAFwk::Want &want, int32_t userId, const std::string &transactId)
436 {
437 APP_LOGI("SendCallBack");
438 sptr<IRemoteObject> amsCallBack = GetAbilityManagerServiceCallBack(transactId);
439 if (amsCallBack == nullptr) {
440 APP_LOGE("Abilitity manager callback is null");
441 return;
442 }
443
444 mapMutex_.lock();
445 if (freeInstallParamsMap_[transactId].serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL &&
446 resultCode != ServiceCenterResultCode::FREE_INSTALL_OK) {
447 APP_LOGE("SendCallBack, freeinstall upgrade return ok");
448 resultCode = ServiceCenterResultCode::FREE_INSTALL_OK;
449 }
450 freeInstallParamsMap_.erase(transactId);
451 APP_LOGI("erase map size = %{public}zu, transactId = %{public}s",
452 freeInstallParamsMap_.size(), transactId.c_str());
453 mapMutex_.unlock();
454 if (freeInstallParamsMap_.size() == 0) {
455 if (connectState_ == ServiceCenterConnectState::CONNECTED) {
456 APP_LOGI("DisconnectDelay");
457 DisconnectDelay();
458 }
459 }
460
461 MessageParcel data;
462 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
463 APP_LOGE("Write interface token failed");
464 return;
465 }
466 if (!data.WriteInt32(resultCode)) {
467 APP_LOGE("Write result code failed");
468 return;
469 }
470 if (!data.WriteParcelable(&want)) {
471 APP_LOGE("Write want failed");
472 return;
473 }
474 if (!data.WriteInt32(userId)) {
475 APP_LOGE("Write userId failed");
476 return;
477 }
478 MessageParcel reply;
479 MessageOption option;
480
481 if (amsCallBack->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
482 APP_LOGE("BundleConnectAbilityMgr::SendCallBack SendRequest failed");
483 }
484 }
485
SendCallBack(const std::string & transactId,const FreeInstallParams & freeInstallParams)486 void BundleConnectAbilityMgr::SendCallBack(const std::string &transactId, const FreeInstallParams &freeInstallParams)
487 {
488 if (freeInstallParams.callback == nullptr) {
489 APP_LOGE("freeInstallParams.callback is null");
490 return;
491 }
492
493 MessageParcel data;
494 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
495 APP_LOGE("Write interface token failed");
496 return;
497 }
498 if (!data.WriteInt32(FreeInstallErrorCode::SERVICE_CENTER_CRASH)) {
499 APP_LOGE("Write result code error");
500 return;
501 }
502 if (!data.WriteParcelable(&(freeInstallParams.want))) {
503 APP_LOGE("Write want failed");
504 return;
505 }
506 if (!data.WriteInt32(freeInstallParams.userId)) {
507 APP_LOGE("Write userId error");
508 return;
509 }
510 MessageParcel reply;
511 MessageOption option;
512 if (freeInstallParams.callback->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
513 APP_LOGE("BundleConnectAbilityMgr::SendCallBack SendRequest failed");
514 }
515 }
516
DeathRecipientSendCallback()517 void BundleConnectAbilityMgr::DeathRecipientSendCallback()
518 {
519 APP_LOGI("DeathRecipientSendCallback start");
520 mapMutex_.lock();
521 APP_LOGI("freeInstallParamsMap size = %{public}zu", freeInstallParamsMap_.size());
522 for (auto &it : freeInstallParamsMap_) {
523 SendCallBack(it.first, it.second);
524 }
525 freeInstallParamsMap_.clear();
526 mapMutex_.unlock();
527
528 connectState_ = ServiceCenterConnectState::DISCONNECTED;
529 serviceCenterRemoteObject_ = nullptr;
530 cv_.notify_all();
531
532 APP_LOGI("DeathRecipientSendCallback end");
533 }
534
OnServiceCenterCall(std::string installResultStr)535 void BundleConnectAbilityMgr::OnServiceCenterCall(std::string installResultStr)
536 {
537 APP_LOGI("OnServiceCenterCall start, installResultStr = %{public}s", installResultStr.c_str());
538 InstallResult installResult;
539 if (!ParseInfoFromJsonStr(installResultStr.c_str(), installResult)) {
540 APP_LOGE("Parse info from json fail");
541 return;
542 }
543 APP_LOGI("OnServiceCenterCall, retCode = %{public}d", installResult.result.retCode);
544 FreeInstallParams freeInstallParams;
545 mapMutex_.lock();
546 auto node = freeInstallParamsMap_.find(installResult.result.transactId);
547 mapMutex_.unlock();
548 if (node == freeInstallParamsMap_.end()) {
549 APP_LOGE("Can not find node in %{public}s function", __func__);
550 return;
551 }
552 if (handler_ == nullptr) {
553 APP_LOGE("OnServiceCenterCall, handler is nullptr");
554 return;
555 }
556 handler_->RemoveTask(installResult.result.transactId);
557 freeInstallParams = node->second;
558 if (installResult.result.retCode == ServiceCenterResultCode::FREE_INSTALL_DOWNLOADING) {
559 APP_LOGI("ServiceCenter is downloading, downloadSize = %{public}d, totalSize = %{public}d",
560 installResult.progress.downloadSize, installResult.progress.totalSize);
561 return;
562 }
563 APP_LOGI("serviceCenterFunction = %{public}d", freeInstallParams.serviceCenterFunction);
564 if (freeInstallParams.serviceCenterFunction == ServiceCenterFunction::CONNECT_UPGRADE_INSTALL &&
565 installResult.result.retCode != ServiceCenterResultCode::FREE_INSTALL_OK) {
566 APP_LOGE("freeinstall upgrade return ok");
567 installResult.result.retCode = ServiceCenterResultCode::FREE_INSTALL_OK;
568 }
569 SendCallBack(installResult.result.retCode, freeInstallParams.want, freeInstallParams.userId,
570 installResult.result.transactId);
571 APP_LOGI("OnServiceCenterCall end");
572 }
573
OutTimeMonitor(std::string transactId)574 void BundleConnectAbilityMgr::OutTimeMonitor(std::string transactId)
575 {
576 APP_LOGI("BundleConnectAbilityMgr::OutTimeMonitor");
577 FreeInstallParams freeInstallParams;
578 mapMutex_.lock();
579 auto node = freeInstallParamsMap_.find(transactId);
580 mapMutex_.unlock();
581 if (node == freeInstallParamsMap_.end()) {
582 APP_LOGE("Can not find node in %{public}s function", __func__);
583 return;
584 }
585 freeInstallParams = node->second;
586 if (handler_ == nullptr) {
587 APP_LOGE("OutTimeMonitor, handler is nullptr");
588 return;
589 }
590 auto RegisterEventListenerFunc = [this, freeInstallParams, transactId]() {
591 APP_LOGI("RegisterEventListenerFunc");
592 this->SendCallBack(FreeInstallErrorCode::SERVICE_CENTER_TIMEOUT,
593 freeInstallParams.want, freeInstallParams.userId, transactId);
594 };
595 handler_->PostTask(RegisterEventListenerFunc, transactId, OUT_TIME, AppExecFwk::EventQueue::Priority::LOW);
596 }
597
SendRequest(int32_t flag,const TargetAbilityInfo & targetAbilityInfo,const Want & want,int32_t userId,const FreeInstallParams & freeInstallParams)598 void BundleConnectAbilityMgr::SendRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want,
599 int32_t userId, const FreeInstallParams &freeInstallParams)
600 {
601 MessageParcel data;
602 MessageParcel reply;
603 MessageOption option(MessageOption::TF_ASYNC);
604 if (!data.WriteInterfaceToken(SERVICE_CENTER_TOKEN)) {
605 APP_LOGE("failed to WriteInterfaceToken");
606 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
607 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
608 return;
609 }
610 const std::string dataString = GetJsonStrFromInfo(targetAbilityInfo);
611 APP_LOGI("TargetAbilityInfo to JsonString : %{public}s", dataString.c_str());
612 if (!data.WriteString16(Str8ToStr16(dataString))) {
613 APP_LOGE("%{public}s failed to WriteParcelable targetAbilityInfo", __func__);
614 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
615 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
616 return;
617 }
618 sptr<ServiceCenterStatusCallback> callback = new(std::nothrow) ServiceCenterStatusCallback(weak_from_this());
619 if (callback == nullptr) {
620 APP_LOGE("callback is nullptr");
621 return;
622 }
623 if (!data.WriteRemoteObject(callback)) {
624 APP_LOGE("%{public}s failed to WriteRemoteObject callbcak", __func__);
625 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
626 SendSysEvent(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId);
627 return;
628 }
629 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
630 if (serviceCenterRemoteObject_ == nullptr) {
631 APP_LOGE("%{public}s failed to get remote object", __func__);
632 CallAbilityManager(FreeInstallErrorCode::CONNECT_ERROR, want, userId, freeInstallParams.callback);
633 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
634 return;
635 }
636 mapMutex_.lock();
637 auto emplaceResult = freeInstallParamsMap_.emplace(targetAbilityInfo.targetInfo.transactId, freeInstallParams);
638 APP_LOGI("emplace map size = %{public}zu, transactId = %{public}s",
639 freeInstallParamsMap_.size(), targetAbilityInfo.targetInfo.transactId.c_str());
640 mapMutex_.unlock();
641 if (!emplaceResult.second) {
642 APP_LOGE("freeInstallParamsMap emplace error");
643 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, freeInstallParams.callback);
644 return;
645 }
646 int32_t result = serviceCenterRemoteObject_->SendRequest(flag, data, reply, option);
647 if (result != ERR_OK) {
648 APP_LOGE("Failed to sendRequest, result = %{public}d", result);
649 SendCallBack(FreeInstallErrorCode::CONNECT_ERROR, want, userId, targetAbilityInfo.targetInfo.transactId);
650 SendSysEvent(FreeInstallErrorCode::CONNECT_ERROR, want, userId);
651 return;
652 }
653 OutTimeMonitor(targetAbilityInfo.targetInfo.transactId);
654 }
655
SendRequest(int32_t code,MessageParcel & data,MessageParcel & reply)656 bool BundleConnectAbilityMgr::SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply)
657 {
658 APP_LOGI("BundleConnectAbilityMgr::SendRequest to service center");
659 serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
660 if (serviceCenterRemoteObject_ == nullptr) {
661 APP_LOGE("failed to get remote object");
662 return false;
663 }
664 MessageOption option(MessageOption::TF_ASYNC);
665 int32_t result = serviceCenterRemoteObject_->SendRequest(code, data, reply, option);
666 if (result != ERR_OK) {
667 APP_LOGE("failed to send request code:%{public}d", code);
668 return false;
669 }
670 return true;
671 }
672
GetAbilityManagerServiceCallBack(std::string transactId)673 sptr<IRemoteObject> BundleConnectAbilityMgr::GetAbilityManagerServiceCallBack(std::string transactId)
674 {
675 APP_LOGI("GetAbilityManagerServiceCallBack");
676 FreeInstallParams freeInstallParams;
677 mapMutex_.lock();
678 auto node = freeInstallParamsMap_.find(transactId);
679 mapMutex_.unlock();
680 if (node == freeInstallParamsMap_.end()) {
681 APP_LOGE("Can not find node transactId = %{public}s", transactId.c_str());
682 return nullptr;
683 }
684 freeInstallParams = node->second;
685 return freeInstallParams.callback;
686 }
687
GetCallingInfo(int32_t userId,int32_t callingUid,std::vector<std::string> & bundleNames,std::vector<std::string> & callingAppIds)688 void BundleConnectAbilityMgr::GetCallingInfo(int32_t userId, int32_t callingUid,
689 std::vector<std::string> &bundleNames, std::vector<std::string> &callingAppIds)
690 {
691 APP_LOGI("enter");
692 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
693 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
694 if (bundleDataMgr_ == nullptr) {
695 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
696 return;
697 }
698 std::string bundleName;
699 if (bundleDataMgr_->GetBundleNameForUid(callingUid, bundleName)) {
700 bundleNames.emplace_back(bundleName);
701 } else {
702 APP_LOGE("GetBundleNameForUid failed");
703 }
704 BundleInfo bundleInfo;
705 if (bundleDataMgr_->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId)) {
706 callingAppIds.emplace_back(bundleInfo.appId);
707 } else {
708 APP_LOGE("GetBundleInfo failed");
709 }
710 }
711
ExistBundleNameInCallingBundles(const std::string & bundleName,const std::vector<std::string> & callingBundleNames)712 bool ExistBundleNameInCallingBundles(const std::string &bundleName, const std::vector<std::string> &callingBundleNames)
713 {
714 for (const auto &bundleNameItem : callingBundleNames) {
715 if (bundleNameItem == bundleName) {
716 return true;
717 }
718 }
719 return false;
720 }
721
GetTargetInfoFlag(const Want & want,const std::string & deviceId,const std::string & bundleName,const std::vector<std::string> & callingBundleNames)722 int32_t GetTargetInfoFlag(const Want &want, const std::string &deviceId, const std::string &bundleName,
723 const std::vector<std::string> &callingBundleNames)
724 {
725 // make int from bits.
726 int32_t flagZero = BIT_ZERO_COMPATIBLE;
727 int32_t flagOne = 0;
728 if ((want.GetFlags() & Want::FLAG_INSTALL_WITH_BACKGROUND_MODE) == 0) {
729 flagOne = BIT_ONE_FRONT_MODE * BIT_ONE;
730 } else {
731 flagOne = BIT_ONE_BACKGROUND_MODE * BIT_ONE;
732 }
733 int32_t flagTwo = BIT_TWO_CUSTOM * BIT_TWO;
734 int32_t flagThree = !deviceId.empty() * BIT_THREE;
735 int32_t flagFour = BIT_FOUR_AZ_DEVICE * BIT_FOUR;
736 int32_t flagFive = !ExistBundleNameInCallingBundles(bundleName, callingBundleNames) * BIT_FIVE;
737 int32_t flagSix = BIT_SIX_SAME_BUNDLE * BIT_SIX;
738 return flagZero + flagOne + flagTwo + flagThree + flagFour + flagFive + flagSix;
739 }
740
GetTargetAbilityInfo(const Want & want,int32_t userId,const InnerBundleInfo & innerBundleInfo,sptr<TargetAbilityInfo> & targetAbilityInfo)741 void BundleConnectAbilityMgr::GetTargetAbilityInfo(const Want &want, int32_t userId,
742 const InnerBundleInfo &innerBundleInfo, sptr<TargetAbilityInfo> &targetAbilityInfo)
743 {
744 ElementName element = want.GetElement();
745 std::string bundleName = element.GetBundleName();
746 std::string moduleName = element.GetModuleName();
747 std::string abilityName = element.GetAbilityName();
748 std::string deviceId = element.GetDeviceID();
749 std::vector<std::string> callingBundleNames;
750 std::vector<std::string> callingAppids;
751 auto wantParams = want.GetParams();
752 std::map<std::string, std::string> extValues;
753 for (auto it : wantParams.GetParams()) {
754 int typeId = WantParams::GetDataType(it.second);
755 auto info = wantParams.GetParam(it.first);
756 std::string value = wantParams.GetStringByType(info, typeId);
757 extValues.emplace(it.first, value);
758 }
759 auto callingUid = want.GetIntParam(PARAM_FREEINSTALL_UID, IPCSkeleton::GetCallingUid());
760
761 targetAbilityInfo->targetExtSetting.extValues = extValues;
762 targetAbilityInfo->targetInfo.transactId = std::to_string(this->GetTransactId());
763 targetAbilityInfo->targetInfo.bundleName = bundleName;
764 targetAbilityInfo->targetInfo.moduleName = moduleName;
765 targetAbilityInfo->targetInfo.abilityName = abilityName;
766 targetAbilityInfo->targetInfo.callingUid = callingUid;
767 targetAbilityInfo->targetInfo.callingAppType = CALLING_TYPE_HARMONY;
768 std::string callingAppId = want.GetStringParam(PARAM_FREEINSTALL_APPID);
769 if (!callingAppId.empty()) {
770 callingAppids.push_back(callingAppId);
771 }
772 callingBundleNames = want.GetStringArrayParam(PARAM_FREEINSTALL_BUNDLENAMES);
773 if (callingAppids.empty() && callingBundleNames.empty()) {
774 this->GetCallingInfo(userId, callingUid, callingBundleNames, callingAppids);
775 }
776 targetAbilityInfo->targetInfo.callingBundleNames = callingBundleNames;
777 targetAbilityInfo->targetInfo.flags = GetTargetInfoFlag(want, deviceId, bundleName, callingBundleNames);
778 targetAbilityInfo->targetInfo.reasonFlag = static_cast<int32_t>(innerBundleInfo.GetModuleUpgradeFlag(moduleName));
779 targetAbilityInfo->targetInfo.callingAppIds = callingAppids;
780 }
781
CallAbilityManager(int32_t resultCode,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)782 void BundleConnectAbilityMgr::CallAbilityManager(
783 int32_t resultCode, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
784 {
785 if (callBack == nullptr) {
786 APP_LOGE("callBack is nullptr");
787 return;
788 }
789 MessageParcel data;
790 MessageParcel reply;
791 MessageOption option;
792 if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
793 APP_LOGE("Write interface token failed");
794 return;
795 }
796 if (!data.WriteInt32(resultCode)) {
797 APP_LOGE("Write result code failed");
798 return;
799 }
800 if (!data.WriteParcelable(&want)) {
801 APP_LOGE("Write want failed");
802 return;
803 }
804 if (!data.WriteInt32(userId)) {
805 APP_LOGE("Write userId failed");
806 return;
807 }
808
809 if (callBack->SendRequest(FREE_INSTALL_DONE, data, reply, option) != ERR_OK) {
810 APP_LOGE("BundleConnectAbilityMgr::CallAbilityManager SendRequest failed");
811 }
812 }
813
CheckIsModuleNeedUpdate(InnerBundleInfo & innerBundleInfo,const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)814 bool BundleConnectAbilityMgr::CheckIsModuleNeedUpdate(
815 InnerBundleInfo &innerBundleInfo, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
816 {
817 APP_LOGI("CheckIsModuleNeedUpdate called");
818 if (innerBundleInfo.GetModuleUpgradeFlag(want.GetModuleName()) != 0) {
819 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
820 if (targetAbilityInfo == nullptr) {
821 APP_LOGE("targetAbilityInfo is nullptr");
822 return false;
823 }
824 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
825 if (targetInfo == nullptr) {
826 APP_LOGE("targetInfo is nullptr");
827 return false;
828 }
829 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
830 if (targetExtSetting == nullptr) {
831 APP_LOGE("targetExtSetting is nullptr");
832 return false;
833 }
834 targetAbilityInfo->targetInfo = *targetInfo;
835 targetAbilityInfo->targetExtSetting = *targetExtSetting;
836 targetAbilityInfo->version = DEFAULT_VERSION;
837 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
838 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
839 if (freeInstallParams == nullptr) {
840 APP_LOGE("freeInstallParams is nullptr");
841 return false;
842 }
843 freeInstallParams->callback = callBack;
844 freeInstallParams->want = want;
845 freeInstallParams->userId = userId;
846 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_UPGRADE_INSTALL;
847 this->UpgradeInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
848 return true;
849 }
850 APP_LOGI("Module is not need update");
851 return false;
852 }
853
CheckDependencies(const std::string & moduleName,const InnerBundleInfo & innerBundleInfo)854 bool BundleConnectAbilityMgr::CheckDependencies(const std::string &moduleName, const InnerBundleInfo &innerBundleInfo)
855 {
856 std::vector<std::string> dependentModuleNames;
857 if (!innerBundleInfo.GetDependentModuleNames(moduleName, dependentModuleNames)) {
858 APP_LOGE("GetDependentModuleNames can not find module %{public}s", moduleName.c_str());
859 return false;
860 }
861 for (const std::string &depend : dependentModuleNames) {
862 if (!innerBundleInfo.FindModule(depend)) {
863 APP_LOGD("%{public}s does not exist locally.", depend.c_str());
864 return false;
865 }
866 }
867 return true;
868 }
869
IsObtainAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack,InnerBundleInfo & innerBundleInfo)870 bool BundleConnectAbilityMgr::IsObtainAbilityInfo(const Want &want, int32_t flags, int32_t userId,
871 AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack, InnerBundleInfo &innerBundleInfo)
872 {
873 APP_LOGD("IsObtainAbilityInfo");
874 std::string bundleName = want.GetElement().GetBundleName();
875 std::string abilityName = want.GetElement().GetAbilityName();
876 std::string moduleName = want.GetElement().GetModuleName();
877 if (bundleName == "" || abilityName == "") {
878 CallAbilityManager(FreeInstallErrorCode::UNDEFINED_ERROR, want, userId, callBack);
879 APP_LOGE("bundle name or ability name is null");
880 return false;
881 }
882 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
883 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
884 if (bundleDataMgr_ == nullptr) {
885 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
886 return false;
887 }
888 bool innerBundleInfoResult = bundleDataMgr_->GetInnerBundleInfoWithFlags(bundleName,
889 flags, innerBundleInfo, userId);
890 bool abilityInfoResult = bundleDataMgr_->QueryAbilityInfo(want, flags, userId, abilityInfo);
891 if (!abilityInfoResult) {
892 std::vector<ExtensionAbilityInfo> extensionInfos;
893 abilityInfoResult = bundleDataMgr_->QueryExtensionAbilityInfos(want, flags, userId, extensionInfos);
894 if (abilityInfoResult && moduleName.empty()) {
895 moduleName = extensionInfos[0].moduleName;
896 }
897 } else {
898 if (moduleName.empty()) {
899 moduleName = abilityInfo.moduleName;
900 }
901 }
902 if (innerBundleInfoResult && abilityInfoResult && CheckDependencies(moduleName, innerBundleInfo)) {
903 bool isModuleNeedUpdate = CheckIsModuleNeedUpdate(innerBundleInfo, want, userId, callBack);
904 if (!isModuleNeedUpdate) {
905 CallAbilityManager(ServiceCenterResultCode::FREE_INSTALL_OK, want, userId, callBack);
906 }
907 return true;
908 }
909 return false;
910 }
911
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)912 bool BundleConnectAbilityMgr::QueryAbilityInfo(const Want &want, int32_t flags,
913 int32_t userId, AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack)
914 {
915 APP_LOGD("QueryAbilityInfo");
916 InnerBundleInfo innerBundleInfo;
917 if (IsObtainAbilityInfo(want, flags, userId, abilityInfo, callBack, innerBundleInfo)) {
918 return true;
919 }
920 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
921 if (targetAbilityInfo == nullptr) {
922 APP_LOGE("targetAbilityInfo is nullptr");
923 return false;
924 }
925 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
926 if (targetInfo == nullptr) {
927 APP_LOGE("targetInfo is nullptr");
928 return false;
929 }
930 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
931 if (targetExtSetting == nullptr) {
932 APP_LOGE("targetExtSetting is nullptr");
933 return false;
934 }
935 targetAbilityInfo->targetInfo = *targetInfo;
936 targetAbilityInfo->targetExtSetting = *targetExtSetting;
937 targetAbilityInfo->version = DEFAULT_VERSION;
938 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
939 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
940 if (freeInstallParams == nullptr) {
941 APP_LOGE("freeInstallParams is nullptr");
942 return false;
943 }
944 freeInstallParams->callback = callBack;
945 freeInstallParams->want = want;
946 freeInstallParams->userId = userId;
947 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
948 this->SilentInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
949 return false;
950 }
951
SilentInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)952 bool BundleConnectAbilityMgr::SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
953 {
954 APP_LOGD("SilentInstall");
955 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
956 if (targetAbilityInfo == nullptr) {
957 APP_LOGE("targetAbilityInfo is nullptr");
958 return false;
959 }
960 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
961 if (targetInfo == nullptr) {
962 APP_LOGE("targetInfo is nullptr");
963 return false;
964 }
965 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
966 if (targetExtSetting == nullptr) {
967 APP_LOGE("targetExtSetting is nullptr");
968 return false;
969 }
970
971 targetAbilityInfo->targetInfo = *targetInfo;
972 targetAbilityInfo->targetExtSetting = *targetExtSetting;
973 targetAbilityInfo->version = DEFAULT_VERSION;
974 InnerBundleInfo innerBundleInfo;
975 GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
976 auto callingUid = IPCSkeleton::GetCallingUid();
977 std::vector<std::string> callingBundleNames;
978 std::vector<std::string> callingAppids;
979 GetCallingInfo(userId, callingUid, callingBundleNames, callingAppids);
980 targetAbilityInfo->targetInfo.callingUid = callingUid;
981 targetAbilityInfo->targetInfo.callingBundleNames = callingBundleNames;
982 targetAbilityInfo->targetInfo.callingAppIds = callingAppids;
983 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
984 if (freeInstallParams == nullptr) {
985 APP_LOGE("freeInstallParams is nullptr");
986 return false;
987 }
988
989 freeInstallParams->callback = callBack;
990 freeInstallParams->want = want;
991 freeInstallParams->userId = userId;
992 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_SILENT_INSTALL;
993 SilentInstall(*targetAbilityInfo, want, *freeInstallParams, userId);
994 return true;
995 }
996
UpgradeAtomicService(const Want & want,int32_t userId)997 void BundleConnectAbilityMgr::UpgradeAtomicService(const Want &want, int32_t userId)
998 {
999 APP_LOGI("UpgradeAtomicService");
1000 std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
1001 std::shared_ptr<BundleDataMgr> bundleDataMgr_ = bms->GetDataMgr();
1002 if (bundleDataMgr_ == nullptr) {
1003 APP_LOGE("GetDataMgr failed, bundleDataMgr_ is nullptr");
1004 return;
1005 }
1006 std::string bundleName = want.GetElement().GetBundleName();
1007 InnerBundleInfo innerBundleInfo;
1008 bundleDataMgr_->GetInnerBundleInfoWithFlags(bundleName, want.GetFlags(), innerBundleInfo, userId);
1009 if (!innerBundleInfo.GetEntryInstallationFree()) {
1010 APP_LOGI("bundleName:%{public}s is atomic application", bundleName.c_str());
1011 return;
1012 }
1013 APP_LOGI("bundleName:%{public}s is atomic service", bundleName.c_str());
1014 sptr<TargetAbilityInfo> targetAbilityInfo = new(std::nothrow) TargetAbilityInfo();
1015 if (targetAbilityInfo == nullptr) {
1016 APP_LOGE("targetAbilityInfo is nullptr");
1017 return;
1018 }
1019 sptr<TargetInfo> targetInfo = new(std::nothrow) TargetInfo();
1020 if (targetInfo == nullptr) {
1021 APP_LOGE("targetInfo is nullptr");
1022 return;
1023 }
1024 sptr<TargetExtSetting> targetExtSetting = new(std::nothrow) TargetExtSetting();
1025 if (targetExtSetting == nullptr) {
1026 APP_LOGE("targetExtSetting is nullptr");
1027 return;
1028 }
1029 targetAbilityInfo->targetInfo = *targetInfo;
1030 targetAbilityInfo->targetExtSetting = *targetExtSetting;
1031 targetAbilityInfo->version = DEFAULT_VERSION;
1032 this->GetTargetAbilityInfo(want, userId, innerBundleInfo, targetAbilityInfo);
1033 sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
1034 if (freeInstallParams == nullptr) {
1035 APP_LOGE("freeInstallParams is nullptr");
1036 return;
1037 }
1038 freeInstallParams->want = want;
1039 freeInstallParams->userId = userId;
1040 freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_UPGRADE_CHECK;
1041 this->UpgradeCheck(*targetAbilityInfo, want, *freeInstallParams, userId);
1042 }
1043 } // namespace AppExecFwk
1044 } // namespace OHOS
1045