blob: b3cc67b811f865d1dc9541272de6aa9bd47b7bc3 [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_ACTION_PROCESSOR_H_
6#define UPDATE_ENGINE_ACTION_PROCESSOR_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
8#include <deque>
9
Ben Chan05735a12014-09-03 07:48:22 -070010#include <base/macros.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011
David Zeuthena99981f2013-04-29 13:42:47 -070012#include "update_engine/error_code.h"
13
rspangler@google.com49fdf182009-10-10 00:57:34 +000014// The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
15// is based on the KSAction* classes from the Google Update Engine code at
16// http://code.google.com/p/update-engine/ . The author of this file sends
17// a big thanks to that team for their high quality design, implementation,
18// and documentation.
19
Alex Vakulenko072359c2014-07-18 11:41:07 -070020// See action.h for an overview of this class and other Action* classes.
rspangler@google.com49fdf182009-10-10 00:57:34 +000021
22// An ActionProcessor keeps a queue of Actions and processes them in order.
23
24namespace chromeos_update_engine {
25
26class AbstractAction;
27class ActionProcessorDelegate;
28
29class ActionProcessor {
30 public:
31 ActionProcessor();
32
Darin Petkovf42cc1c2010-09-01 09:03:02 -070033 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
35 // Starts processing the first Action in the queue. If there's a delegate,
36 // when all processing is complete, ProcessingDone() will be called on the
37 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070038 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
40 // Aborts processing. If an Action is running, it will have
41 // TerminateProcessing() called on it. The Action that was running
42 // will be lost and must be re-enqueued if this Processor is to use it.
43 void StopProcessing();
44
45 // Returns true iff an Action is currently processing.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070046 bool IsRunning() const { return nullptr != current_action_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
48 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070049 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000050
Alex Vakulenko88b591f2014-08-28 16:48:57 -070051 // Sets/gets the current delegate. Set to null to remove a delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070052 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000053 void set_delegate(ActionProcessorDelegate *delegate) {
54 delegate_ = delegate;
55 }
56
57 // Returns a pointer to the current Action that's processing.
58 AbstractAction* current_action() const {
59 return current_action_;
60 }
61
62 // Called by an action to notify processor that it's done. Caller passes self.
David Zeuthena99981f2013-04-29 13:42:47 -070063 void ActionComplete(AbstractAction* actionptr, ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000064
65 private:
66 // Actions that have not yet begun processing, in the order in which
67 // they'll be processed.
68 std::deque<AbstractAction*> actions_;
69
Alex Vakulenko072359c2014-07-18 11:41:07 -070070 // A pointer to the currently processing Action, if any.
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 AbstractAction* current_action_;
72
Alex Vakulenko88b591f2014-08-28 16:48:57 -070073 // A pointer to the delegate, or null if none.
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 ActionProcessorDelegate *delegate_;
75 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
76};
77
78// A delegate object can be used to be notified of events that happen
79// in an ActionProcessor. An instance of this class can be passed to an
80// ActionProcessor to register itself.
81class ActionProcessorDelegate {
82 public:
83 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -070084 // to the ActionProcessor is passed. |code| is set to the exit code of the
85 // last completed action.
86 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070087 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000088
89 // Called when processing has stopped. Does not mean that all Actions have
90 // completed. If/when all Actions complete, ProcessingDone() will be called.
91 virtual void ProcessingStopped(const ActionProcessor* processor) {}
92
93 // Called whenever an action has finished processing, either successfully
94 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +000095 virtual void ActionCompleted(ActionProcessor* processor,
96 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070097 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000098};
99
100} // namespace chromeos_update_engine
101
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700102#endif // UPDATE_ENGINE_ACTION_PROCESSOR_H_