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() |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 15 | : current_action_(nullptr), delegate_(nullptr) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
| 17 | ActionProcessor::~ActionProcessor() { |
| 18 | if (IsRunning()) { |
| 19 | StopProcessing(); |
| 20 | } |
| 21 | for (std::deque<AbstractAction*>::iterator it = actions_.begin(); |
| 22 | it != actions_.end(); ++it) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 23 | (*it)->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 48 | current_action_->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 | LOG(INFO) << "ActionProcessor::StopProcessing: aborted " |
| 50 | << current_action_->Type(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 51 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 62 | current_action_->ActionCompleted(code); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 63 | current_action_->SetProcessor(nullptr); |
| 64 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | if (actions_.empty()) { |
| 66 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 67 | " type " << old_type; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 68 | } else if (code != ErrorCode::kSuccess) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 69 | LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type |
| 70 | << " action failed. Aborting processing."; |
| 71 | actions_.clear(); |
| 72 | } |
| 73 | if (actions_.empty()) { |
| 74 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 75 | " type " << old_type; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | if (delegate_) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 77 | delegate_->ProcessingDone(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | } |
| 79 | return; |
| 80 | } |
| 81 | current_action_ = actions_.front(); |
| 82 | actions_.pop_front(); |
| 83 | LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type |
| 84 | << ", starting " << current_action_->Type(); |
| 85 | current_action_->PerformAction(); |
| 86 | } |
| 87 | |
| 88 | } // namespace chromeos_update_engine |