Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/action_processor.h" |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 18 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 19 | #include <string> |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 20 | |
| 21 | #include <base/logging.h> |
| 22 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 23 | #include "update_engine/common/action.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 25 | using std::string; |
| 26 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | ActionProcessor::ActionProcessor() |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 30 | : current_action_(nullptr), delegate_(nullptr) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | |
| 32 | ActionProcessor::~ActionProcessor() { |
| 33 | if (IsRunning()) { |
| 34 | StopProcessing(); |
| 35 | } |
| 36 | for (std::deque<AbstractAction*>::iterator it = actions_.begin(); |
| 37 | it != actions_.end(); ++it) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 38 | (*it)->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | void ActionProcessor::EnqueueAction(AbstractAction* action) { |
| 43 | actions_.push_back(action); |
| 44 | action->SetProcessor(this); |
| 45 | } |
| 46 | |
| 47 | void ActionProcessor::StartProcessing() { |
| 48 | CHECK(!IsRunning()); |
| 49 | if (!actions_.empty()) { |
| 50 | current_action_ = actions_.front(); |
| 51 | LOG(INFO) << "ActionProcessor::StartProcessing: " |
| 52 | << current_action_->Type(); |
| 53 | actions_.pop_front(); |
| 54 | current_action_->PerformAction(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void ActionProcessor::StopProcessing() { |
| 59 | CHECK(IsRunning()); |
| 60 | CHECK(current_action_); |
| 61 | current_action_->TerminateProcessing(); |
| 62 | CHECK(current_action_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 63 | current_action_->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | LOG(INFO) << "ActionProcessor::StopProcessing: aborted " |
| 65 | << current_action_->Type(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 66 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | if (delegate_) |
| 68 | delegate_->ProcessingStopped(this); |
| 69 | } |
| 70 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 71 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 72 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | CHECK_EQ(actionptr, current_action_); |
| 74 | if (delegate_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 75 | delegate_->ActionCompleted(this, actionptr, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | string old_type = current_action_->Type(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 77 | current_action_->ActionCompleted(code); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 78 | current_action_->SetProcessor(nullptr); |
| 79 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 80 | if (actions_.empty()) { |
| 81 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 82 | " type " << old_type; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 83 | } else if (code != ErrorCode::kSuccess) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 84 | LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type |
| 85 | << " action failed. Aborting processing."; |
| 86 | actions_.clear(); |
| 87 | } |
| 88 | if (actions_.empty()) { |
| 89 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 90 | " type " << old_type; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 91 | if (delegate_) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 92 | delegate_->ProcessingDone(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | } |
| 94 | return; |
| 95 | } |
| 96 | current_action_ = actions_.front(); |
| 97 | actions_.pop_front(); |
| 98 | LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type |
| 99 | << ", starting " << current_action_->Type(); |
| 100 | current_action_->PerformAction(); |
| 101 | } |
| 102 | |
| 103 | } // namespace chromeos_update_engine |