1*5a923131SAndroid Build Coastguard Worker // 2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2011 The Android Open Source Project 3*5a923131SAndroid Build Coastguard Worker // 4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at 7*5a923131SAndroid Build Coastguard Worker // 8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0 9*5a923131SAndroid Build Coastguard Worker // 10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 14*5a923131SAndroid Build Coastguard Worker // limitations under the License. 15*5a923131SAndroid Build Coastguard Worker // 16*5a923131SAndroid Build Coastguard Worker 17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_ 18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_ 19*5a923131SAndroid Build Coastguard Worker 20*5a923131SAndroid Build Coastguard Worker #include <deque> 21*5a923131SAndroid Build Coastguard Worker #include <memory> 22*5a923131SAndroid Build Coastguard Worker #include <vector> 23*5a923131SAndroid Build Coastguard Worker 24*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h> 25*5a923131SAndroid Build Coastguard Worker 26*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/error_code.h" 27*5a923131SAndroid Build Coastguard Worker 28*5a923131SAndroid Build Coastguard Worker #include <gtest/gtest_prod.h> 29*5a923131SAndroid Build Coastguard Worker 30*5a923131SAndroid Build Coastguard Worker // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.) 31*5a923131SAndroid Build Coastguard Worker // is based on the KSAction* classes from the Google Update Engine code at 32*5a923131SAndroid Build Coastguard Worker // http://code.google.com/p/update-engine/ . The author of this file sends 33*5a923131SAndroid Build Coastguard Worker // a big thanks to that team for their high quality design, implementation, 34*5a923131SAndroid Build Coastguard Worker // and documentation. 35*5a923131SAndroid Build Coastguard Worker 36*5a923131SAndroid Build Coastguard Worker // See action.h for an overview of this class and other Action* classes. 37*5a923131SAndroid Build Coastguard Worker 38*5a923131SAndroid Build Coastguard Worker // An ActionProcessor keeps a queue of Actions and processes them in order. 39*5a923131SAndroid Build Coastguard Worker 40*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine { 41*5a923131SAndroid Build Coastguard Worker 42*5a923131SAndroid Build Coastguard Worker class AbstractAction; 43*5a923131SAndroid Build Coastguard Worker class ActionProcessorDelegate; 44*5a923131SAndroid Build Coastguard Worker 45*5a923131SAndroid Build Coastguard Worker class ActionProcessor { 46*5a923131SAndroid Build Coastguard Worker public: 47*5a923131SAndroid Build Coastguard Worker ActionProcessor() = default; 48*5a923131SAndroid Build Coastguard Worker 49*5a923131SAndroid Build Coastguard Worker virtual ~ActionProcessor(); 50*5a923131SAndroid Build Coastguard Worker 51*5a923131SAndroid Build Coastguard Worker // Starts processing the first Action in the queue. If there's a delegate, 52*5a923131SAndroid Build Coastguard Worker // when all processing is complete, ProcessingDone() will be called on the 53*5a923131SAndroid Build Coastguard Worker // delegate. 54*5a923131SAndroid Build Coastguard Worker virtual void StartProcessing(); 55*5a923131SAndroid Build Coastguard Worker 56*5a923131SAndroid Build Coastguard Worker // Aborts processing. If an Action is running, it will have 57*5a923131SAndroid Build Coastguard Worker // TerminateProcessing() called on it. The Action that was running and all the 58*5a923131SAndroid Build Coastguard Worker // remaining actions will be lost and must be re-enqueued if this Processor is 59*5a923131SAndroid Build Coastguard Worker // to use it. 60*5a923131SAndroid Build Coastguard Worker void StopProcessing(); 61*5a923131SAndroid Build Coastguard Worker 62*5a923131SAndroid Build Coastguard Worker // Suspend the processing. If an Action is running, it will have the 63*5a923131SAndroid Build Coastguard Worker // SuspendProcessing() called on it, and it should suspend operations until 64*5a923131SAndroid Build Coastguard Worker // ResumeProcessing() is called on this class to continue. While suspended, 65*5a923131SAndroid Build Coastguard Worker // no new actions will be started. Calling SuspendProcessing while the 66*5a923131SAndroid Build Coastguard Worker // processing is suspended or not running this method performs no action. 67*5a923131SAndroid Build Coastguard Worker void SuspendProcessing(); 68*5a923131SAndroid Build Coastguard Worker 69*5a923131SAndroid Build Coastguard Worker // Resume the suspended processing. If the ActionProcessor is not suspended 70*5a923131SAndroid Build Coastguard Worker // or not running in the first place this method performs no action. 71*5a923131SAndroid Build Coastguard Worker void ResumeProcessing(); 72*5a923131SAndroid Build Coastguard Worker 73*5a923131SAndroid Build Coastguard Worker // Returns true iff the processing was started but not yet completed nor 74*5a923131SAndroid Build Coastguard Worker // stopped. 75*5a923131SAndroid Build Coastguard Worker bool IsRunning() const; 76*5a923131SAndroid Build Coastguard Worker 77*5a923131SAndroid Build Coastguard Worker // Adds another Action to the end of the queue. 78*5a923131SAndroid Build Coastguard Worker virtual void EnqueueAction(std::unique_ptr<AbstractAction> action); 79*5a923131SAndroid Build Coastguard Worker 80*5a923131SAndroid Build Coastguard Worker // Sets/gets the current delegate. Set to null to remove a delegate. delegate()81*5a923131SAndroid Build Coastguard Worker ActionProcessorDelegate* delegate() const { return delegate_; } set_delegate(ActionProcessorDelegate * delegate)82*5a923131SAndroid Build Coastguard Worker void set_delegate(ActionProcessorDelegate* delegate) { delegate_ = delegate; } 83*5a923131SAndroid Build Coastguard Worker 84*5a923131SAndroid Build Coastguard Worker // Returns a pointer to the current Action that's processing. current_action()85*5a923131SAndroid Build Coastguard Worker AbstractAction* current_action() const { return current_action_.get(); } 86*5a923131SAndroid Build Coastguard Worker 87*5a923131SAndroid Build Coastguard Worker // Called by an action to notify processor that it's done. Caller passes self. 88*5a923131SAndroid Build Coastguard Worker // But this call deletes the action if there no other object has a reference 89*5a923131SAndroid Build Coastguard Worker // to it, so in that case, the caller should not try to access any of its 90*5a923131SAndroid Build Coastguard Worker // member variables after this call. 91*5a923131SAndroid Build Coastguard Worker virtual void ActionComplete(AbstractAction* actionptr, ErrorCode code); 92*5a923131SAndroid Build Coastguard Worker 93*5a923131SAndroid Build Coastguard Worker private: 94*5a923131SAndroid Build Coastguard Worker FRIEND_TEST(ActionProcessorTest, ChainActionsTest); 95*5a923131SAndroid Build Coastguard Worker 96*5a923131SAndroid Build Coastguard Worker // Continue processing actions (if any) after the last action terminated with 97*5a923131SAndroid Build Coastguard Worker // the passed error code. If there are no more actions to process, the 98*5a923131SAndroid Build Coastguard Worker // processing will terminate. 99*5a923131SAndroid Build Coastguard Worker void StartNextActionOrFinish(ErrorCode code); 100*5a923131SAndroid Build Coastguard Worker 101*5a923131SAndroid Build Coastguard Worker // Actions that have not yet begun processing, in the order in which 102*5a923131SAndroid Build Coastguard Worker // they'll be processed. 103*5a923131SAndroid Build Coastguard Worker std::deque<std::unique_ptr<AbstractAction>> actions_; 104*5a923131SAndroid Build Coastguard Worker 105*5a923131SAndroid Build Coastguard Worker // A pointer to the currently processing Action, if any. 106*5a923131SAndroid Build Coastguard Worker std::unique_ptr<AbstractAction> current_action_; 107*5a923131SAndroid Build Coastguard Worker 108*5a923131SAndroid Build Coastguard Worker // The ErrorCode reported by an action that was suspended but finished while 109*5a923131SAndroid Build Coastguard Worker // being suspended. This error code is stored here to be reported back to the 110*5a923131SAndroid Build Coastguard Worker // delegate once the processor is resumed. 111*5a923131SAndroid Build Coastguard Worker ErrorCode suspended_error_code_{ErrorCode::kSuccess}; 112*5a923131SAndroid Build Coastguard Worker 113*5a923131SAndroid Build Coastguard Worker // Whether the action processor is or should be suspended. 114*5a923131SAndroid Build Coastguard Worker bool suspended_{false}; 115*5a923131SAndroid Build Coastguard Worker 116*5a923131SAndroid Build Coastguard Worker // A pointer to the delegate, or null if none. 117*5a923131SAndroid Build Coastguard Worker ActionProcessorDelegate* delegate_{nullptr}; 118*5a923131SAndroid Build Coastguard Worker 119*5a923131SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ActionProcessor); 120*5a923131SAndroid Build Coastguard Worker }; 121*5a923131SAndroid Build Coastguard Worker 122*5a923131SAndroid Build Coastguard Worker // A delegate object can be used to be notified of events that happen 123*5a923131SAndroid Build Coastguard Worker // in an ActionProcessor. An instance of this class can be passed to an 124*5a923131SAndroid Build Coastguard Worker // ActionProcessor to register itself. 125*5a923131SAndroid Build Coastguard Worker class ActionProcessorDelegate { 126*5a923131SAndroid Build Coastguard Worker public: 127*5a923131SAndroid Build Coastguard Worker virtual ~ActionProcessorDelegate() = default; 128*5a923131SAndroid Build Coastguard Worker 129*5a923131SAndroid Build Coastguard Worker // Called when all processing in an ActionProcessor has completed. A pointer 130*5a923131SAndroid Build Coastguard Worker // to the ActionProcessor is passed. |code| is set to the exit code of the 131*5a923131SAndroid Build Coastguard Worker // last completed action. ProcessingDone(const ActionProcessor * processor,ErrorCode code)132*5a923131SAndroid Build Coastguard Worker virtual void ProcessingDone([[maybe_unused]] const ActionProcessor* processor, 133*5a923131SAndroid Build Coastguard Worker [[maybe_unused]] ErrorCode code) {} 134*5a923131SAndroid Build Coastguard Worker 135*5a923131SAndroid Build Coastguard Worker // Called when processing has stopped. Does not mean that all Actions have 136*5a923131SAndroid Build Coastguard Worker // completed. If/when all Actions complete, ProcessingDone() will be called. ProcessingStopped(const ActionProcessor * processor)137*5a923131SAndroid Build Coastguard Worker virtual void ProcessingStopped( 138*5a923131SAndroid Build Coastguard Worker [[maybe_unused]] const ActionProcessor* processor) {} 139*5a923131SAndroid Build Coastguard Worker 140*5a923131SAndroid Build Coastguard Worker // Called whenever an action has finished processing, either successfully 141*5a923131SAndroid Build Coastguard Worker // or otherwise. ActionCompleted(ActionProcessor * processor,AbstractAction * action,ErrorCode code)142*5a923131SAndroid Build Coastguard Worker virtual void ActionCompleted([[maybe_unused]] ActionProcessor* processor, 143*5a923131SAndroid Build Coastguard Worker [[maybe_unused]] AbstractAction* action, 144*5a923131SAndroid Build Coastguard Worker [[maybe_unused]] ErrorCode code) {} 145*5a923131SAndroid Build Coastguard Worker }; 146*5a923131SAndroid Build Coastguard Worker 147*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine 148*5a923131SAndroid Build Coastguard Worker 149*5a923131SAndroid Build Coastguard Worker #endif // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_ 150