| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_H__ |
| 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_H__ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <tr1/memory> | 9 #include <tr1/memory> |
| 10 #include <iostream> | 10 #include <iostream> |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 // DownloadAction::InputObjectType. | 55 // DownloadAction::InputObjectType. |
| 56 // | 56 // |
| 57 // Each concrete Action class derives from Action<T>. This means that during | 57 // Each concrete Action class derives from Action<T>. This means that during |
| 58 // template instatiation of Action<T>, T is declared but not defined, which | 58 // template instatiation of Action<T>, T is declared but not defined, which |
| 59 // means that T::InputObjectType (and OutputObjectType) is not defined. | 59 // means that T::InputObjectType (and OutputObjectType) is not defined. |
| 60 // However, the traits class is constructed in such a way that it will be | 60 // However, the traits class is constructed in such a way that it will be |
| 61 // template instatiated first, so Action<T> *can* find the types it needs by | 61 // template instatiated first, so Action<T> *can* find the types it needs by |
| 62 // consulting ActionTraits<T>::InputObjectType (and OutputObjectType). | 62 // consulting ActionTraits<T>::InputObjectType (and OutputObjectType). |
| 63 // This is why the ActionTraits classes are needed. | 63 // This is why the ActionTraits classes are needed. |
| 64 | 64 |
| 65 using std::tr1::shared_ptr; | |
| 66 | |
| 67 namespace chromeos_update_engine { | 65 namespace chromeos_update_engine { |
| 68 | 66 |
| 69 // It is handy to have a non-templated base class of all Actions. | 67 // It is handy to have a non-templated base class of all Actions. |
| 70 class AbstractAction { | 68 class AbstractAction { |
| 71 public: | 69 public: |
| 72 AbstractAction() : processor_(NULL) {} | 70 AbstractAction() : processor_(NULL) {} |
| 73 | 71 |
| 74 // Begin performing the action. Since this code is asynchronous, when this | 72 // Begin performing the action. Since this code is asynchronous, when this |
| 75 // method returns, it means only that the action has started, not necessarily | 73 // method returns, it means only that the action has started, not necessarily |
| 76 // completed. However, it's acceptable for this method to perform the | 74 // completed. However, it's acceptable for this method to perform the |
| (...skipping 27 matching lines...) Expand all Loading... |
| 104 // there's any cleanup to do. There is no need to call | 102 // there's any cleanup to do. There is no need to call |
| 105 // ActionProcessor::ActionComplete() because the processor knows this | 103 // ActionProcessor::ActionComplete() because the processor knows this |
| 106 // action is terminating. | 104 // action is terminating. |
| 107 // Only the ActionProcessor should call this. | 105 // Only the ActionProcessor should call this. |
| 108 virtual void TerminateProcessing() {}; | 106 virtual void TerminateProcessing() {}; |
| 109 | 107 |
| 110 // These methods are useful for debugging. TODO(adlr): consider using | 108 // These methods are useful for debugging. TODO(adlr): consider using |
| 111 // std::type_info for this? | 109 // std::type_info for this? |
| 112 // Type() returns a string of the Action type. I.e., for DownloadAction, | 110 // Type() returns a string of the Action type. I.e., for DownloadAction, |
| 113 // Type() would return "DownloadAction". | 111 // Type() would return "DownloadAction". |
| 114 virtual string Type() const = 0; | 112 virtual std::string Type() const = 0; |
| 115 | 113 |
| 116 protected: | 114 protected: |
| 117 // A weak pointer to the processor that owns this Action. | 115 // A weak pointer to the processor that owns this Action. |
| 118 ActionProcessor* processor_; | 116 ActionProcessor* processor_; |
| 119 }; | 117 }; |
| 120 | 118 |
| 121 // Forward declare a couple classes we use. | 119 // Forward declare a couple classes we use. |
| 122 template<typename T> | 120 template<typename T> |
| 123 class ActionPipe; | 121 class ActionPipe; |
| 124 template<typename T> | 122 template<typename T> |
| 125 class ActionTraits; | 123 class ActionTraits; |
| 126 | 124 |
| 127 template<typename SubClass> | 125 template<typename SubClass> |
| 128 class Action : public AbstractAction { | 126 class Action : public AbstractAction { |
| 129 public: | 127 public: |
| 130 virtual ~Action() { | 128 virtual ~Action() { |
| 131 LOG(INFO) << "Action died"; | 129 LOG(INFO) << "Action died"; |
| 132 } | 130 } |
| 133 | 131 |
| 134 // Attaches an input pipe to this Action. This is optional; an Action | 132 // Attaches an input pipe to this Action. This is optional; an Action |
| 135 // doesn't need to have an input pipe. The input pipe must be of the type | 133 // doesn't need to have an input pipe. The input pipe must be of the type |
| 136 // of object that this class expects. | 134 // of object that this class expects. |
| 137 // This is generally called by ActionPipe::Bond() | 135 // This is generally called by ActionPipe::Bond() |
| 138 void set_in_pipe( | 136 void set_in_pipe( |
| 139 // this type is a fancy way of saying: a shared_ptr to an | 137 // this type is a fancy way of saying: a shared_ptr to an |
| 140 // ActionPipe<InputObjectType>. | 138 // ActionPipe<InputObjectType>. |
| 141 const shared_ptr<ActionPipe< | 139 const std::tr1::shared_ptr<ActionPipe< |
| 142 typename ActionTraits<SubClass>::InputObjectType> >& | 140 typename ActionTraits<SubClass>::InputObjectType> >& |
| 143 in_pipe) { | 141 in_pipe) { |
| 144 in_pipe_ = in_pipe; | 142 in_pipe_ = in_pipe; |
| 145 } | 143 } |
| 146 | 144 |
| 147 // Attaches an output pipe to this Action. This is optional; an Action | 145 // Attaches an output pipe to this Action. This is optional; an Action |
| 148 // doesn't need to have an output pipe. The output pipe must be of the type | 146 // doesn't need to have an output pipe. The output pipe must be of the type |
| 149 // of object that this class expects. | 147 // of object that this class expects. |
| 150 // This is generally called by ActionPipe::Bond() | 148 // This is generally called by ActionPipe::Bond() |
| 151 void set_out_pipe( | 149 void set_out_pipe( |
| 152 // this type is a fancy way of saying: a shared_ptr to an | 150 // this type is a fancy way of saying: a shared_ptr to an |
| 153 // ActionPipe<OutputObjectType>. | 151 // ActionPipe<OutputObjectType>. |
| 154 const shared_ptr<ActionPipe< | 152 const std::tr1::shared_ptr<ActionPipe< |
| 155 typename ActionTraits<SubClass>::OutputObjectType> >& | 153 typename ActionTraits<SubClass>::OutputObjectType> >& |
| 156 out_pipe) { | 154 out_pipe) { |
| 157 out_pipe_ = out_pipe; | 155 out_pipe_ = out_pipe; |
| 158 } | 156 } |
| 159 | 157 |
| 160 // Returns true iff there is an associated input pipe. If there's an input | 158 // Returns true iff there is an associated input pipe. If there's an input |
| 161 // pipe, there's an input object, but it may have been constructed with the | 159 // pipe, there's an input object, but it may have been constructed with the |
| 162 // default ctor if the previous action didn't call SetOutputObject(). | 160 // default ctor if the previous action didn't call SetOutputObject(). |
| 163 bool HasInputObject() const { return in_pipe_.get(); } | 161 bool HasInputObject() const { return in_pipe_.get(); } |
| 164 | 162 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 187 const typename ActionTraits<SubClass>::OutputObjectType& GetOutputObject() { | 185 const typename ActionTraits<SubClass>::OutputObjectType& GetOutputObject() { |
| 188 CHECK(HasOutputPipe()); | 186 CHECK(HasOutputPipe()); |
| 189 return out_pipe_->contents(); | 187 return out_pipe_->contents(); |
| 190 } | 188 } |
| 191 | 189 |
| 192 protected: | 190 protected: |
| 193 // We use a shared_ptr to the pipe. shared_ptr objects destroy what they | 191 // We use a shared_ptr to the pipe. shared_ptr objects destroy what they |
| 194 // point to when the last such shared_ptr object dies. We consider the | 192 // point to when the last such shared_ptr object dies. We consider the |
| 195 // Actions on either end of a pipe to "own" the pipe. When the last Action | 193 // Actions on either end of a pipe to "own" the pipe. When the last Action |
| 196 // of the two dies, the ActionPipe will die, too. | 194 // of the two dies, the ActionPipe will die, too. |
| 197 shared_ptr<ActionPipe<typename ActionTraits<SubClass>::InputObjectType> > | 195 std::tr1::shared_ptr< |
| 196 ActionPipe<typename ActionTraits<SubClass>::InputObjectType> > |
| 198 in_pipe_; | 197 in_pipe_; |
| 199 shared_ptr<ActionPipe<typename ActionTraits<SubClass>::OutputObjectType> > | 198 std::tr1::shared_ptr< |
| 199 ActionPipe<typename ActionTraits<SubClass>::OutputObjectType> > |
| 200 out_pipe_; | 200 out_pipe_; |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 }; // namespace chromeos_update_engine | 203 }; // namespace chromeos_update_engine |
| 204 | 204 |
| 205 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_H__ | 205 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_ACTION_H__ |
| OLD | NEW |