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_fixer.h"
17
18 #include <cinttypes>
19
20 #include "app_log_wrapper.h"
21 #include "quick_fix_mgr.h"
22 #include "quick_fix_deleter.h"
23 #include "quick_fix_deployer.h"
24 #include "quick_fix_switcher.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
QuickFixer(const int64_t quickFixerId,const std::shared_ptr<EventHandler> & handler,const sptr<IQuickFixStatusCallback> & statusCallback)28 QuickFixer::QuickFixer(const int64_t quickFixerId, const std::shared_ptr<EventHandler> &handler,
29 const sptr<IQuickFixStatusCallback> &statusCallback) : quickFixerId_(quickFixerId), handler_(handler),
30 statusCallback_(statusCallback)
31 {
32 APP_LOGI("enter QuickFixer");
33 }
34
DeployQuickFix(const std::vector<std::string> & bundleFilePaths)35 void QuickFixer::DeployQuickFix(const std::vector<std::string> &bundleFilePaths)
36 {
37 APP_LOGI("DeployQuickFix start");
38 if (statusCallback_ == nullptr) {
39 APP_LOGE("DeployQuickFix failed due to nullptr statusCallback");
40 }
41
42 std::unique_ptr<QuickFixDeployer> deployer = std::make_unique<QuickFixDeployer>(bundleFilePaths);
43 auto ret = deployer->Execute();
44
45 // callback operation
46 DeployQuickFixResult result = deployer->GetDeployQuickFixResult();
47 result.resultCode = ret;
48 std::shared_ptr<QuickFixResult> deployRes = std::make_shared<DeployQuickFixResult>(result);
49 if (statusCallback_ != nullptr) {
50 statusCallback_->OnPatchDeployed(deployRes);
51 }
52
53 SendRemoveEvent();
54 }
55
SwitchQuickFix(const std::string & bundleName,bool enable)56 void QuickFixer::SwitchQuickFix(const std::string &bundleName, bool enable)
57 {
58 APP_LOGI("SwitchQuickFix start");
59 if (statusCallback_ == nullptr) {
60 APP_LOGE("SwitchQuickFix failed due to nullptr statusCallback");
61 }
62
63 std::unique_ptr<IQuickFix> switcher = std::make_unique<QuickFixSwitcher>(bundleName, enable);
64 auto ret = switcher->Execute();
65
66 // callback operation
67 SwitchQuickFixResult result;
68 result.resultCode = ret;
69 result.bundleName = bundleName;
70 std::shared_ptr<QuickFixResult> switchRes = std::make_shared<SwitchQuickFixResult>(result);
71 if (statusCallback_ != nullptr) {
72 statusCallback_->OnPatchSwitched(switchRes);
73 }
74
75 SendRemoveEvent();
76 }
77
DeleteQuickFix(const std::string & bundleName)78 void QuickFixer::DeleteQuickFix(const std::string &bundleName)
79 {
80 APP_LOGI("DeleteQuickFix start");
81 if (statusCallback_ == nullptr) {
82 APP_LOGE("DeleteQuickFix failed due to nullptr statusCallback");
83 }
84
85 std::unique_ptr<IQuickFix> deleter = std::make_unique<QuickFixDeleter>(bundleName);
86 auto ret = deleter->Execute();
87
88 // callback operation
89 DeleteQuickFixResult result;
90 result.resultCode = ret;
91 result.bundleName = bundleName;
92 std::shared_ptr<QuickFixResult> deleteRes = std::make_shared<DeleteQuickFixResult>(result);
93 if (statusCallback_ != nullptr) {
94 statusCallback_->OnPatchDeleted(deleteRes);
95 }
96
97 SendRemoveEvent();
98 }
99
SendRemoveEvent() const100 void QuickFixer::SendRemoveEvent() const
101 {
102 if (auto handler = handler_.lock()) {
103 APP_LOGD("SendRemoveEvent begin");
104 handler->SendEvent(InnerEvent::Get(QuickFixMgr::MessageId::REMOVE_QUICK_FIXER, quickFixerId_));
105 return;
106 }
107 APP_LOGE("fail to remove %{public}" PRId64 " quickFixer due to handler is expired", quickFixerId_);
108 }
109 } // AppExecFwk
110 } // OHOS