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