• 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 "quick_fix_manager_service.h"
17 
18 #include "hilog_wrapper.h"
19 #include "hitrace_meter.h"
20 #include "permission_verification.h"
21 #include "quick_fix_error_utils.h"
22 #include "quick_fix_utils.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 std::mutex QuickFixManagerService::mutex_;
27 sptr<QuickFixManagerService> QuickFixManagerService::instance_;
28 
GetInstance()29 sptr<QuickFixManagerService> QuickFixManagerService::GetInstance()
30 {
31     std::lock_guard<std::mutex> lock(mutex_);
32 
33     if (instance_ == nullptr) {
34         instance_ = new QuickFixManagerService();
35     }
36     return instance_;
37 }
38 
Init()39 bool QuickFixManagerService::Init()
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     eventRunner_ = AppExecFwk::EventRunner::Create("QuickFixMgrSvrMain");
43     if (eventRunner_ == nullptr) {
44         HILOG_ERROR("Create event runner failed.");
45         return false;
46     }
47 
48     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventRunner_);
49     if (eventHandler_ == nullptr) {
50         HILOG_ERROR("Create event handler failed.");
51         return false;
52     }
53 
54     return true;
55 }
56 
ApplyQuickFix(const std::vector<std::string> & quickFixFiles)57 int32_t QuickFixManagerService::ApplyQuickFix(const std::vector<std::string> &quickFixFiles)
58 {
59     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
60     HILOG_DEBUG("function called.");
61 
62     if (!AAFwk::PermissionVerification::GetInstance()->VerifyInstallBundlePermission()) {
63         return QUICK_FIX_VERIFY_PERMISSION_FAILED;
64     }
65 
66     auto bundleQfMgr = QuickFixUtil::GetBundleQuickFixMgrProxy();
67     if (bundleQfMgr == nullptr) {
68         HILOG_ERROR("Bundle quick fix manager is nullptr.");
69         return QUICK_FIX_CONNECT_FAILED;
70     }
71 
72     auto appMgr = QuickFixUtil::GetAppManagerProxy();
73     if (appMgr == nullptr) {
74         HILOG_ERROR("App manager is nullptr.");
75         return QUICK_FIX_CONNECT_FAILED;
76     }
77 
78     auto applyTask = std::make_shared<QuickFixManagerApplyTask>(bundleQfMgr, appMgr, eventHandler_, this);
79     AddApplyTask(applyTask);
80     applyTask->Run(quickFixFiles);
81 
82     HILOG_DEBUG("function finished.");
83     return QUICK_FIX_OK;
84 }
85 
GetApplyedQuickFixInfo(const std::string & bundleName,ApplicationQuickFixInfo & quickFixInfo)86 int32_t QuickFixManagerService::GetApplyedQuickFixInfo(const std::string &bundleName,
87     ApplicationQuickFixInfo &quickFixInfo)
88 {
89     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
90     HILOG_DEBUG("function called.");
91 
92     if (!AAFwk::PermissionVerification::GetInstance()->VerifyGetBundleInfoPrivilegedPermission()) {
93         return QUICK_FIX_VERIFY_PERMISSION_FAILED;
94     }
95 
96     auto bundleMgr = QuickFixUtil::GetBundleManagerProxy();
97     if (bundleMgr == nullptr) {
98         HILOG_ERROR("Failed to get bundle manager.");
99         return QUICK_FIX_CONNECT_FAILED;
100     }
101 
102     AppExecFwk::BundleInfo bundleInfo;
103     if (!bundleMgr->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo,
104         AppExecFwk::Constants::ANY_USERID)) {
105         HILOG_ERROR("Get bundle info failed.");
106         return QUICK_FIX_GET_BUNDLE_INFO_FAILED;
107     }
108 
109     quickFixInfo.bundleName = bundleName;
110     quickFixInfo.bundleVersionCode = bundleInfo.versionCode;
111     quickFixInfo.bundleVersionName = bundleInfo.versionName;
112     quickFixInfo.appqfInfo = bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo;
113 
114     HILOG_DEBUG("function finished.");
115     return QUICK_FIX_OK;
116 }
117 
AddApplyTask(std::shared_ptr<QuickFixManagerApplyTask> applyTask)118 void QuickFixManagerService::AddApplyTask(std::shared_ptr<QuickFixManagerApplyTask> applyTask)
119 {
120     std::lock_guard<std::mutex> lock(mutex_);
121     applyTasks_.emplace_back(applyTask);
122 }
123 
RemoveApplyTask(std::shared_ptr<QuickFixManagerApplyTask> applyTask)124 void QuickFixManagerService::RemoveApplyTask(std::shared_ptr<QuickFixManagerApplyTask> applyTask)
125 {
126     std::lock_guard<std::mutex> lock(mutex_);
127     for (auto it = applyTasks_.begin(); it != applyTasks_.end();) {
128         if (*it == applyTask) {
129             it = applyTasks_.erase(it);
130         } else {
131             it++;
132         }
133     }
134 }
135 }  // namespace AAFwk
136 }  // namespace OHOS
137