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::getInterface(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/02/01 07:45:16
Nit: no .get(). Also... it would be best to just f
| |
30 return; | |
31 | |
32 if (!main_thread_task_runner_->BelongsToCurrentThread()) { | |
33 main_thread_task_runner_->PostTask( | |
34 FROM_HERE, | |
35 base::Bind(&BlinkConnectorImpl::getInterface, GetWeakPtr(), | |
esprehn
2017/02/01 06:02:23
Woah, this round trips through main to connect to
dcheng
2017/02/01 07:45:16
FWIW, this already happens today in https://cs.chr
Ken Rockot(use gerrit already)
2017/02/01 19:16:00
Correct, we already tolerate this behavior. It's n
Ken Rockot(use gerrit already)
2017/02/01 19:16:00
Correct, we already tolerate this behavior. It's n
| |
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)); | |
esprehn
2017/02/01 06:02:23
this should implicitly construct, no need to write
| |
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]->insert( | |
65 std::pair<std::string, Binder>(interface_name, binder)); | |
66 } | |
67 | |
68 void BlinkConnectorImpl::ClearOverridesForTesting() { | |
69 service_binders_.clear(); | |
70 } | |
71 | |
72 base::WeakPtr<BlinkConnectorImpl> BlinkConnectorImpl::GetWeakPtr() { | |
73 return weak_ptr_factory_.GetWeakPtr(); | |
74 } | |
75 | |
76 BlinkConnectorImpl::InterfaceBinderMap* | |
77 BlinkConnectorImpl::GetOverridesForService(const char* service_name) { | |
78 // Short-circuit out in the case where there are no overrides (always true in | |
79 // production). | |
80 if (service_binders_.empty()) | |
81 return nullptr; | |
82 | |
83 auto it = service_binders_.find(std::string(service_name)); | |
84 | |
85 if (it != service_binders_.end()) | |
86 return it->second.get(); | |
87 | |
88 return nullptr; | |
89 } | |
90 | |
91 } // namespace content | |
OLD | NEW |