| 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/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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 const ServiceRequestHandler& handler) { | 132 const ServiceRequestHandler& handler) { |
| 133 io_task_runner_->PostTask( | 133 io_task_runner_->PostTask( |
| 134 FROM_HERE, base::Bind(&ServiceManagerConnectionImpl::IOThreadContext:: | 134 FROM_HERE, base::Bind(&ServiceManagerConnectionImpl::IOThreadContext:: |
| 135 AddServiceRequestHandlerOnIoThread, | 135 AddServiceRequestHandlerOnIoThread, |
| 136 this, name, handler)); | 136 this, name, handler)); |
| 137 } | 137 } |
| 138 | 138 |
| 139 private: | 139 private: |
| 140 friend class base::RefCountedThreadSafe<IOThreadContext>; | 140 friend class base::RefCountedThreadSafe<IOThreadContext>; |
| 141 | 141 |
| 142 struct PendingRequest { | |
| 143 std::string service_name; | |
| 144 service_manager::mojom::ServiceRequest request; | |
| 145 }; | |
| 146 | |
| 147 class MessageLoopObserver : public base::MessageLoop::DestructionObserver { | 142 class MessageLoopObserver : public base::MessageLoop::DestructionObserver { |
| 148 public: | 143 public: |
| 149 explicit MessageLoopObserver(base::WeakPtr<IOThreadContext> context) | 144 explicit MessageLoopObserver(base::WeakPtr<IOThreadContext> context) |
| 150 : context_(context) { | 145 : context_(context) { |
| 151 base::MessageLoop::current()->AddDestructionObserver(this); | 146 base::MessageLoop::current()->AddDestructionObserver(this); |
| 152 } | 147 } |
| 153 | 148 |
| 154 ~MessageLoopObserver() override { | 149 ~MessageLoopObserver() override { |
| 155 base::MessageLoop::current()->RemoveDestructionObserver(this); | 150 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 156 } | 151 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 embedded_services_.insert(std::make_pair(name, std::move(service))); | 249 embedded_services_.insert(std::make_pair(name, std::move(service))); |
| 255 DCHECK(result.second); | 250 DCHECK(result.second); |
| 256 } | 251 } |
| 257 | 252 |
| 258 void AddServiceRequestHandlerOnIoThread( | 253 void AddServiceRequestHandlerOnIoThread( |
| 259 const std::string& name, | 254 const std::string& name, |
| 260 const ServiceRequestHandler& handler) { | 255 const ServiceRequestHandler& handler) { |
| 261 DCHECK(io_thread_checker_.CalledOnValidThread()); | 256 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 262 auto result = request_handlers_.insert(std::make_pair(name, handler)); | 257 auto result = request_handlers_.insert(std::make_pair(name, handler)); |
| 263 DCHECK(result.second); | 258 DCHECK(result.second); |
| 264 auto iter = pending_requests_.begin(); | |
| 265 while (iter != pending_requests_.end()) { | |
| 266 if ((*iter)->service_name == name) { | |
| 267 std::unique_ptr<PendingRequest> pending_request = std::move(*iter); | |
| 268 iter = pending_requests_.erase(iter); | |
| 269 handler.Run(std::move(pending_request->request)); | |
| 270 } else { | |
| 271 ++iter; | |
| 272 } | |
| 273 } | |
| 274 } | 259 } |
| 275 | 260 |
| 276 ///////////////////////////////////////////////////////////////////////////// | 261 ///////////////////////////////////////////////////////////////////////////// |
| 277 // service_manager::Service implementation | 262 // service_manager::Service implementation |
| 278 | 263 |
| 279 void OnStart() override { | 264 void OnStart() override { |
| 280 DCHECK(io_thread_checker_.CalledOnValidThread()); | 265 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 281 DCHECK(!local_info_available_callback_.is_null()); | 266 DCHECK(!local_info_available_callback_.is_null()); |
| 282 local_info_ = context()->local_info(); | 267 local_info_ = context()->local_info(); |
| 283 | 268 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 return true; | 322 return true; |
| 338 } | 323 } |
| 339 | 324 |
| 340 ///////////////////////////////////////////////////////////////////////////// | 325 ///////////////////////////////////////////////////////////////////////////// |
| 341 // service_manager::mojom::ServiceFactory implementation | 326 // service_manager::mojom::ServiceFactory implementation |
| 342 | 327 |
| 343 void CreateService(service_manager::mojom::ServiceRequest request, | 328 void CreateService(service_manager::mojom::ServiceRequest request, |
| 344 const std::string& name) override { | 329 const std::string& name) override { |
| 345 DCHECK(io_thread_checker_.CalledOnValidThread()); | 330 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 346 auto it = request_handlers_.find(name); | 331 auto it = request_handlers_.find(name); |
| 347 if (it == request_handlers_.end()) { | 332 DCHECK(it != request_handlers_.end()) |
| 348 std::unique_ptr<PendingRequest> pending_request = | 333 << "Can't create service " << name << ". No handler found."; |
| 349 base::MakeUnique<PendingRequest>(); | |
| 350 pending_request->service_name = name; | |
| 351 pending_request->request = std::move(request); | |
| 352 pending_requests_.push_back(std::move(pending_request)); | |
| 353 return; | |
| 354 } | |
| 355 it->second.Run(std::move(request)); | 334 it->second.Run(std::move(request)); |
| 356 } | 335 } |
| 357 | 336 |
| 358 static void CallBinderOnTaskRunner( | 337 static void CallBinderOnTaskRunner( |
| 359 scoped_refptr<base::SequencedTaskRunner> task_runner, | 338 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 360 const service_manager::InterfaceRegistry::Binder& binder, | 339 const service_manager::InterfaceRegistry::Binder& binder, |
| 361 const std::string& interface_name, | 340 const std::string& interface_name, |
| 362 mojo::ScopedMessagePipeHandle request_handle) { | 341 mojo::ScopedMessagePipeHandle request_handle) { |
| 363 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name, | 342 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name, |
| 364 base::Passed(&request_handle))); | 343 base::Passed(&request_handle))); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 MessageLoopObserver* message_loop_observer_ = nullptr; | 385 MessageLoopObserver* message_loop_observer_ = nullptr; |
| 407 | 386 |
| 408 // Guards |connection_filters_|. | 387 // Guards |connection_filters_|. |
| 409 base::Lock lock_; | 388 base::Lock lock_; |
| 410 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; | 389 std::map<int, std::unique_ptr<ConnectionFilter>> connection_filters_; |
| 411 | 390 |
| 412 std::unordered_map<std::string, std::unique_ptr<EmbeddedServiceRunner>> | 391 std::unordered_map<std::string, std::unique_ptr<EmbeddedServiceRunner>> |
| 413 embedded_services_; | 392 embedded_services_; |
| 414 std::unordered_map<std::string, ServiceRequestHandler> request_handlers_; | 393 std::unordered_map<std::string, ServiceRequestHandler> request_handlers_; |
| 415 | 394 |
| 416 // Requests before the service have been registered are added here. Typically | |
| 417 // there are very few elements, so we use a vector. | |
| 418 std::vector<std::unique_ptr<PendingRequest>> pending_requests_; | |
| 419 | |
| 420 mojo::Binding<mojom::Child> child_binding_; | 395 mojo::Binding<mojom::Child> child_binding_; |
| 421 | 396 |
| 422 base::WeakPtrFactory<IOThreadContext> weak_factory_; | 397 base::WeakPtrFactory<IOThreadContext> weak_factory_; |
| 423 | 398 |
| 424 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | 399 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); |
| 425 }; | 400 }; |
| 426 | 401 |
| 427 //////////////////////////////////////////////////////////////////////////////// | 402 //////////////////////////////////////////////////////////////////////////////// |
| 428 // ServiceManagerConnection, public: | 403 // ServiceManagerConnection, public: |
| 429 | 404 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 } | 554 } |
| 580 | 555 |
| 581 void ServiceManagerConnectionImpl::GetInterface( | 556 void ServiceManagerConnectionImpl::GetInterface( |
| 582 service_manager::mojom::InterfaceProvider* provider, | 557 service_manager::mojom::InterfaceProvider* provider, |
| 583 const std::string& interface_name, | 558 const std::string& interface_name, |
| 584 mojo::ScopedMessagePipeHandle request_handle) { | 559 mojo::ScopedMessagePipeHandle request_handle) { |
| 585 provider->GetInterface(interface_name, std::move(request_handle)); | 560 provider->GetInterface(interface_name, std::move(request_handle)); |
| 586 } | 561 } |
| 587 | 562 |
| 588 } // namespace content | 563 } // namespace content |
| OLD | NEW |