Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2011 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 | #ifndef UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_ |
| 18 | #define UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
| 20 | #include <deque> |
| 21 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 22 | #include <base/macros.h> |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame^] | 23 | #include <brillo/errors/error.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/error_code.h" |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 26 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.) |
| 28 | // is based on the KSAction* classes from the Google Update Engine code at |
| 29 | // http://code.google.com/p/update-engine/ . The author of this file sends |
| 30 | // a big thanks to that team for their high quality design, implementation, |
| 31 | // and documentation. |
| 32 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 33 | // See action.h for an overview of this class and other Action* classes. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | |
| 35 | // An ActionProcessor keeps a queue of Actions and processes them in order. |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | class AbstractAction; |
| 40 | class ActionProcessorDelegate; |
| 41 | |
| 42 | class ActionProcessor { |
| 43 | public: |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame^] | 44 | ActionProcessor() = default; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 45 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 46 | virtual ~ActionProcessor(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | |
| 48 | // Starts processing the first Action in the queue. If there's a delegate, |
| 49 | // when all processing is complete, ProcessingDone() will be called on the |
| 50 | // delegate. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 51 | virtual void StartProcessing(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | |
| 53 | // Aborts processing. If an Action is running, it will have |
| 54 | // TerminateProcessing() called on it. The Action that was running |
| 55 | // will be lost and must be re-enqueued if this Processor is to use it. |
| 56 | void StopProcessing(); |
| 57 | |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame^] | 58 | // Suspend the processing. If an Action is running, it will have the |
| 59 | // SuspendProcessing() called on it, and it should suspend operations until |
| 60 | // ResumeProcessing() is called on this class to continue. While suspended, |
| 61 | // no new actions will be started. Calling SuspendProcessing while the |
| 62 | // processing is suspended or not running this method performs no action. |
| 63 | void SuspendProcessing(); |
| 64 | |
| 65 | // Resume the suspended processing. If the ActionProcessor is not suspended |
| 66 | // or not running on the first place this method performs no action. |
| 67 | void ResumeProcessing(); |
| 68 | |
| 69 | // Returns true iff the processing was started but not yet completed nor |
| 70 | // stopped. |
| 71 | bool IsRunning() const { return current_action_ != nullptr || suspended_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 | |
| 73 | // Adds another Action to the end of the queue. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 74 | virtual void EnqueueAction(AbstractAction* action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 76 | // Sets/gets the current delegate. Set to null to remove a delegate. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 77 | ActionProcessorDelegate* delegate() const { return delegate_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | void set_delegate(ActionProcessorDelegate *delegate) { |
| 79 | delegate_ = delegate; |
| 80 | } |
| 81 | |
| 82 | // Returns a pointer to the current Action that's processing. |
| 83 | AbstractAction* current_action() const { |
| 84 | return current_action_; |
| 85 | } |
| 86 | |
| 87 | // Called by an action to notify processor that it's done. Caller passes self. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 88 | void ActionComplete(AbstractAction* actionptr, ErrorCode code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 89 | |
| 90 | private: |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame^] | 91 | // Continue processing actions (if any) after the last action terminated with |
| 92 | // the passed error code. If there are no more actions to process, the |
| 93 | // processing will terminate. |
| 94 | void StartNextActionOrFinish(ErrorCode code); |
| 95 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | // Actions that have not yet begun processing, in the order in which |
| 97 | // they'll be processed. |
| 98 | std::deque<AbstractAction*> actions_; |
| 99 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 100 | // A pointer to the currently processing Action, if any. |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame^] | 101 | AbstractAction* current_action_{nullptr}; |
| 102 | |
| 103 | // The ErrorCode reported by an action that was suspended but finished while |
| 104 | // being suspended. This error code is stored here to be reported back to the |
| 105 | // delegate once the processor is resumed. |
| 106 | ErrorCode suspended_error_code_{ErrorCode::kSuccess}; |
| 107 | |
| 108 | // Whether the action processor is or should be suspended. |
| 109 | bool suspended_{false}; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 111 | // A pointer to the delegate, or null if none. |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame^] | 112 | ActionProcessorDelegate* delegate_{nullptr}; |
| 113 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | DISALLOW_COPY_AND_ASSIGN(ActionProcessor); |
| 115 | }; |
| 116 | |
| 117 | // A delegate object can be used to be notified of events that happen |
| 118 | // in an ActionProcessor. An instance of this class can be passed to an |
| 119 | // ActionProcessor to register itself. |
| 120 | class ActionProcessorDelegate { |
| 121 | public: |
Alex Deymo | e894870 | 2014-11-11 21:44:45 -0800 | [diff] [blame] | 122 | virtual ~ActionProcessorDelegate() = default; |
| 123 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 124 | // Called when all processing in an ActionProcessor has completed. A pointer |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 125 | // to the ActionProcessor is passed. |code| is set to the exit code of the |
| 126 | // last completed action. |
| 127 | virtual void ProcessingDone(const ActionProcessor* processor, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 128 | ErrorCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 129 | |
| 130 | // Called when processing has stopped. Does not mean that all Actions have |
| 131 | // completed. If/when all Actions complete, ProcessingDone() will be called. |
| 132 | virtual void ProcessingStopped(const ActionProcessor* processor) {} |
| 133 | |
| 134 | // Called whenever an action has finished processing, either successfully |
| 135 | // or otherwise. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 136 | virtual void ActionCompleted(ActionProcessor* processor, |
| 137 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 138 | ErrorCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // namespace chromeos_update_engine |
| 142 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 143 | #endif // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_ |