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

Side by Side Diff: content/common/service_manager/service_manager_connection_impl.cc

Issue 2742523005: Revert Change ServiceManagerConnectionImpl to run service request handlers on the IO thread (Closed)
Patch Set: Created 3 years, 9 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 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/service_manager/service_manager_connection_impl.h" 5 #include "content/common/service_manager/service_manager_connection_impl.h"
6 6
7 #include <queue> 7 #include <queue>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // bindings. 42 // bindings.
43 class ServiceManagerConnectionImpl::IOThreadContext 43 class ServiceManagerConnectionImpl::IOThreadContext
44 : public base::RefCountedThreadSafe<IOThreadContext>, 44 : public base::RefCountedThreadSafe<IOThreadContext>,
45 public service_manager::Service, 45 public service_manager::Service,
46 public service_manager::InterfaceFactory< 46 public service_manager::InterfaceFactory<
47 service_manager::mojom::ServiceFactory>, 47 service_manager::mojom::ServiceFactory>,
48 public service_manager::mojom::ServiceFactory { 48 public service_manager::mojom::ServiceFactory {
49 public: 49 public:
50 using InitializeCallback = 50 using InitializeCallback =
51 base::Callback<void(const service_manager::Identity&)>; 51 base::Callback<void(const service_manager::Identity&)>;
52 using ServiceFactoryCallback =
53 base::Callback<void(service_manager::mojom::ServiceRequest,
54 const std::string&)>;
52 55
53 IOThreadContext( 56 IOThreadContext(
54 service_manager::mojom::ServiceRequest service_request, 57 service_manager::mojom::ServiceRequest service_request,
55 scoped_refptr<base::SequencedTaskRunner> io_task_runner, 58 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
56 std::unique_ptr<service_manager::Connector> io_thread_connector, 59 std::unique_ptr<service_manager::Connector> io_thread_connector,
57 service_manager::mojom::ConnectorRequest connector_request) 60 service_manager::mojom::ConnectorRequest connector_request)
58 : pending_service_request_(std::move(service_request)), 61 : pending_service_request_(std::move(service_request)),
59 io_task_runner_(io_task_runner), 62 io_task_runner_(io_task_runner),
60 io_thread_connector_(std::move(io_thread_connector)), 63 io_thread_connector_(std::move(io_thread_connector)),
61 pending_connector_request_(std::move(connector_request)), 64 pending_connector_request_(std::move(connector_request)),
62 weak_factory_(this) { 65 weak_factory_(this) {
63 // This will be reattached by any of the IO thread functions on first call. 66 // This will be reattached by any of the IO thread functions on first call.
64 io_thread_checker_.DetachFromThread(); 67 io_thread_checker_.DetachFromThread();
65 } 68 }
66 69
67 // Safe to call from any thread. 70 // Safe to call from any thread.
68 void Start( 71 void Start(
69 const InitializeCallback& initialize_callback, 72 const InitializeCallback& initialize_callback,
70 const ServiceManagerConnection::OnConnectHandler& on_connect_callback, 73 const ServiceManagerConnection::OnConnectHandler& on_connect_callback,
74 const ServiceFactoryCallback& create_service_callback,
71 const base::Closure& stop_callback) { 75 const base::Closure& stop_callback) {
72 DCHECK(!started_); 76 DCHECK(!started_);
73 77
74 started_ = true; 78 started_ = true;
75 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 79 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get();
76 initialize_handler_ = initialize_callback; 80 initialize_handler_ = initialize_callback;
77 on_connect_callback_ = on_connect_callback; 81 on_connect_callback_ = on_connect_callback;
82 create_service_callback_ = create_service_callback;
78 stop_callback_ = stop_callback; 83 stop_callback_ = stop_callback;
79 io_task_runner_->PostTask( 84 io_task_runner_->PostTask(
80 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); 85 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this));
81 } 86 }
82 87
83 // Safe to call from whichever thread called Start() (or may have called 88 // Safe to call from whichever thread called Start() (or may have called
84 // Start()). Must be called before IO thread shutdown. 89 // Start()). Must be called before IO thread shutdown.
85 void ShutDown() { 90 void ShutDown() {
86 if (!started_) 91 if (!started_)
87 return; 92 return;
(...skipping 26 matching lines...) Expand all
114 119
115 // Safe to call any time before Start() is called. 120 // Safe to call any time before Start() is called.
116 void SetDefaultBinderForBrowserConnection( 121 void SetDefaultBinderForBrowserConnection(
117 const service_manager::InterfaceRegistry::Binder& binder) { 122 const service_manager::InterfaceRegistry::Binder& binder) {
118 DCHECK(!started_); 123 DCHECK(!started_);
119 default_browser_binder_ = base::Bind( 124 default_browser_binder_ = base::Bind(
120 &IOThreadContext::CallBinderOnTaskRunner, 125 &IOThreadContext::CallBinderOnTaskRunner,
121 base::ThreadTaskRunnerHandle::Get(), binder); 126 base::ThreadTaskRunnerHandle::Get(), binder);
122 } 127 }
123 128
124 void AddEmbeddedService(const std::string& name, const ServiceInfo& info) {
125 io_task_runner_->PostTask(
126 FROM_HERE, base::Bind(&ServiceManagerConnectionImpl::IOThreadContext::
127 AddEmbeddedServiceRequestHandlerOnIoThread,
128 this, name, info));
129 }
130
131 void AddServiceRequestHandler(const std::string& name,
132 const ServiceRequestHandler& handler) {
133 io_task_runner_->PostTask(
134 FROM_HERE, base::Bind(&ServiceManagerConnectionImpl::IOThreadContext::
135 AddServiceRequestHandlerOnIoThread,
136 this, name, handler));
137 }
138
139 private: 129 private:
140 friend class base::RefCountedThreadSafe<IOThreadContext>; 130 friend class base::RefCountedThreadSafe<IOThreadContext>;
141 131
142 class MessageLoopObserver : public base::MessageLoop::DestructionObserver { 132 class MessageLoopObserver : public base::MessageLoop::DestructionObserver {
143 public: 133 public:
144 explicit MessageLoopObserver(base::WeakPtr<IOThreadContext> context) 134 explicit MessageLoopObserver(base::WeakPtr<IOThreadContext> context)
145 : context_(context) { 135 : context_(context) {
146 base::MessageLoop::current()->AddDestructionObserver(this); 136 base::MessageLoop::current()->AddDestructionObserver(this);
147 } 137 }
148 138
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 196
207 // Resetting the ServiceContext below may otherwise release the last 197 // Resetting the ServiceContext below may otherwise release the last
208 // reference to this IOThreadContext. We keep it alive until the stack 198 // reference to this IOThreadContext. We keep it alive until the stack
209 // unwinds. 199 // unwinds.
210 scoped_refptr<IOThreadContext> keepalive(this); 200 scoped_refptr<IOThreadContext> keepalive(this);
211 201
212 factory_bindings_.CloseAllBindings(); 202 factory_bindings_.CloseAllBindings();
213 service_context_.reset(); 203 service_context_.reset();
214 204
215 ClearConnectionFiltersOnIOThread(); 205 ClearConnectionFiltersOnIOThread();
216
217 request_handlers_.clear();
218 embedded_services_.clear();
219 } 206 }
220 207
221 void ClearConnectionFiltersOnIOThread() { 208 void ClearConnectionFiltersOnIOThread() {
222 base::AutoLock lock(lock_); 209 base::AutoLock lock(lock_);
223 connection_filters_.clear(); 210 connection_filters_.clear();
224 } 211 }
225 212
226 void RemoveConnectionFilterOnIOThread(int filter_id) { 213 void RemoveConnectionFilterOnIOThread(int filter_id) {
227 base::AutoLock lock(lock_); 214 base::AutoLock lock(lock_);
228 auto it = connection_filters_.find(filter_id); 215 auto it = connection_filters_.find(filter_id);
229 // During shutdown the connection filters might have been cleared already 216 // During shutdown the connection filters might have been cleared already
230 // by ClearConnectionFiltersOnIOThread() above, so this id might not exist. 217 // by ClearConnectionFiltersOnIOThread() above, so this id might not exist.
231 if (it != connection_filters_.end()) 218 if (it != connection_filters_.end())
232 connection_filters_.erase(it); 219 connection_filters_.erase(it);
233 } 220 }
234 221
235 void OnBrowserConnectionLost() { 222 void OnBrowserConnectionLost() {
236 DCHECK(io_thread_checker_.CalledOnValidThread()); 223 DCHECK(io_thread_checker_.CalledOnValidThread());
237 has_browser_connection_ = false; 224 has_browser_connection_ = false;
238 } 225 }
239 226
240 void AddEmbeddedServiceRequestHandlerOnIoThread(const std::string& name,
241 const ServiceInfo& info) {
242 DCHECK(io_thread_checker_.CalledOnValidThread());
243 std::unique_ptr<EmbeddedServiceRunner> service(
244 new EmbeddedServiceRunner(name, info));
245 AddServiceRequestHandler(
246 name, base::Bind(&EmbeddedServiceRunner::BindServiceRequest,
247 base::Unretained(service.get())));
248 auto result =
249 embedded_services_.insert(std::make_pair(name, std::move(service)));
250 DCHECK(result.second);
251 }
252
253 void AddServiceRequestHandlerOnIoThread(
254 const std::string& name,
255 const ServiceRequestHandler& handler) {
256 DCHECK(io_thread_checker_.CalledOnValidThread());
257 auto result = request_handlers_.insert(std::make_pair(name, handler));
258 DCHECK(result.second);
259 }
260
261 ///////////////////////////////////////////////////////////////////////////// 227 /////////////////////////////////////////////////////////////////////////////
262 // service_manager::Service implementation 228 // service_manager::Service implementation
263 229
264 void OnStart() override { 230 void OnStart() override {
265 DCHECK(io_thread_checker_.CalledOnValidThread()); 231 DCHECK(io_thread_checker_.CalledOnValidThread());
266 DCHECK(!initialize_handler_.is_null()); 232 DCHECK(!initialize_handler_.is_null());
267 local_info_ = context()->local_info(); 233 local_info_ = context()->local_info();
268 234
269 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); 235 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_);
270 callback_task_runner_->PostTask(FROM_HERE, 236 callback_task_runner_->PostTask(FROM_HERE,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 DCHECK(io_thread_checker_.CalledOnValidThread()); 288 DCHECK(io_thread_checker_.CalledOnValidThread());
323 factory_bindings_.AddBinding(this, std::move(request)); 289 factory_bindings_.AddBinding(this, std::move(request));
324 } 290 }
325 291
326 ///////////////////////////////////////////////////////////////////////////// 292 /////////////////////////////////////////////////////////////////////////////
327 // service_manager::mojom::ServiceFactory implementation 293 // service_manager::mojom::ServiceFactory implementation
328 294
329 void CreateService(service_manager::mojom::ServiceRequest request, 295 void CreateService(service_manager::mojom::ServiceRequest request,
330 const std::string& name) override { 296 const std::string& name) override {
331 DCHECK(io_thread_checker_.CalledOnValidThread()); 297 DCHECK(io_thread_checker_.CalledOnValidThread());
332 auto it = request_handlers_.find(name); 298 callback_task_runner_->PostTask(
333 DCHECK(it != request_handlers_.end()) 299 FROM_HERE,
334 << "Can't create service " << name << ". No handler found."; 300 base::Bind(create_service_callback_, base::Passed(&request), name));
335 it->second.Run(std::move(request));
336 } 301 }
337 302
338 static void CallBinderOnTaskRunner( 303 static void CallBinderOnTaskRunner(
339 scoped_refptr<base::SequencedTaskRunner> task_runner, 304 scoped_refptr<base::SequencedTaskRunner> task_runner,
340 const service_manager::InterfaceRegistry::Binder& binder, 305 const service_manager::InterfaceRegistry::Binder& binder,
341 const std::string& interface_name, 306 const std::string& interface_name,
342 mojo::ScopedMessagePipeHandle request_handle) { 307 mojo::ScopedMessagePipeHandle request_handle) {
343 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name, 308 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name,
344 base::Passed(&request_handle))); 309 base::Passed(&request_handle)));
345 } 310 }
(...skipping 11 matching lines...) Expand all
357 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to 322 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to
358 // Start(). 323 // Start().
359 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; 324 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_;
360 325
361 // Callback to run once Service::OnStart is invoked. 326 // Callback to run once Service::OnStart is invoked.
362 InitializeCallback initialize_handler_; 327 InitializeCallback initialize_handler_;
363 328
364 // Callback to run when a connection request is received. 329 // Callback to run when a connection request is received.
365 ServiceManagerConnection::OnConnectHandler on_connect_callback_; 330 ServiceManagerConnection::OnConnectHandler on_connect_callback_;
366 331
332 // Callback to run when a new Service request is received.
333 ServiceFactoryCallback create_service_callback_;
334
367 // Callback to run if the service is stopped by the service manager. 335 // Callback to run if the service is stopped by the service manager.
368 base::Closure stop_callback_; 336 base::Closure stop_callback_;
369 337
370 // Called once a connection has been received from the browser process & the 338 // Called once a connection has been received from the browser process & the
371 // default binder (below) has been set up. 339 // default binder (below) has been set up.
372 bool has_browser_connection_ = false; 340 bool has_browser_connection_ = false;
373 341
374 service_manager::ServiceInfo local_info_; 342 service_manager::ServiceInfo local_info_;
375 343
376 // Default binder callback used for the browser connection's 344 // Default binder callback used for the browser connection's
377 // InterfaceRegistry. 345 // InterfaceRegistry.
378 // 346 //
379 // TODO(rockot): Remove this once all interfaces exposed to the browser are 347 // TODO(rockot): Remove this once all interfaces exposed to the browser are
380 // exposed via a ConnectionFilter. 348 // exposed via a ConnectionFilter.
381 service_manager::InterfaceRegistry::Binder default_browser_binder_; 349 service_manager::InterfaceRegistry::Binder default_browser_binder_;
382 350
383 std::unique_ptr<service_manager::ServiceContext> service_context_; 351 std::unique_ptr<service_manager::ServiceContext> service_context_;
384 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_; 352 mojo::BindingSet<service_manager::mojom::ServiceFactory> factory_bindings_;
385 int next_filter_id_ = kInvalidConnectionFilterId; 353 int next_filter_id_ = kInvalidConnectionFilterId;
386 354
387 // Not owned. 355 // Not owned.
388 MessageLoopObserver* message_loop_observer_ = nullptr; 356 MessageLoopObserver* message_loop_observer_ = nullptr;
389 357
390 // Guards |connection_filters_|. 358 // Guards |connection_filters_|.
391 base::Lock lock_; 359 base::Lock lock_;
392 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; 360 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_;
393 361
394 std::unordered_map<std::string, std::unique_ptr<EmbeddedServiceRunner>>
395 embedded_services_;
396 std::unordered_map<std::string, ServiceRequestHandler> request_handlers_;
397
398 base::WeakPtrFactory<IOThreadContext> weak_factory_; 362 base::WeakPtrFactory<IOThreadContext> weak_factory_;
399 363
400 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); 364 DISALLOW_COPY_AND_ASSIGN(IOThreadContext);
401 }; 365 };
402 366
403 //////////////////////////////////////////////////////////////////////////////// 367 ////////////////////////////////////////////////////////////////////////////////
404 // ServiceManagerConnection, public: 368 // ServiceManagerConnection, public:
405 369
406 // static 370 // static
407 void ServiceManagerConnection::SetForProcess( 371 void ServiceManagerConnection::SetForProcess(
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 426
463 //////////////////////////////////////////////////////////////////////////////// 427 ////////////////////////////////////////////////////////////////////////////////
464 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation: 428 // ServiceManagerConnectionImpl, ServiceManagerConnection implementation:
465 429
466 void ServiceManagerConnectionImpl::Start() { 430 void ServiceManagerConnectionImpl::Start() {
467 context_->Start( 431 context_->Start(
468 base::Bind(&ServiceManagerConnectionImpl::OnContextInitialized, 432 base::Bind(&ServiceManagerConnectionImpl::OnContextInitialized,
469 weak_factory_.GetWeakPtr()), 433 weak_factory_.GetWeakPtr()),
470 base::Bind(&ServiceManagerConnectionImpl::OnConnect, 434 base::Bind(&ServiceManagerConnectionImpl::OnConnect,
471 weak_factory_.GetWeakPtr()), 435 weak_factory_.GetWeakPtr()),
436 base::Bind(&ServiceManagerConnectionImpl::CreateService,
437 weak_factory_.GetWeakPtr()),
472 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost, 438 base::Bind(&ServiceManagerConnectionImpl::OnConnectionLost,
473 weak_factory_.GetWeakPtr())); 439 weak_factory_.GetWeakPtr()));
474 } 440 }
475 441
476 service_manager::Connector* ServiceManagerConnectionImpl::GetConnector() { 442 service_manager::Connector* ServiceManagerConnectionImpl::GetConnector() {
477 return connector_.get(); 443 return connector_.get();
478 } 444 }
479 445
480 const service_manager::Identity& ServiceManagerConnectionImpl::GetIdentity() 446 const service_manager::Identity& ServiceManagerConnectionImpl::GetIdentity()
481 const { 447 const {
(...skipping 21 matching lines...) Expand all
503 std::unique_ptr<ConnectionFilter> filter) { 469 std::unique_ptr<ConnectionFilter> filter) {
504 return context_->AddConnectionFilter(std::move(filter)); 470 return context_->AddConnectionFilter(std::move(filter));
505 } 471 }
506 472
507 void ServiceManagerConnectionImpl::RemoveConnectionFilter(int filter_id) { 473 void ServiceManagerConnectionImpl::RemoveConnectionFilter(int filter_id) {
508 context_->RemoveConnectionFilter(filter_id); 474 context_->RemoveConnectionFilter(filter_id);
509 } 475 }
510 476
511 void ServiceManagerConnectionImpl::AddEmbeddedService(const std::string& name, 477 void ServiceManagerConnectionImpl::AddEmbeddedService(const std::string& name,
512 const ServiceInfo& info) { 478 const ServiceInfo& info) {
513 context_->AddEmbeddedService(name, info); 479 std::unique_ptr<EmbeddedServiceRunner> service(
480 new EmbeddedServiceRunner(name, info));
481 AddServiceRequestHandler(
482 name, base::Bind(&EmbeddedServiceRunner::BindServiceRequest,
483 base::Unretained(service.get())));
484 auto result =
485 embedded_services_.insert(std::make_pair(name, std::move(service)));
486 DCHECK(result.second);
514 } 487 }
515 488
516 void ServiceManagerConnectionImpl::AddServiceRequestHandler( 489 void ServiceManagerConnectionImpl::AddServiceRequestHandler(
517 const std::string& name, 490 const std::string& name,
518 const ServiceRequestHandler& handler) { 491 const ServiceRequestHandler& handler) {
519 context_->AddServiceRequestHandler(name, handler); 492 auto result = request_handlers_.insert(std::make_pair(name, handler));
493 DCHECK(result.second);
520 } 494 }
521 495
522 int ServiceManagerConnectionImpl::AddOnConnectHandler( 496 int ServiceManagerConnectionImpl::AddOnConnectHandler(
523 const OnConnectHandler& handler) { 497 const OnConnectHandler& handler) {
524 int id = ++next_on_connect_handler_id_; 498 int id = ++next_on_connect_handler_id_;
525 on_connect_handlers_[id] = handler; 499 on_connect_handlers_[id] = handler;
526 return id; 500 return id;
527 } 501 }
528 502
529 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) { 503 void ServiceManagerConnectionImpl::RemoveOnConnectHandler(int id) {
530 auto it = on_connect_handlers_.find(id); 504 auto it = on_connect_handlers_.find(id);
531 DCHECK(it != on_connect_handlers_.end()); 505 DCHECK(it != on_connect_handlers_.end());
532 on_connect_handlers_.erase(it); 506 on_connect_handlers_.erase(it);
533 } 507 }
534 508
509 void ServiceManagerConnectionImpl::CreateService(
510 service_manager::mojom::ServiceRequest request,
511 const std::string& name) {
512 auto it = request_handlers_.find(name);
513 DCHECK(it != request_handlers_.end())
514 << "Can't create service " << name << ". No handler found.";
515 if (it != request_handlers_.end())
516 it->second.Run(std::move(request));
517 }
518
535 void ServiceManagerConnectionImpl::OnContextInitialized( 519 void ServiceManagerConnectionImpl::OnContextInitialized(
536 const service_manager::Identity& identity) { 520 const service_manager::Identity& identity) {
537 identity_ = identity; 521 identity_ = identity;
538 } 522 }
539 523
540 void ServiceManagerConnectionImpl::OnConnectionLost() { 524 void ServiceManagerConnectionImpl::OnConnectionLost() {
541 if (!connection_lost_handler_.is_null()) 525 if (!connection_lost_handler_.is_null())
542 connection_lost_handler_.Run(); 526 connection_lost_handler_.Run();
543 } 527 }
544 528
545 void ServiceManagerConnectionImpl::OnConnect( 529 void ServiceManagerConnectionImpl::OnConnect(
546 const service_manager::ServiceInfo& local_info, 530 const service_manager::ServiceInfo& local_info,
547 const service_manager::ServiceInfo& remote_info) { 531 const service_manager::ServiceInfo& remote_info) {
548 local_info_ = local_info; 532 local_info_ = local_info;
549 for (auto& handler : on_connect_handlers_) 533 for (auto& handler : on_connect_handlers_)
550 handler.second.Run(local_info, remote_info); 534 handler.second.Run(local_info, remote_info);
551 } 535 }
552 536
553 void ServiceManagerConnectionImpl::GetInterface( 537 void ServiceManagerConnectionImpl::GetInterface(
554 service_manager::mojom::InterfaceProvider* provider, 538 service_manager::mojom::InterfaceProvider* provider,
555 const std::string& interface_name, 539 const std::string& interface_name,
556 mojo::ScopedMessagePipeHandle request_handle) { 540 mojo::ScopedMessagePipeHandle request_handle) {
557 provider->GetInterface(interface_name, std::move(request_handle)); 541 provider->GetInterface(interface_name, std::move(request_handle));
558 } 542 }
559 543
560 } // namespace content 544 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698