blob: 9a27aac6d5b901af649b91479a4987964be6d912 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/action_processor.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -08006
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08007#include <string>
Alex Deymo8427b4a2014-11-05 14:00:32 -08008
9#include <base/logging.h>
10
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#include "update_engine/action.h"
12
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080013using std::string;
14
rspangler@google.com49fdf182009-10-10 00:57:34 +000015namespace chromeos_update_engine {
16
17ActionProcessor::ActionProcessor()
Alex Vakulenko88b591f2014-08-28 16:48:57 -070018 : current_action_(nullptr), delegate_(nullptr) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
20ActionProcessor::~ActionProcessor() {
21 if (IsRunning()) {
22 StopProcessing();
23 }
24 for (std::deque<AbstractAction*>::iterator it = actions_.begin();
25 it != actions_.end(); ++it) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070026 (*it)->SetProcessor(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +000027 }
28}
29
30void ActionProcessor::EnqueueAction(AbstractAction* action) {
31 actions_.push_back(action);
32 action->SetProcessor(this);
33}
34
35void ActionProcessor::StartProcessing() {
36 CHECK(!IsRunning());
37 if (!actions_.empty()) {
38 current_action_ = actions_.front();
39 LOG(INFO) << "ActionProcessor::StartProcessing: "
40 << current_action_->Type();
41 actions_.pop_front();
42 current_action_->PerformAction();
43 }
44}
45
46void ActionProcessor::StopProcessing() {
47 CHECK(IsRunning());
48 CHECK(current_action_);
49 current_action_->TerminateProcessing();
50 CHECK(current_action_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070051 current_action_->SetProcessor(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +000052 LOG(INFO) << "ActionProcessor::StopProcessing: aborted "
53 << current_action_->Type();
Alex Vakulenko88b591f2014-08-28 16:48:57 -070054 current_action_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 if (delegate_)
56 delegate_->ProcessingStopped(this);
57}
58
adlr@google.comc98a7ed2009-12-04 18:54:03 +000059void ActionProcessor::ActionComplete(AbstractAction* actionptr,
David Zeuthena99981f2013-04-29 13:42:47 -070060 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 CHECK_EQ(actionptr, current_action_);
62 if (delegate_)
Darin Petkovc1a8b422010-07-19 11:34:49 -070063 delegate_->ActionCompleted(this, actionptr, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 string old_type = current_action_->Type();
David Zeuthen33bae492014-02-25 16:16:18 -080065 current_action_->ActionCompleted(code);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070066 current_action_->SetProcessor(nullptr);
67 current_action_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 if (actions_.empty()) {
69 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
70 " type " << old_type;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070071 } else if (code != ErrorCode::kSuccess) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000072 LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type
73 << " action failed. Aborting processing.";
74 actions_.clear();
75 }
76 if (actions_.empty()) {
77 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
78 " type " << old_type;
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 if (delegate_) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070080 delegate_->ProcessingDone(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 }
82 return;
83 }
84 current_action_ = actions_.front();
85 actions_.pop_front();
86 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
87 << ", starting " << current_action_->Type();
88 current_action_->PerformAction();
89}
90
91} // namespace chromeos_update_engine