blob: 38d63ae3e16ee0e3177c0c0a3d6b8938ece5e038 [file] [log] [blame]
Darin Petkov18c7bce2011-06-16 14:07:00 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
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
22namespace chromeos_update_engine {
23
Darin Petkovc1a8b422010-07-19 11:34:49 -070024// Action exit codes.
25enum ActionExitCode {
26 kActionCodeSuccess = 0,
27 kActionCodeError = 1,
Darin Petkov777dbfa2010-07-20 15:03:37 -070028 kActionCodeOmahaRequestError = 2,
29 kActionCodeOmahaResponseHandlerError = 3,
30 kActionCodeFilesystemCopierError = 4,
31 kActionCodePostinstallRunnerError = 5,
Darin Petkovd34a4212010-11-08 15:04:07 -080032 kActionCodeSetBootableFlagError = 6, // TODO(petkov): Unused. Recycle?
Darin Petkov777dbfa2010-07-20 15:03:37 -070033 kActionCodeInstallDeviceOpenError = 7,
34 kActionCodeKernelDeviceOpenError = 8,
35 kActionCodeDownloadTransferError = 9,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070036 kActionCodePayloadHashMismatchError = 10,
37 kActionCodePayloadSizeMismatchError = 11,
Darin Petkovd7061ab2010-10-06 14:37:09 -070038 kActionCodeDownloadPayloadVerificationError = 12,
Darin Petkov3aefa862010-12-07 14:45:00 -080039 kActionCodeDownloadNewPartitionInfoError = 13,
Darin Petkov698d0412010-10-13 10:59:44 -070040 kActionCodeDownloadWriteError = 14,
Darin Petkov3aefa862010-12-07 14:45:00 -080041 kActionCodeNewRootfsVerificationError = 15,
42 kActionCodeNewKernelVerificationError = 16,
Darin Petkovabc7bc02011-02-23 14:39:43 -080043 kActionCodeSignedDeltaPayloadExpectedError = 17,
Andrew de los Reyes21816e12011-04-07 14:18:56 -070044 kActionCodeDownloadPayloadPubKeyVerificationError = 18,
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070045 kActionCodePostinstallBootedFromFirmwareB = 19,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070046 kActionCodeDownloadStateInitializationError = 20,
Jay Srinivasanf4318702012-09-24 11:56:24 -070047 kActionCodeDownloadInvalidMetadataMagicString = 21,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070048 kActionCodeDownloadSignatureMissingInManifest = 22,
49 kActionCodeDownloadManifestParseError = 23,
Jay Srinivasanf4318702012-09-24 11:56:24 -070050 kActionCodeDownloadMetadataSignatureError = 24,
51 kActionCodeDownloadMetadataSignatureVerificationError = 25,
52 kActionCodeDownloadMetadataSignatureMismatch = 26,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070053 kActionCodeDownloadOperationHashVerificationError = 27,
54 kActionCodeDownloadOperationExecutionError = 28,
55 kActionCodeDownloadOperationHashMismatch = 29,
Jay Srinivasanf0572052012-10-23 18:12:56 -070056 kActionCodeOmahaRequestEmptyResponseError = 30,
57 kActionCodeOmahaRequestXMLParseError = 31,
Jay Srinivasan23b92a52012-10-27 02:00:21 -070058 kActionCodeDownloadInvalidMetadataSize = 32,
59 kActionCodeDownloadInvalidMetadataSignature = 33,
60 kActionCodeOmahaResponseInvalid = 34,
Jay Srinivasanf0572052012-10-23 18:12:56 -070061 kActionCodeOmahaUpdateIgnoredPerPolicy = 35,
62 kActionCodeOmahaUpdateDeferredPerPolicy = 36,
63 kActionCodeOmahaErrorInHTTPResponse = 37,
64 kActionCodeDownloadOperationHashMissingError = 38,
Jay Srinivasan23b92a52012-10-27 02:00:21 -070065 kActionCodeDownloadMetadataSignatureMissingError = 39,
Jay Srinivasan08262882012-12-28 19:29:43 -080066 kActionCodeOmahaUpdateDeferredForBackoff = 40,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070067 kActionCodePostinstallPowerwashError = 41,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070068 kActionCodeUpdateCanceledByChannelChange = 42,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070069
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080070 // Note: When adding new error codes, please remember to add the
71 // error into one of the buckets in PayloadState::UpdateFailed method so
Jay Srinivasan08262882012-12-28 19:29:43 -080072 // that the retries across URLs and the payload backoff mechanism work
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080073 // correctly for those new error codes.
74
75 // Any code above this is sent to both Omaha and UMA as-is, except
76 // kActionCodeOmahaErrorInHTTPResponse (see error code 2000 for more details).
Jay Srinivasanf0572052012-10-23 18:12:56 -070077 // Codes/flags below this line is sent only to Omaha and not to UMA.
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070078
Jay Srinivasanedce2832012-10-24 18:57:47 -070079 // kActionCodeUmaReportedMax is not an error code per se, it's just the count
Jay Srinivasanf0572052012-10-23 18:12:56 -070080 // of the number of enums above. Add any new errors above this line if you
81 // want them to show up on UMA. Stuff below this line will not be sent to UMA
82 // but is used for other errors that are sent to Omaha. We don't assign any
83 // particular value for this enum so that it's just one more than the last
84 // one above and thus always represents the correct count of UMA metrics
85 // buckets, even when new enums are added above this line in future. See
Jay Srinivasanedce2832012-10-24 18:57:47 -070086 // utils::SendErrorCodeToUma on how this enum is used.
87 kActionCodeUmaReportedMax,
Jay Srinivasanf0572052012-10-23 18:12:56 -070088
89 // use the 2xxx range to encode HTTP errors. These errors are available in
90 // Dremel with the individual granularity. But for UMA purposes, all these
91 // errors are aggregated into one: kActionCodeOmahaErrorInHTTPResponse.
Darin Petkovedc522e2010-11-05 09:35:17 -070092 kActionCodeOmahaRequestHTTPResponseBase = 2000, // + HTTP response code
Darin Petkovc91dd6b2011-01-10 12:31:34 -080093
Jay Srinivasanedce2832012-10-24 18:57:47 -070094 // TODO(jaysri): Move out all the bit masks into separate constants
95 // outside the enum as part of fixing bug 34369.
Jay Srinivasanf0572052012-10-23 18:12:56 -070096 // Bit flags. Remember to update the mask below for new bits.
Jay Srinivasanf0572052012-10-23 18:12:56 -070097
Jay Srinivasan55f50c22013-01-10 19:24:35 -080098 // Set if boot mode not normal.
99 kActionCodeDevModeFlag = 1 << 31,
100
101 // Set if resuming an interruped update.
102 kActionCodeResumedFlag = 1 << 30,
103
104 // Set if using a dev/test image as opposed to an MP-signed image.
105 kActionCodeTestImageFlag = 1 << 29,
106
107 // Set if using devserver or Omaha sandbox (using crosh autest).
108 kActionCodeTestOmahaUrlFlag = 1 << 28,
109
110 // Mask that indicates bit positions that are used to indicate special flags
111 // that are embedded in the error code to provide additional context about
112 // the system in which the error was encountered.
113 kSpecialFlags = (kActionCodeDevModeFlag |
114 kActionCodeResumedFlag |
115 kActionCodeTestImageFlag |
116 kActionCodeTestOmahaUrlFlag)
Darin Petkovc1a8b422010-07-19 11:34:49 -0700117};
118
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119class AbstractAction;
120class ActionProcessorDelegate;
121
122class ActionProcessor {
123 public:
124 ActionProcessor();
125
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700126 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127
128 // Starts processing the first Action in the queue. If there's a delegate,
129 // when all processing is complete, ProcessingDone() will be called on the
130 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700131 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132
133 // Aborts processing. If an Action is running, it will have
134 // TerminateProcessing() called on it. The Action that was running
135 // will be lost and must be re-enqueued if this Processor is to use it.
136 void StopProcessing();
137
138 // Returns true iff an Action is currently processing.
139 bool IsRunning() const { return NULL != current_action_; }
140
141 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700142 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700144 // Sets/gets the current delegate. Set to NULL to remove a delegate.
145 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 void set_delegate(ActionProcessorDelegate *delegate) {
147 delegate_ = delegate;
148 }
149
150 // Returns a pointer to the current Action that's processing.
151 AbstractAction* current_action() const {
152 return current_action_;
153 }
154
155 // Called by an action to notify processor that it's done. Caller passes self.
Darin Petkovc1a8b422010-07-19 11:34:49 -0700156 void ActionComplete(AbstractAction* actionptr, ActionExitCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000157
158 private:
159 // Actions that have not yet begun processing, in the order in which
160 // they'll be processed.
161 std::deque<AbstractAction*> actions_;
162
163 // A pointer to the currrently processing Action, if any.
164 AbstractAction* current_action_;
165
166 // A pointer to the delegate, or NULL if none.
167 ActionProcessorDelegate *delegate_;
168 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
169};
170
171// A delegate object can be used to be notified of events that happen
172// in an ActionProcessor. An instance of this class can be passed to an
173// ActionProcessor to register itself.
174class ActionProcessorDelegate {
175 public:
176 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700177 // to the ActionProcessor is passed. |code| is set to the exit code of the
178 // last completed action.
179 virtual void ProcessingDone(const ActionProcessor* processor,
180 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181
182 // Called when processing has stopped. Does not mean that all Actions have
183 // completed. If/when all Actions complete, ProcessingDone() will be called.
184 virtual void ProcessingStopped(const ActionProcessor* processor) {}
185
186 // Called whenever an action has finished processing, either successfully
187 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000188 virtual void ActionCompleted(ActionProcessor* processor,
189 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700190 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191};
192
193} // namespace chromeos_update_engine
194
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000195#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__