blob: c9c179ea38b17cde0a9822b8414a5597684ecbe0 [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
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_
18#define UPDATE_ENGINE_COMMON_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>
Alex Deymo14fd1ec2016-02-24 22:03:57 -080023#include <brillo/errors/error.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/error_code.h"
David Zeuthena99981f2013-04-29 13:42:47 -070026
rspangler@google.com49fdf182009-10-10 00:57:34 +000027// The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
28// is based on the KSAction* classes from the Google Update Engine code at
29// http://code.google.com/p/update-engine/ . The author of this file sends
30// a big thanks to that team for their high quality design, implementation,
31// and documentation.
32
Alex Vakulenko072359c2014-07-18 11:41:07 -070033// See action.h for an overview of this class and other Action* classes.
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
35// An ActionProcessor keeps a queue of Actions and processes them in order.
36
37namespace chromeos_update_engine {
38
39class AbstractAction;
40class ActionProcessorDelegate;
41
42class ActionProcessor {
43 public:
Alex Deymo14fd1ec2016-02-24 22:03:57 -080044 ActionProcessor() = default;
rspangler@google.com49fdf182009-10-10 00:57:34 +000045
Darin Petkovf42cc1c2010-09-01 09:03:02 -070046 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
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.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070051 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000052
53 // Aborts processing. If an Action is running, it will have
Alex Deymof2858572016-02-25 11:20:13 -080054 // TerminateProcessing() called on it. The Action that was running and all the
55 // remaining actions will be lost and must be re-enqueued if this Processor is
56 // to use it.
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 void StopProcessing();
58
Alex Deymo14fd1ec2016-02-24 22:03:57 -080059 // Suspend the processing. If an Action is running, it will have the
60 // SuspendProcessing() called on it, and it should suspend operations until
61 // ResumeProcessing() is called on this class to continue. While suspended,
62 // no new actions will be started. Calling SuspendProcessing while the
63 // processing is suspended or not running this method performs no action.
64 void SuspendProcessing();
65
66 // Resume the suspended processing. If the ActionProcessor is not suspended
Alex Deymof2858572016-02-25 11:20:13 -080067 // or not running in the first place this method performs no action.
Alex Deymo14fd1ec2016-02-24 22:03:57 -080068 void ResumeProcessing();
69
70 // Returns true iff the processing was started but not yet completed nor
71 // stopped.
72 bool IsRunning() const { return current_action_ != nullptr || suspended_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000073
74 // Adds another Action to the end of the queue.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070075 virtual void EnqueueAction(AbstractAction* action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000076
Alex Vakulenko88b591f2014-08-28 16:48:57 -070077 // Sets/gets the current delegate. Set to null to remove a delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070078 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 void set_delegate(ActionProcessorDelegate *delegate) {
80 delegate_ = delegate;
81 }
82
83 // Returns a pointer to the current Action that's processing.
84 AbstractAction* current_action() const {
85 return current_action_;
86 }
87
88 // Called by an action to notify processor that it's done. Caller passes self.
David Zeuthena99981f2013-04-29 13:42:47 -070089 void ActionComplete(AbstractAction* actionptr, ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000090
91 private:
Alex Deymo14fd1ec2016-02-24 22:03:57 -080092 // Continue processing actions (if any) after the last action terminated with
93 // the passed error code. If there are no more actions to process, the
94 // processing will terminate.
95 void StartNextActionOrFinish(ErrorCode code);
96
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 // Actions that have not yet begun processing, in the order in which
98 // they'll be processed.
99 std::deque<AbstractAction*> actions_;
100
Alex Vakulenko072359c2014-07-18 11:41:07 -0700101 // A pointer to the currently processing Action, if any.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800102 AbstractAction* current_action_{nullptr};
103
104 // The ErrorCode reported by an action that was suspended but finished while
105 // being suspended. This error code is stored here to be reported back to the
106 // delegate once the processor is resumed.
107 ErrorCode suspended_error_code_{ErrorCode::kSuccess};
108
109 // Whether the action processor is or should be suspended.
110 bool suspended_{false};
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700112 // A pointer to the delegate, or null if none.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800113 ActionProcessorDelegate* delegate_{nullptr};
114
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
116};
117
118// A delegate object can be used to be notified of events that happen
119// in an ActionProcessor. An instance of this class can be passed to an
120// ActionProcessor to register itself.
121class ActionProcessorDelegate {
122 public:
Alex Deymoe8948702014-11-11 21:44:45 -0800123 virtual ~ActionProcessorDelegate() = default;
124
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700126 // to the ActionProcessor is passed. |code| is set to the exit code of the
127 // last completed action.
128 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700129 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130
131 // Called when processing has stopped. Does not mean that all Actions have
132 // completed. If/when all Actions complete, ProcessingDone() will be called.
133 virtual void ProcessingStopped(const ActionProcessor* processor) {}
134
135 // Called whenever an action has finished processing, either successfully
136 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000137 virtual void ActionCompleted(ActionProcessor* processor,
138 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700139 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140};
141
142} // namespace chromeos_update_engine
143
Alex Deymo39910dc2015-11-09 17:04:30 -0800144#endif // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_