OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/common/mojo/mojo_shell_connection_impl.h" | 5 #include "content/common/mojo/mojo_shell_connection_impl.h" |
6 | 6 |
| 7 #include <queue> |
| 8 #include <utility> |
| 9 #include <vector> |
| 10 |
7 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback_helpers.h" |
8 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
10 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_checker.h" |
11 #include "content/common/mojo/embedded_application_runner.h" | 17 #include "content/common/mojo/embedded_application_runner.h" |
| 18 #include "content/public/common/connection_filter.h" |
| 19 #include "mojo/public/cpp/bindings/binding_set.h" |
| 20 #include "mojo/public/cpp/system/message_pipe.h" |
12 #include "services/shell/public/cpp/service.h" | 21 #include "services/shell/public/cpp/service.h" |
13 #include "services/shell/public/cpp/service_context.h" | 22 #include "services/shell/public/cpp/service_context.h" |
| 23 #include "services/shell/public/interfaces/service_factory.mojom.h" |
14 #include "services/shell/runner/common/client_util.h" | 24 #include "services/shell/runner/common/client_util.h" |
15 | 25 |
16 namespace content { | 26 namespace content { |
17 namespace { | 27 namespace { |
18 | 28 |
19 using MojoShellConnectionPtr = | 29 base::LazyInstance<std::unique_ptr<MojoShellConnection>>::Leaky |
20 base::ThreadLocalPointer<MojoShellConnection>; | 30 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; |
21 | |
22 // Env is thread local so that aura may be used on multiple threads. | |
23 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = | |
24 LAZY_INSTANCE_INITIALIZER; | |
25 | 31 |
26 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; | 32 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; |
27 | 33 |
28 } // namespace | 34 } // namespace |
29 | 35 |
| 36 // A ref-counted object which owns the IO thread state of a |
| 37 // MojoShellConnectionImpl. This includes Service and ServiceFactory |
| 38 // bindings. |
| 39 class MojoShellConnectionImpl::IOThreadContext |
| 40 : public base::RefCountedThreadSafe<IOThreadContext>, |
| 41 public shell::Service, |
| 42 public shell::InterfaceFactory<shell::mojom::ServiceFactory>, |
| 43 public shell::mojom::ServiceFactory { |
| 44 public: |
| 45 using InitializeCallback = base::Callback<void(const shell::Identity&)>; |
| 46 using ServiceFactoryCallback = |
| 47 base::Callback<void(shell::mojom::ServiceRequest, const mojo::String&)>; |
| 48 |
| 49 IOThreadContext(shell::mojom::ServiceRequest service_request, |
| 50 scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
| 51 std::unique_ptr<shell::Connector> io_thread_connector, |
| 52 shell::mojom::ConnectorRequest connector_request) |
| 53 : pending_service_request_(std::move(service_request)), |
| 54 io_task_runner_(io_task_runner), |
| 55 io_thread_connector_(std::move(io_thread_connector)), |
| 56 pending_connector_request_(std::move(connector_request)) { |
| 57 // This will be reattached by any of the IO thread functions on first call. |
| 58 io_thread_checker_.DetachFromThread(); |
| 59 } |
| 60 |
| 61 // Safe to call from any thread. |
| 62 void Start(const InitializeCallback& initialize_callback, |
| 63 const ServiceFactoryCallback& create_service_callback, |
| 64 const base::Closure& stop_callback) { |
| 65 DCHECK(!started_); |
| 66 |
| 67 started_ = true; |
| 68 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 69 initialize_handler_ = initialize_callback; |
| 70 create_service_callback_ = create_service_callback; |
| 71 stop_callback_ = stop_callback; |
| 72 io_task_runner_->PostTask( |
| 73 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); |
| 74 } |
| 75 |
| 76 // Safe to call from whichever thread called Start() (or may have called |
| 77 // Start()). Must be called before IO thread shutdown. |
| 78 void ShutDown() { |
| 79 if (!started_) |
| 80 return; |
| 81 |
| 82 bool posted = io_task_runner_->PostTask( |
| 83 FROM_HERE, base::Bind(&IOThreadContext::ShutDownOnIOThread, this)); |
| 84 DCHECK(posted); |
| 85 } |
| 86 |
| 87 // Safe to call any time before Start() is called. |
| 88 void AddConnectionFilter(std::unique_ptr<ConnectionFilter> filter) { |
| 89 DCHECK(!started_); |
| 90 connection_filters_.emplace_back(std::move(filter)); |
| 91 } |
| 92 |
| 93 // Safe to call any time before Start() is called. |
| 94 void SetDefaultBinderForBrowserConnection( |
| 95 const shell::InterfaceRegistry::Binder& binder) { |
| 96 DCHECK(!started_); |
| 97 default_browser_binder_ = base::Bind( |
| 98 &IOThreadContext::CallBinderOnTaskRunner, |
| 99 base::ThreadTaskRunnerHandle::Get(), binder); |
| 100 } |
| 101 |
| 102 // Safe to call any time after Start() is called. |
| 103 void GetRemoteInterface(const mojo::String& interface_name, |
| 104 mojo::ScopedMessagePipeHandle request_handle) { |
| 105 DCHECK(started_); |
| 106 io_task_runner_->PostTask( |
| 107 FROM_HERE, |
| 108 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, this, |
| 109 interface_name, base::Passed(&request_handle))); |
| 110 } |
| 111 |
| 112 private: |
| 113 friend class base::RefCountedThreadSafe<IOThreadContext>; |
| 114 |
| 115 ~IOThreadContext() override {} |
| 116 |
| 117 void StartOnIOThread() { |
| 118 // Should bind |io_thread_checker_| to the context's thread. |
| 119 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 120 service_context_.reset(new shell::ServiceContext( |
| 121 this, std::move(pending_service_request_), |
| 122 std::move(io_thread_connector_), |
| 123 std::move(pending_connector_request_))); |
| 124 } |
| 125 |
| 126 void ShutDownOnIOThread() { |
| 127 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 128 service_context_.reset(); |
| 129 factory_bindings_.CloseAllBindings(); |
| 130 } |
| 131 |
| 132 void GetRemoteInterfaceOnIOThread( |
| 133 const mojo::String& interface_name, |
| 134 mojo::ScopedMessagePipeHandle request_handle) { |
| 135 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 136 if (browser_connection_) { |
| 137 browser_connection_->GetRemoteInterfaces()->GetInterface( |
| 138 interface_name, std::move(request_handle)); |
| 139 } else { |
| 140 pending_remote_interface_requests_.emplace(interface_name, |
| 141 std::move(request_handle)); |
| 142 } |
| 143 } |
| 144 |
| 145 void OnBrowserConnectionLost() { |
| 146 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 147 browser_connection_ = nullptr; |
| 148 } |
| 149 |
| 150 ///////////////////////////////////////////////////////////////////////////// |
| 151 // shell::Service implementation |
| 152 |
| 153 void OnStart(shell::Connector* connector, |
| 154 const shell::Identity& identity, |
| 155 uint32_t id) override { |
| 156 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 157 DCHECK(!initialize_handler_.is_null()); |
| 158 |
| 159 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); |
| 160 callback_task_runner_->PostTask(FROM_HERE, base::Bind(handler, identity)); |
| 161 } |
| 162 |
| 163 bool OnConnect(shell::Connection* connection) override { |
| 164 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 165 std::string remote_app = connection->GetRemoteIdentity().name(); |
| 166 if (remote_app == "mojo:shell") { |
| 167 // Only expose the SCF interface to the shell. |
| 168 connection->AddInterface<shell::mojom::ServiceFactory>(this); |
| 169 return true; |
| 170 } |
| 171 |
| 172 bool accept = false; |
| 173 for (auto& filter : connection_filters_) { |
| 174 accept = accept || |
| 175 filter->OnConnect(connection, service_context_->connector()); |
| 176 } |
| 177 |
| 178 // Capture the browser connection if possible. |
| 179 if (remote_app == "exe:content_browser" && !browser_connection_) { |
| 180 browser_connection_ = connection; |
| 181 connection->GetInterfaceRegistry()->set_default_binder( |
| 182 default_browser_binder_); |
| 183 connection->SetConnectionLostClosure( |
| 184 base::Bind(&IOThreadContext::OnBrowserConnectionLost, this)); |
| 185 shell::InterfaceProvider* remote_interfaces = |
| 186 connection->GetRemoteInterfaces(); |
| 187 |
| 188 // Flush any pending outgoing interface requests. |
| 189 while (!pending_remote_interface_requests_.empty()) { |
| 190 auto& request = pending_remote_interface_requests_.front(); |
| 191 remote_interfaces->GetInterface( |
| 192 request.first, std::move(request.second)); |
| 193 pending_remote_interface_requests_.pop(); |
| 194 } |
| 195 return true; |
| 196 } |
| 197 |
| 198 // If no filters were interested, reject the connection. |
| 199 return accept; |
| 200 } |
| 201 |
| 202 bool OnStop() override { |
| 203 callback_task_runner_->PostTask(FROM_HERE, stop_callback_); |
| 204 return true; |
| 205 } |
| 206 |
| 207 ///////////////////////////////////////////////////////////////////////////// |
| 208 // shell::InterfaceFactory<shell::mojom::ServiceFactory> implementation |
| 209 |
| 210 void Create(shell::Connection* connection, |
| 211 shell::mojom::ServiceFactoryRequest request) override { |
| 212 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 213 factory_bindings_.AddBinding(this, std::move(request)); |
| 214 } |
| 215 |
| 216 ///////////////////////////////////////////////////////////////////////////// |
| 217 // shell::mojom::ServiceFactory implementation |
| 218 |
| 219 void CreateService(shell::mojom::ServiceRequest request, |
| 220 const mojo::String& name) override { |
| 221 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 222 callback_task_runner_->PostTask( |
| 223 FROM_HERE, |
| 224 base::Bind(create_service_callback_, base::Passed(&request), name)); |
| 225 } |
| 226 |
| 227 static void CallBinderOnTaskRunner( |
| 228 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 229 const shell::InterfaceRegistry::Binder& binder, |
| 230 const mojo::String& interface_name, |
| 231 mojo::ScopedMessagePipeHandle request_handle) { |
| 232 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name, |
| 233 base::Passed(&request_handle))); |
| 234 } |
| 235 |
| 236 base::ThreadChecker io_thread_checker_; |
| 237 bool started_ = false; |
| 238 |
| 239 // Temporary state established on construction and consumed on the IO thread |
| 240 // once the connection is started. |
| 241 shell::mojom::ServiceRequest pending_service_request_; |
| 242 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 243 std::unique_ptr<shell::Connector> io_thread_connector_; |
| 244 shell::mojom::ConnectorRequest pending_connector_request_; |
| 245 |
| 246 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to |
| 247 // Start(). |
| 248 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; |
| 249 |
| 250 // Callback to run once Service::OnStart is invoked. |
| 251 InitializeCallback initialize_handler_; |
| 252 |
| 253 // Callback to run when a new Service request is received. |
| 254 ServiceFactoryCallback create_service_callback_; |
| 255 |
| 256 // Callback to run if the service is stopped by the service manager. |
| 257 base::Closure stop_callback_; |
| 258 |
| 259 // The incoming Connection from the browser process. This is captured the |
| 260 // first time the browser connects to this Service and persists until shutdown |
| 261 // or a connection error is detected. This connection is used to fulfill |
| 262 // remote interface requests from legacy code which does not use |
| 263 // shell::Connector. |
| 264 // |
| 265 // TODO(rockot): Remove this once all child-to-browser interface connections |
| 266 // are made via a Connector rather than directly through an InterfaceProvider |
| 267 // and all interfaces exposed to the browser are exposed via a |
| 268 // ConnectionFilter. |
| 269 shell::Connection* browser_connection_ = nullptr; |
| 270 |
| 271 // A queue of remote interface requests destined for the browser, which will |
| 272 // remain pending until an incoming connection is accepted from the browser. |
| 273 // |
| 274 // TODO(rockot): Remove this once all child-to-browser interface connections |
| 275 // are made via a Connector rather than directly through an InterfaceProvider. |
| 276 std::queue<std::pair<mojo::String, mojo::ScopedMessagePipeHandle>> |
| 277 pending_remote_interface_requests_; |
| 278 |
| 279 // Default binder callback used for the browser connection's |
| 280 // InterfaceRegistry. |
| 281 // |
| 282 // TODO(rockot): Remove this once all interfaces exposed to the browser are |
| 283 // exposed via a ConnectionFilter. |
| 284 shell::InterfaceRegistry::Binder default_browser_binder_; |
| 285 |
| 286 std::unique_ptr<shell::ServiceContext> service_context_; |
| 287 mojo::BindingSet<shell::mojom::ServiceFactory> factory_bindings_; |
| 288 std::vector<std::unique_ptr<ConnectionFilter>> connection_filters_; |
| 289 |
| 290 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); |
| 291 }; |
| 292 |
30 //////////////////////////////////////////////////////////////////////////////// | 293 //////////////////////////////////////////////////////////////////////////////// |
31 // MojoShellConnection, public: | 294 // MojoShellConnection, public: |
32 | 295 |
33 // static | 296 // static |
34 void MojoShellConnection::SetForProcess( | 297 void MojoShellConnection::SetForProcess( |
35 std::unique_ptr<MojoShellConnection> connection) { | 298 std::unique_ptr<MojoShellConnection> connection) { |
36 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 299 DCHECK(!g_connection_for_process.Get()); |
37 lazy_tls_ptr.Pointer()->Set(connection.release()); | 300 g_connection_for_process.Get() = std::move(connection); |
38 } | 301 } |
39 | 302 |
40 // static | 303 // static |
41 MojoShellConnection* MojoShellConnection::GetForProcess() { | 304 MojoShellConnection* MojoShellConnection::GetForProcess() { |
42 return lazy_tls_ptr.Pointer()->Get(); | 305 return g_connection_for_process.Get().get(); |
43 } | 306 } |
44 | 307 |
45 // static | 308 // static |
46 void MojoShellConnection::DestroyForProcess() { | 309 void MojoShellConnection::DestroyForProcess() { |
47 // This joins the shell controller thread. | 310 // This joins the shell controller thread. |
48 delete GetForProcess(); | 311 g_connection_for_process.Get().reset(); |
49 lazy_tls_ptr.Pointer()->Set(nullptr); | |
50 } | 312 } |
51 | 313 |
52 // static | 314 // static |
53 void MojoShellConnection::SetFactoryForTest(Factory* factory) { | 315 void MojoShellConnection::SetFactoryForTest(Factory* factory) { |
54 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 316 DCHECK(!g_connection_for_process.Get()); |
55 mojo_shell_connection_factory = factory; | 317 mojo_shell_connection_factory = factory; |
56 } | 318 } |
57 | 319 |
58 // static | 320 // static |
59 std::unique_ptr<MojoShellConnection> MojoShellConnection::Create( | 321 std::unique_ptr<MojoShellConnection> MojoShellConnection::Create( |
60 shell::mojom::ServiceRequest request) { | 322 shell::mojom::ServiceRequest request, |
| 323 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { |
61 if (mojo_shell_connection_factory) | 324 if (mojo_shell_connection_factory) |
62 return mojo_shell_connection_factory->Run(); | 325 return mojo_shell_connection_factory->Run(); |
63 return base::WrapUnique(new MojoShellConnectionImpl(std::move(request))); | 326 return base::MakeUnique<MojoShellConnectionImpl>( |
| 327 std::move(request), io_task_runner); |
64 } | 328 } |
65 | 329 |
66 MojoShellConnection::~MojoShellConnection() {} | 330 MojoShellConnection::~MojoShellConnection() {} |
67 | 331 |
68 //////////////////////////////////////////////////////////////////////////////// | 332 //////////////////////////////////////////////////////////////////////////////// |
69 // MojoShellConnectionImpl, public: | 333 // MojoShellConnectionImpl, public: |
70 | 334 |
71 MojoShellConnectionImpl::MojoShellConnectionImpl( | 335 MojoShellConnectionImpl::MojoShellConnectionImpl( |
72 shell::mojom::ServiceRequest request) | 336 shell::mojom::ServiceRequest request, |
73 : shell_connection_(new shell::ServiceContext(this, std::move(request))) {} | 337 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
| 338 : weak_factory_(this) { |
| 339 shell::mojom::ConnectorRequest connector_request; |
| 340 connector_ = shell::Connector::Create(&connector_request); |
74 | 341 |
75 MojoShellConnectionImpl::~MojoShellConnectionImpl() {} | 342 std::unique_ptr<shell::Connector> io_thread_connector = connector_->Clone(); |
76 | 343 context_ = new IOThreadContext( |
77 //////////////////////////////////////////////////////////////////////////////// | 344 std::move(request), io_task_runner, std::move(io_thread_connector), |
78 // MojoShellConnectionImpl, shell::Service implementation: | 345 std::move(connector_request)); |
79 | |
80 void MojoShellConnectionImpl::OnStart(shell::Connector* connector, | |
81 const shell::Identity& identity, | |
82 uint32_t id) { | |
83 for (auto& client : embedded_services_) | |
84 client->OnStart(connector, identity, id); | |
85 } | 346 } |
86 | 347 |
87 bool MojoShellConnectionImpl::OnConnect(shell::Connection* connection) { | 348 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
88 std::string remote_app = connection->GetRemoteIdentity().name(); | 349 context_->ShutDown(); |
89 if (remote_app == "mojo:shell") { | |
90 // Only expose the SCF interface to the shell. | |
91 connection->AddInterface<shell::mojom::ServiceFactory>(this); | |
92 return true; | |
93 } | |
94 | |
95 bool accept = false; | |
96 for (auto& client : embedded_services_) | |
97 accept |= client->OnConnect(connection); | |
98 | |
99 // Reject all other connections to this application. | |
100 return accept; | |
101 } | |
102 | |
103 shell::InterfaceRegistry* | |
104 MojoShellConnectionImpl::GetInterfaceRegistryForConnection() { | |
105 // TODO(beng): This is really horrible since obviously subject to issues | |
106 // of ordering, but is no more horrible than this API is in general. | |
107 shell::InterfaceRegistry* registry = nullptr; | |
108 for (auto& client : embedded_services_) { | |
109 registry = client->GetInterfaceRegistryForConnection(); | |
110 if (registry) | |
111 return registry; | |
112 } | |
113 return nullptr; | |
114 } | |
115 | |
116 shell::InterfaceProvider* | |
117 MojoShellConnectionImpl::GetInterfaceProviderForConnection() { | |
118 // TODO(beng): This is really horrible since obviously subject to issues | |
119 // of ordering, but is no more horrible than this API is in general. | |
120 shell::InterfaceProvider* provider = nullptr; | |
121 for (auto& client : embedded_services_) { | |
122 provider = client->GetInterfaceProviderForConnection(); | |
123 if (provider) | |
124 return provider; | |
125 } | |
126 return nullptr; | |
127 } | |
128 | |
129 //////////////////////////////////////////////////////////////////////////////// | |
130 // MojoShellConnectionImpl, | |
131 // shell::InterfaceFactory<shell::mojom::ServiceFactory> implementation: | |
132 | |
133 void MojoShellConnectionImpl::Create( | |
134 shell::Connection* connection, | |
135 shell::mojom::ServiceFactoryRequest request) { | |
136 factory_bindings_.AddBinding(this, std::move(request)); | |
137 } | |
138 | |
139 //////////////////////////////////////////////////////////////////////////////// | |
140 // MojoShellConnectionImpl, shell::mojom::ServiceFactory implementation: | |
141 | |
142 void MojoShellConnectionImpl::CreateService( | |
143 shell::mojom::ServiceRequest request, | |
144 const mojo::String& name) { | |
145 auto it = request_handlers_.find(name); | |
146 if (it != request_handlers_.end()) | |
147 it->second.Run(std::move(request)); | |
148 } | 350 } |
149 | 351 |
150 //////////////////////////////////////////////////////////////////////////////// | 352 //////////////////////////////////////////////////////////////////////////////// |
151 // MojoShellConnectionImpl, MojoShellConnection implementation: | 353 // MojoShellConnectionImpl, MojoShellConnection implementation: |
152 | 354 |
153 shell::ServiceContext* MojoShellConnectionImpl::GetShellConnection() { | 355 void MojoShellConnectionImpl::Start() { |
154 return shell_connection_.get(); | 356 context_->Start( |
| 357 base::Bind(&MojoShellConnectionImpl::OnContextInitialized, |
| 358 weak_factory_.GetWeakPtr()), |
| 359 base::Bind(&MojoShellConnectionImpl::CreateService, |
| 360 weak_factory_.GetWeakPtr()), |
| 361 base::Bind(&MojoShellConnectionImpl::OnConnectionLost, |
| 362 weak_factory_.GetWeakPtr())); |
| 363 } |
| 364 |
| 365 void MojoShellConnectionImpl::SetInitializeHandler( |
| 366 const base::Closure& handler) { |
| 367 DCHECK(initialize_handler_.is_null()); |
| 368 initialize_handler_ = handler; |
155 } | 369 } |
156 | 370 |
157 shell::Connector* MojoShellConnectionImpl::GetConnector() { | 371 shell::Connector* MojoShellConnectionImpl::GetConnector() { |
158 DCHECK(shell_connection_); | 372 return connector_.get(); |
159 return shell_connection_->connector(); | |
160 } | 373 } |
161 | 374 |
162 const shell::Identity& MojoShellConnectionImpl::GetIdentity() const { | 375 const shell::Identity& MojoShellConnectionImpl::GetIdentity() const { |
163 DCHECK(shell_connection_); | 376 return identity_; |
164 return shell_connection_->identity(); | |
165 } | 377 } |
166 | 378 |
167 void MojoShellConnectionImpl::SetConnectionLostClosure( | 379 void MojoShellConnectionImpl::SetConnectionLostClosure( |
168 const base::Closure& closure) { | 380 const base::Closure& closure) { |
169 shell_connection_->SetConnectionLostClosure(closure); | 381 connection_lost_handler_ = closure; |
170 } | 382 } |
171 | 383 |
172 void MojoShellConnectionImpl::MergeService( | 384 void MojoShellConnectionImpl::SetupInterfaceRequestProxies( |
173 std::unique_ptr<shell::Service> service) { | 385 shell::InterfaceRegistry* registry, |
174 embedded_services_.push_back(service.get()); | 386 shell::InterfaceProvider* provider) { |
175 owned_services_.push_back(std::move(service)); | 387 // It's safe to bind |registry| as a raw pointer because the caller must |
| 388 // guarantee that it outlives |this|, and |this| is bound as a weak ptr here. |
| 389 context_->SetDefaultBinderForBrowserConnection( |
| 390 base::Bind(&MojoShellConnectionImpl::GetInterface, |
| 391 weak_factory_.GetWeakPtr(), registry)); |
| 392 |
| 393 // Forward all remote interface requests on |provider| to our IO-thread |
| 394 // context. This will ensure they're forwarded to the provider on the |
| 395 // incoming browser connection. |
| 396 provider->Forward(base::Bind(&IOThreadContext::GetRemoteInterface, context_)); |
176 } | 397 } |
177 | 398 |
178 void MojoShellConnectionImpl::MergeService( | 399 void MojoShellConnectionImpl::AddConnectionFilter( |
179 shell::Service* service) { | 400 std::unique_ptr<ConnectionFilter> filter) { |
180 embedded_services_.push_back(service); | 401 context_->AddConnectionFilter(std::move(filter)); |
181 } | 402 } |
182 | 403 |
183 void MojoShellConnectionImpl::AddEmbeddedService( | 404 void MojoShellConnectionImpl::AddEmbeddedService( |
184 const std::string& name, | 405 const std::string& name, |
185 const MojoApplicationInfo& info) { | 406 const MojoApplicationInfo& info) { |
186 std::unique_ptr<EmbeddedApplicationRunner> app( | 407 std::unique_ptr<EmbeddedApplicationRunner> app( |
187 new EmbeddedApplicationRunner(name, info)); | 408 new EmbeddedApplicationRunner(name, info)); |
188 AddServiceRequestHandler( | 409 AddServiceRequestHandler( |
189 name, base::Bind(&EmbeddedApplicationRunner::BindServiceRequest, | 410 name, base::Bind(&EmbeddedApplicationRunner::BindServiceRequest, |
190 base::Unretained(app.get()))); | 411 base::Unretained(app.get()))); |
191 auto result = embedded_apps_.insert(std::make_pair(name, std::move(app))); | 412 auto result = embedded_apps_.insert(std::make_pair(name, std::move(app))); |
192 DCHECK(result.second); | 413 DCHECK(result.second); |
193 } | 414 } |
194 | 415 |
195 void MojoShellConnectionImpl::AddServiceRequestHandler( | 416 void MojoShellConnectionImpl::AddServiceRequestHandler( |
196 const std::string& name, | 417 const std::string& name, |
197 const ServiceRequestHandler& handler) { | 418 const ServiceRequestHandler& handler) { |
198 auto result = request_handlers_.insert(std::make_pair(name, handler)); | 419 auto result = request_handlers_.insert(std::make_pair(name, handler)); |
199 DCHECK(result.second); | 420 DCHECK(result.second); |
200 } | 421 } |
201 | 422 |
| 423 void MojoShellConnectionImpl::CreateService( |
| 424 shell::mojom::ServiceRequest request, |
| 425 const mojo::String& name) { |
| 426 auto it = request_handlers_.find(name); |
| 427 if (it != request_handlers_.end()) |
| 428 it->second.Run(std::move(request)); |
| 429 } |
| 430 |
| 431 void MojoShellConnectionImpl::OnContextInitialized( |
| 432 const shell::Identity& identity) { |
| 433 identity_ = identity; |
| 434 if (!initialize_handler_.is_null()) |
| 435 base::ResetAndReturn(&initialize_handler_).Run(); |
| 436 } |
| 437 |
| 438 void MojoShellConnectionImpl::OnConnectionLost() { |
| 439 if (!connection_lost_handler_.is_null()) |
| 440 connection_lost_handler_.Run(); |
| 441 } |
| 442 |
| 443 void MojoShellConnectionImpl::GetInterface( |
| 444 shell::mojom::InterfaceProvider* provider, |
| 445 const mojo::String& interface_name, |
| 446 mojo::ScopedMessagePipeHandle request_handle) { |
| 447 provider->GetInterface(interface_name, std::move(request_handle)); |
| 448 } |
| 449 |
202 } // namespace content | 450 } // namespace content |
OLD | NEW |