rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 6 | #include "chromeos/obsolete_logging.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | #include "update_engine/action.h" |
| 8 | |
| 9 | namespace chromeos_update_engine { |
| 10 | |
| 11 | ActionProcessor::ActionProcessor() |
| 12 | : current_action_(NULL), delegate_(NULL) {} |
| 13 | |
| 14 | ActionProcessor::~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 | |
| 24 | void ActionProcessor::EnqueueAction(AbstractAction* action) { |
| 25 | actions_.push_back(action); |
| 26 | action->SetProcessor(this); |
| 27 | } |
| 28 | |
| 29 | void 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 | |
| 40 | void 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 53 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 64 | } 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 | 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 |