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, |
Darin Petkov | d34a421 | 2010-11-08 15:04:07 -0800 | [diff] [blame] | 32 | kActionCodeSetBootableFlagError = 6, // TODO(petkov): Unused. Recycle? |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 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 | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 38 | kActionCodeDownloadPayloadVerificationError = 12, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 39 | kActionCodeDownloadNewPartitionInfoError = 13, |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 40 | kActionCodeDownloadWriteError = 14, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 41 | kActionCodeNewRootfsVerificationError = 15, |
| 42 | kActionCodeNewKernelVerificationError = 16, |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 43 | kActionCodeSignedDeltaPayloadExpectedError = 17, |
Andrew de los Reyes | 21816e1 | 2011-04-07 14:18:56 -0700 | [diff] [blame] | 44 | kActionCodeDownloadPayloadPubKeyVerificationError = 18, |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame^] | 45 | kActionCodePostinstallBootedFromFirmwareB = 19, |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 46 | kActionCodeOmahaRequestEmptyResponseError = 200, |
| 47 | kActionCodeOmahaRequestXMLParseError = 201, |
| 48 | kActionCodeOmahaRequestNoUpdateCheckNode = 202, |
| 49 | kActionCodeOmahaRequestNoUpdateCheckStatus = 203, |
| 50 | kActionCodeOmahaRequestBadUpdateCheckStatus = 204, |
| 51 | kActionCodeOmahaRequestHTTPResponseBase = 2000, // + HTTP response code |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 52 | |
| 53 | // Bit flags. |
| 54 | kActionCodeBootModeFlag = 1 << 31, // Set if boot mode not normal. |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | class AbstractAction; |
| 58 | class ActionProcessorDelegate; |
| 59 | |
| 60 | class ActionProcessor { |
| 61 | public: |
| 62 | ActionProcessor(); |
| 63 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 64 | virtual ~ActionProcessor(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | |
| 66 | // Starts processing the first Action in the queue. If there's a delegate, |
| 67 | // when all processing is complete, ProcessingDone() will be called on the |
| 68 | // delegate. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 69 | virtual void StartProcessing(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | |
| 71 | // Aborts processing. If an Action is running, it will have |
| 72 | // TerminateProcessing() called on it. The Action that was running |
| 73 | // will be lost and must be re-enqueued if this Processor is to use it. |
| 74 | void StopProcessing(); |
| 75 | |
| 76 | // Returns true iff an Action is currently processing. |
| 77 | bool IsRunning() const { return NULL != current_action_; } |
| 78 | |
| 79 | // Adds another Action to the end of the queue. |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 80 | virtual void EnqueueAction(AbstractAction* action); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 82 | // Sets/gets the current delegate. Set to NULL to remove a delegate. |
| 83 | ActionProcessorDelegate* delegate() const { return delegate_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | void set_delegate(ActionProcessorDelegate *delegate) { |
| 85 | delegate_ = delegate; |
| 86 | } |
| 87 | |
| 88 | // Returns a pointer to the current Action that's processing. |
| 89 | AbstractAction* current_action() const { |
| 90 | return current_action_; |
| 91 | } |
| 92 | |
| 93 | // 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] | 94 | void ActionComplete(AbstractAction* actionptr, ActionExitCode code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | // Actions that have not yet begun processing, in the order in which |
| 98 | // they'll be processed. |
| 99 | std::deque<AbstractAction*> actions_; |
| 100 | |
| 101 | // A pointer to the currrently processing Action, if any. |
| 102 | AbstractAction* current_action_; |
| 103 | |
| 104 | // A pointer to the delegate, or NULL if none. |
| 105 | ActionProcessorDelegate *delegate_; |
| 106 | DISALLOW_COPY_AND_ASSIGN(ActionProcessor); |
| 107 | }; |
| 108 | |
| 109 | // A delegate object can be used to be notified of events that happen |
| 110 | // in an ActionProcessor. An instance of this class can be passed to an |
| 111 | // ActionProcessor to register itself. |
| 112 | class ActionProcessorDelegate { |
| 113 | public: |
| 114 | // Called when all processing in an ActionProcessor has completed. A pointer |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 115 | // to the ActionProcessor is passed. |code| is set to the exit code of the |
| 116 | // last completed action. |
| 117 | virtual void ProcessingDone(const ActionProcessor* processor, |
| 118 | ActionExitCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 119 | |
| 120 | // Called when processing has stopped. Does not mean that all Actions have |
| 121 | // completed. If/when all Actions complete, ProcessingDone() will be called. |
| 122 | virtual void ProcessingStopped(const ActionProcessor* processor) {} |
| 123 | |
| 124 | // Called whenever an action has finished processing, either successfully |
| 125 | // or otherwise. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 126 | virtual void ActionCompleted(ActionProcessor* processor, |
| 127 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 128 | ActionExitCode code) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace chromeos_update_engine |
| 132 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 133 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__ |