blob: 5a4286fd1d9d78c54ddaa1062e4372f9b83a8b52 [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>
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/error_code.h"
David Zeuthena99981f2013-04-29 13:42:47 -070027
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070028#include <gtest/gtest_prod.h>
29
rspangler@google.com49fdf182009-10-10 00:57:34 +000030// The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
31// is based on the KSAction* classes from the Google Update Engine code at
32// http://code.google.com/p/update-engine/ . The author of this file sends
33// a big thanks to that team for their high quality design, implementation,
34// and documentation.
35
Alex Vakulenko072359c2014-07-18 11:41:07 -070036// See action.h for an overview of this class and other Action* classes.
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
38// An ActionProcessor keeps a queue of Actions and processes them in order.
39
40namespace chromeos_update_engine {
41
42class AbstractAction;
43class ActionProcessorDelegate;
44
45class ActionProcessor {
46 public:
Alex Deymo14fd1ec2016-02-24 22:03:57 -080047 ActionProcessor() = default;
rspangler@google.com49fdf182009-10-10 00:57:34 +000048
Darin Petkovf42cc1c2010-09-01 09:03:02 -070049 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000050
51 // Starts processing the first Action in the queue. If there's a delegate,
52 // when all processing is complete, ProcessingDone() will be called on the
53 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070054 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000055
56 // Aborts processing. If an Action is running, it will have
Alex Deymof2858572016-02-25 11:20:13 -080057 // TerminateProcessing() called on it. The Action that was running and all the
58 // remaining actions will be lost and must be re-enqueued if this Processor is
59 // to use it.
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 void StopProcessing();
61
Alex Deymo14fd1ec2016-02-24 22:03:57 -080062 // Suspend the processing. If an Action is running, it will have the
63 // SuspendProcessing() called on it, and it should suspend operations until
64 // ResumeProcessing() is called on this class to continue. While suspended,
65 // no new actions will be started. Calling SuspendProcessing while the
66 // processing is suspended or not running this method performs no action.
67 void SuspendProcessing();
68
69 // Resume the suspended processing. If the ActionProcessor is not suspended
Alex Deymof2858572016-02-25 11:20:13 -080070 // or not running in the first place this method performs no action.
Alex Deymo14fd1ec2016-02-24 22:03:57 -080071 void ResumeProcessing();
72
73 // Returns true iff the processing was started but not yet completed nor
74 // stopped.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070075 bool IsRunning() const;
rspangler@google.com49fdf182009-10-10 00:57:34 +000076
77 // Adds another Action to the end of the queue.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070078 virtual void EnqueueAction(std::unique_ptr<AbstractAction> action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000079
Alex Vakulenko88b591f2014-08-28 16:48:57 -070080 // Sets/gets the current delegate. Set to null to remove a delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070081 ActionProcessorDelegate* delegate() const { return delegate_; }
Amin Hassanib2689592019-01-13 17:04:28 -080082 void set_delegate(ActionProcessorDelegate* delegate) { delegate_ = delegate; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000083
84 // Returns a pointer to the current Action that's processing.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070085 AbstractAction* current_action() const { return current_action_.get(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000086
87 // Called by an action to notify processor that it's done. Caller passes self.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070088 // But this call deletes the action if there no other object has a reference
89 // to it, so in that case, the caller should not try to access any of its
90 // member variables after this call.
Kelvin Zhang9dd93052020-07-21 17:31:19 -040091 virtual void ActionComplete(AbstractAction* actionptr, ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000092
93 private:
Amin Hassanid3f4bea2018-04-30 14:52:40 -070094 FRIEND_TEST(ActionProcessorTest, ChainActionsTest);
95
Alex Deymo14fd1ec2016-02-24 22:03:57 -080096 // Continue processing actions (if any) after the last action terminated with
97 // the passed error code. If there are no more actions to process, the
98 // processing will terminate.
99 void StartNextActionOrFinish(ErrorCode code);
100
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 // Actions that have not yet begun processing, in the order in which
102 // they'll be processed.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700103 std::deque<std::unique_ptr<AbstractAction>> actions_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104
Alex Vakulenko072359c2014-07-18 11:41:07 -0700105 // A pointer to the currently processing Action, if any.
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700106 std::unique_ptr<AbstractAction> current_action_;
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800107
108 // The ErrorCode reported by an action that was suspended but finished while
109 // being suspended. This error code is stored here to be reported back to the
110 // delegate once the processor is resumed.
111 ErrorCode suspended_error_code_{ErrorCode::kSuccess};
112
113 // Whether the action processor is or should be suspended.
114 bool suspended_{false};
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700116 // A pointer to the delegate, or null if none.
Alex Deymo14fd1ec2016-02-24 22:03:57 -0800117 ActionProcessorDelegate* delegate_{nullptr};
118
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 DISALLOW_COPY_AND_ASSIGN(ActionProcessor);
120};
121
122// A delegate object can be used to be notified of events that happen
123// in an ActionProcessor. An instance of this class can be passed to an
124// ActionProcessor to register itself.
125class ActionProcessorDelegate {
126 public:
Alex Deymoe8948702014-11-11 21:44:45 -0800127 virtual ~ActionProcessorDelegate() = default;
128
rspangler@google.com49fdf182009-10-10 00:57:34 +0000129 // Called when all processing in an ActionProcessor has completed. A pointer
Darin Petkovc1a8b422010-07-19 11:34:49 -0700130 // to the ActionProcessor is passed. |code| is set to the exit code of the
131 // last completed action.
Kelvin Zhang7608cd42021-12-08 13:02:25 -0800132 virtual void ProcessingDone([[maybe_unused]] const ActionProcessor* processor,
133 [[maybe_unused]] ErrorCode code) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134
135 // Called when processing has stopped. Does not mean that all Actions have
136 // completed. If/when all Actions complete, ProcessingDone() will be called.
Kelvin Zhang7608cd42021-12-08 13:02:25 -0800137 virtual void ProcessingStopped(
138 [[maybe_unused]] const ActionProcessor* processor) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000139
140 // Called whenever an action has finished processing, either successfully
141 // or otherwise.
Kelvin Zhang7608cd42021-12-08 13:02:25 -0800142 virtual void ActionCompleted([[maybe_unused]] ActionProcessor* processor,
143 [[maybe_unused]] AbstractAction* action,
144 [[maybe_unused]] 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_