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. Every Service must minimally | 15 // lifecycle notifications and connection requests. |
16 // implement OnConnect(). | |
17 class Service { | 16 class Service { |
18 public: | 17 public: |
| 18 Service(); |
19 virtual ~Service(); | 19 virtual ~Service(); |
20 | 20 |
21 // Called once a bidirectional connection with the Service Manager has been | 21 // Called exactly once, when a bidirectional connection with the Service |
22 // established. | 22 // Manager has been established. No calls to OnConnect() will be received |
23 // | 23 // before this. |
24 // |context| is the ServiceContext for this instance of the service. It's | 24 virtual void OnStart(); |
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); | |
34 | 25 |
35 // Called each time a connection to this service is brokered by the Service | 26 // Called each time a connection to this service is brokered by the Service |
36 // Manager. Implement this to expose interfaces to other services. | 27 // Manager. Implement this to expose interfaces to other services. |
37 // | 28 // |
38 // Return true if the connection should succeed or false if the connection | 29 // Return true if the connection should succeed or false if the connection |
39 // should be rejected. | 30 // should be rejected. |
| 31 // |
| 32 // The default implementation returns false. |
40 virtual bool OnConnect(const ServiceInfo& remote_info, | 33 virtual bool OnConnect(const ServiceInfo& remote_info, |
41 InterfaceRegistry* registry) = 0; | 34 InterfaceRegistry* registry); |
42 | 35 |
43 // Called when the Service Manager has stopped tracking this instance. The | 36 // Called when the Service Manager has stopped tracking this instance. The |
44 // service should use this as a signal to shut down, and in fact its process | 37 // service should use this as a signal to shut down, and in fact its process |
45 // may be reaped shortly afterward if applicable. | 38 // may be reaped shortly afterward if applicable. |
46 // | 39 // |
47 // Return true from this method to tell the ServiceContext to signal its | 40 // Return true from this method to tell the ServiceContext to signal its |
48 // shutdown extenrally (i.e. to invoke it's "connection lost" closure if set), | 41 // shutdown extenrally (i.e. to invoke it's "connection lost" closure if set), |
49 // or return false to defer the signal. If deferred, the Service should | 42 // or return false to defer the signal. If deferred, the Service should |
50 // explicitly call QuitNow() on the ServiceContext when it's ready to be | 43 // explicitly call QuitNow() on the ServiceContext when it's ready to be |
51 // torn down. | 44 // torn down. |
52 // | 45 // |
53 // The default implementation returns true. | 46 // The default implementation returns true. |
54 // | 47 // |
55 // While it's possible for this to be invoked before either OnStart() or | 48 // While it's possible for this to be invoked before either OnStart() or |
56 // OnConnect() is invoked, neither will be invoked at any point after this | 49 // OnConnect() is invoked, neither will be invoked at any point after this |
57 // OnStop(). | 50 // OnStop(). |
58 virtual bool OnStop(); | 51 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; |
59 }; | 86 }; |
60 | 87 |
61 } // namespace service_manager | 88 } // namespace service_manager |
62 | 89 |
63 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 90 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
OLD | NEW |