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