blob: 0d8c046a9105e89c0432f9ff28c9baf5ae8bece7 [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,
Darin Petkovc1a8b422010-07-19 11:34:49 -070057 ActionExitCode 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();
62 current_action_->SetProcessor(NULL);
63 current_action_ = NULL;
64 if (actions_.empty()) {
65 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
66 " type " << old_type;
Darin Petkovc1a8b422010-07-19 11:34:49 -070067 } else if (code != kActionCodeSuccess) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000068 LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type
69 << " action failed. Aborting processing.";
70 actions_.clear();
71 }
72 if (actions_.empty()) {
73 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
74 " type " << old_type;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 if (delegate_) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070076 delegate_->ProcessingDone(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000077 }
78 return;
79 }
80 current_action_ = actions_.front();
81 actions_.pop_front();
82 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
83 << ", starting " << current_action_->Type();
84 current_action_->PerformAction();
85}
86
87} // namespace chromeos_update_engine