rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 24 | class AbstractAction; |
| 25 | class ActionProcessorDelegate; |
| 26 | |
| 27 | class ActionProcessor { |
| 28 | public: |
| 29 | ActionProcessor(); |
| 30 | |
| 31 | ~ActionProcessor(); |
| 32 | |
| 33 | // Starts processing the first Action in the queue. If there's a delegate, |
| 34 | // when all processing is complete, ProcessingDone() will be called on the |
| 35 | // delegate. |
| 36 | void StartProcessing(); |
| 37 | |
| 38 | // Aborts processing. If an Action is running, it will have |
| 39 | // TerminateProcessing() called on it. The Action that was running |
| 40 | // will be lost and must be re-enqueued if this Processor is to use it. |
| 41 | void StopProcessing(); |
| 42 | |
| 43 | // Returns true iff an Action is currently processing. |
| 44 | bool IsRunning() const { return NULL != current_action_; } |
| 45 | |
| 46 | // Adds another Action to the end of the queue. |
| 47 | void EnqueueAction(AbstractAction* action); |
| 48 | |
| 49 | // Sets the current delegate. Set to NULL to remove a delegate. |
| 50 | void set_delegate(ActionProcessorDelegate *delegate) { |
| 51 | delegate_ = delegate; |
| 52 | } |
| 53 | |
| 54 | // Returns a pointer to the current Action that's processing. |
| 55 | AbstractAction* current_action() const { |
| 56 | return current_action_; |
| 57 | } |
| 58 | |
| 59 | // Called by an action to notify processor that it's done. Caller passes self. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 60 | void ActionComplete(AbstractAction* actionptr, bool success); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | // Actions that have not yet begun processing, in the order in which |
| 64 | // they'll be processed. |
| 65 | std::deque<AbstractAction*> actions_; |
| 66 | |
| 67 | // A pointer to the currrently processing Action, if any. |
| 68 | AbstractAction* current_action_; |
| 69 | |
| 70 | // A pointer to the delegate, or NULL if none. |
| 71 | ActionProcessorDelegate *delegate_; |
| 72 | DISALLOW_COPY_AND_ASSIGN(ActionProcessor); |
| 73 | }; |
| 74 | |
| 75 | // A delegate object can be used to be notified of events that happen |
| 76 | // in an ActionProcessor. An instance of this class can be passed to an |
| 77 | // ActionProcessor to register itself. |
| 78 | class ActionProcessorDelegate { |
| 79 | public: |
| 80 | // Called when all processing in an ActionProcessor has completed. A pointer |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 81 | // to the ActionProcessor is passed. success is true iff all actions |
| 82 | // completed successfully |
| 83 | virtual void ProcessingDone(const ActionProcessor* processor, bool success) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | |
| 85 | // Called when processing has stopped. Does not mean that all Actions have |
| 86 | // completed. If/when all Actions complete, ProcessingDone() will be called. |
| 87 | virtual void ProcessingStopped(const ActionProcessor* processor) {} |
| 88 | |
| 89 | // Called whenever an action has finished processing, either successfully |
| 90 | // or otherwise. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 91 | virtual void ActionCompleted(ActionProcessor* processor, |
| 92 | AbstractAction* action, |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | bool success) {} |
| 94 | }; |
| 95 | |
| 96 | } // namespace chromeos_update_engine |
| 97 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 98 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__ |