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

Side by Side Diff: services/service_manager/public/cpp/service_context.cc

Issue 2859343003: Enable overriding interface binders for any services running in current process. (Closed)
Patch Set: Created 3 years, 7 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "services/service_manager/public/cpp/service_context.h" 5 #include "services/service_manager/public/cpp/service_context.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "mojo/public/cpp/bindings/interface_request.h" 12 #include "mojo/public/cpp/bindings/interface_request.h"
13 #include "services/service_manager/public/cpp/binder_registry.h"
12 #include "services/service_manager/public/cpp/connector.h" 14 #include "services/service_manager/public/cpp/connector.h"
13 #include "services/service_manager/public/cpp/service.h" 15 #include "services/service_manager/public/cpp/service.h"
14 16
15 namespace service_manager { 17 namespace service_manager {
16 18
19 namespace {
20
21 using IdentityToBinderRegistryMap = std::map<Identity, BinderRegistry>;
22
23 base::LazyInstance<std::unique_ptr<IdentityToBinderRegistryMap>>::Leaky
24 g_overridden_binder_registries = LAZY_INSTANCE_INITIALIZER;
25
26 // Returns the overridden binder registry which can intercept interface bind
27 // requests to the |service|, returns nullptr if no such one.
28 BinderRegistry* GetGlobalBinderRegistryForService(const Identity& service) {
29 const auto& registries = g_overridden_binder_registries.Get();
30 if (registries) {
31 // Find the registry matching the exact service identity.
32 auto it = registries->find(service);
33 if (it != registries->end())
34 return &it->second;
35 // Find the general registry matching the service name.
36 it = registries->find(Identity(service.name()));
37 if (it != registries->end())
38 return &it->second;
39 }
40
41 return nullptr;
42 }
43
44 } // namespace
45
17 //////////////////////////////////////////////////////////////////////////////// 46 ////////////////////////////////////////////////////////////////////////////////
18 // ServiceContext, public: 47 // ServiceContext, public:
19 48
49 // static
50 void ServiceContext::TestApi::SetGlobalBinder(
51 const Identity& service,
52 const std::string& interface_name,
53 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder,
54 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) {
55 if (!g_overridden_binder_registries.Get()) {
56 g_overridden_binder_registries.Get() =
57 base::MakeUnique<IdentityToBinderRegistryMap>();
58 }
59
60 (*g_overridden_binder_registries.Get())[service].AddInterface(
61 interface_name, binder, task_runner);
62 }
63
64 // static
65 void ServiceContext::TestApi::ClearGlobalBinders(const Identity& service) {
66 if (!g_overridden_binder_registries.Get()) {
67 return;
68 }
69
70 g_overridden_binder_registries.Get()->erase(service);
71 }
72
20 ServiceContext::ServiceContext( 73 ServiceContext::ServiceContext(
21 std::unique_ptr<service_manager::Service> service, 74 std::unique_ptr<service_manager::Service> service,
22 mojom::ServiceRequest request, 75 mojom::ServiceRequest request,
23 std::unique_ptr<Connector> connector, 76 std::unique_ptr<Connector> connector,
24 mojom::ConnectorRequest connector_request) 77 mojom::ConnectorRequest connector_request)
25 : pending_connector_request_(std::move(connector_request)), 78 : pending_connector_request_(std::move(connector_request)),
26 service_(std::move(service)), 79 service_(std::move(service)),
27 binding_(this, std::move(request)), 80 binding_(this, std::move(request)),
28 connector_(std::move(connector)), 81 connector_(std::move(connector)),
29 weak_factory_(this) { 82 weak_factory_(this) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 } 129 }
77 130
78 void ServiceContext::OnBindInterface( 131 void ServiceContext::OnBindInterface(
79 const BindSourceInfo& source_info, 132 const BindSourceInfo& source_info,
80 const std::string& interface_name, 133 const std::string& interface_name,
81 mojo::ScopedMessagePipeHandle interface_pipe, 134 mojo::ScopedMessagePipeHandle interface_pipe,
82 const OnBindInterfaceCallback& callback) { 135 const OnBindInterfaceCallback& callback) {
83 // Acknowledge the request regardless of whether it's accepted. 136 // Acknowledge the request regardless of whether it's accepted.
84 callback.Run(); 137 callback.Run();
85 138
139 BinderRegistry* global_registry =
140 GetGlobalBinderRegistryForService(identity_);
141 if (global_registry && global_registry->CanBindInterface(interface_name)) {
142 // Just use the binder overridden globally.
143 global_registry->BindInterface(source_info, interface_name,
144 std::move(interface_pipe));
145 return;
146 }
86 service_->OnBindInterface(source_info, interface_name, 147 service_->OnBindInterface(source_info, interface_name,
87 std::move(interface_pipe)); 148 std::move(interface_pipe));
88 } 149 }
89 150
90 //////////////////////////////////////////////////////////////////////////////// 151 ////////////////////////////////////////////////////////////////////////////////
91 // ServiceContext, private: 152 // ServiceContext, private:
92 153
93 void ServiceContext::OnConnectionError() { 154 void ServiceContext::OnConnectionError() {
94 if (service_->OnServiceManagerConnectionLost()) { 155 if (service_->OnServiceManagerConnectionLost()) {
95 // CAUTION: May delete |this|. 156 // CAUTION: May delete |this|.
96 QuitNow(); 157 QuitNow();
97 } 158 }
98 } 159 }
99 160
100 } // namespace service_manager 161 } // namespace service_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698