• 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_mgr.h"
17 
18 #include <cinttypes>
19 
20 #include "app_log_wrapper.h"
21 #include "bundle_mgr_service.h"
22 #include "datetime_ex.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string ADD_QUICK_FIXER_FAILED = "fail to add quick fixer";
28 } // namespace
QuickFixMgr(const std::shared_ptr<EventRunner> & runner)29 QuickFixMgr::QuickFixMgr(const std::shared_ptr<EventRunner> &runner) : EventHandler(runner)
30 {
31     APP_LOGI("create quick fixer async manager instance");
32 }
33 
~QuickFixMgr()34 QuickFixMgr::~QuickFixMgr()
35 {
36     APP_LOGI("destory quick fixer async manager instance");
37 }
38 
ProcessEvent(const InnerEvent::Pointer & event)39 void QuickFixMgr::ProcessEvent(const InnerEvent::Pointer &event)
40 {
41     APP_LOGD("process event : %{public}u", event->GetInnerEventId());
42     if (event->GetInnerEventId() == REMOVE_QUICK_FIXER) {
43         RemoveQuickFixer(event->GetParam());
44         return;
45     }
46     APP_LOGW("the eventId is not supported");
47 }
48 
DeployQuickFix(const std::vector<std::string> & bundleFilePaths,const sptr<IQuickFixStatusCallback> & statusCallback)49 ErrCode QuickFixMgr::DeployQuickFix(const std::vector<std::string> &bundleFilePaths,
50     const sptr<IQuickFixStatusCallback> &statusCallback)
51 {
52     APP_LOGI("DeployQuickFix begin");
53     auto quickFixer = CreateQuickFixer(statusCallback);
54     if (quickFixer == nullptr) {
55         APP_LOGE("DeployQuickFix failed due to nullptr quick fixer");
56         return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
57     }
58 
59     auto task = [quickFixer, bundleFilePaths] {
60         quickFixer->DeployQuickFix(bundleFilePaths);
61     };
62 
63     ThreadPool &installersPool = DelayedSingleton<BundleMgrService>::GetInstance()->GetThreadPool();
64     installersPool.AddTask(task);
65     return ERR_OK;
66 }
67 
SwitchQuickFix(const std::string & bundleName,bool enable,const sptr<IQuickFixStatusCallback> & statusCallback)68 ErrCode QuickFixMgr::SwitchQuickFix(const std::string &bundleName, bool enable,
69     const sptr<IQuickFixStatusCallback> &statusCallback)
70 {
71     APP_LOGI("SwitchQuickFix begin");
72     auto quickFixer = CreateQuickFixer(statusCallback);
73     if (quickFixer == nullptr) {
74         APP_LOGE("SwitchQuickFix failed due to nullptr quick fixer");
75         return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
76     }
77 
78     auto task = [quickFixer, bundleName, enable] {
79         quickFixer->SwitchQuickFix(bundleName, enable);
80     };
81 
82     ThreadPool &installersPool = DelayedSingleton<BundleMgrService>::GetInstance()->GetThreadPool();
83     installersPool.AddTask(task);
84     return ERR_OK;
85 }
86 
DeleteQuickFix(const std::string & bundleName,const sptr<IQuickFixStatusCallback> & statusCallback)87 ErrCode QuickFixMgr::DeleteQuickFix(const std::string &bundleName,
88     const sptr<IQuickFixStatusCallback> &statusCallback)
89 {
90     APP_LOGI("DeleteQuickFix begin");
91     auto quickFixer = CreateQuickFixer(statusCallback);
92     if (quickFixer == nullptr) {
93         APP_LOGE("DeleteQuickFix failed due to nullptr quick fixer");
94         return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
95     }
96 
97     auto task = [quickFixer, bundleName] {
98         quickFixer->DeleteQuickFix(bundleName);
99     };
100 
101     ThreadPool &installersPool = DelayedSingleton<BundleMgrService>::GetInstance()->GetThreadPool();
102     installersPool.AddTask(task);
103     return ERR_OK;
104 }
105 
CreateQuickFixer(const sptr<IQuickFixStatusCallback> & statusCallback)106 std::shared_ptr<QuickFixer> QuickFixMgr::CreateQuickFixer(const sptr<IQuickFixStatusCallback> &statusCallback)
107 {
108     int64_t quickFixerId = GetMicroTickCount();
109     auto fixer = std::make_shared<QuickFixer>(quickFixerId, shared_from_this(), statusCallback);
110     bool isSuccess = false;
111     {
112         std::lock_guard<std::mutex> lock(mutex_);
113         auto result = quickFixer_.try_emplace(quickFixerId, fixer);
114         isSuccess = result.second;
115     }
116 
117     if (!isSuccess) {
118         APP_LOGE("fail to add bundle quickFixer");
119         fixer.reset();
120         return fixer;
121     }
122     APP_LOGD("add the specific %{public}" PRId64 " quickFixer", quickFixerId);
123     return fixer;
124 }
125 
RemoveQuickFixer(const int64_t fixerId)126 void QuickFixMgr::RemoveQuickFixer(const int64_t fixerId)
127 {
128     APP_LOGD("start to remove quick fixer the specific %{public}" PRId64 " quickFixer", fixerId);
129     std::lock_guard<std::mutex> lock(mutex_);
130     if (quickFixer_.erase(fixerId) > 0) {
131         APP_LOGD("erase the specific %{public}" PRId64 " quickFixer", fixerId);
132     }
133 }
134 } // AppExecFwk
135 } // OHOS