| Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 1 | // Copyright 2016 The Android Open Source Project | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | #ifndef LIBWEAVED_SERVICE_H_ | 
|  | 16 | #define LIBWEAVED_SERVICE_H_ | 
|  | 17 |  | 
|  | 18 | #include <memory> | 
|  | 19 | #include <string> | 
|  | 20 | #include <vector> | 
|  | 21 |  | 
|  | 22 | #include <base/callback.h> | 
|  | 23 | #include <base/compiler_specific.h> | 
|  | 24 | #include <base/macros.h> | 
|  | 25 | #include <brillo/errors/error.h> | 
|  | 26 | #include <libweaved/command.h> | 
|  | 27 | #include <libweaved/export.h> | 
|  | 28 |  | 
|  | 29 | namespace brillo { | 
|  | 30 | class MessageLoop; | 
|  | 31 | }  // namespace brillo | 
|  | 32 |  | 
|  | 33 | namespace weaved { | 
|  | 34 |  | 
|  | 35 | // A weaved service is an abstract interface representing an instance of weave | 
|  | 36 | // services for a particular client daemon. Apart from providing an API to | 
|  | 37 | // weaved process, it manages resources specific for an instance of the client. | 
|  | 38 | // For example, when a client exits, it removes any resources (e.g. components) | 
|  | 39 | // that were added by this client from the weaved's component tree. | 
|  | 40 | class LIBWEAVED_EXPORT Service { | 
|  | 41 | public: | 
|  | 42 | // Callback type for AddCommandHandler. | 
|  | 43 | using CommandHandlerCallback = | 
|  | 44 | base::Callback<void(std::unique_ptr<Command> command)>; | 
|  | 45 |  | 
|  | 46 | Service() = default; | 
|  | 47 | virtual ~Service() = default; | 
|  | 48 |  | 
|  | 49 | // Adds a new component instance to device. | 
|  | 50 | // |component| is a component name being added. | 
|  | 51 | // |traits| is a list of trait names this component supports. | 
|  | 52 | virtual bool AddComponent(const std::string& component, | 
|  | 53 | const std::vector<std::string>& traits, | 
|  | 54 | brillo::ErrorPtr* error) = 0; | 
|  | 55 |  | 
|  | 56 | // Sets handler for new commands added to the queue for a given |component|. | 
| Alex Vakulenko | c984451 | 2016-01-07 10:06:23 -0800 | [diff] [blame^] | 57 | // |command_name| is the name of the command to handle (e.g. "reboot"). | 
|  | 58 | // |trait_name| is the name of a trait the command belongs to (e.g. "base"). | 
| Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 59 | // Each command can have no more than one handler. | 
|  | 60 | virtual void AddCommandHandler(const std::string& component, | 
| Alex Vakulenko | c984451 | 2016-01-07 10:06:23 -0800 | [diff] [blame^] | 61 | const std::string& trait_name, | 
| Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 62 | const std::string& command_name, | 
|  | 63 | const CommandHandlerCallback& callback) = 0; | 
|  | 64 |  | 
|  | 65 | // Sets a number of state properties for a given |component|. | 
|  | 66 | // |dict| is a dictionary containing property-name/property-value pairs. | 
|  | 67 | virtual bool SetStateProperties(const std::string& component, | 
|  | 68 | const brillo::VariantDictionary& dict, | 
|  | 69 | brillo::ErrorPtr* error) = 0; | 
|  | 70 |  | 
|  | 71 | // Sets value of the single property. | 
| Alex Vakulenko | c984451 | 2016-01-07 10:06:23 -0800 | [diff] [blame^] | 72 | // |component| is a path to the component to set the property value to. | 
|  | 73 | // |trait_name| is the name of the trait this property is part of. | 
|  | 74 | // |property_name| is the property name. | 
| Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 75 | virtual bool SetStateProperty(const std::string& component, | 
| Alex Vakulenko | c984451 | 2016-01-07 10:06:23 -0800 | [diff] [blame^] | 76 | const std::string& trait_name, | 
|  | 77 | const std::string& property_name, | 
| Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 78 | const brillo::Any& value, | 
|  | 79 | brillo::ErrorPtr* error) = 0; | 
|  | 80 |  | 
|  | 81 | // Service creation functionality. | 
|  | 82 | // Subscription is a base class for an object responsible for life-time | 
|  | 83 | // management for the service. The service instance is kept alive for as long | 
|  | 84 | // as the service connection is alive. See comments for Service::Connect for | 
|  | 85 | // more details. | 
|  | 86 | class Subscription { | 
|  | 87 | public: | 
|  | 88 | virtual ~Subscription() = default; | 
|  | 89 |  | 
|  | 90 | protected: | 
|  | 91 | Subscription() = default; | 
|  | 92 |  | 
|  | 93 | private: | 
|  | 94 | DISALLOW_COPY_AND_ASSIGN(Subscription); | 
|  | 95 | }; | 
|  | 96 |  | 
|  | 97 | using ConnectionCallback = | 
|  | 98 | base::Callback<void(const std::weak_ptr<Service>& service)>; | 
|  | 99 |  | 
|  | 100 | // Creates an instance of weaved service asynchronously. This not only creates | 
|  | 101 | // the service class instance but also establishes an RPC connection to | 
|  | 102 | // weaved daemon. Upon connection having been established, a |callback| is | 
|  | 103 | // invoked and an instance of Service is passed to it as an argument. | 
|  | 104 | // The service instance provided to the callback is a weak pointer to the | 
|  | 105 | // actual service which may be destroyed at any time if RPC connection to | 
|  | 106 | // weaved is lost. If this happens, a connection is re-established and the | 
|  | 107 | // |callback| is called again with a new instance of the service. | 
|  | 108 | // Therefore, if locking the |service| produces nullptr, this means that the | 
|  | 109 | // service got disconnected, so no further action can be taken. Since the | 
|  | 110 | // |callback| will be invoked with the new service instance when connection | 
|  | 111 | // is re-established, it's a good idea to update the device state on each | 
|  | 112 | // invocation of the callback (along with registering command handlers, etc). | 
|  | 113 | // IMPORTANT: Keep the returned subscription object around for as long as the | 
|  | 114 | // service is needed. As soon as the subscription is destroyed, the connection | 
|  | 115 | // to weaved is terminated and the service instance is discarded. | 
|  | 116 | static std::unique_ptr<Subscription> Connect( | 
|  | 117 | brillo::MessageLoop* message_loop, | 
|  | 118 | const ConnectionCallback& callback) WARN_UNUSED_RESULT; | 
|  | 119 |  | 
|  | 120 | private: | 
|  | 121 | DISALLOW_COPY_AND_ASSIGN(Service); | 
|  | 122 | }; | 
|  | 123 |  | 
|  | 124 | }  // namespace weaved | 
|  | 125 |  | 
|  | 126 | #endif  // LIBWEAVED_SERVICE_H_ |