blob: ad98cc9ced7237f68791227110241ffc6cd20470 [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_; }
Amin Hassanib2689592019-01-13 17:04:28 -080083 void set_delegate(ActionProcessorDelegate* delegate) { delegate_ = delegate; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000084
85 // Returns a pointer to the current Action that's processing.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070086 AbstractAction* current_action() const { return current_action_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000087
88 // Called by an action to notify processor that it's done. Caller passes self.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070089 // But this call deletes the action if there no other object has a reference
90 // to it, so in that case, the caller should not try to access any of its
91 // member variables after this call.
Kelvin Zhang9dd93052020-07-21 17:31:19 -040092 virtual void ActionComplete(AbstractAction* actionptr, ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000093
94 private:
Amin Hassanid3f4bea2018-04-30 14:52:40 -070095 FRIEND_TEST(ActionProcessorTest, ChainActionsTest);
96
Alex Deymo14fd1ec2016-02-24 22:03:57 -080097 // Continue processing actions (if any) after the last action terminated with
98 // the passed error code. If there are no more actions to process, the
99 // processing will terminate.
100 void StartNextActionOrFinish(ErrorCode code);
101
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 // Actions that have not yet begun processing, in the order in which
103 // they'll be processed.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700104 std::deque<std::unique_ptr<AbstractAction>> actions_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000105
Alex Vakulenko072359c2014-07-18 11:41:07 -0700106 // A pointer to the currently processing Action, if any.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700107 std::unique_ptr<AbstractAction> current_action_;
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800108
109 // The ErrorCode reported by an action that was suspended but finished while
110 // being suspended. This error code is stored here to be reported back to the
111 // delegate once the processor is resumed.
112 ErrorCode suspended_error_code_{ErrorCode::kSuccess};
113
114 // Whether the action processor is or should be suspended.
115 bool suspended_{false};
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700117 // A pointer to the delegate, or null if none.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800118 ActionProcessorDelegate* delegate_{nullptr};
119
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
121};
122
123// A delegate object can be used to be notified of events that happen
124// in an ActionProcessor. An instance of this class can be passed to an
125// ActionProcessor to register itself.
126class ActionProcessorDelegate {
127 public:
Alex Deymoe8948702014-11-11 21:44:45 -0800128 virtual ~ActionProcessorDelegate() = default;
129
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700131 // to the ActionProcessor is passed. |code| is set to the exit code of the
132 // last completed action.
133 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700134 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135
136 // Called when processing has stopped. Does not mean that all Actions have
137 // completed. If/when all Actions complete, ProcessingDone() will be called.
138 virtual void ProcessingStopped(const ActionProcessor* processor) {}
139
140 // Called whenever an action has finished processing, either successfully
141 // or otherwise.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000142 virtual void ActionCompleted(ActionProcessor* processor,
143 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700144 ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145};
146
147} // namespace chromeos_update_engine
148
Alex Deymo39910dc2015-11-09 17:04:30 -0800149#endif // UPDATE_ENGINE_COMMON_ACTION_PROCESSOR_H_