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