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 #include "services/service_manager/public/cpp/service_context.h" | 5 #include "services/service_manager/public/cpp/service_context.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
16 #include "mojo/public/cpp/bindings/interface_request.h" | 11 #include "mojo/public/cpp/bindings/interface_request.h" |
17 #include "services/service_manager/public/cpp/interface_provider_spec.h" | |
18 #include "services/service_manager/public/cpp/interface_registry.h" | |
19 #include "services/service_manager/public/cpp/lib/connector_impl.h" | 12 #include "services/service_manager/public/cpp/lib/connector_impl.h" |
20 #include "services/service_manager/public/cpp/service.h" | 13 #include "services/service_manager/public/cpp/service.h" |
21 | 14 |
22 namespace service_manager { | 15 namespace service_manager { |
23 | 16 |
24 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
25 // ServiceContext, public: | 18 // ServiceContext, public: |
26 | 19 |
27 ServiceContext::ServiceContext( | 20 ServiceContext::ServiceContext( |
28 std::unique_ptr<service_manager::Service> service, | 21 std::unique_ptr<service_manager::Service> service, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // ServiceContext, mojom::Service implementation: | 74 // ServiceContext, mojom::Service implementation: |
82 | 75 |
83 void ServiceContext::OnStart(const ServiceInfo& info, | 76 void ServiceContext::OnStart(const ServiceInfo& info, |
84 const OnStartCallback& callback) { | 77 const OnStartCallback& callback) { |
85 local_info_ = info; | 78 local_info_ = info; |
86 callback.Run(std::move(pending_connector_request_), | 79 callback.Run(std::move(pending_connector_request_), |
87 mojo::MakeRequest(&service_control_)); | 80 mojo::MakeRequest(&service_control_)); |
88 service_->OnStart(); | 81 service_->OnStart(); |
89 } | 82 } |
90 | 83 |
91 void ServiceContext::OnConnect( | |
92 const ServiceInfo& source_info, | |
93 mojom::InterfaceProviderRequest interfaces, | |
94 const OnConnectCallback& callback) { | |
95 InterfaceProviderSpec source_spec, target_spec; | |
96 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, | |
97 local_info_.interface_provider_specs, &target_spec); | |
98 GetInterfaceProviderSpec(mojom::kServiceManager_ConnectorSpec, | |
99 source_info.interface_provider_specs, &source_spec); | |
100 | |
101 // Acknowledge the request regardless of whether it's accepted. | |
102 callback.Run(); | |
103 | |
104 CallOnConnect(source_info, source_spec, target_spec, std::move(interfaces)); | |
105 } | |
106 | |
107 void ServiceContext::OnBindInterface( | 84 void ServiceContext::OnBindInterface( |
108 const ServiceInfo& source_info, | 85 const ServiceInfo& source_info, |
109 const std::string& interface_name, | 86 const std::string& interface_name, |
110 mojo::ScopedMessagePipeHandle interface_pipe, | 87 mojo::ScopedMessagePipeHandle interface_pipe, |
111 const OnBindInterfaceCallback& callback) { | 88 const OnBindInterfaceCallback& callback) { |
112 // Acknowledge the request regardless of whether it's accepted. | 89 // Acknowledge the request regardless of whether it's accepted. |
113 callback.Run(); | 90 callback.Run(); |
114 | 91 |
115 service_->OnBindInterface(source_info, interface_name, | 92 service_->OnBindInterface(source_info, interface_name, |
116 std::move(interface_pipe)); | 93 std::move(interface_pipe)); |
117 } | 94 } |
118 | 95 |
119 //////////////////////////////////////////////////////////////////////////////// | 96 //////////////////////////////////////////////////////////////////////////////// |
120 // ServiceContext, private: | 97 // ServiceContext, private: |
121 | 98 |
122 void ServiceContext::CallOnConnect(const ServiceInfo& source_info, | |
123 const InterfaceProviderSpec& source_spec, | |
124 const InterfaceProviderSpec& target_spec, | |
125 mojom::InterfaceProviderRequest interfaces) { | |
126 auto registry = | |
127 base::MakeUnique<InterfaceRegistry>(mojom::kServiceManager_ConnectorSpec); | |
128 registry->Bind(std::move(interfaces), local_info_.identity, target_spec, | |
129 source_info.identity, source_spec); | |
130 | |
131 if (!service_->OnConnect(source_info, registry.get())) | |
132 return; | |
133 | |
134 InterfaceRegistry* raw_registry = registry.get(); | |
135 registry->AddConnectionLostClosure(base::Bind( | |
136 &ServiceContext::OnRegistryConnectionError, base::Unretained(this), | |
137 raw_registry)); | |
138 connection_interface_registries_.insert( | |
139 std::make_pair(raw_registry, std::move(registry))); | |
140 } | |
141 | |
142 void ServiceContext::OnConnectionError() { | 99 void ServiceContext::OnConnectionError() { |
143 if (service_->OnServiceManagerConnectionLost()) { | 100 if (service_->OnServiceManagerConnectionLost()) { |
144 // CAUTION: May delete |this|. | 101 // CAUTION: May delete |this|. |
145 QuitNow(); | 102 QuitNow(); |
146 } | 103 } |
147 } | 104 } |
148 | 105 |
149 void ServiceContext::OnRegistryConnectionError(InterfaceRegistry* registry) { | |
150 // NOTE: We destroy the InterfaceRegistry asynchronously since it's calling | |
151 // into us from its own connection error handler which may continue to access | |
152 // the InterfaceRegistry's own state after we return. | |
153 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
154 FROM_HERE, | |
155 base::Bind(&ServiceContext::DestroyConnectionInterfaceRegistry, | |
156 weak_factory_.GetWeakPtr(), registry)); | |
157 } | |
158 | |
159 void ServiceContext::DestroyConnectionInterfaceRegistry( | |
160 InterfaceRegistry* registry) { | |
161 auto it = connection_interface_registries_.find(registry); | |
162 CHECK(it != connection_interface_registries_.end()); | |
163 connection_interface_registries_.erase(it); | |
164 } | |
165 | |
166 } // namespace service_manager | 106 } // namespace service_manager |
OLD | NEW |