| 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(), OnBindInterface(), | 27 // Manager has been established. No calls to OnConnect() will be received |
| 28 // or OnStop() will be made before this. Note that this call is mutually | 28 // before this. |
| 29 // exclusive to OnStartFailed() - either one or the other will be the first | |
| 30 // call on any given Service instance. | |
| 31 virtual void OnStart(); | 29 virtual void OnStart(); |
| 32 | 30 |
| 33 // Called if the Service loses its connection to the Service Manager before | |
| 34 // OnStart() could be invoked. Once this called, none of the other public | |
| 35 // Service interface methods will be called. Note that this call is mutually | |
| 36 // exclusive to OnStart() - either one or the other will be the first call on | |
| 37 // any given Service instance. | |
| 38 // | |
| 39 // The default implementation calls QuitNow() on context(). | |
| 40 virtual void OnStartFailed(); | |
| 41 | |
| 42 // 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 |
| 43 // Manager. Implement this to expose interfaces to other services. | 32 // Manager. Implement this to expose interfaces to other services. |
| 44 // | 33 // |
| 45 // 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 |
| 46 // should be rejected. | 35 // should be rejected. |
| 47 // | 36 // |
| 48 // The default implementation returns false. | 37 // The default implementation returns false. |
| 49 virtual bool OnConnect(const ServiceInfo& remote_info, | 38 virtual bool OnConnect(const ServiceInfo& remote_info, |
| 50 InterfaceRegistry* registry); | 39 InterfaceRegistry* registry); |
| 51 | 40 |
| 52 // Called when the service identified by |source_info| requests this service | 41 // Called when the service identified by |source_info| requests this service |
| 53 // 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 |
| 54 // service manager has already determined that policy permits this interface | 43 // service manager has already determined that policy permits this interface |
| 55 // 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 |
| 56 // just blindly bind it under most conditions. | 45 // just blindly bind it under most conditions. |
| 57 virtual void OnBindInterface(const ServiceInfo& source_info, | 46 virtual void OnBindInterface(const ServiceInfo& source_info, |
| 58 const std::string& interface_name, | 47 const std::string& interface_name, |
| 59 mojo::ScopedMessagePipeHandle interface_pipe); | 48 mojo::ScopedMessagePipeHandle interface_pipe); |
| 60 | 49 |
| 61 // Called when the Service Manager has stopped tracking this instance. The | 50 // Called when the Service Manager has stopped tracking this instance. The |
| 62 // 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 |
| 63 // may be reaped shortly afterward if applicable. | 52 // may be reaped shortly afterward if applicable. |
| 64 // | 53 // |
| 65 // Returning true from this method signals that the Service instance can be | 54 // Return true from this method to tell the ServiceContext to signal its |
| 66 // destroyed immediately. More precisely, it will cause the context()'s | 55 // shutdown externally (i.e. to invoke its "connection lost" closure if set), |
| 67 // QuitNow() method to be invoked immediately after this OnStop() call. | 56 // or return false to defer the signal. If deferred, the Service should |
| 68 // | 57 // explicitly call QuitNow() on the ServiceContext when it's ready to be |
| 69 // If shutdown is deferred by returning false, the Service itself is | 58 // torn down. |
| 70 // responsible for explicitly calling QuitNow() on context() when it's ready | |
| 71 // to be destroyed. | |
| 72 // | 59 // |
| 73 // The default implementation returns true. | 60 // The default implementation returns true. |
| 74 // | 61 // |
| 75 // NOTE: This will only be called after OnStart(), and none of the other | 62 // While it's possible for this to be invoked before either OnStart() or |
| 76 // public Service methods will be called after this. | 63 // OnConnect() is invoked, neither will be invoked at any point after this |
| 64 // OnStop(). |
| 77 virtual bool OnStop(); | 65 virtual bool OnStop(); |
| 78 | 66 |
| 79 protected: | 67 protected: |
| 80 // Accesses the ServiceContext associated with this Service. Note that this is | 68 // Access the ServiceContext associated with this Service. Note that this is |
| 81 // only valid during or after OnStart() or OnStartFailed(), but never before. | 69 // only valid to call during or after OnStart(), but never before! As such, |
| 70 // it's always safe to call in OnStart() and OnConnect(), but should generally |
| 71 // be avoided in OnStop(). |
| 82 ServiceContext* context() const; | 72 ServiceContext* context() const; |
| 83 | 73 |
| 84 private: | 74 private: |
| 85 friend class ForwardingService; | 75 friend class ForwardingService; |
| 86 friend class ServiceContext; | 76 friend class ServiceContext; |
| 87 | 77 |
| 88 // NOTE: This is guaranteed to be called before OnStart(). | 78 // NOTE: This is guaranteed to be called before OnStart(). |
| 89 void set_context(ServiceContext* context) { service_context_ = context; } | 79 void set_context(ServiceContext* context) { service_context_ = context; } |
| 90 | 80 |
| 91 ServiceContext* service_context_ = nullptr; | 81 ServiceContext* service_context_ = nullptr; |
| 92 | 82 |
| 93 DISALLOW_COPY_AND_ASSIGN(Service); | 83 DISALLOW_COPY_AND_ASSIGN(Service); |
| 94 }; | 84 }; |
| 95 | 85 |
| 96 // TODO(rockot): Remove this. It's here to satisfy a few remaining use cases | 86 // TODO(rockot): Remove this. It's here to satisfy a few remaining use cases |
| 97 // where a Service impl is owned by something other than its ServiceContext. | 87 // where a Service impl is owned by something other than its ServiceContext. |
| 98 class ForwardingService : public Service { | 88 class ForwardingService : public Service { |
| 99 public: | 89 public: |
| 100 // |target| must outlive this object. | 90 // |target| must outlive this object. |
| 101 explicit ForwardingService(Service* target); | 91 explicit ForwardingService(Service* target); |
| 102 ~ForwardingService() override; | 92 ~ForwardingService() override; |
| 103 | 93 |
| 104 // Service: | 94 // Service: |
| 105 void OnStart() override; | 95 void OnStart() override; |
| 106 bool OnConnect(const ServiceInfo& remote_info, | 96 bool OnConnect(const ServiceInfo& remote_info, |
| 107 InterfaceRegistry* registry) override; | 97 InterfaceRegistry* registry) override; |
| 108 void OnBindInterface(const ServiceInfo& remote_info, | |
| 109 const std::string& interface_name, | |
| 110 mojo::ScopedMessagePipeHandle interface_pipe) override; | |
| 111 bool OnStop() override; | 98 bool OnStop() override; |
| 112 void OnStartFailed() override; | |
| 113 | 99 |
| 114 private: | 100 private: |
| 115 Service* const target_ = nullptr; | 101 Service* const target_ = nullptr; |
| 116 | 102 |
| 117 DISALLOW_COPY_AND_ASSIGN(ForwardingService); | 103 DISALLOW_COPY_AND_ASSIGN(ForwardingService); |
| 118 }; | 104 }; |
| 119 | 105 |
| 120 } // namespace service_manager | 106 } // namespace service_manager |
| 121 | 107 |
| 122 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 108 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
| OLD | NEW |