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() { |
Alex Deymo | 2b4268c | 2015-12-04 13:56:25 -0800 | [diff] [blame^] | 33 | if (IsRunning()) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | StopProcessing(); |
Alex Deymo | 2b4268c | 2015-12-04 13:56:25 -0800 | [diff] [blame^] | 35 | for (auto action : actions_) |
| 36 | action->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void ActionProcessor::EnqueueAction(AbstractAction* action) { |
| 40 | actions_.push_back(action); |
| 41 | action->SetProcessor(this); |
| 42 | } |
| 43 | |
| 44 | void ActionProcessor::StartProcessing() { |
| 45 | CHECK(!IsRunning()); |
| 46 | if (!actions_.empty()) { |
| 47 | current_action_ = actions_.front(); |
| 48 | LOG(INFO) << "ActionProcessor::StartProcessing: " |
| 49 | << current_action_->Type(); |
| 50 | actions_.pop_front(); |
| 51 | current_action_->PerformAction(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void ActionProcessor::StopProcessing() { |
| 56 | CHECK(IsRunning()); |
| 57 | CHECK(current_action_); |
| 58 | current_action_->TerminateProcessing(); |
| 59 | CHECK(current_action_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 60 | current_action_->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | LOG(INFO) << "ActionProcessor::StopProcessing: aborted " |
| 62 | << current_action_->Type(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 63 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | if (delegate_) |
| 65 | delegate_->ProcessingStopped(this); |
| 66 | } |
| 67 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 68 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 69 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | CHECK_EQ(actionptr, current_action_); |
| 71 | if (delegate_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 72 | delegate_->ActionCompleted(this, actionptr, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | string old_type = current_action_->Type(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 74 | current_action_->ActionCompleted(code); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 75 | current_action_->SetProcessor(nullptr); |
| 76 | current_action_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 77 | if (actions_.empty()) { |
| 78 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 79 | " type " << old_type; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 80 | } else if (code != ErrorCode::kSuccess) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 81 | LOG(INFO) << "ActionProcessor::ActionComplete: " << old_type |
| 82 | << " action failed. Aborting processing."; |
| 83 | actions_.clear(); |
| 84 | } |
| 85 | if (actions_.empty()) { |
| 86 | LOG(INFO) << "ActionProcessor::ActionComplete: finished last action of" |
| 87 | " type " << old_type; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | if (delegate_) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 89 | delegate_->ProcessingDone(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | } |
| 91 | return; |
| 92 | } |
| 93 | current_action_ = actions_.front(); |
| 94 | actions_.pop_front(); |
| 95 | LOG(INFO) << "ActionProcessor::ActionComplete: finished " << old_type |
| 96 | << ", starting " << current_action_->Type(); |
| 97 | current_action_->PerformAction(); |
| 98 | } |
| 99 | |
| 100 | } // namespace chromeos_update_engine |