blob: 57b1d37685e43674be09cd9b7c730322c5c7c46f [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,
36 kActionCodeDownloadHashMismatchError = 10,
Darin Petkov50332f12010-09-24 11:44:47 -070037 kActionCodeDownloadSizeMismatchError = 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,
Darin Petkovedc522e2010-11-05 09:35:17 -070046 kActionCodeOmahaRequestEmptyResponseError = 200,
47 kActionCodeOmahaRequestXMLParseError = 201,
48 kActionCodeOmahaRequestNoUpdateCheckNode = 202,
49 kActionCodeOmahaRequestNoUpdateCheckStatus = 203,
50 kActionCodeOmahaRequestBadUpdateCheckStatus = 204,
51 kActionCodeOmahaRequestHTTPResponseBase = 2000, // + HTTP response code
Darin Petkovc91dd6b2011-01-10 12:31:34 -080052
53 // Bit flags.
Darin Petkov18c7bce2011-06-16 14:07:00 -070054 kActionCodeResumedFlag = 1 << 30, // Set if resuming an interruped update.
Darin Petkovc91dd6b2011-01-10 12:31:34 -080055 kActionCodeBootModeFlag = 1 << 31, // Set if boot mode not normal.
Darin Petkovc1a8b422010-07-19 11:34:49 -070056};
57
rspangler@google.com49fdf182009-10-10 00:57:34 +000058class AbstractAction;
59class ActionProcessorDelegate;
60
61class ActionProcessor {
62 public:
63 ActionProcessor();
64
Darin Petkovf42cc1c2010-09-01 09:03:02 -070065 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000066
67 // Starts processing the first Action in the queue. If there's a delegate,
68 // when all processing is complete, ProcessingDone() will be called on the
69 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070070 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000071
72 // Aborts processing. If an Action is running, it will have
73 // TerminateProcessing() called on it. The Action that was running
74 // will be lost and must be re-enqueued if this Processor is to use it.
75 void StopProcessing();
76
77 // Returns true iff an Action is currently processing.
78 bool IsRunning() const { return NULL != current_action_; }
79
80 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070081 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
Darin Petkovf42cc1c2010-09-01 09:03:02 -070083 // Sets/gets the current delegate. Set to NULL to remove a delegate.
84 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000085 void set_delegate(ActionProcessorDelegate *delegate) {
86 delegate_ = delegate;
87 }
88
89 // Returns a pointer to the current Action that's processing.
90 AbstractAction* current_action() const {
91 return current_action_;
92 }
93
94 // Called by an action to notify processor that it's done. Caller passes self.
Darin Petkovc1a8b422010-07-19 11:34:49 -070095 void ActionComplete(AbstractAction* actionptr, ActionExitCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000096
97 private:
98 // Actions that have not yet begun processing, in the order in which
99 // they'll be processed.
100 std::deque<AbstractAction*> actions_;
101
102 // A pointer to the currrently processing Action, if any.
103 AbstractAction* current_action_;
104
105 // A pointer to the delegate, or NULL if none.
106 ActionProcessorDelegate *delegate_;
107 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
108};
109
110// A delegate object can be used to be notified of events that happen
111// in an ActionProcessor. An instance of this class can be passed to an
112// ActionProcessor to register itself.
113class ActionProcessorDelegate {
114 public:
115 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700116 // to the ActionProcessor is passed. |code| is set to the exit code of the
117 // last completed action.
118 virtual void ProcessingDone(const ActionProcessor* processor,
119 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120
121 // Called when processing has stopped. Does not mean that all Actions have
122 // completed. If/when all Actions complete, ProcessingDone() will be called.
123 virtual void ProcessingStopped(const ActionProcessor* processor) {}
124
125 // Called whenever an action has finished processing, either successfully
126 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000127 virtual void ActionCompleted(ActionProcessor* processor,
128 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700129 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130};
131
132} // namespace chromeos_update_engine
133
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000134#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__