Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: services/service_manager/public/cpp/service_context.h

Issue 2701883002: service_manager: More consistent Service lifecycle API (Closed)
Patch Set: . Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_CONTEXT_H_ 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_CONTEXT_H_
6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_CONTEXT_H_ 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_CONTEXT_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "mojo/public/cpp/bindings/binding.h" 14 #include "mojo/public/cpp/bindings/binding.h"
15 #include "mojo/public/cpp/system/core.h" 15 #include "mojo/public/cpp/system/core.h"
16 #include "services/service_manager/public/cpp/connector.h" 16 #include "services/service_manager/public/cpp/connector.h"
17 #include "services/service_manager/public/cpp/service.h" 17 #include "services/service_manager/public/cpp/service.h"
18 #include "services/service_manager/public/interfaces/connector.mojom.h" 18 #include "services/service_manager/public/interfaces/connector.mojom.h"
19 #include "services/service_manager/public/interfaces/service.mojom.h" 19 #include "services/service_manager/public/interfaces/service.mojom.h"
20 #include "services/service_manager/public/interfaces/service_control.mojom.h" 20 #include "services/service_manager/public/interfaces/service_control.mojom.h"
21 21
22 namespace service_manager { 22 namespace service_manager {
23 23
24 // Encapsulates a connection to the Service Manager in two parts: 24 // Encapsulates a connection to the Service Manager in two parts:
25 //
25 // - a bound InterfacePtr to mojom::Connector, the primary mechanism 26 // - a bound InterfacePtr to mojom::Connector, the primary mechanism
26 // by which the instantiating service connects to other services, 27 // by which the instantiating service connects to other services,
27 // brokered by the Service Manager. 28 // brokered by the Service Manager.
29 //
28 // - a bound InterfaceRequest of mojom::Service, an interface used by the 30 // - a bound InterfaceRequest of mojom::Service, an interface used by the
29 // Service Manager to inform this service of lifecycle events and 31 // Service Manager to inform this service of lifecycle events and
30 // inbound connections brokered by it. 32 // inbound connections brokered by it.
31 // 33 //
32 // This class owns an instance of a Service implementation, and there should be 34 // This class owns an instance of a Service implementation, and there should be
33 // exactly one instance of this class for every logical service instance running 35 // exactly one instance of this class for every logical service instance running
34 // in the system. 36 // in the system.
35 // 37 //
36 // This class is generally used to handle incoming mojom::ServiceRequests from 38 // This class is generally used to handle incoming mojom::ServiceRequests from
37 // the Service Manager. These can either come from ServiceRunner, from the 39 // the Service Manager. These can either come from ServiceRunner, from the
(...skipping 17 matching lines...) Expand all
55 mojom::ServiceRequest request, 57 mojom::ServiceRequest request,
56 std::unique_ptr<Connector> connector = nullptr, 58 std::unique_ptr<Connector> connector = nullptr,
57 mojom::ConnectorRequest connector_request = nullptr); 59 mojom::ConnectorRequest connector_request = nullptr);
58 60
59 ~ServiceContext() override; 61 ~ServiceContext() override;
60 62
61 Connector* connector() { return connector_.get(); } 63 Connector* connector() { return connector_.get(); }
62 const ServiceInfo& local_info() const { return local_info_; } 64 const ServiceInfo& local_info() const { return local_info_; }
63 const Identity& identity() const { return local_info_.identity; } 65 const Identity& identity() const { return local_info_.identity; }
64 66
65 // Specify a function to be called when the connection to the service manager 67 // Specify a closure to be run when the Service calls QuitNow(), typically
66 // is lost. Note that if connection has already been lost, then |closure| is 68 // in response to Service::OnServiceManagerConnectionLost().
67 // called immediately.
68 // 69 //
69 // It is acceptable for |closure| to delete this ServiceContext. 70 // Note that if the Service has already called QuitNow(), |closure| is run
70 void SetConnectionLostClosure(const base::Closure& closure); 71 // immediately from this method.
72 //
73 // NOTE: It is acceptable for |closure| to delete this ServiceContext.
74 void SetQuitClosure(const base::Closure& closure);
71 75
72 // Informs the Service Manager that this instance is ready to terminate. If 76 // Informs the Service Manager that this instance is ready to terminate. If
73 // the Service Manager has any outstanding connection requests for this 77 // the Service Manager has any outstanding connection requests for this
74 // instance, the request is ignored; the instance will eventually receive 78 // instance, the request is ignored; the instance will eventually receive
75 // the pending request(s) and can then appropriately decide whether or not 79 // the pending request(s) and can then appropriately decide whether or not
76 // it still wants to quit. 80 // it still wants to quit.
77 // 81 //
78 // If the request is granted, the Service Manager will service the connection 82 // If the request is granted, the Service Manager will soon sever the
79 // to this ServiceContext and Service::OnStop() will eventually be invoked. 83 // connection to this ServiceContext, and
84 // Service::OnServiceManagerConnectionLost() will be invoked at that time.
80 void RequestQuit(); 85 void RequestQuit();
81 86
82 // Immediately severs the connection to the Service Manager. 87 // Immediately severs the connection to the Service Manager.
83 // 88 //
84 // Note that calling this before the Service receives OnStop() can lead to 89 // Note that calling this before the Service receives
85 // unpredictable behavior, specifically because clients may have inbound 90 // OnServiceManagerConnectionLost() can lead to unpredictable behavior, as the
86 // connections in transit which may have already been brokered by the Service 91 // Service Manager may have already brokered new inbound connections from
87 // Manager and thus will be irreparably broken on the client side. 92 // other services to this Service instance, and those connections will be
93 // abruptly terminated as they can no longer result in OnConnect() or
94 // OnBindInterface() calls on the Service.
88 // 95 //
89 // Use of this call before OnStop() should be reserved for exceptional cases. 96 // To put it another way: unless you want flaky connections to be a normal
97 // experience for consumers of your service, avoid calling this before
98 // receiving Service::OnServiceManagerConnectionLost().
90 void DisconnectFromServiceManager(); 99 void DisconnectFromServiceManager();
91 100
92 // Immediately severs the connection to the Service Manager. 101 // Immediately severs the connection to the Service Manager and invokes the
93 // 102 // quit closure (see SetQuitClosure() above) if one has been set.
94 // If a connection-lost closure was set, it is immediately invoked. Note that
95 // it is never necessary or meaningful to call this after the Service
96 // has received OnStop().
97 // 103 //
98 // See comments on DisconnectFromServiceManager() regarding abrupt 104 // See comments on DisconnectFromServiceManager() regarding abrupt
99 // disconnection from the Service Manager. 105 // disconnection from the Service Manager.
100 void QuitNow(); 106 void QuitNow();
101 107
102 // Simliar to QuitNow() above but also destroys the Service instance.
103 void DestroyService();
104
105 private: 108 private:
106 friend class service_manager::Service; 109 friend class service_manager::Service;
107 110
108 using InterfaceRegistryMap = 111 using InterfaceRegistryMap =
109 std::map<InterfaceRegistry*, std::unique_ptr<InterfaceRegistry>>; 112 std::map<InterfaceRegistry*, std::unique_ptr<InterfaceRegistry>>;
110 113
111 // mojom::Service: 114 // mojom::Service:
112 void OnStart(const ServiceInfo& info, 115 void OnStart(const ServiceInfo& info,
113 const OnStartCallback& callback) override; 116 const OnStartCallback& callback) override;
114 void OnConnect(const ServiceInfo& source_info, 117 void OnConnect(const ServiceInfo& source_info,
(...skipping 24 matching lines...) Expand all
139 142
140 std::unique_ptr<service_manager::Service> service_; 143 std::unique_ptr<service_manager::Service> service_;
141 mojo::Binding<mojom::Service> binding_; 144 mojo::Binding<mojom::Service> binding_;
142 std::unique_ptr<Connector> connector_; 145 std::unique_ptr<Connector> connector_;
143 service_manager::ServiceInfo local_info_; 146 service_manager::ServiceInfo local_info_;
144 147
145 // This instance's control interface to the service manager. Note that this 148 // This instance's control interface to the service manager. Note that this
146 // is unbound and therefore invalid until OnStart() is called. 149 // is unbound and therefore invalid until OnStart() is called.
147 mojom::ServiceControlAssociatedPtr service_control_; 150 mojom::ServiceControlAssociatedPtr service_control_;
148 151
152 // The Service may call QuitNow() before SetConnectionLostClosure(), and the
153 // latter is expected to invoke the closure immediately in that case. This is
154 // used to track that condition.
155 //
156 // TODO(rockot): Figure out who depends on this behavior and make them stop.
157 // It's weird and shouldn't be necessary.
149 bool service_quit_ = false; 158 bool service_quit_ = false;
150 159
151 base::Closure connection_lost_closure_; 160 // The closure to run when QuitNow() is invoked. May delete |this|.
161 base::Closure quit_closure_;
152 162
153 base::WeakPtrFactory<ServiceContext> weak_factory_; 163 base::WeakPtrFactory<ServiceContext> weak_factory_;
154 164
155 DISALLOW_COPY_AND_ASSIGN(ServiceContext); 165 DISALLOW_COPY_AND_ASSIGN(ServiceContext);
156 }; 166 };
157 167
158 } // namespace service_manager 168 } // namespace service_manager
159 169
160 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_CONTEXT_H_ 170 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_SERVICE_CONTEXT_H_
OLDNEW
« no previous file with comments | « services/service_manager/public/cpp/service.h ('k') | services/service_manager/tests/connect/connect_test_package.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698