blob: 21a88f1d175ac5368c18bb9cb28a1f804b7c768b [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"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00006#include "chromeos/obsolete_logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +00007#include "update_engine/action.h"
8
9namespace chromeos_update_engine {
10
11ActionProcessor::ActionProcessor()
12 : current_action_(NULL), delegate_(NULL) {}
13
14ActionProcessor::~ActionProcessor() {
15 if (IsRunning()) {
16 StopProcessing();
17 }
18 for (std::deque<AbstractAction*>::iterator it = actions_.begin();
19 it != actions_.end(); ++it) {
20 (*it)->SetProcessor(NULL);
21 }
22}
23
24void ActionProcessor::EnqueueAction(AbstractAction* action) {
25 actions_.push_back(action);
26 action->SetProcessor(this);
27}
28
29void ActionProcessor::StartProcessing() {
30 CHECK(!IsRunning());
31 if (!actions_.empty()) {
32 current_action_ = actions_.front();
33 LOG(INFO) << "ActionProcessor::StartProcessing: "
34 << current_action_->Type();
35 actions_.pop_front();
36 current_action_->PerformAction();
37 }
38}
39
40void ActionProcessor::StopProcessing() {
41 CHECK(IsRunning());
42 CHECK(current_action_);
43 current_action_->TerminateProcessing();
44 CHECK(current_action_);
45 current_action_->SetProcessor(NULL);
46 LOG(INFO) << "ActionProcessor::StopProcessing: aborted "
47 << current_action_->Type();
48 current_action_ = NULL;
49 if (delegate_)
50 delegate_->ProcessingStopped(this);
51}
52
adlr@google.comc98a7ed2009-12-04 18:54:03 +000053void ActionProcessor::ActionComplete(AbstractAction* actionptr,
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 bool success) {
55 CHECK_EQ(actionptr, current_action_);
56 if (delegate_)
57 delegate_->ActionCompleted(this, actionptr, success);
58 string old_type = current_action_->Type();
59 current_action_->SetProcessor(NULL);
60 current_action_ = NULL;
61 if (actions_.empty()) {
62 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
63 " type " << old_type;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000064 } else if (!success) {
65 LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type
66 << " action failed. Aborting processing.";
67 actions_.clear();
68 }
69 if (actions_.empty()) {
70 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
71 " type " << old_type;
rspangler@google.com49fdf182009-10-10 00:57:34 +000072 if (delegate_) {
73 delegate_->ProcessingDone(this);
74 }
75 return;
76 }
77 current_action_ = actions_.front();
78 actions_.pop_front();
79 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
80 << ", starting " << current_action_->Type();
81 current_action_->PerformAction();
82}
83
84} // namespace chromeos_update_engine