blob: 1a67c9930a84f12cb0c8245939a693c4f091e54f [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>
Amin Hassanid3f4bea2018-04-30 14:52:40 -070021#include <memory>
22#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Alex Deymo14fd1ec2016-02-24 22:03:57 -080025#include <brillo/errors/error.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/error_code.h"
David Zeuthena99981f2013-04-29 13:42:47 -070028
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070029#include <gtest/gtest_prod.h>
30
rspangler@google.com49fdf182009-10-10 00:57:34 +000031// The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
32// is based on the KSAction* classes from the Google Update Engine code at
33// http://code.google.com/p/update-engine/ . The author of this file sends
34// a big thanks to that team for their high quality design, implementation,
35// and documentation.
36
Alex Vakulenko072359c2014-07-18 11:41:07 -070037// See action.h for an overview of this class and other Action* classes.
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
39// An ActionProcessor keeps a queue of Actions and processes them in order.
40
41namespace chromeos_update_engine {
42
43class AbstractAction;
44class ActionProcessorDelegate;
45
46class ActionProcessor {
47 public:
Alex Deymo14fd1ec2016-02-24 22:03:57 -080048 ActionProcessor() = default;
rspangler@google.com49fdf182009-10-10 00:57:34 +000049
Darin Petkovf42cc1c2010-09-01 09:03:02 -070050 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000051
52 // Starts processing the first Action in the queue. If there's a delegate,
53 // when all processing is complete, ProcessingDone() will be called on the
54 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070055 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000056
57 // Aborts processing. If an Action is running, it will have
Alex Deymof2858572016-02-25 11:20:13 -080058 // TerminateProcessing() called on it. The Action that was running and all the
59 // remaining actions will be lost and must be re-enqueued if this Processor is
60 // to use it.
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 void StopProcessing();
62
Alex Deymo14fd1ec2016-02-24 22:03:57 -080063 // Suspend the processing. If an Action is running, it will have the
64 // SuspendProcessing() called on it, and it should suspend operations until
65 // ResumeProcessing() is called on this class to continue. While suspended,
66 // no new actions will be started. Calling SuspendProcessing while the
67 // processing is suspended or not running this method performs no action.
68 void SuspendProcessing();
69
70 // Resume the suspended processing. If the ActionProcessor is not suspended
Alex Deymof2858572016-02-25 11:20:13 -080071 // or not running in the first place this method performs no action.
Alex Deymo14fd1ec2016-02-24 22:03:57 -080072 void ResumeProcessing();
73
74 // Returns true iff the processing was started but not yet completed nor
75 // stopped.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070076 bool IsRunning() const;
rspangler@google.com49fdf182009-10-10 00:57:34 +000077
78 // Adds another Action to the end of the queue.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070079 virtual void EnqueueAction(std::unique_ptr<AbstractAction> action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000080
Alex Vakulenko88b591f2014-08-28 16:48:57 -070081 // Sets/gets the current delegate. Set to null to remove a delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070082 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 void set_delegate(ActionProcessorDelegate *delegate) {
84 delegate_ = delegate;
85 }
86
87 // Returns a pointer to the current Action that's processing.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070088 AbstractAction* current_action() const { return current_action_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000089
90 // Called by an action to notify processor that it's done. Caller passes self.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070091 // But this call deletes the action if there no other object has a reference
92 // to it, so in that case, the caller should not try to access any of its
93 // member variables after this call.
David Zeuthena99981f2013-04-29 13:42:47 -070094 void ActionComplete(AbstractAction* actionptr, ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000095
96 private:
Amin Hassanid3f4bea2018-04-30 14:52:40 -070097 FRIEND_TEST(ActionProcessorTest, ChainActionsTest);
98
Alex Deymo14fd1ec2016-02-24 22:03:57 -080099 // Continue processing actions (if any) after the last action terminated with
100 // the passed error code. If there are no more actions to process, the
101 // processing will terminate.
102 void StartNextActionOrFinish(ErrorCode code);
103
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104 // Actions that have not yet begun processing, in the order in which
105 // they'll be processed.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700106 std::deque<std::unique_ptr<AbstractAction>> actions_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107
Alex Vakulenko072359c2014-07-18 11:41:07 -0700108 // A pointer to the currently processing Action, if any.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700109 std::unique_ptr<AbstractAction> current_action_;
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800110
111 // The ErrorCode reported by an action that was suspended but finished while
112 // being suspended. This error code is stored here to be reported back to the
113 // delegate once the processor is resumed.
114 ErrorCode suspended_error_code_{ErrorCode::kSuccess};
115
116 // Whether the action processor is or should be suspended.
117 bool suspended_{false};
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700119 // A pointer to the delegate, or null if none.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800120 ActionProcessorDelegate* delegate_{nullptr};
121
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
123};
124
125// A delegate object can be used to be notified of events that happen
126// in an ActionProcessor. An instance of this class can be passed to an
127// ActionProcessor to register itself.
128class ActionProcessorDelegate {
129 public:
Alex Deymoe8948702014-11-11 21:44:45 -0800130 virtual ~ActionProcessorDelegate() = default;
131
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133 // to the ActionProcessor is passed. |code| is set to the exit code of the
134 // last completed action.
135 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700136 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137
138 // Called when processing has stopped. Does not mean that all Actions have
139 // completed. If/when all Actions complete, ProcessingDone() will be called.
140 virtual void ProcessingStopped(const ActionProcessor* processor) {}
141
142 // Called whenever an action has finished processing, either successfully
143 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000144 virtual void ActionCompleted(ActionProcessor* processor,
145 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700146 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000147};
148
149} // namespace chromeos_update_engine
150
Alex Deymo39910dc2015-11-09 17:04:30 -0800151#endif // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_