| 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 <stdint.h> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 13 | |
| 14 namespace service_manager { | 8 namespace service_manager { |
| 15 | 9 |
| 16 class Connector; | |
| 17 class Identity; | |
| 18 class InterfaceRegistry; | 10 class InterfaceRegistry; |
| 19 class ServiceContext; | 11 class ServiceContext; |
| 20 struct ServiceInfo; | 12 struct ServiceInfo; |
| 21 | 13 |
| 22 // The primary contract between a Service and the Service Manager, receiving | 14 // The primary contract between a Service and the Service Manager, receiving |
| 23 // lifecycle notifications and connection requests. | 15 // lifecycle notifications and connection requests. Every Service must minimally |
| 16 // implement OnConnect(). |
| 24 class Service { | 17 class Service { |
| 25 public: | 18 public: |
| 26 Service(); | |
| 27 virtual ~Service(); | 19 virtual ~Service(); |
| 28 | 20 |
| 29 // Called once a bidirectional connection with the Service Manager has been | 21 // Called once a bidirectional connection with the Service Manager has been |
| 30 // established. | 22 // established. |
| 31 // |info| contains information about this instance from the Service Manager | 23 // |
| 32 // and the service manifest. | 24 // |context| is the ServiceContext for this instance of the service. It's |
| 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 // |
| 33 // Called exactly once before any calls to OnConnect(). | 32 // Called exactly once before any calls to OnConnect(). |
| 34 virtual void OnStart(const ServiceInfo& info); | 33 virtual void OnStart(ServiceContext* context); |
| 35 | 34 |
| 36 // Called when a connection to this service is brokered by the Service | 35 // Called each time a connection to this service is brokered by the Service |
| 37 // Manager. Implement to expose interfaces to the remote service. Return true | 36 // Manager. Implement this to expose interfaces to other services. |
| 38 // if the connection should succeed. Return false if the connection should | 37 // |
| 39 // be rejected and the underlying pipe closed. The default implementation | 38 // Return true if the connection should succeed or false if the connection |
| 40 // returns false. | 39 // should be rejected. |
| 41 virtual bool OnConnect(const ServiceInfo& remote_info, | 40 virtual bool OnConnect(const ServiceInfo& remote_info, |
| 42 InterfaceRegistry* registry); | 41 InterfaceRegistry* registry) = 0; |
| 43 | 42 |
| 44 // Called when the Service Manager has stopped tracking this instance. The | 43 // Called when the Service Manager has stopped tracking this instance. The |
| 45 // service should use this as a signal to exit, and in fact its process may | 44 // service should use this as a signal to shut down, and in fact its process |
| 46 // be reaped shortly afterward. | 45 // may be reaped shortly afterward if applicable. |
| 47 // Return true from this method to tell the ServiceContext to run its | 46 // |
| 48 // connection lost closure if it has one, false to prevent it from being run. | 47 // 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), |
| 49 // 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 |
| 51 // torn down. |
| 52 // |
| 49 // The default implementation returns true. | 53 // The default implementation returns true. |
| 50 // When used in conjunction with ApplicationRunner, returning true here quits | 54 // |
| 51 // the message loop created by ApplicationRunner, which results in the service | 55 // While it's possible for this to be invoked before either OnStart() or |
| 52 // quitting. | 56 // OnConnect() is invoked, neither will be invoked at any point after this |
| 53 // No calls to either OnStart() nor OnConnect() may be received after this is | 57 // OnStop(). |
| 54 // called. It is however possible for this to be called without OnStart() ever | |
| 55 // having been called. | |
| 56 virtual bool OnStop(); | 58 virtual bool OnStop(); |
| 57 | |
| 58 Connector* connector(); | |
| 59 ServiceContext* context(); | |
| 60 void set_context(std::unique_ptr<ServiceContext> context); | |
| 61 | |
| 62 private: | |
| 63 std::unique_ptr<ServiceContext> context_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(Service); | |
| 66 }; | 59 }; |
| 67 | 60 |
| 68 } // namespace service_manager | 61 } // namespace service_manager |
| 69 | 62 |
| 70 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ | 63 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_H_ |
| OLD | NEW |