blob: 1884060936d826987500df9e48e7345bcea55def [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
17#include "update_engine/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
rspangler@google.com49fdf182009-10-10 00:57:34 +000023#include "update_engine/action.h"
24
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() {
33 if (IsRunning()) {
34 StopProcessing();
35 }
36 for (std::deque<AbstractAction*>::iterator it = actions_.begin();
37 it != actions_.end(); ++it) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070038 (*it)->SetProcessor(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +000039 }
40}
41
42void ActionProcessor::EnqueueAction(AbstractAction* action) {
43 actions_.push_back(action);
44 action->SetProcessor(this);
45}
46
47void ActionProcessor::StartProcessing() {
48 CHECK(!IsRunning());
49 if (!actions_.empty()) {
50 current_action_ = actions_.front();
51 LOG(INFO) << "ActionProcessor::StartProcessing: "
52 << current_action_->Type();
53 actions_.pop_front();
54 current_action_->PerformAction();
55 }
56}
57
58void ActionProcessor::StopProcessing() {
59 CHECK(IsRunning());
60 CHECK(current_action_);
61 current_action_->TerminateProcessing();
62 CHECK(current_action_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070063 current_action_->SetProcessor(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 LOG(INFO) << "ActionProcessor::StopProcessing: aborted "
65 << current_action_->Type();
Alex Vakulenko88b591f2014-08-28 16:48:57 -070066 current_action_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000067 if (delegate_)
68 delegate_->ProcessingStopped(this);
69}
70
adlr@google.comc98a7ed2009-12-04 18:54:03 +000071void ActionProcessor::ActionComplete(AbstractAction* actionptr,
David Zeuthena99981f2013-04-29 13:42:47 -070072 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 CHECK_EQ(actionptr, current_action_);
74 if (delegate_)
Darin Petkovc1a8b422010-07-19 11:34:49 -070075 delegate_->ActionCompleted(this, actionptr, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000076 string old_type = current_action_->Type();
David Zeuthen33bae492014-02-25 16:16:18 -080077 current_action_->ActionCompleted(code);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070078 current_action_->SetProcessor(nullptr);
79 current_action_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000080 if (actions_.empty()) {
81 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
82 " type " << old_type;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070083 } else if (code != ErrorCode::kSuccess) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000084 LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type
85 << " action failed. Aborting processing.";
86 actions_.clear();
87 }
88 if (actions_.empty()) {
89 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
90 " type " << old_type;
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 if (delegate_) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070092 delegate_->ProcessingDone(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000093 }
94 return;
95 }
96 current_action_ = actions_.front();
97 actions_.pop_front();
98 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
99 << ", starting " << current_action_->Type();
100 current_action_->PerformAction();
101}
102
103} // namespace chromeos_update_engine