blob: 623e3d25f766962d857c95fe8fdb8a4994a84bdd [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"
6#include "update_engine/action.h"
7
8namespace chromeos_update_engine {
9
10ActionProcessor::ActionProcessor()
11 : current_action_(NULL), delegate_(NULL) {}
12
13ActionProcessor::~ActionProcessor() {
14 if (IsRunning()) {
15 StopProcessing();
16 }
17 for (std::deque<AbstractAction*>::iterator it = actions_.begin();
18 it != actions_.end(); ++it) {
19 (*it)->SetProcessor(NULL);
20 }
21}
22
23void ActionProcessor::EnqueueAction(AbstractAction* action) {
24 actions_.push_back(action);
25 action->SetProcessor(this);
26}
27
28void ActionProcessor::StartProcessing() {
29 CHECK(!IsRunning());
30 if (!actions_.empty()) {
31 current_action_ = actions_.front();
32 LOG(INFO) << "ActionProcessor::StartProcessing: "
33 << current_action_->Type();
34 actions_.pop_front();
35 current_action_->PerformAction();
36 }
37}
38
39void ActionProcessor::StopProcessing() {
40 CHECK(IsRunning());
41 CHECK(current_action_);
42 current_action_->TerminateProcessing();
43 CHECK(current_action_);
44 current_action_->SetProcessor(NULL);
45 LOG(INFO) << "ActionProcessor::StopProcessing: aborted "
46 << current_action_->Type();
47 current_action_ = NULL;
48 if (delegate_)
49 delegate_->ProcessingStopped(this);
50}
51
52void ActionProcessor::ActionComplete(const AbstractAction* actionptr,
53 bool success) {
54 CHECK_EQ(actionptr, current_action_);
55 if (delegate_)
56 delegate_->ActionCompleted(this, actionptr, success);
57 string old_type = current_action_->Type();
58 current_action_->SetProcessor(NULL);
59 current_action_ = NULL;
60 if (actions_.empty()) {
61 LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of"
62 " type " << old_type;
63 if (delegate_) {
64 delegate_->ProcessingDone(this);
65 }
66 return;
67 }
68 current_action_ = actions_.front();
69 actions_.pop_front();
70 LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type
71 << ", starting " << current_action_->Type();
72 current_action_->PerformAction();
73}
74
75} // namespace chromeos_update_engine