Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 8 | #include <deque> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | |
| 12 | // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.) |
| 13 | // is based on the KSAction* classes from the Google Update Engine code at |
| 14 | // http://code.google.com/p/update-engine/ . The author of this file sends |
| 15 | // a big thanks to that team for their high quality design, implementation, |
| 16 | // and documentation. |
| 17 | |
| 18 | // See action.h for an overview of this class and other other Action* classes. |
| 19 | |
| 20 | // An ActionProcessor keeps a queue of Actions and processes them in order. |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 24 | // Action exit codes. |
| 25 | enum ActionExitCode { |
| 26 | kActionCodeSuccess = 0, |
| 27 | kActionCodeError = 1, |
| 28 | }; |
| 29 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | class AbstractAction; |
| 31 | class ActionProcessorDelegate; |
| 32 | |
| 33 | class ActionProcessor { |
| 34 | public: |
| 35 | ActionProcessor(); |
| 36 | |
| 37 | ~ActionProcessor(); |
| 38 | |
| 39 | // Starts processing the first Action in the queue. If there's a delegate, |
| 40 | // when all processing is complete, ProcessingDone() will be called on the |
| 41 | // delegate. |
| 42 | void StartProcessing(); |
| 43 | |
| 44 | // Aborts processing. If an Action is running, it will have |
| 45 | // TerminateProcessing() called on it. The Action that was running |
| 46 | // will be lost and must be re-enqueued if this Processor is to use it. |
| 47 | void StopProcessing(); |
| 48 | |
| 49 | // Returns true iff an Action is currently processing. |
| 50 | bool IsRunning() const { return NULL != current_action_; } |
| 51 | |
| 52 | // Adds another Action to the end of the queue. |
| 53 | void EnqueueAction(AbstractAction* action); |
| 54 | |
| 55 | // Sets the current delegate. Set to NULL to remove a delegate. |
| 56 | void set_delegate(ActionProcessorDelegate *delegate) { |
| 57 | delegate_ = delegate; |
| 58 | } |
| 59 | |
| 60 | // Returns a pointer to the current Action that's processing. |
| 61 | AbstractAction* current_action() const { |
| 62 | return current_action_; |
| 63 | } |
| 64 | |
| 65 | // Called by an action to notify processor that it's done. Caller passes self. |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 66 | void ActionComplete(AbstractAction* actionptr, ActionExitCode code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | // Actions that have not yet begun processing, in the order in which |
| 70 | // they'll be processed. |
| 71 | std::deque<AbstractAction*> actions_; |
| 72 | |
| 73 | // A pointer to the currrently processing Action, if any. |
| 74 | AbstractAction* current_action_; |
| 75 | |
| 76 | // A pointer to the delegate, or NULL if none. |
| 77 | ActionProcessorDelegate *delegate_; |
| 78 | DISALLOW_COPY_AND_ASSIGN(ActionProcessor); |
| 79 | }; |
| 80 | |
| 81 | // A delegate object can be used to be notified of events that happen |
| 82 | // in an ActionProcessor. An instance of this class can be passed to an |
| 83 | // ActionProcessor to register itself. |
| 84 | class ActionProcessorDelegate { |
| 85 | public: |
| 86 | // Called when all processing in an ActionProcessor has completed. A pointer |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 87 | // to the ActionProcessor is passed. |code| is set to the exit code of the |
| 88 | // last completed action. |
| 89 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 90 | ActionExitCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 91 | |
| 92 | // Called when processing has stopped. Does not mean that all Actions have |
| 93 | // completed. If/when all Actions complete, ProcessingDone() will be called. |
| 94 | virtual void ProcessingStopped(const ActionProcessor* processor) {} |
| 95 | |
| 96 | // Called whenever an action has finished processing, either successfully |
| 97 | // or otherwise. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 98 | virtual void ActionCompleted(ActionProcessor* processor, |
| 99 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 100 | ActionExitCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace chromeos_update_engine |
| 104 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 105 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__ |