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" |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 6 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 7 | #include <string> |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 8 | |
| 9 | #include <base/logging.h> |
| 10 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 11 | #include "update_engine/action.h" |
| 12 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 13 | using std::string; |
| 14 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | namespace chromeos_update_engine { |
| 16 | |
| 17 | ActionProcessor::ActionProcessor() |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 18 | : current_action_(nullptr), delegate_(nullptr) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
| 20 | ActionProcessor::~ActionProcessor() { |
| 21 | if (IsRunning()) { |
| 22 | StopProcessing(); |
| 23 | } |
| 24 | for (std::deque<AbstractAction*>::iterator it = actions_.begin(); |
| 25 | it != actions_.end(); ++it) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 26 | (*it)->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
| 30 | void ActionProcessor::EnqueueAction(AbstractAction* action) { |
| 31 | actions_.push_back(action); |
| 32 | action->SetProcessor(this); |
| 33 | } |
| 34 | |
| 35 | void ActionProcessor::StartProcessing() { |
| 36 | CHECK(!IsRunning()); |
| 37 | if (!actions_.empty()) { |
| 38 | current_action_ = actions_.front(); |
| 39 | LOG(INFO) << "ActionProcessor::StartProcessing: " |
| 40 | << current_action_->Type(); |
| 41 | actions_.pop_front(); |
| 42 | current_action_->PerformAction(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void ActionProcessor::StopProcessing() { |
| 47 | CHECK(IsRunning()); |
| 48 | CHECK(current_action_); |
| 49 | current_action_->TerminateProcessing(); |
| 50 | CHECK(current_action_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 51 | current_action_->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | LOG(INFO) << "ActionProcessor::StopProcessing: aborted " |
| 53 | << current_action_->Type(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 54 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | if (delegate_) |
| 56 | delegate_->ProcessingStopped(this); |
| 57 | } |
| 58 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 59 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 60 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | CHECK_EQ(actionptr, current_action_); |
| 62 | if (delegate_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 63 | delegate_->ActionCompleted(this, actionptr, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | string old_type = current_action_->Type(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 65 | current_action_->ActionCompleted(code); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 66 | current_action_->SetProcessor(nullptr); |
| 67 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | if (actions_.empty()) { |
| 69 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 70 | " type " << old_type; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 71 | } else if (code != ErrorCode::kSuccess) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 72 | LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type |
| 73 | << " action failed. Aborting processing."; |
| 74 | actions_.clear(); |
| 75 | } |
| 76 | if (actions_.empty()) { |
| 77 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 78 | " type " << old_type; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | if (delegate_) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 80 | delegate_->ProcessingDone(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | } |
| 82 | return; |
| 83 | } |
| 84 | current_action_ = actions_.front(); |
| 85 | actions_.pop_front(); |
| 86 | LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type |
| 87 | << ", starting " << current_action_->Type(); |
| 88 | current_action_->PerformAction(); |
| 89 | } |
| 90 | |
| 91 | } // namespace chromeos_update_engine |