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 "quick_fix_mgr.h"
17 
18 #include "app_log_tag_wrapper.h"
19 #include "bundle_memory_guard.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
QuickFixMgr()23 QuickFixMgr::QuickFixMgr()
24 {
25     LOG_I(BMS_TAG_DEFAULT, "create quick fixer async manager instance");
26 }
27 
~QuickFixMgr()28 QuickFixMgr::~QuickFixMgr()
29 {
30     LOG_I(BMS_TAG_DEFAULT, "destory quick fixer async manager instance");
31 }
32 
DeployQuickFix(const std::vector<std::string> & bundleFilePaths,const sptr<IQuickFixStatusCallback> & statusCallback,bool isDebug,const std::string & targetPath)33 ErrCode QuickFixMgr::DeployQuickFix(const std::vector<std::string> &bundleFilePaths,
34     const sptr<IQuickFixStatusCallback> &statusCallback, bool isDebug, const std::string &targetPath)
35 {
36     LOG_I(BMS_TAG_DEFAULT, "DeployQuickFix begin");
37     auto quickFixer = CreateQuickFixer(statusCallback);
38     if (quickFixer == nullptr) {
39         LOG_E(BMS_TAG_DEFAULT, "DeployQuickFix failed due to nullptr quick fixer");
40         return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
41     }
42 
43     auto task = [quickFixer, bundleFilePaths, isDebug, targetPath] {
44         BundleMemoryGuard memoryGuard;
45         quickFixer->DeployQuickFix(bundleFilePaths, isDebug, targetPath);
46     };
47 
48     ffrt::submit(task);
49     return ERR_OK;
50 }
51 
SwitchQuickFix(const std::string & bundleName,bool enable,const sptr<IQuickFixStatusCallback> & statusCallback)52 ErrCode QuickFixMgr::SwitchQuickFix(const std::string &bundleName, bool enable,
53     const sptr<IQuickFixStatusCallback> &statusCallback)
54 {
55     LOG_I(BMS_TAG_DEFAULT, "SwitchQuickFix begin");
56     auto quickFixer = CreateQuickFixer(statusCallback);
57     if (quickFixer == nullptr) {
58         LOG_E(BMS_TAG_DEFAULT, "SwitchQuickFix failed due to nullptr quick fixer");
59         return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
60     }
61 
62     auto task = [quickFixer, bundleName, enable] {
63         BundleMemoryGuard memoryGuard;
64         quickFixer->SwitchQuickFix(bundleName, enable);
65     };
66 
67     ffrt::submit(task);
68     return ERR_OK;
69 }
70 
DeleteQuickFix(const std::string & bundleName,const sptr<IQuickFixStatusCallback> & statusCallback)71 ErrCode QuickFixMgr::DeleteQuickFix(const std::string &bundleName,
72     const sptr<IQuickFixStatusCallback> &statusCallback)
73 {
74     LOG_I(BMS_TAG_DEFAULT, "DeleteQuickFix begin");
75     auto quickFixer = CreateQuickFixer(statusCallback);
76     if (quickFixer == nullptr) {
77         LOG_E(BMS_TAG_DEFAULT, "DeleteQuickFix failed due to nullptr quick fixer");
78         return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
79     }
80 
81     auto task = [quickFixer, bundleName] {
82         BundleMemoryGuard memoryGuard;
83         quickFixer->DeleteQuickFix(bundleName);
84     };
85 
86     ffrt::submit(task);
87     return ERR_OK;
88 }
89 
CreateQuickFixer(const sptr<IQuickFixStatusCallback> & statusCallback)90 std::shared_ptr<QuickFixer> QuickFixMgr::CreateQuickFixer(const sptr<IQuickFixStatusCallback> &statusCallback)
91 {
92     return std::make_shared<QuickFixer>(statusCallback);
93 }
94 } // AppExecFwk
95 } // OHOS