blob: 4de4f844eb607a3d8c2d27f27d7316d21c43f42a [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 {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070026 // use the 0xx and 1xx ranges for client errors.
Darin Petkovc1a8b422010-07-19 11:34:49 -070027 kActionCodeSuccess = 0,
28 kActionCodeError = 1,
Darin Petkov777dbfa2010-07-20 15:03:37 -070029 kActionCodeOmahaRequestError = 2,
30 kActionCodeOmahaResponseHandlerError = 3,
31 kActionCodeFilesystemCopierError = 4,
32 kActionCodePostinstallRunnerError = 5,
Darin Petkovd34a4212010-11-08 15:04:07 -080033 kActionCodeSetBootableFlagError = 6, // TODO(petkov): Unused. Recycle?
Darin Petkov777dbfa2010-07-20 15:03:37 -070034 kActionCodeInstallDeviceOpenError = 7,
35 kActionCodeKernelDeviceOpenError = 8,
36 kActionCodeDownloadTransferError = 9,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070037 kActionCodePayloadHashMismatchError = 10,
38 kActionCodePayloadSizeMismatchError = 11,
Darin Petkovd7061ab2010-10-06 14:37:09 -070039 kActionCodeDownloadPayloadVerificationError = 12,
Darin Petkov3aefa862010-12-07 14:45:00 -080040 kActionCodeDownloadNewPartitionInfoError = 13,
Darin Petkov698d0412010-10-13 10:59:44 -070041 kActionCodeDownloadWriteError = 14,
Darin Petkov3aefa862010-12-07 14:45:00 -080042 kActionCodeNewRootfsVerificationError = 15,
43 kActionCodeNewKernelVerificationError = 16,
Darin Petkovabc7bc02011-02-23 14:39:43 -080044 kActionCodeSignedDeltaPayloadExpectedError = 17,
Andrew de los Reyes21816e12011-04-07 14:18:56 -070045 kActionCodeDownloadPayloadPubKeyVerificationError = 18,
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070046 kActionCodePostinstallBootedFromFirmwareB = 19,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070047 kActionCodeDownloadStateInitializationError = 20,
Jay Srinivasanf4318702012-09-24 11:56:24 -070048 kActionCodeDownloadInvalidMetadataMagicString = 21,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070049 kActionCodeDownloadSignatureMissingInManifest = 22,
50 kActionCodeDownloadManifestParseError = 23,
Jay Srinivasanf4318702012-09-24 11:56:24 -070051 kActionCodeDownloadMetadataSignatureError = 24,
52 kActionCodeDownloadMetadataSignatureVerificationError = 25,
53 kActionCodeDownloadMetadataSignatureMismatch = 26,
Jay Srinivasan51dcf262012-09-13 17:24:32 -070054 kActionCodeDownloadOperationHashVerificationError = 27,
55 kActionCodeDownloadOperationExecutionError = 28,
56 kActionCodeDownloadOperationHashMismatch = 29,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070057
58 // use the 2xx range for errors in Omaha response.
Darin Petkovedc522e2010-11-05 09:35:17 -070059 kActionCodeOmahaRequestEmptyResponseError = 200,
60 kActionCodeOmahaRequestXMLParseError = 201,
61 kActionCodeOmahaRequestNoUpdateCheckNode = 202,
62 kActionCodeOmahaRequestNoUpdateCheckStatus = 203,
63 kActionCodeOmahaRequestBadUpdateCheckStatus = 204,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070064
65 // use the 2xxx range to encode HTTP errors.
Darin Petkovedc522e2010-11-05 09:35:17 -070066 kActionCodeOmahaRequestHTTPResponseBase = 2000, // + HTTP response code
Darin Petkovc91dd6b2011-01-10 12:31:34 -080067
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070068 // use the 5xxx range for return codes that are not really errors,
69 // but deferred updates. these have to be logged with a different
70 // result in Omaha so that they don't show up as errors in borgmon charts.
71 kActionCodeOmahaUpdateIgnoredPerPolicy = 5000,
72 kActionCodeOmahaUpdateDeferredPerPolicy = 5001,
73
Darin Petkovc91dd6b2011-01-10 12:31:34 -080074 // Bit flags.
Darin Petkov18c7bce2011-06-16 14:07:00 -070075 kActionCodeResumedFlag = 1 << 30, // Set if resuming an interruped update.
Darin Petkovc91dd6b2011-01-10 12:31:34 -080076 kActionCodeBootModeFlag = 1 << 31, // Set if boot mode not normal.
Darin Petkovc1a8b422010-07-19 11:34:49 -070077};
78
rspangler@google.com49fdf182009-10-10 00:57:34 +000079class AbstractAction;
80class ActionProcessorDelegate;
81
82class ActionProcessor {
83 public:
84 ActionProcessor();
85
Darin Petkovf42cc1c2010-09-01 09:03:02 -070086 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000087
88 // Starts processing the first Action in the queue. If there's a delegate,
89 // when all processing is complete, ProcessingDone() will be called on the
90 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070091 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000092
93 // Aborts processing. If an Action is running, it will have
94 // TerminateProcessing() called on it. The Action that was running
95 // will be lost and must be re-enqueued if this Processor is to use it.
96 void StopProcessing();
97
98 // Returns true iff an Action is currently processing.
99 bool IsRunning() const { return NULL != current_action_; }
100
101 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700102 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700104 // Sets/gets the current delegate. Set to NULL to remove a delegate.
105 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106 void set_delegate(ActionProcessorDelegate *delegate) {
107 delegate_ = delegate;
108 }
109
110 // Returns a pointer to the current Action that's processing.
111 AbstractAction* current_action() const {
112 return current_action_;
113 }
114
115 // Called by an action to notify processor that it's done. Caller passes self.
Darin Petkovc1a8b422010-07-19 11:34:49 -0700116 void ActionComplete(AbstractAction* actionptr, ActionExitCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117
118 private:
119 // Actions that have not yet begun processing, in the order in which
120 // they'll be processed.
121 std::deque<AbstractAction*> actions_;
122
123 // A pointer to the currrently processing Action, if any.
124 AbstractAction* current_action_;
125
126 // A pointer to the delegate, or NULL if none.
127 ActionProcessorDelegate *delegate_;
128 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
129};
130
131// A delegate object can be used to be notified of events that happen
132// in an ActionProcessor. An instance of this class can be passed to an
133// ActionProcessor to register itself.
134class ActionProcessorDelegate {
135 public:
136 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700137 // to the ActionProcessor is passed. |code| is set to the exit code of the
138 // last completed action.
139 virtual void ProcessingDone(const ActionProcessor* processor,
140 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141
142 // Called when processing has stopped. Does not mean that all Actions have
143 // completed. If/when all Actions complete, ProcessingDone() will be called.
144 virtual void ProcessingStopped(const ActionProcessor* processor) {}
145
146 // Called whenever an action has finished processing, either successfully
147 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000148 virtual void ActionCompleted(ActionProcessor* processor,
149 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700150 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000151};
152
153} // namespace chromeos_update_engine
154
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000155#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__