1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2009 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
19*5a923131SAndroid Build Coastguard Worker
20*5a923131SAndroid Build Coastguard Worker #include <stdio.h>
21*5a923131SAndroid Build Coastguard Worker
22*5a923131SAndroid Build Coastguard Worker #include <map>
23*5a923131SAndroid Build Coastguard Worker #include <memory>
24*5a923131SAndroid Build Coastguard Worker #include <string>
25*5a923131SAndroid Build Coastguard Worker
26*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
27*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h>
28*5a923131SAndroid Build Coastguard Worker
29*5a923131SAndroid Build Coastguard Worker // The structure of these classes (Action, ActionPipe, ActionProcessor, etc.)
30*5a923131SAndroid Build Coastguard Worker // is based on the KSAction* classes from the Google Update Engine code at
31*5a923131SAndroid Build Coastguard Worker // http://code.google.com/p/update-engine/ . The author of this file sends
32*5a923131SAndroid Build Coastguard Worker // a big thanks to that team for their high quality design, implementation,
33*5a923131SAndroid Build Coastguard Worker // and documentation.
34*5a923131SAndroid Build Coastguard Worker
35*5a923131SAndroid Build Coastguard Worker // This class serves as a temporary holding area for an object passed out
36*5a923131SAndroid Build Coastguard Worker // from one Action and into another Action. It's templated so that it may
37*5a923131SAndroid Build Coastguard Worker // contain any type of object that an Action outputs/inputs. Actions
38*5a923131SAndroid Build Coastguard Worker // cannot be bonded (i.e., connected with a pipe) if their output/input
39*5a923131SAndroid Build Coastguard Worker // object types differ (a compiler error will result).
40*5a923131SAndroid Build Coastguard Worker //
41*5a923131SAndroid Build Coastguard Worker // An ActionPipe is generally created with the Bond() method and owned by
42*5a923131SAndroid Build Coastguard Worker // the two Action objects. a shared_ptr is used so that when the last Action
43*5a923131SAndroid Build Coastguard Worker // pointing to an ActionPipe dies, the ActionPipe dies, too.
44*5a923131SAndroid Build Coastguard Worker
45*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
46*5a923131SAndroid Build Coastguard Worker
47*5a923131SAndroid Build Coastguard Worker // Used by Actions an InputObjectType or OutputObjectType to specify that
48*5a923131SAndroid Build Coastguard Worker // for that type, no object is taken/given.
49*5a923131SAndroid Build Coastguard Worker class NoneType {};
50*5a923131SAndroid Build Coastguard Worker
51*5a923131SAndroid Build Coastguard Worker template <typename T>
52*5a923131SAndroid Build Coastguard Worker class Action;
53*5a923131SAndroid Build Coastguard Worker
54*5a923131SAndroid Build Coastguard Worker template <typename ObjectType>
55*5a923131SAndroid Build Coastguard Worker class ActionPipe {
56*5a923131SAndroid Build Coastguard Worker public:
~ActionPipe()57*5a923131SAndroid Build Coastguard Worker virtual ~ActionPipe() {}
58*5a923131SAndroid Build Coastguard Worker
59*5a923131SAndroid Build Coastguard Worker // This should be called by an Action on its input pipe.
60*5a923131SAndroid Build Coastguard Worker // Returns a reference to the stored object.
contents()61*5a923131SAndroid Build Coastguard Worker const ObjectType& contents() const { return contents_; }
62*5a923131SAndroid Build Coastguard Worker
63*5a923131SAndroid Build Coastguard Worker // This should be called by an Action on its output pipe.
64*5a923131SAndroid Build Coastguard Worker // Stores a copy of the passed object in this pipe.
set_contents(const ObjectType & contents)65*5a923131SAndroid Build Coastguard Worker void set_contents(const ObjectType& contents) { contents_ = contents; }
66*5a923131SAndroid Build Coastguard Worker
67*5a923131SAndroid Build Coastguard Worker // Bonds two Actions together with a new ActionPipe. The ActionPipe is
68*5a923131SAndroid Build Coastguard Worker // jointly owned by the two Actions and will be automatically destroyed
69*5a923131SAndroid Build Coastguard Worker // when the last Action is destroyed.
70*5a923131SAndroid Build Coastguard Worker template <typename FromAction, typename ToAction>
Bond(FromAction * from,ToAction * to)71*5a923131SAndroid Build Coastguard Worker static void Bond(FromAction* from, ToAction* to) {
72*5a923131SAndroid Build Coastguard Worker std::shared_ptr<ActionPipe<ObjectType>> pipe(new ActionPipe<ObjectType>);
73*5a923131SAndroid Build Coastguard Worker from->set_out_pipe(pipe);
74*5a923131SAndroid Build Coastguard Worker
75*5a923131SAndroid Build Coastguard Worker to->set_in_pipe(pipe); // If you get an error on this line, then
76*5a923131SAndroid Build Coastguard Worker // it most likely means that the From object's OutputObjectType is
77*5a923131SAndroid Build Coastguard Worker // different from the To object's InputObjectType.
78*5a923131SAndroid Build Coastguard Worker }
79*5a923131SAndroid Build Coastguard Worker
80*5a923131SAndroid Build Coastguard Worker private:
81*5a923131SAndroid Build Coastguard Worker ObjectType contents_;
82*5a923131SAndroid Build Coastguard Worker // Give unit test access
83*5a923131SAndroid Build Coastguard Worker friend class DownloadActionTest;
84*5a923131SAndroid Build Coastguard Worker
85*5a923131SAndroid Build Coastguard Worker // The ctor is private. This is because this class should construct itself
86*5a923131SAndroid Build Coastguard Worker // via the static Bond() method.
ActionPipe()87*5a923131SAndroid Build Coastguard Worker ActionPipe() {}
88*5a923131SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ActionPipe);
89*5a923131SAndroid Build Coastguard Worker };
90*5a923131SAndroid Build Coastguard Worker
91*5a923131SAndroid Build Coastguard Worker // Utility function
92*5a923131SAndroid Build Coastguard Worker template <typename FromAction, typename ToAction>
BondActions(FromAction * from,ToAction * to)93*5a923131SAndroid Build Coastguard Worker void BondActions(FromAction* from, ToAction* to) {
94*5a923131SAndroid Build Coastguard Worker static_assert(
95*5a923131SAndroid Build Coastguard Worker std::is_same<typename FromAction::OutputObjectType,
96*5a923131SAndroid Build Coastguard Worker typename ToAction::InputObjectType>::value,
97*5a923131SAndroid Build Coastguard Worker "FromAction::OutputObjectType doesn't match ToAction::InputObjectType");
98*5a923131SAndroid Build Coastguard Worker ActionPipe<typename FromAction::OutputObjectType>::Bond(from, to);
99*5a923131SAndroid Build Coastguard Worker }
100*5a923131SAndroid Build Coastguard Worker
101*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine
102*5a923131SAndroid Build Coastguard Worker
103*5a923131SAndroid Build Coastguard Worker #endif // UPDATE_ENGINE_COMMON_ACTION_PIPE_H_
104