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