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