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