| 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> | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 20 | #include <utility> | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 21 |  | 
 | 22 | #include <base/logging.h> | 
 | 23 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 24 | #include "update_engine/common/action.h" | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 25 | #include "update_engine/common/error_code_utils.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 |  | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 27 | using std::string; | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 28 | using std::unique_ptr; | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 29 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | namespace chromeos_update_engine { | 
 | 31 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | } | 
 | 36 |  | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 37 | void ActionProcessor::EnqueueAction(unique_ptr<AbstractAction> action) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 |   action->SetProcessor(this); | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 39 |   actions_.push_back(std::move(action)); | 
 | 40 | } | 
 | 41 |  | 
 | 42 | bool ActionProcessor::IsRunning() const { | 
 | 43 |   return current_action_ != nullptr || suspended_; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | } | 
 | 45 |  | 
 | 46 | void ActionProcessor::StartProcessing() { | 
 | 47 |   CHECK(!IsRunning()); | 
 | 48 |   if (!actions_.empty()) { | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 49 |     current_action_ = std::move(actions_.front()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 |     actions_.pop_front(); | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 51 |     LOG(INFO) << "ActionProcessor: starting " << current_action_->Type(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 |     current_action_->PerformAction(); | 
 | 53 |   } | 
 | 54 | } | 
 | 55 |  | 
 | 56 | void ActionProcessor::StopProcessing() { | 
 | 57 |   CHECK(IsRunning()); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 58 |   if (current_action_) { | 
 | 59 |     current_action_->TerminateProcessing(); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 60 |   } | 
 | 61 |   LOG(INFO) << "ActionProcessor: aborted " | 
 | 62 |             << (current_action_ ? current_action_->Type() : "") | 
 | 63 |             << (suspended_ ? " while suspended" : ""); | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 64 |   current_action_.reset(); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 65 |   suspended_ = false; | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 66 |   // Delete all the actions before calling the delegate. | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 67 |   actions_.clear(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 |   if (delegate_) | 
 | 69 |     delegate_->ProcessingStopped(this); | 
 | 70 | } | 
 | 71 |  | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 72 | void ActionProcessor::SuspendProcessing() { | 
 | 73 |   // No current_action_ when not suspended means that the action processor was | 
 | 74 |   // never started or already finished. | 
 | 75 |   if (suspended_ || !current_action_) { | 
 | 76 |     LOG(WARNING) << "Called SuspendProcessing while not processing."; | 
 | 77 |     return; | 
 | 78 |   } | 
 | 79 |   suspended_ = true; | 
 | 80 |  | 
 | 81 |   // If there's a current action we should notify it that it should suspend, but | 
 | 82 |   // the action can ignore that and terminate at any point. | 
 | 83 |   LOG(INFO) << "ActionProcessor: suspending " << current_action_->Type(); | 
 | 84 |   current_action_->SuspendAction(); | 
 | 85 | } | 
 | 86 |  | 
 | 87 | void ActionProcessor::ResumeProcessing() { | 
 | 88 |   if (!suspended_) { | 
 | 89 |     LOG(WARNING) << "Called ResumeProcessing while not suspended."; | 
 | 90 |     return; | 
 | 91 |   } | 
 | 92 |   suspended_ = false; | 
 | 93 |   if (current_action_) { | 
 | 94 |     // The current_action_ did not call ActionComplete while suspended, so we | 
 | 95 |     // should notify it of the resume operation. | 
 | 96 |     LOG(INFO) << "ActionProcessor: resuming " << current_action_->Type(); | 
 | 97 |     current_action_->ResumeAction(); | 
 | 98 |   } else { | 
 | 99 |     // The last action called ActionComplete while suspended, so there is | 
 | 100 |     // already a log message with the type of the finished action. We simply | 
 | 101 |     // state that we are resuming processing and the next function will log the | 
 | 102 |     // start of the next action or processing completion. | 
 | 103 |     LOG(INFO) << "ActionProcessor: resuming processing"; | 
 | 104 |     StartNextActionOrFinish(suspended_error_code_); | 
 | 105 |   } | 
 | 106 | } | 
 | 107 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 108 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 109 |                                      ErrorCode code) { | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 110 |   CHECK_EQ(actionptr, current_action_.get()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 |   if (delegate_) | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 112 |     delegate_->ActionCompleted(this, actionptr, code); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 113 |   string old_type = current_action_->Type(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 114 |   current_action_->ActionCompleted(code); | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 115 |   current_action_.reset(); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 116 |   LOG(INFO) << "ActionProcessor: finished " | 
 | 117 |             << (actions_.empty() ? "last action " : "") << old_type | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 118 |             << (suspended_ ? " while suspended" : "") << " with code " | 
 | 119 |             << utils::ErrorCodeToString(code); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 120 |   if (!actions_.empty() && code != ErrorCode::kSuccess) { | 
 | 121 |     LOG(INFO) << "ActionProcessor: Aborting processing due to failure."; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 122 |     actions_.clear(); | 
 | 123 |   } | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 124 |   if (suspended_) { | 
 | 125 |     // If an action finished while suspended we don't start the next action (or | 
 | 126 |     // terminate the processing) until the processor is resumed. This condition | 
 | 127 |     // will be flagged by a nullptr current_action_ while suspended_ is true. | 
 | 128 |     suspended_error_code_ = code; | 
 | 129 |     return; | 
 | 130 |   } | 
 | 131 |   StartNextActionOrFinish(code); | 
 | 132 | } | 
 | 133 |  | 
 | 134 | void ActionProcessor::StartNextActionOrFinish(ErrorCode code) { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 135 |   if (actions_.empty()) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 136 |     if (delegate_) { | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 137 |       delegate_->ProcessingDone(this, code); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 138 |     } | 
 | 139 |     return; | 
 | 140 |   } | 
| Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 141 |   current_action_ = std::move(actions_.front()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 142 |   actions_.pop_front(); | 
| Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 143 |   LOG(INFO) << "ActionProcessor: starting " << current_action_->Type(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 144 |   current_action_->PerformAction(); | 
 | 145 | } | 
 | 146 |  | 
 | 147 | }  // namespace chromeos_update_engine |