blob: f651b8e3277a1db66866d9ce8c60fedca38da682 [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
rspangler@google.com49fdf182009-10-10 00:57:34 +000029// The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
30// is based on the KSAction* classes from the Google Update Engine code at
31// http://code.google.com/p/update-engine/ . The author of this file sends
32// a big thanks to that team for their high quality design, implementation,
33// and documentation.
34
Alex Vakulenko072359c2014-07-18 11:41:07 -070035// See action.h for an overview of this class and other Action* classes.
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
37// An ActionProcessor keeps a queue of Actions and processes them in order.
38
39namespace chromeos_update_engine {
40
41class AbstractAction;
42class ActionProcessorDelegate;
43
44class ActionProcessor {
45 public:
Alex Deymo14fd1ec2016-02-24 22:03:57 -080046 ActionProcessor() = default;
rspangler@google.com49fdf182009-10-10 00:57:34 +000047
Darin Petkovf42cc1c2010-09-01 09:03:02 -070048 virtual ~ActionProcessor();
rspangler@google.com49fdf182009-10-10 00:57:34 +000049
50 // Starts processing the first Action in the queue. If there's a delegate,
51 // when all processing is complete, ProcessingDone() will be called on the
52 // delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070053 virtual void StartProcessing();
rspangler@google.com49fdf182009-10-10 00:57:34 +000054
55 // Aborts processing. If an Action is running, it will have
Alex Deymof2858572016-02-25 11:20:13 -080056 // TerminateProcessing() called on it. The Action that was running and all the
57 // remaining actions will be lost and must be re-enqueued if this Processor is
58 // to use it.
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 void StopProcessing();
60
Alex Deymo14fd1ec2016-02-24 22:03:57 -080061 // Suspend the processing. If an Action is running, it will have the
62 // SuspendProcessing() called on it, and it should suspend operations until
63 // ResumeProcessing() is called on this class to continue. While suspended,
64 // no new actions will be started. Calling SuspendProcessing while the
65 // processing is suspended or not running this method performs no action.
66 void SuspendProcessing();
67
68 // Resume the suspended processing. If the ActionProcessor is not suspended
Alex Deymof2858572016-02-25 11:20:13 -080069 // or not running in the first place this method performs no action.
Alex Deymo14fd1ec2016-02-24 22:03:57 -080070 void ResumeProcessing();
71
72 // Returns true iff the processing was started but not yet completed nor
73 // stopped.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070074 bool IsRunning() const;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075
76 // Adds another Action to the end of the queue.
Amin Hassanid3f4bea2018-04-30 14:52:40 -070077 virtual void EnqueueAction(std::unique_ptr<AbstractAction> action);
rspangler@google.com49fdf182009-10-10 00:57:34 +000078
Alex Vakulenko88b591f2014-08-28 16:48:57 -070079 // Sets/gets the current delegate. Set to null to remove a delegate.
Darin Petkovf42cc1c2010-09-01 09:03:02 -070080 ActionProcessorDelegate* delegate() const { return delegate_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 void set_delegate(ActionProcessorDelegate *delegate) {
82 delegate_ = delegate;
83 }
84
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.
David Zeuthena99981f2013-04-29 13:42:47 -070092 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_