blob: 96e16f05f6b20155368c000477c724c1c4162474 [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 Petkovc1a8b422010-07-19 11:34:49 -070037};
38
rspangler@google.com49fdf182009-10-10 00:57:34 +000039class AbstractAction;
40class ActionProcessorDelegate;
41
42class ActionProcessor {
43 public:
44 ActionProcessor();
45
46 ~ActionProcessor();
47
48 // Starts processing the first Action in the queue. If there's a delegate,
49 // when all processing is complete, ProcessingDone() will be called on the
50 // delegate.
51 void StartProcessing();
52
53 // Aborts processing. If an Action is running, it will have
54 // TerminateProcessing() called on it. The Action that was running
55 // will be lost and must be re-enqueued if this Processor is to use it.
56 void StopProcessing();
57
58 // Returns true iff an Action is currently processing.
59 bool IsRunning() const { return NULL != current_action_; }
60
61 // Adds another Action to the end of the queue.
62 void EnqueueAction(AbstractAction* action);
63
64 // Sets the current delegate. Set to NULL to remove a delegate.
65 void set_delegate(ActionProcessorDelegate *delegate) {
66 delegate_ = delegate;
67 }
68
69 // Returns a pointer to the current Action that's processing.
70 AbstractAction* current_action() const {
71 return current_action_;
72 }
73
74 // Called by an action to notify processor that it's done. Caller passes self.
Darin Petkovc1a8b422010-07-19 11:34:49 -070075 void ActionComplete(AbstractAction* actionptr, ActionExitCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000076
77 private:
78 // Actions that have not yet begun processing, in the order in which
79 // they'll be processed.
80 std::deque<AbstractAction*> actions_;
81
82 // A pointer to the currrently processing Action, if any.
83 AbstractAction* current_action_;
84
85 // A pointer to the delegate, or NULL if none.
86 ActionProcessorDelegate *delegate_;
87 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
88};
89
90// A delegate object can be used to be notified of events that happen
91// in an ActionProcessor. An instance of this class can be passed to an
92// ActionProcessor to register itself.
93class ActionProcessorDelegate {
94 public:
95 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -070096 // to the ActionProcessor is passed. |code| is set to the exit code of the
97 // last completed action.
98 virtual void ProcessingDone(const ActionProcessor* processor,
99 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100
101 // Called when processing has stopped. Does not mean that all Actions have
102 // completed. If/when all Actions complete, ProcessingDone() will be called.
103 virtual void ProcessingStopped(const ActionProcessor* processor) {}
104
105 // Called whenever an action has finished processing, either successfully
106 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000107 virtual void ActionCompleted(ActionProcessor* processor,
108 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700109 ActionExitCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110};
111
112} // namespace chromeos_update_engine
113
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000114#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_PROCESSOR_H__