OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/mojo/blink_connector_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "services/service_manager/public/cpp/identity.h" | |
13 #include "services/service_manager/public/cpp/interface_provider.h" | |
14 | |
15 namespace content { | |
16 | |
17 BlinkConnectorImpl::BlinkConnectorImpl( | |
18 std::unique_ptr<service_manager::Connector> connector) | |
19 : connector_(std::move(connector)), | |
20 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
21 weak_ptr_factory_(this) {} | |
22 | |
23 BlinkConnectorImpl::~BlinkConnectorImpl() = default; | |
24 | |
25 void BlinkConnectorImpl::bindInterface(const char* service_name, | |
26 const char* interface_name, | |
27 mojo::ScopedMessagePipeHandle handle) { | |
28 // |connector_| is null in some testing contexts. | |
29 if (!connector_.get()) | |
dcheng
2017/03/21 18:56:18
Nit: no .get()
(Also, is this 'null in some testi
blundell
2017/03/23 10:40:33
Done.
| |
30 return; | |
31 | |
32 if (!main_thread_task_runner_->BelongsToCurrentThread()) { | |
33 main_thread_task_runner_->PostTask( | |
34 FROM_HERE, | |
35 base::Bind(&BlinkConnectorImpl::bindInterface, GetWeakPtr(), | |
36 service_name, interface_name, base::Passed(&handle))); | |
37 return; | |
38 } | |
39 | |
40 // Tests might have overridden this interface with a local implementation. | |
41 InterfaceBinderMap* overrides_for_service = | |
42 GetOverridesForService(service_name); | |
43 if (overrides_for_service) { | |
44 auto it = overrides_for_service->find(std::string(interface_name)); | |
45 if (it != overrides_for_service->end()) { | |
46 it->second.Run(std::move(handle)); | |
47 return; | |
48 } | |
49 } | |
50 | |
51 service_manager::Identity service_identity( | |
52 service_name, service_manager::mojom::kInheritUserID); | |
53 connector_->BindInterface(service_identity, interface_name, | |
54 std::move(handle)); | |
55 } | |
56 | |
57 void BlinkConnectorImpl::AddOverrideForTesting( | |
58 const std::string& service_name, | |
59 const std::string& interface_name, | |
60 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { | |
61 if (service_binders_.find(service_name) == service_binders_.end()) | |
62 service_binders_[service_name] = base::MakeUnique<InterfaceBinderMap>(); | |
63 | |
64 (*(service_binders_[service_name].get()))[interface_name] = binder; | |
dcheng
2017/03/21 18:56:18
I think this can be simplified slightly to:
(*(se
blundell
2017/03/23 10:40:33
Done.
| |
65 } | |
66 | |
67 void BlinkConnectorImpl::ClearOverridesForTesting() { | |
68 service_binders_.clear(); | |
69 } | |
70 | |
71 base::WeakPtr<BlinkConnectorImpl> BlinkConnectorImpl::GetWeakPtr() { | |
72 return weak_ptr_factory_.GetWeakPtr(); | |
73 } | |
74 | |
75 BlinkConnectorImpl::InterfaceBinderMap* | |
76 BlinkConnectorImpl::GetOverridesForService(const char* service_name) { | |
77 // Short-circuit out in the case where there are no overrides (always true in | |
78 // production). | |
79 if (service_binders_.empty()) | |
80 return nullptr; | |
81 | |
82 auto it = service_binders_.find(std::string(service_name)); | |
83 | |
84 if (it != service_binders_.end()) | |
85 return it->second.get(); | |
86 | |
87 return nullptr; | |
88 } | |
89 | |
90 } // namespace content | |
OLD | NEW |