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, |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 28 | kActionCodeOmahaRequestError = 2, |
| 29 | kActionCodeOmahaResponseHandlerError = 3, |
| 30 | kActionCodeFilesystemCopierError = 4, |
| 31 | kActionCodePostinstallRunnerError = 5, |
| 32 | kActionCodeSetBootableFlagError = 6, |
| 33 | kActionCodeInstallDeviceOpenError = 7, |
| 34 | kActionCodeKernelDeviceOpenError = 8, |
| 35 | kActionCodeDownloadTransferError = 9, |
| 36 | kActionCodeDownloadHashMismatchError = 10, |
Darin Petkov | 50332f1 | 2010-09-24 11:44:47 -0700 | [diff] [blame^] | 37 | kActionCodeDownloadSizeMismatchError = 11, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 40 | class AbstractAction; |
| 41 | class ActionProcessorDelegate; |
| 42 | |
| 43 | class ActionProcessor { |
| 44 | public: |
| 45 | ActionProcessor(); |
| 46 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 47 | virtual ~ActionProcessor(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 48 | |
| 49 | // Starts processing the first Action in the queue. If there's a delegate, |
| 50 | // when all processing is complete, ProcessingDone() will be called on the |
| 51 | // delegate. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 52 | virtual void StartProcessing(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | |
| 54 | // Aborts processing. If an Action is running, it will have |
| 55 | // TerminateProcessing() called on it. The Action that was running |
| 56 | // will be lost and must be re-enqueued if this Processor is to use it. |
| 57 | void StopProcessing(); |
| 58 | |
| 59 | // Returns true iff an Action is currently processing. |
| 60 | bool IsRunning() const { return NULL != current_action_; } |
| 61 | |
| 62 | // Adds another Action to the end of the queue. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 63 | virtual void EnqueueAction(AbstractAction* action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 65 | // Sets/gets the current delegate. Set to NULL to remove a delegate. |
| 66 | ActionProcessorDelegate* delegate() const { return delegate_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | void set_delegate(ActionProcessorDelegate *delegate) { |
| 68 | delegate_ = delegate; |
| 69 | } |
| 70 | |
| 71 | // Returns a pointer to the current Action that's processing. |
| 72 | AbstractAction* current_action() const { |
| 73 | return current_action_; |
| 74 | } |
| 75 | |
| 76 | // 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] | 77 | void ActionComplete(AbstractAction* actionptr, ActionExitCode code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | // Actions that have not yet begun processing, in the order in which |
| 81 | // they'll be processed. |
| 82 | std::deque<AbstractAction*> actions_; |
| 83 | |
| 84 | // A pointer to the currrently processing Action, if any. |
| 85 | AbstractAction* current_action_; |
| 86 | |
| 87 | // A pointer to the delegate, or NULL if none. |
| 88 | ActionProcessorDelegate *delegate_; |
| 89 | DISALLOW_COPY_AND_ASSIGN(ActionProcessor); |
| 90 | }; |
| 91 | |
| 92 | // A delegate object can be used to be notified of events that happen |
| 93 | // in an ActionProcessor. An instance of this class can be passed to an |
| 94 | // ActionProcessor to register itself. |
| 95 | class ActionProcessorDelegate { |
| 96 | public: |
| 97 | // Called when all processing in an ActionProcessor has completed. A pointer |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 98 | // to the ActionProcessor is passed. |code| is set to the exit code of the |
| 99 | // last completed action. |
| 100 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 101 | ActionExitCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 102 | |
| 103 | // Called when processing has stopped. Does not mean that all Actions have |
| 104 | // completed. If/when all Actions complete, ProcessingDone() will be called. |
| 105 | virtual void ProcessingStopped(const ActionProcessor* processor) {} |
| 106 | |
| 107 | // Called whenever an action has finished processing, either successfully |
| 108 | // or otherwise. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 109 | virtual void ActionCompleted(ActionProcessor* processor, |
| 110 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 111 | ActionExitCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace chromeos_update_engine |
| 115 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 116 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__ |