• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "form_bms_helper.h"
18 #include "form_mgr_errors.h"
19 #include "form_share_mgr.h"
20 #include "form_util.h"
21 #include "hilog_wrapper.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
FormFreeInstallOperator(const std::string & formShareInfoKey,const std::shared_ptr<FormEventHandler> & handler)25 FormFreeInstallOperator::FormFreeInstallOperator(const std::string &formShareInfoKey,
26     const std::shared_ptr<FormEventHandler> &handler)
27     : formShareInfoKey_(formShareInfoKey), handler_(handler)
28 {
29 }
30 
~FormFreeInstallOperator()31 FormFreeInstallOperator::~FormFreeInstallOperator()
32 {
33     freeInstallStatusCallBack_ = nullptr;
34 }
35 
StartFreeInstall(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName)36 int32_t FormFreeInstallOperator::StartFreeInstall(
37     const std::string &bundleName, const std::string &moduleName, const std::string &abilityName)
38 {
39     HILOG_DEBUG("%{public}s called, bundleName: %{public}s, abilityName: %{public}s",
40         __func__, bundleName.c_str(), abilityName.c_str());
41 
42     freeInstallStatusCallBack_ = new (std::nothrow) FreeInstallStatusCallBack(weak_from_this());
43     if (freeInstallStatusCallBack_ == nullptr) {
44         HILOG_ERROR("new FreeInstallStatusCallBack failed");
45         return ERR_APPEXECFWK_FORM_COMMON_CODE;
46     }
47 
48     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
49     if (iBundleMgr == nullptr) {
50         HILOG_ERROR("get IBundleMgr failed");
51         return ERR_APPEXECFWK_FORM_COMMON_CODE;
52     }
53 
54     Want want;
55     want.SetElementName(bundleName, abilityName);
56     want.SetModuleName(moduleName);
57     AbilityInfo abilityInfo = {};
58     constexpr auto flag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
59     if (iBundleMgr->QueryAbilityInfo(
60         want, flag, FormUtil::GetCurrentAccountId(), abilityInfo, freeInstallStatusCallBack_)) {
61         HILOG_DEBUG("The app has installed.");
62     }
63 
64     return ERR_OK;
65 }
66 
OnInstallFinished(int32_t resultCode)67 void FormFreeInstallOperator::OnInstallFinished(int32_t resultCode)
68 {
69     HILOG_DEBUG("%{public}s called, resultCode: %{public}d", __func__, resultCode);
70     if (handler_ == nullptr) {
71         return;
72     }
73     auto self = shared_from_this();
74     auto task = [self, resultCode]() {
75         DelayedSingleton<FormShareMgr>::GetInstance()->OnInstallFinished(self, resultCode, self->formShareInfoKey_);
76     };
77     handler_->PostTask(task);
78 }
79 
FreeInstallStatusCallBack(const std::weak_ptr<FormFreeInstallOperator> & freeInstallOperator)80 FreeInstallStatusCallBack::FreeInstallStatusCallBack(
81     const std::weak_ptr<FormFreeInstallOperator> &freeInstallOperator)
82     : formFreeInstallOperator_(freeInstallOperator)
83 {
84 }
85 
OnInstallFinished(int32_t resultCode,const Want & want,int32_t userId)86 void FreeInstallStatusCallBack::OnInstallFinished(int32_t resultCode, const Want &want, int32_t userId)
87 {
88     HILOG_DEBUG("%{public}s called, resultCode:%{public}d.", __func__, resultCode);
89 
90     auto freeInstallOperator = formFreeInstallOperator_.lock();
91     if (freeInstallOperator == nullptr) {
92         HILOG_ERROR("freeInstallOperator is nullptr");
93         return;
94     }
95     freeInstallOperator->OnInstallFinished(resultCode);
96     HILOG_DEBUG("%{public}s end.", __func__);
97 }
98 } // namespace AppExecFwk
99 } // namespace OHOS
100