blob: e3862c4c3901969d11c4c5f6cdc05fe66942f1c2 [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"
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08006#include <string>
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07007#include "base/logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include "update_engine/action.h"
9
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080010using std::string;
11
rspangler@google.com49fdf182009-10-10 00:57:34 +000012namespace chromeos_update_engine {
13
14ActionProcessor::ActionProcessor()
15 : current_action_(NULL), delegate_(NULL) {}
16
17ActionProcessor::~ActionProcessor() {
18 if (IsRunning()) {
19 StopProcessing();
20 }
21 for (std::deque<AbstractAction*>::iterator it = actions_.begin();
22 it != actions_.end(); ++it) {
23 (*it)->SetProcessor(NULL);
24 }
25}
26
27void ActionProcessor::EnqueueAction(AbstractAction* action) {
28 actions_.push_back(action);
29 action->SetProcessor(this);
30}
31
32void ActionProcessor::StartProcessing() {
33 CHECK(!IsRunning());
34 if (!actions_.empty()) {
35 current_action_ = actions_.front();
36 LOG(INFO) << "ActionProcessor::StartProcessing: "
37 << current_action_->Type();
38 actions_.pop_front();
39 current_action_->PerformAction();
40 }
41}
42
43void ActionProcessor::StopProcessing() {
44 CHECK(IsRunning());
45 CHECK(current_action_);
46 current_action_->TerminateProcessing();
47 CHECK(current_action_);
48 current_action_->SetProcessor(NULL);
49 LOG(INFO) << "ActionProcessor::StopProcessing: aborted "
50 << current_action_->Type();
51 current_action_ = NULL;
52 if (delegate_)
53 delegate_->ProcessingStopped(this);
54}
55
adlr@google.comc98a7ed2009-12-04 18:54:03 +000056void ActionProcessor::ActionComplete(AbstractAction* actionptr,
David Zeuthena99981f2013-04-29 13:42:47 -070057 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000058 CHECK_EQ(actionptr, current_action_);
59 if (delegate_)
Darin Petkovc1a8b422010-07-19 11:34:49 -070060 delegate_->ActionCompleted(this, actionptr, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 string old_type = current_action_->Type();
David Zeuthen33bae492014-02-25 16:16:18 -080062 current_action_->ActionCompleted(code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 current_action_->SetProcessor(NULL);
64 current_action_ = NULL;
65 if (actions_.empty()) {
66 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
67 " type " << old_type;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070068 } else if (code != ErrorCode::kSuccess) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000069 LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type
70 << " action failed. Aborting processing.";
71 actions_.clear();
72 }
73 if (actions_.empty()) {
74 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
75 " type " << old_type;
rspangler@google.com49fdf182009-10-10 00:57:34 +000076 if (delegate_) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070077 delegate_->ProcessingDone(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000078 }
79 return;
80 }
81 current_action_ = actions_.front();
82 actions_.pop_front();
83 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
84 << ", starting " << current_action_->Type();
85 current_action_->PerformAction();
86}
87
88} // namespace chromeos_update_engine