blob: cd458ad163bd69931369c5117a17445fe525f61a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_ACTION_PROCESSOR_H_
18#define UPDATE_ENGINE_ACTION_PROCESSOR_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
20#include <deque>
21
Ben Chan05735a12014-09-03 07:48:22 -070022#include <base/macros.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
David Zeuthena99981f2013-04-29 13:42:47 -070024#include "update_engine/error_code.h"
25
rspangler@google.com49fdf182009-10-10 00:57:34 +000026// The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
27// is based on the KSAction* classes from the Google Update Engine code at
28// http://code.google.com/p/update-engine/ . The author of this file sends
29// a big thanks to that team for their high quality design, implementation,
30// and documentation.
31
Alex Vakulenko072359c2014-07-18 11:41:07 -070032// See action.h for an overview of this class and other Action* classes.
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
34// An ActionProcessor keeps a queue of Actions and processes them in order.
35
36namespace chromeos_update_engine {
37
38class AbstractAction;
39class ActionProcessorDelegate;
40
41class ActionProcessor {
42 public:
43 ActionProcessor();
44
Darin Petkovf42cc1c2010-09-01 09:03:02 -070045 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000046
47 // Starts processing the first Action in the queue. If there's a delegate,
48 // when all processing is complete, ProcessingDone() will be called on the
49 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070050 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000051
52 // Aborts processing. If an Action is running, it will have
53 // TerminateProcessing() called on it. The Action that was running
54 // will be lost and must be re-enqueued if this Processor is to use it.
55 void StopProcessing();
56
57 // Returns true iff an Action is currently processing.
Alex Vakulenko88b591f2014-08-28 16:48:57 -070058 bool IsRunning() const { return nullptr != current_action_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
60 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070061 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000062
Alex Vakulenko88b591f2014-08-28 16:48:57 -070063 // Sets/gets the current delegate. Set to null to remove a delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070064 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000065 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.
David Zeuthena99981f2013-04-29 13:42:47 -070075 void ActionComplete(AbstractAction* actionptr, ErrorCode 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
Alex Vakulenko072359c2014-07-18 11:41:07 -070082 // A pointer to the currently processing Action, if any.
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 AbstractAction* current_action_;
84
Alex Vakulenko88b591f2014-08-28 16:48:57 -070085 // A pointer to the delegate, or null if none.
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 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:
Alex Deymoe8948702014-11-11 21:44:45 -080095 virtual ~ActionProcessorDelegate() = default;
96
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -070098 // to the ActionProcessor is passed. |code| is set to the exit code of the
99 // last completed action.
100 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700101 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102
103 // Called when processing has stopped. Does not mean that all Actions have
104 // completed. If/when all Actions complete, ProcessingDone() will be called.
105 virtual void ProcessingStopped(const ActionProcessor* processor) {}
106
107 // Called whenever an action has finished processing, either successfully
108 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000109 virtual void ActionCompleted(ActionProcessor* processor,
110 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700111 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000112};
113
114} // namespace chromeos_update_engine
115
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700116#endif // UPDATE_ENGINE_ACTION_PROCESSOR_H_