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 "action_processer.h"
17
18 #include "log/log.h"
19
20 namespace OHOS {
21 namespace SysInstaller {
22 using namespace Updater;
23
GetInstance()24 ActionProcesser &ActionProcesser::GetInstance()
25 {
26 static ActionProcesser instance;
27 return instance;
28 }
29
IsRunning()30 bool ActionProcesser::IsRunning()
31 {
32 return isRunning_ || isSuspend_;
33 }
34
AddAction(std::unique_ptr<IAction> action)35 void ActionProcesser::AddAction(std::unique_ptr<IAction> action)
36 {
37 if (isRunning_ || action == nullptr) {
38 LOG(ERROR) << "action running or action empty";
39 return;
40 }
41
42 LOG(INFO) << "add " << action->GetActionName();
43 auto callBack = [this](InstallerErrCode errCode, const std::string &errStr) {
44 CompletedAction(errCode, errStr);
45 };
46 action->SetCallback(callBack);
47 actionQue_.push_back(std::move(action));
48 }
49
Start()50 void ActionProcesser::Start()
51 {
52 if (isRunning_ || actionQue_.empty()) {
53 LOG(WARNING) << "Action running or queue empty";
54 return;
55 }
56
57 isRunning_ = true;
58 isSuspend_ = false;
59 statusManager_->UpdateCallback(UPDATE_STATE_ONGOING, 0, "");
60 curAction_ = std::move(actionQue_.front());
61 actionQue_.pop_front();
62 if (curAction_ == nullptr) {
63 LOG(WARNING) << "curAction_ nullptr";
64 return;
65 }
66 LOG(INFO) << "Start " << curAction_->GetActionName();
67 curAction_->PerformAction();
68 }
69
Stop()70 void ActionProcesser::Stop()
71 {
72 if (!isRunning_) {
73 LOG(WARNING) << "Action not running";
74 return;
75 }
76
77 if (curAction_ != nullptr) {
78 LOG(INFO) << "Stop " << curAction_->GetActionName();
79 curAction_->TerminateAction();
80 }
81
82 isRunning_ = false;
83 isSuspend_ = false;
84 curAction_.reset();
85 actionQue_.clear();
86 }
87
Suspend()88 void ActionProcesser::Suspend()
89 {
90 if (!isRunning_ || curAction_ == nullptr) {
91 LOG(WARNING) << "ActionProcesser not running or action empty";
92 return;
93 }
94
95 LOG(INFO) << "Suspend " << curAction_->GetActionName();
96 isSuspend_ = true;
97 curAction_->SuspendAction();
98 }
99
Resume()100 void ActionProcesser::Resume()
101 {
102 if (isSuspend_) {
103 LOG(WARNING) << "ActionProcesser is running";
104 return;
105 }
106
107 isSuspend_ = false;
108 if (curAction_ != nullptr) {
109 LOG(INFO) << "Resume " << curAction_->GetActionName();
110 return curAction_->ResumeAction();
111 }
112
113 StartNextAction();
114 }
115
CompletedAction(InstallerErrCode errCode,const std::string & errStr)116 void ActionProcesser::CompletedAction(InstallerErrCode errCode, const std::string &errStr)
117 {
118 if (curAction_ == nullptr) {
119 LOG(ERROR) << "curAction_ null error ";
120 return;
121 }
122
123 LOG(INFO) << "Completed " << curAction_->GetActionName();
124 curAction_.reset();
125 if (errCode != SYS_UPDATE_SUCCESS) {
126 isRunning_ = false;
127 isSuspend_ = false;
128 statusManager_->UpdateCallback(UPDATE_STATE_FAILED, 100, errStr); // 100 : action failed
129 actionQue_.clear();
130 LOG(ERROR) << "CompletedAction errCode:" << errCode << " str:" << errStr;
131 return;
132 }
133
134 if (isSuspend_) {
135 LOG(INFO) << "suspend";
136 return;
137 }
138
139 StartNextAction();
140 }
141
142
StartNextAction()143 void ActionProcesser::StartNextAction()
144 {
145 if (actionQue_.empty()) {
146 LOG(INFO) << "Action queue empty, successful";
147 isRunning_ = false;
148 isSuspend_ = false;
149 statusManager_->UpdateCallback(UPDATE_STATE_SUCCESSFUL, 100, ""); // 100 : action completed
150 return;
151 }
152
153 curAction_ = std::move(actionQue_.front());
154 LOG(INFO) << "StartNextAction " << curAction_->GetActionName();
155 actionQue_.pop_front();
156 curAction_->PerformAction();
157 }
158 } // namespace SysInstaller
159 } // namespace OHOS
160