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 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "mojo/public/cpp/system/message_pipe.h" | 11 #include "mojo/public/cpp/system/message_pipe.h" |
12 | 12 |
13 namespace service_manager { | 13 namespace service_manager { |
14 | 14 |
15 class InterfaceRegistry; | 15 class InterfaceRegistry; |
16 class ServiceContext; | 16 class ServiceContext; |
17 struct ServiceInfo; | 17 struct ServiceInfo; |
18 | 18 |
19 // The primary contract between a Service and the Service Manager, receiving | 19 // The primary contract between a Service and the Service Manager, receiving |
20 // lifecycle notifications and connection requests. | 20 // lifecycle notifications and connection requests. |
21 class Service { | 21 class Service { |
22 public: | 22 public: |
23 Service(); | 23 Service(); |
24 virtual ~Service(); | 24 virtual ~Service(); |
25 | 25 |
26 // Called exactly once, when a bidirectional connection with the Service | 26 // Called exactly once when a bidirectional connection with the Service |
27 // Manager has been established. No calls to OnConnect() will be received | 27 // Manager has been established. No calls to OnConnect() or OnBindInterface() |
28 // before this. | 28 // will be made before this. |
29 virtual void OnStart(); | 29 virtual void OnStart(); |
30 | 30 |
31 // Called each time a connection to this service is brokered by the Service | 31 // Called each time a connection to this service is brokered by the Service |
32 // Manager. Implement this to expose interfaces to other services. | 32 // Manager. Implement this to expose interfaces to other services. |
33 // | 33 // |
34 // Return true if the connection should succeed or false if the connection | 34 // Return true if the connection should succeed or false if the connection |
35 // should be rejected. | 35 // should be rejected. |
36 // | 36 // |
37 // The default implementation returns false. | 37 // The default implementation returns false. |
38 virtual bool OnConnect(const ServiceInfo& remote_info, | 38 virtual bool OnConnect(const ServiceInfo& remote_info, |
39 InterfaceRegistry* registry); | 39 InterfaceRegistry* registry); |
40 | 40 |
41 // Called when the service identified by |source_info| requests this service | 41 // Called when the service identified by |source_info| requests this service |
42 // bind a request for |interface_name|. If this method has been called, the | 42 // bind a request for |interface_name|. If this method has been called, the |
43 // service manager has already determined that policy permits this interface | 43 // service manager has already determined that policy permits this interface |
44 // to be bound, so the implementation of this method can trust that it should | 44 // to be bound, so the implementation of this method can trust that it should |
45 // just blindly bind it under most conditions. | 45 // just blindly bind it under most conditions. |
46 virtual void OnBindInterface(const ServiceInfo& source_info, | 46 virtual void OnBindInterface(const ServiceInfo& source_info, |
47 const std::string& interface_name, | 47 const std::string& interface_name, |
48 mojo::ScopedMessagePipeHandle interface_pipe); | 48 mojo::ScopedMessagePipeHandle interface_pipe); |
49 | 49 |
50 // Called when the Service Manager has stopped tracking this instance. The | 50 // Called when the Service Manager has stopped tracking this instance. The |
51 // service should use this as a signal to shut down, and in fact its process | 51 // service should use this as a signal to shut down, and in fact its process |
52 // may be reaped shortly afterward if applicable. | 52 // may be reaped shortly afterward if applicable. |
53 // | 53 // |
54 // Return true from this method to tell the ServiceContext to signal its | 54 // If this returns |true| then QuitNow() will be invoked immediately upon |
55 // shutdown externally (i.e. to invoke its "connection lost" closure if set), | 55 // return to the ServiceContext. Otherwise the Service is responsible for |
56 // or return false to defer the signal. If deferred, the Service should | 56 // eventually calling QuitNow(). |
57 // explicitly call QuitNow() on the ServiceContext when it's ready to be | |
58 // torn down. | |
59 // | 57 // |
60 // The default implementation returns true. | 58 // The default implementation returns |true|. |
61 // | 59 // |
62 // While it's possible for this to be invoked before either OnStart() or | 60 // NOTE: This may be called at any time, and once it's been called, none of |
63 // OnConnect() is invoked, neither will be invoked at any point after this | 61 // the other public Service methods will be invoked by the ServiceContext. |
64 // OnStop(). | 62 virtual bool OnServiceManagerConnectionLost(); |
65 virtual bool OnStop(); | |
66 | 63 |
67 protected: | 64 protected: |
68 // Access the ServiceContext associated with this Service. Note that this is | 65 // Accesses the ServiceContext associated with this Service. Note that this is |
69 // only valid to call during or after OnStart(), but never before! As such, | 66 // only valid AFTER the Service's constructor has run. |
70 // it's always safe to call in OnStart() and OnConnect(), but should generally | |
71 // be avoided in OnStop(). | |
72 ServiceContext* context() const; | 67 ServiceContext* context() const; |
73 | 68 |
74 private: | 69 private: |
75 friend class ForwardingService; | 70 friend class ForwardingService; |
76 friend class ServiceContext; | 71 friend class ServiceContext; |
77 | 72 |
78 // NOTE: This is guaranteed to be called before OnStart(). | 73 // NOTE: This MUST be called before any public Service methods. ServiceContext |
79 void set_context(ServiceContext* context) { service_context_ = context; } | 74 // satisfies this guarantee for any Service instance it owns. |
| 75 virtual void SetContext(ServiceContext* context); |
80 | 76 |
81 ServiceContext* service_context_ = nullptr; | 77 ServiceContext* service_context_ = nullptr; |
82 | 78 |
83 DISALLOW_COPY_AND_ASSIGN(Service); | 79 DISALLOW_COPY_AND_ASSIGN(Service); |
84 }; | 80 }; |
85 | 81 |
86 // TODO(rockot): Remove this. It's here to satisfy a few remaining use cases | 82 // TODO(rockot): Remove this. It's here to satisfy a few remaining use cases |
87 // where a Service impl is owned by something other than its ServiceContext. | 83 // where a Service impl is owned by something other than its ServiceContext. |
88 class ForwardingService : public Service { | 84 class ForwardingService : public Service { |
89 public: | 85 public: |
90 // |target| must outlive this object. | 86 // |target| must outlive this object. |
91 explicit ForwardingService(Service* target); | 87 explicit ForwardingService(Service* target); |
92 ~ForwardingService() override; | 88 ~ForwardingService() override; |
93 | 89 |
94 // Service: | 90 // Service: |
95 void OnStart() override; | 91 void OnStart() override; |
96 bool OnConnect(const ServiceInfo& remote_info, | 92 bool OnConnect(const ServiceInfo& remote_info, |
97 InterfaceRegistry* registry) override; | 93 InterfaceRegistry* registry) override; |
98 bool OnStop() override; | 94 void OnBindInterface(const ServiceInfo& remote_info, |
| 95 const std::string& interface_name, |
| 96 mojo::ScopedMessagePipeHandle interface_pipe) override; |
| 97 bool OnServiceManagerConnectionLost() override; |
99 | 98 |
100 private: | 99 private: |
| 100 void SetContext(ServiceContext* context) override; |
| 101 |
101 Service* const target_ = nullptr; | 102 Service* const target_ = nullptr; |
102 | 103 |
103 DISALLOW_COPY_AND_ASSIGN(ForwardingService); | 104 DISALLOW_COPY_AND_ASSIGN(ForwardingService); |
104 }; | 105 }; |
105 | 106 |
106 } // namespace service_manager | 107 } // namespace service_manager |
107 | 108 |
108 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 109 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
OLD | NEW |