blob: 7ccdfbd3cc953d1595fea8924f4382a5e1c5f1a5 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/action_processor.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080018
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080019#include <string>
Alex Deymo8427b4a2014-11-05 14:00:32 -080020
21#include <base/logging.h>
22
Alex Deymo39910dc2015-11-09 17:04:30 -080023#include "update_engine/common/action.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080025using std::string;
26
rspangler@google.com49fdf182009-10-10 00:57:34 +000027namespace chromeos_update_engine {
28
29ActionProcessor::ActionProcessor()
Alex Vakulenko88b591f2014-08-28 16:48:57 -070030 : current_action_(nullptr), delegate_(nullptr) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000031
32ActionProcessor::~ActionProcessor() {
Alex Deymo2b4268c2015-12-04 13:56:25 -080033 if (IsRunning())
rspangler@google.com49fdf182009-10-10 00:57:34 +000034 StopProcessing();
Alex Deymo2b4268c2015-12-04 13:56:25 -080035 for (auto action : actions_)
36 action->SetProcessor(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +000037}
38
39void ActionProcessor::EnqueueAction(AbstractAction* action) {
40 actions_.push_back(action);
41 action->SetProcessor(this);
42}
43
44void ActionProcessor::StartProcessing() {
45 CHECK(!IsRunning());
46 if (!actions_.empty()) {
47 current_action_ = actions_.front();
48 LOG(INFO) << "ActionProcessor::StartProcessing: "
49 << current_action_->Type();
50 actions_.pop_front();
51 current_action_->PerformAction();
52 }
53}
54
55void ActionProcessor::StopProcessing() {
56 CHECK(IsRunning());
57 CHECK(current_action_);
58 current_action_->TerminateProcessing();
59 CHECK(current_action_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070060 current_action_->SetProcessor(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 LOG(INFO) << "ActionProcessor::StopProcessing: aborted "
62 << current_action_->Type();
Alex Vakulenko88b591f2014-08-28 16:48:57 -070063 current_action_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 if (delegate_)
65 delegate_->ProcessingStopped(this);
66}
67
adlr@google.comc98a7ed2009-12-04 18:54:03 +000068void ActionProcessor::ActionComplete(AbstractAction* actionptr,
David Zeuthena99981f2013-04-29 13:42:47 -070069 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 CHECK_EQ(actionptr, current_action_);
71 if (delegate_)
Darin Petkovc1a8b422010-07-19 11:34:49 -070072 delegate_->ActionCompleted(this, actionptr, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 string old_type = current_action_->Type();
David Zeuthen33bae492014-02-25 16:16:18 -080074 current_action_->ActionCompleted(code);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070075 current_action_->SetProcessor(nullptr);
76 current_action_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000077 if (actions_.empty()) {
78 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
79 " type " << old_type;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070080 } else if (code != ErrorCode::kSuccess) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081 LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type
82 << " action failed. Aborting processing.";
83 actions_.clear();
84 }
85 if (actions_.empty()) {
86 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
87 " type " << old_type;
rspangler@google.com49fdf182009-10-10 00:57:34 +000088 if (delegate_) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070089 delegate_->ProcessingDone(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000090 }
91 return;
92 }
93 current_action_ = actions_.front();
94 actions_.pop_front();
95 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
96 << ", starting " << current_action_->Type();
97 current_action_->PerformAction();
98}
99
100} // namespace chromeos_update_engine