| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
| 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
| 7 | 7 |
| 8 namespace service_manager { | 8 namespace service_manager { |
| 9 | 9 |
| 10 class InterfaceRegistry; | 10 class InterfaceRegistry; |
| 11 class ServiceContext; | 11 class ServiceContext; |
| 12 struct ServiceInfo; | 12 struct ServiceInfo; |
| 13 | 13 |
| 14 // The primary contract between a Service and the Service Manager, receiving | 14 // The primary contract between a Service and the Service Manager, receiving |
| 15 // lifecycle notifications and connection requests. | 15 // lifecycle notifications and connection requests. Every Service must minimally |
| 16 // implement OnConnect(). |
| 16 class Service { | 17 class Service { |
| 17 public: | 18 public: |
| 18 Service(); | |
| 19 virtual ~Service(); | 19 virtual ~Service(); |
| 20 | 20 |
| 21 // Called exactly once, when a bidirectional connection with the Service | 21 // Called once a bidirectional connection with the Service Manager has been |
| 22 // Manager has been established. No calls to OnConnect() will be received | 22 // established. |
| 23 // before this. | 23 // |
| 24 virtual void OnStart(); | 24 // |context| is the ServiceContext for this instance of the service. It's |
| 25 // guaranteed to outlive the Service instance and therefore may be retained by |
| 26 // it. |
| 27 // |
| 28 // Use the context to retrieve information about the service instance, make |
| 29 // outgoing service connections, and issue other requests to the Service |
| 30 // Manager on behalf of this instance. |
| 31 // |
| 32 // Called exactly once before any calls to OnConnect(). |
| 33 virtual void OnStart(ServiceContext* context); |
| 25 | 34 |
| 26 // Called each time a connection to this service is brokered by the Service | 35 // Called each time a connection to this service is brokered by the Service |
| 27 // Manager. Implement this to expose interfaces to other services. | 36 // Manager. Implement this to expose interfaces to other services. |
| 28 // | 37 // |
| 29 // Return true if the connection should succeed or false if the connection | 38 // Return true if the connection should succeed or false if the connection |
| 30 // should be rejected. | 39 // should be rejected. |
| 31 // | |
| 32 // The default implementation returns false. | |
| 33 virtual bool OnConnect(const ServiceInfo& remote_info, | 40 virtual bool OnConnect(const ServiceInfo& remote_info, |
| 34 InterfaceRegistry* registry); | 41 InterfaceRegistry* registry) = 0; |
| 35 | 42 |
| 36 // Called when the Service Manager has stopped tracking this instance. The | 43 // Called when the Service Manager has stopped tracking this instance. The |
| 37 // service should use this as a signal to shut down, and in fact its process | 44 // service should use this as a signal to shut down, and in fact its process |
| 38 // may be reaped shortly afterward if applicable. | 45 // may be reaped shortly afterward if applicable. |
| 39 // | 46 // |
| 40 // Return true from this method to tell the ServiceContext to signal its | 47 // Return true from this method to tell the ServiceContext to signal its |
| 41 // shutdown extenrally (i.e. to invoke it's "connection lost" closure if set), | 48 // shutdown extenrally (i.e. to invoke it's "connection lost" closure if set), |
| 42 // or return false to defer the signal. If deferred, the Service should | 49 // or return false to defer the signal. If deferred, the Service should |
| 43 // explicitly call QuitNow() on the ServiceContext when it's ready to be | 50 // explicitly call QuitNow() on the ServiceContext when it's ready to be |
| 44 // torn down. | 51 // torn down. |
| 45 // | 52 // |
| 46 // The default implementation returns true. | 53 // The default implementation returns true. |
| 47 // | 54 // |
| 48 // While it's possible for this to be invoked before either OnStart() or | 55 // While it's possible for this to be invoked before either OnStart() or |
| 49 // OnConnect() is invoked, neither will be invoked at any point after this | 56 // OnConnect() is invoked, neither will be invoked at any point after this |
| 50 // OnStop(). | 57 // OnStop(). |
| 51 virtual bool OnStop(); | 58 virtual bool OnStop(); |
| 52 | |
| 53 protected: | |
| 54 // Access the ServiceContext associated with this Service. Note that this is | |
| 55 // only valid to call during or after OnStart(), but never before! As such, | |
| 56 // it's always safe to call in OnStart() and OnConnect(), but should generally | |
| 57 // be avoided in OnStop(). | |
| 58 ServiceContext* context() const; | |
| 59 | |
| 60 private: | |
| 61 friend class ForwardingService; | |
| 62 friend class ServiceContext; | |
| 63 | |
| 64 // NOTE: This is guaranteed to be called before OnStart(). | |
| 65 void set_context(ServiceContext* context) { service_context_ = context; } | |
| 66 | |
| 67 ServiceContext* service_context_ = nullptr; | |
| 68 }; | |
| 69 | |
| 70 // TODO(rockot): Remove this. It's here to satisfy a few remaining use cases | |
| 71 // where a Service impl is owned by something other than its ServiceContext. | |
| 72 class ForwardingService : public Service { | |
| 73 public: | |
| 74 // |target| must outlive this object. | |
| 75 explicit ForwardingService(Service* target); | |
| 76 ~ForwardingService() override; | |
| 77 | |
| 78 private: | |
| 79 // Service: | |
| 80 void OnStart() override; | |
| 81 bool OnConnect(const ServiceInfo& remote_info, | |
| 82 InterfaceRegistry* registry) override; | |
| 83 bool OnStop() override; | |
| 84 | |
| 85 Service* const target_ = nullptr; | |
| 86 }; | 59 }; |
| 87 | 60 |
| 88 } // namespace service_manager | 61 } // namespace service_manager |
| 89 | 62 |
| 90 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 63 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
| OLD | NEW |