1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "form_free_install_operator.h"
17
18 #include "fms_log_wrapper.h"
19 #include "form_bms_helper.h"
20 #include "form_mgr_errors.h"
21 #include "form_serial_queue.h"
22 #include "form_share_mgr.h"
23 #include "form_util.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
FormFreeInstallOperator(const std::string & formShareInfoKey,const std::shared_ptr<FormSerialQueue> & serialQueue)27 FormFreeInstallOperator::FormFreeInstallOperator(const std::string &formShareInfoKey,
28 const std::shared_ptr<FormSerialQueue> &serialQueue)
29 : formShareInfoKey_(formShareInfoKey), serialQueue_(serialQueue)
30 {
31 }
32
~FormFreeInstallOperator()33 FormFreeInstallOperator::~FormFreeInstallOperator()
34 {
35 freeInstallStatusCallBack_ = nullptr;
36 }
37
StartFreeInstall(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName)38 int32_t FormFreeInstallOperator::StartFreeInstall(
39 const std::string &bundleName, const std::string &moduleName, const std::string &abilityName)
40 {
41 HILOG_DEBUG("bundleName:%{public}s,abilityName:%{public}s",
42 bundleName.c_str(), abilityName.c_str());
43
44 freeInstallStatusCallBack_ = new (std::nothrow) FreeInstallStatusCallBack(weak_from_this());
45 if (freeInstallStatusCallBack_ == nullptr) {
46 HILOG_ERROR("new FreeInstallStatusCallBack failed");
47 return ERR_APPEXECFWK_FORM_COMMON_CODE;
48 }
49
50 sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
51 if (iBundleMgr == nullptr) {
52 HILOG_ERROR("get IBundleMgr failed");
53 return ERR_APPEXECFWK_FORM_COMMON_CODE;
54 }
55
56 Want want;
57 want.SetElementName(bundleName, abilityName);
58 want.SetModuleName(moduleName);
59 AbilityInfo abilityInfo = {};
60 constexpr auto flag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
61 if (iBundleMgr->QueryAbilityInfo(
62 want, flag, FormUtil::GetCurrentAccountId(), abilityInfo, freeInstallStatusCallBack_)) {
63 HILOG_DEBUG("The app has installed");
64 }
65
66 return ERR_OK;
67 }
68
OnInstallFinished(int32_t resultCode)69 void FormFreeInstallOperator::OnInstallFinished(int32_t resultCode)
70 {
71 HILOG_DEBUG("resultCode:%{public}d", resultCode);
72 if (serialQueue_ == nullptr) {
73 return;
74 }
75 auto self = shared_from_this();
76 auto task = [self, resultCode]() {
77 DelayedSingleton<FormShareMgr>::GetInstance()->OnInstallFinished(self, resultCode, self->formShareInfoKey_);
78 };
79 serialQueue_->ScheduleTask(0, task);
80 }
81
FreeInstallStatusCallBack(const std::weak_ptr<FormFreeInstallOperator> & freeInstallOperator)82 FreeInstallStatusCallBack::FreeInstallStatusCallBack(
83 const std::weak_ptr<FormFreeInstallOperator> &freeInstallOperator)
84 : formFreeInstallOperator_(freeInstallOperator)
85 {
86 }
87
OnInstallFinished(int32_t resultCode,const Want & want,int32_t userId)88 void FreeInstallStatusCallBack::OnInstallFinished(int32_t resultCode, const Want &want, int32_t userId)
89 {
90 HILOG_DEBUG("resultCode:%{public}d", resultCode);
91
92 auto freeInstallOperator = formFreeInstallOperator_.lock();
93 if (freeInstallOperator == nullptr) {
94 HILOG_ERROR("null freeInstallOperator");
95 return;
96 }
97 freeInstallOperator->OnInstallFinished(resultCode);
98 HILOG_DEBUG("end");
99 }
100 } // namespace AppExecFwk
101 } // namespace OHOS
102