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 //
16 
17 #ifndef UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_
18 #define UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_
19 
20 #include <deque>
21 #include <memory>
22 #include <vector>
23 
24 #include <base/macros.h>
25 
26 #include "update_engine/common/error_code.h"
27 
28 #include <gtest/gtest_prod.h>
29 
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 
36 // See action.h for an overview of this class and other Action* classes.
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:
47   ActionProcessor() = default;
48 
49   virtual ~ActionProcessor();
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.
54   virtual void StartProcessing();
55 
56   // Aborts processing. If an Action is running, it will have
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.
60   void StopProcessing();
61 
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
70   // or not running in the first place this method performs no action.
71   void ResumeProcessing();
72 
73   // Returns true iff the processing was started but not yet completed nor
74   // stopped.
75   bool IsRunning() const;
76 
77   // Adds another Action to the end of the queue.
78   virtual void EnqueueAction(std::unique_ptr<AbstractAction> action);
79 
80   // Sets/gets the current delegate. Set to null to remove a delegate.
delegate()81   ActionProcessorDelegate* delegate() const { return delegate_; }
set_delegate(ActionProcessorDelegate * delegate)82   void set_delegate(ActionProcessorDelegate* delegate) { delegate_ = delegate; }
83 
84   // Returns a pointer to the current Action that's processing.
current_action()85   AbstractAction* current_action() const { return current_action_.get(); }
86 
87   // Called by an action to notify processor that it's done. Caller passes self.
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.
91   virtual void ActionComplete(AbstractAction* actionptr, ErrorCode code);
92 
93  private:
94   FRIEND_TEST(ActionProcessorTest, ChainActionsTest);
95 
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 
101   // Actions that have not yet begun processing, in the order in which
102   // they'll be processed.
103   std::deque<std::unique_ptr<AbstractAction>> actions_;
104 
105   // A pointer to the currently processing Action, if any.
106   std::unique_ptr<AbstractAction> current_action_;
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};
115 
116   // A pointer to the delegate, or null if none.
117   ActionProcessorDelegate* delegate_{nullptr};
118 
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:
127   virtual ~ActionProcessorDelegate() = default;
128 
129   // Called when all processing in an ActionProcessor has completed. A pointer
130   // to the ActionProcessor is passed. |code| is set to the exit code of the
131   // last completed action.
ProcessingDone(const ActionProcessor * processor,ErrorCode code)132   virtual void ProcessingDone([[maybe_unused]] const ActionProcessor* processor,
133                               [[maybe_unused]] ErrorCode code) {}
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.
ProcessingStopped(const ActionProcessor * processor)137   virtual void ProcessingStopped(
138       [[maybe_unused]] const ActionProcessor* processor) {}
139 
140   // Called whenever an action has finished processing, either successfully
141   // or otherwise.
ActionCompleted(ActionProcessor * processor,AbstractAction * action,ErrorCode code)142   virtual void ActionCompleted([[maybe_unused]] ActionProcessor* processor,
143                                [[maybe_unused]] AbstractAction* action,
144                                [[maybe_unused]] ErrorCode code) {}
145 };
146 
147 }  // namespace chromeos_update_engine
148 
149 #endif  // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_
150