| 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" | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 6 | #include <string> | 
| Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 7 | #include "base/logging.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include "update_engine/action.h" | 
|  | 9 |  | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 10 | using std::string; | 
|  | 11 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | namespace chromeos_update_engine { | 
|  | 13 |  | 
|  | 14 | ActionProcessor::ActionProcessor() | 
|  | 15 | : current_action_(NULL), delegate_(NULL) {} | 
|  | 16 |  | 
|  | 17 | ActionProcessor::~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 |  | 
|  | 27 | void ActionProcessor::EnqueueAction(AbstractAction* action) { | 
|  | 28 | actions_.push_back(action); | 
|  | 29 | action->SetProcessor(this); | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | void 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 |  | 
|  | 43 | void 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 56 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 57 | ErrorCode code) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | CHECK_EQ(actionptr, current_action_); | 
|  | 59 | if (delegate_) | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 60 | delegate_->ActionCompleted(this, actionptr, code); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | 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; | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 67 | } else if (code != kErrorCodeSuccess) { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 68 | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | if (delegate_) { | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 76 | delegate_->ProcessingDone(this, code); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 77 | } | 
|  | 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 |