Chromium Code Reviews| 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/associated_interface_provider_impl.h" | 5 #include "content/common/associated_interface_provider_impl.h" |
| 6 #include "base/callback.h" | |
| 7 #include "base/run_loop.h" | |
| 6 #include "mojo/public/cpp/bindings/associated_binding.h" | 8 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 7 | 9 |
| 8 namespace content { | 10 namespace content { |
| 9 | 11 |
| 10 class AssociatedInterfaceProviderImpl::LocalProvider | 12 class AssociatedInterfaceProviderImpl::LocalProvider |
| 11 : public mojom::AssociatedInterfaceProvider { | 13 : public mojom::AssociatedInterfaceProvider { |
| 12 public: | 14 public: |
| 13 explicit LocalProvider(mojom::AssociatedInterfaceProviderAssociatedPtr* proxy) | 15 explicit LocalProvider(mojom::AssociatedInterfaceProviderAssociatedPtr* proxy) |
| 14 : associated_interface_provider_binding_(this) { | 16 : associated_interface_provider_binding_(this) { |
| 15 associated_interface_provider_binding_.Bind( | 17 associated_interface_provider_binding_.Bind( |
| 16 mojo::MakeIsolatedRequest(proxy)); | 18 mojo::MakeIsolatedRequest(proxy)); |
| 17 } | 19 } |
| 18 | 20 |
| 19 ~LocalProvider() override {} | 21 ~LocalProvider() override {} |
| 20 | 22 |
| 21 void SetBinderForName( | 23 void SetBinderForName( |
| 22 const std::string& name, | 24 const std::string& name, |
| 23 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { | 25 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { |
| 24 binders_[name] = binder; | 26 binders_[name] = binder; |
| 25 } | 27 } |
| 26 | 28 |
| 29 void WaitForBinding(const std::string& name) { | |
|
ncarter (slow)
2017/04/21 20:42:13
Assuming this is test-only code, we should find a
Charlie Harrison
2017/04/21 20:48:02
Sorry, this is no longer up to date since rockot@
| |
| 30 DCHECK(binders_.find(name) != binders_.end()); | |
| 31 base::RunLoop run_loop; | |
| 32 quit_closure_ = run_loop.QuitClosure(); | |
| 33 name_to_wait_for_ = name; | |
| 34 run_loop.Run(); | |
| 35 } | |
| 36 | |
| 27 private: | 37 private: |
| 28 // mojom::AssociatedInterfaceProvider: | 38 // mojom::AssociatedInterfaceProvider: |
| 29 void GetAssociatedInterface( | 39 void GetAssociatedInterface( |
| 30 const std::string& name, | 40 const std::string& name, |
| 31 mojom::AssociatedInterfaceAssociatedRequest request) override { | 41 mojom::AssociatedInterfaceAssociatedRequest request) override { |
| 32 auto it = binders_.find(name); | 42 auto it = binders_.find(name); |
| 33 if (it != binders_.end()) | 43 if (it != binders_.end()) |
| 34 it->second.Run(request.PassHandle()); | 44 it->second.Run(request.PassHandle()); |
| 45 | |
| 46 if (name == name_to_wait_for_) { | |
| 47 name_to_wait_for_.clear(); | |
| 48 std::move(quit_closure_).Run(); | |
| 49 } | |
| 35 } | 50 } |
| 36 | 51 |
| 37 using BinderMap = | 52 using BinderMap = |
| 38 std::map<std::string, | 53 std::map<std::string, |
| 39 base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>>; | 54 base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>>; |
| 40 BinderMap binders_; | 55 BinderMap binders_; |
| 41 | 56 |
| 57 base::Closure quit_closure_; | |
| 58 std::string name_to_wait_for_; | |
| 59 | |
| 42 mojo::AssociatedBinding<mojom::AssociatedInterfaceProvider> | 60 mojo::AssociatedBinding<mojom::AssociatedInterfaceProvider> |
| 43 associated_interface_provider_binding_; | 61 associated_interface_provider_binding_; |
| 44 }; | 62 }; |
| 45 | 63 |
| 46 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl( | 64 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl( |
| 47 mojom::AssociatedInterfaceProviderAssociatedPtr proxy) | 65 mojom::AssociatedInterfaceProviderAssociatedPtr proxy) |
| 48 : proxy_(std::move(proxy)) { | 66 : proxy_(std::move(proxy)) { |
| 49 DCHECK(proxy_.is_bound()); | 67 DCHECK(proxy_.is_bound()); |
| 50 } | 68 } |
| 51 | 69 |
| 52 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl() | 70 AssociatedInterfaceProviderImpl::AssociatedInterfaceProviderImpl() |
| 53 : local_provider_(new LocalProvider(&proxy_)) {} | 71 : local_provider_(new LocalProvider(&proxy_)) {} |
| 54 | 72 |
| 55 AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {} | 73 AssociatedInterfaceProviderImpl::~AssociatedInterfaceProviderImpl() {} |
| 56 | 74 |
| 57 void AssociatedInterfaceProviderImpl::GetInterface( | 75 void AssociatedInterfaceProviderImpl::GetInterface( |
| 58 const std::string& name, | 76 const std::string& name, |
| 59 mojo::ScopedInterfaceEndpointHandle handle) { | 77 mojo::ScopedInterfaceEndpointHandle handle) { |
| 60 mojom::AssociatedInterfaceAssociatedRequest request; | 78 mojom::AssociatedInterfaceAssociatedRequest request; |
| 61 request.Bind(std::move(handle)); | 79 request.Bind(std::move(handle)); |
| 62 return proxy_->GetAssociatedInterface(name, std::move(request)); | 80 proxy_->GetAssociatedInterface(name, std::move(request)); |
| 81 | |
| 82 // In tests, make sure the interface is bound before continuing, to ensure we | |
| 83 // don't deadlock for sync messages. | |
| 84 if (local_provider_) | |
| 85 local_provider_->WaitForBinding(name); | |
|
Ken Rockot(use gerrit already)
2017/04/21 18:26:58
Hmm. On one hand, I'm really not a fan of code whi
Charlie Harrison
2017/04/21 18:46:10
Hm, this makes sense but it's a bit tricky because
| |
| 63 } | 86 } |
| 64 | 87 |
| 65 void AssociatedInterfaceProviderImpl::OverrideBinderForTesting( | 88 void AssociatedInterfaceProviderImpl::OverrideBinderForTesting( |
| 66 const std::string& name, | 89 const std::string& name, |
| 67 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { | 90 const base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>& binder) { |
| 68 DCHECK(local_provider_); | 91 DCHECK(local_provider_); |
| 69 local_provider_->SetBinderForName(name, binder); | 92 local_provider_->SetBinderForName(name, binder); |
| 70 } | 93 } |
| 71 | 94 |
| 72 } // namespace content | 95 } // namespace content |
| OLD | NEW |