blob: 2618c3961f0cb192e49d611a1ef2dacaba45c975 [file] [log] [blame]
Darin Petkovc1a8b422010-07-19 11:34:49 -07001// Copyright (c) 2010 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,
32 kActionCodeSetBootableFlagError = 6,
33 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 Petkov2dd01092010-10-08 15:43:05 -070039 kActionCodeDownloadAppliedUpdateVerificationError = 13,
Darin Petkov698d0412010-10-13 10:59:44 -070040 kActionCodeDownloadWriteError = 14,
Darin Petkovedc522e2010-11-05 09:35:17 -070041 kActionCodeOmahaRequestEmptyResponseError = 200,
42 kActionCodeOmahaRequestXMLParseError = 201,
43 kActionCodeOmahaRequestNoUpdateCheckNode = 202,
44 kActionCodeOmahaRequestNoUpdateCheckStatus = 203,
45 kActionCodeOmahaRequestBadUpdateCheckStatus = 204,
46 kActionCodeOmahaRequestHTTPResponseBase = 2000, // + HTTP response code
Darin Petkovc1a8b422010-07-19 11:34:49 -070047};
48
rspangler@google.com49fdf182009-10-10 00:57:34 +000049class AbstractAction;
50class ActionProcessorDelegate;
51
52class ActionProcessor {
53 public:
54 ActionProcessor();
55
Darin Petkovf42cc1c2010-09-01 09:03:02 -070056 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000057
58 // Starts processing the first Action in the queue. If there's a delegate,
59 // when all processing is complete, ProcessingDone() will be called on the
60 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000062
63 // Aborts processing. If an Action is running, it will have
64 // TerminateProcessing() called on it. The Action that was running
65 // will be lost and must be re-enqueued if this Processor is to use it.
66 void StopProcessing();
67
68 // Returns true iff an Action is currently processing.
69 bool IsRunning() const { return NULL != current_action_; }
70
71 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070072 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000073
Darin Petkovf42cc1c2010-09-01 09:03:02 -070074 // Sets/gets the current delegate. Set to NULL to remove a delegate.
75 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000076 void set_delegate(ActionProcessorDelegate *delegate) {
77 delegate_ = delegate;
78 }
79
80 // Returns a pointer to the current Action that's processing.
81 AbstractAction* current_action() const {
82 return current_action_;
83 }
84
85 // Called by an action to notify processor that it's done. Caller passes self.
Darin Petkovc1a8b422010-07-19 11:34:49 -070086 void ActionComplete(AbstractAction* actionptr, ActionExitCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000087
88 private:
89 // Actions that have not yet begun processing, in the order in which
90 // they'll be processed.
91 std::deque<AbstractAction*> actions_;
92
93 // A pointer to the currrently processing Action, if any.
94 AbstractAction* current_action_;
95
96 // A pointer to the delegate, or NULL if none.
97 ActionProcessorDelegate *delegate_;
98 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
99};
100
101// A delegate object can be used to be notified of events that happen
102// in an ActionProcessor. An instance of this class can be passed to an
103// ActionProcessor to register itself.
104class ActionProcessorDelegate {
105 public:
106 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700107 // to the ActionProcessor is passed. |code| is set to the exit code of the
108 // last completed action.
109 virtual void ProcessingDone(const ActionProcessor* processor,
110 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111
112 // Called when processing has stopped. Does not mean that all Actions have
113 // completed. If/when all Actions complete, ProcessingDone() will be called.
114 virtual void ProcessingStopped(const ActionProcessor* processor) {}
115
116 // Called whenever an action has finished processing, either successfully
117 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000118 virtual void ActionCompleted(ActionProcessor* processor,
119 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700120 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121};
122
123} // namespace chromeos_update_engine
124
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000125#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__