rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_ACTION_H_ |
| 6 | #define UPDATE_ENGINE_ACTION_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 8 | #include <stdio.h> |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 9 | |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 10 | #include <memory> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 11 | #include <string> |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 12 | |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 14 | #include <base/macros.h> |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 15 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 16 | #include "update_engine/action_pipe.h" |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 17 | #include "update_engine/action_processor.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | |
| 19 | // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.) |
| 20 | // is based on the KSAction* classes from the Google Update Engine code at |
| 21 | // http://code.google.com/p/update-engine/ . The author of this file sends |
| 22 | // a big thanks to that team for their high quality design, implementation, |
| 23 | // and documentation. |
| 24 | // |
| 25 | // Readers may want to consult this wiki page from the Update Engine site: |
| 26 | // http://code.google.com/p/update-engine/wiki/ActionProcessor |
| 27 | // Although it's referring to the Objective-C KSAction* classes, much |
| 28 | // applies here as well. |
| 29 | // |
| 30 | // How it works: |
| 31 | // |
| 32 | // First off, there is only one thread and all I/O should be asynchronous. |
Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 33 | // A message loop blocks whenever there is no work to be done. This happens |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | // where there is no CPU work to be done and no I/O ready to transfer in or |
Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 35 | // out. Two kinds of events can wake up the message loop: timer alarm or file |
| 36 | // descriptors. If either of these happens, the message loop finds out the owner |
| 37 | // of what fired and calls the appropriate code to handle it. As such, all the |
| 38 | // code in the Action* classes and the code that is calls is non-blocking. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | // |
| 40 | // An ActionProcessor contains a queue of Actions to perform. When |
| 41 | // ActionProcessor::StartProcessing() is called, it executes the first action. |
| 42 | // Each action tells the processor when it has completed, which causes the |
| 43 | // Processor to execute the next action. ActionProcessor may have a delegate |
| 44 | // (an object of type ActionProcessorDelegate). If it does, the delegate |
| 45 | // is called to be notified of events as they happen. |
| 46 | // |
| 47 | // ActionPipe classes |
| 48 | // |
| 49 | // See action_pipe.h |
| 50 | // |
| 51 | // ActionTraits |
| 52 | // |
| 53 | // We need to use an extra class ActionTraits. ActionTraits is a simple |
| 54 | // templated class that contains only two typedefs: OutputObjectType and |
| 55 | // InputObjectType. Each action class also has two typedefs of the same name |
| 56 | // that are of the same type. So, to get the input/output types of, e.g., the |
| 57 | // DownloadAction class, we look at the type of |
| 58 | // DownloadAction::InputObjectType. |
| 59 | // |
| 60 | // Each concrete Action class derives from Action<T>. This means that during |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 61 | // template instantiation of Action<T>, T is declared but not defined, which |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 62 | // means that T::InputObjectType (and OutputObjectType) is not defined. |
| 63 | // However, the traits class is constructed in such a way that it will be |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 64 | // template instantiated first, so Action<T> *can* find the types it needs by |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | // consulting ActionTraits<T>::InputObjectType (and OutputObjectType). |
| 66 | // This is why the ActionTraits classes are needed. |
| 67 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | namespace chromeos_update_engine { |
| 69 | |
| 70 | // It is handy to have a non-templated base class of all Actions. |
| 71 | class AbstractAction { |
| 72 | public: |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 73 | AbstractAction() : processor_(nullptr) {} |
Alex Deymo | e894870 | 2014-11-11 21:44:45 -0800 | [diff] [blame] | 74 | virtual ~AbstractAction() = default; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | |
| 76 | // Begin performing the action. Since this code is asynchronous, when this |
| 77 | // method returns, it means only that the action has started, not necessarily |
| 78 | // completed. However, it's acceptable for this method to perform the |
| 79 | // action synchronously; Action authors should understand the implications |
| 80 | // of synchronously performing, though, because this is a single-threaded |
| 81 | // app, the entire process will be blocked while the action performs. |
| 82 | // |
| 83 | // When the action is complete, it must call |
| 84 | // ActionProcessor::ActionComplete(this); to notify the processor that it's |
| 85 | // done. |
| 86 | virtual void PerformAction() = 0; |
| 87 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 88 | // Called on ActionProcess::ActionComplete() by ActionProcessor. |
| 89 | virtual void ActionCompleted(ErrorCode code) {} |
| 90 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 91 | // Called by the ActionProcessor to tell this Action which processor |
| 92 | // it belongs to. |
| 93 | void SetProcessor(ActionProcessor* processor) { |
| 94 | if (processor) |
| 95 | CHECK(!processor_); |
| 96 | else |
| 97 | CHECK(processor_); |
| 98 | processor_ = processor; |
| 99 | } |
| 100 | |
| 101 | // Returns true iff the action is the current action of its ActionProcessor. |
| 102 | bool IsRunning() const { |
| 103 | if (!processor_) |
| 104 | return false; |
| 105 | return processor_->current_action() == this; |
| 106 | } |
| 107 | |
| 108 | // Called on asynchronous actions if canceled. Actions may implement if |
| 109 | // there's any cleanup to do. There is no need to call |
| 110 | // ActionProcessor::ActionComplete() because the processor knows this |
| 111 | // action is terminating. |
| 112 | // Only the ActionProcessor should call this. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 113 | virtual void TerminateProcessing() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | |
| 115 | // These methods are useful for debugging. TODO(adlr): consider using |
| 116 | // std::type_info for this? |
| 117 | // Type() returns a string of the Action type. I.e., for DownloadAction, |
| 118 | // Type() would return "DownloadAction". |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 119 | virtual std::string Type() const = 0; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 120 | |
| 121 | protected: |
| 122 | // A weak pointer to the processor that owns this Action. |
| 123 | ActionProcessor* processor_; |
| 124 | }; |
| 125 | |
| 126 | // Forward declare a couple classes we use. |
| 127 | template<typename T> |
| 128 | class ActionPipe; |
| 129 | template<typename T> |
| 130 | class ActionTraits; |
| 131 | |
| 132 | template<typename SubClass> |
| 133 | class Action : public AbstractAction { |
| 134 | public: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 135 | ~Action() override {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 136 | |
| 137 | // Attaches an input pipe to this Action. This is optional; an Action |
| 138 | // doesn't need to have an input pipe. The input pipe must be of the type |
| 139 | // of object that this class expects. |
| 140 | // This is generally called by ActionPipe::Bond() |
| 141 | void set_in_pipe( |
| 142 | // this type is a fancy way of saying: a shared_ptr to an |
| 143 | // ActionPipe<InputObjectType>. |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 144 | const std::shared_ptr<ActionPipe< |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 145 | typename ActionTraits<SubClass>::InputObjectType>>& in_pipe) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | in_pipe_ = in_pipe; |
| 147 | } |
| 148 | |
| 149 | // Attaches an output pipe to this Action. This is optional; an Action |
| 150 | // doesn't need to have an output pipe. The output pipe must be of the type |
| 151 | // of object that this class expects. |
| 152 | // This is generally called by ActionPipe::Bond() |
| 153 | void set_out_pipe( |
| 154 | // this type is a fancy way of saying: a shared_ptr to an |
| 155 | // ActionPipe<OutputObjectType>. |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 156 | const std::shared_ptr<ActionPipe< |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 157 | typename ActionTraits<SubClass>::OutputObjectType>>& out_pipe) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 158 | out_pipe_ = out_pipe; |
| 159 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 160 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 161 | // Returns true iff there is an associated input pipe. If there's an input |
| 162 | // pipe, there's an input object, but it may have been constructed with the |
| 163 | // default ctor if the previous action didn't call SetOutputObject(). |
| 164 | bool HasInputObject() const { return in_pipe_.get(); } |
| 165 | |
| 166 | // returns a const reference to the object in the input pipe. |
| 167 | const typename ActionTraits<SubClass>::InputObjectType& GetInputObject() |
| 168 | const { |
| 169 | CHECK(HasInputObject()); |
| 170 | return in_pipe_->contents(); |
| 171 | } |
| 172 | |
| 173 | // Returns true iff there's an output pipe. |
| 174 | bool HasOutputPipe() const { |
| 175 | return out_pipe_.get(); |
| 176 | } |
| 177 | |
| 178 | // Copies the object passed into the output pipe. It will be accessible to |
| 179 | // the next Action via that action's input pipe (which is the same as this |
| 180 | // Action's output pipe). |
| 181 | void SetOutputObject( |
| 182 | const typename ActionTraits<SubClass>::OutputObjectType& out_obj) { |
| 183 | CHECK(HasOutputPipe()); |
| 184 | out_pipe_->set_contents(out_obj); |
| 185 | } |
| 186 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 187 | // Returns a reference to the object sitting in the output pipe. |
| 188 | const typename ActionTraits<SubClass>::OutputObjectType& GetOutputObject() { |
| 189 | CHECK(HasOutputPipe()); |
| 190 | return out_pipe_->contents(); |
| 191 | } |
| 192 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 193 | protected: |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 194 | // We use a shared_ptr to the pipe. shared_ptr objects destroy what they |
| 195 | // point to when the last such shared_ptr object dies. We consider the |
| 196 | // Actions on either end of a pipe to "own" the pipe. When the last Action |
| 197 | // of the two dies, the ActionPipe will die, too. |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 198 | std::shared_ptr<ActionPipe<typename ActionTraits<SubClass>::InputObjectType>> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 199 | in_pipe_; |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 200 | std::shared_ptr<ActionPipe<typename ActionTraits<SubClass>::OutputObjectType>> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 201 | out_pipe_; |
| 202 | }; |
| 203 | |
| 204 | }; // namespace chromeos_update_engine |
| 205 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 206 | #endif // UPDATE_ENGINE_ACTION_H_ |