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