blob: 256648d56301856ef147a2f37683de38b7bc477c [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,
37 kActionCodeDownloadHashMismatchError = 10,
Darin Petkov50332f12010-09-24 11:44:47 -070038 kActionCodeDownloadSizeMismatchError = 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 Srinivasan56d5aa42012-03-26 14:27:59 -070047
48 // use the 2xx range for errors in Omaha response.
Darin Petkovedc522e2010-11-05 09:35:17 -070049 kActionCodeOmahaRequestEmptyResponseError = 200,
50 kActionCodeOmahaRequestXMLParseError = 201,
51 kActionCodeOmahaRequestNoUpdateCheckNode = 202,
52 kActionCodeOmahaRequestNoUpdateCheckStatus = 203,
53 kActionCodeOmahaRequestBadUpdateCheckStatus = 204,
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070054
55 // use the 2xxx range to encode HTTP errors.
Darin Petkovedc522e2010-11-05 09:35:17 -070056 kActionCodeOmahaRequestHTTPResponseBase = 2000, // + HTTP response code
Darin Petkovc91dd6b2011-01-10 12:31:34 -080057
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070058 // 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 Petkovc91dd6b2011-01-10 12:31:34 -080064 // Bit flags.
Darin Petkov18c7bce2011-06-16 14:07:00 -070065 kActionCodeResumedFlag = 1 << 30, // Set if resuming an interruped update.
Darin Petkovc91dd6b2011-01-10 12:31:34 -080066 kActionCodeBootModeFlag = 1 << 31, // Set if boot mode not normal.
Darin Petkovc1a8b422010-07-19 11:34:49 -070067};
68
rspangler@google.com49fdf182009-10-10 00:57:34 +000069class AbstractAction;
70class ActionProcessorDelegate;
71
72class ActionProcessor {
73 public:
74 ActionProcessor();
75
Darin Petkovf42cc1c2010-09-01 09:03:02 -070076 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000077
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 Petkovf42cc1c2010-09-01 09:03:02 -070081 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
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 Petkovf42cc1c2010-09-01 09:03:02 -070092 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000093
Darin Petkovf42cc1c2010-09-01 09:03:02 -070094 // Sets/gets the current delegate. Set to NULL to remove a delegate.
95 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000096 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 Petkovc1a8b422010-07-19 11:34:49 -0700106 void ActionComplete(AbstractAction* actionptr, ActionExitCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107
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.
124class ActionProcessorDelegate {
125 public:
126 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700127 // 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.com49fdf182009-10-10 00:57:34 +0000131
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.comc98a7ed2009-12-04 18:54:03 +0000138 virtual void ActionCompleted(ActionProcessor* processor,
139 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700140 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141};
142
143} // namespace chromeos_update_engine
144
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000145#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__