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 "services/shell/public/cpp/interface_registry.h" | 5 #include "services/shell/public/cpp/interface_registry.h" |
6 | 6 |
7 #include "services/shell/public/cpp/connection.h" | 7 #include "services/shell/public/cpp/connection.h" |
8 | 8 |
9 namespace shell { | 9 namespace shell { |
10 | 10 |
11 InterfaceRegistry::InterfaceRegistry(Connection* connection) | 11 InterfaceRegistry::InterfaceRegistry(Connection* connection) |
12 : InterfaceRegistry(nullptr, nullptr, connection) {} | 12 : binding_(this), connection_(connection) {} |
| 13 InterfaceRegistry::~InterfaceRegistry() {} |
13 | 14 |
14 InterfaceRegistry::InterfaceRegistry( | 15 void InterfaceRegistry::Bind( |
15 mojom::InterfaceProviderPtr remote_interfaces, | 16 mojom::InterfaceProviderRequest local_interfaces_request) { |
16 mojom::InterfaceProviderRequest local_interfaces_request, | 17 DCHECK(!binding_.is_bound()); |
17 Connection* connection) | |
18 : binding_(this), | |
19 connection_(connection), | |
20 remote_interfaces_(std::move(remote_interfaces)) { | |
21 if (!local_interfaces_request.is_pending()) | |
22 local_interfaces_request = GetProxy(&client_handle_); | |
23 binding_.Bind(std::move(local_interfaces_request)); | 18 binding_.Bind(std::move(local_interfaces_request)); |
24 } | 19 } |
25 | 20 |
26 InterfaceRegistry::~InterfaceRegistry() {} | 21 void InterfaceRegistry::RemoveInterface(const std::string& name) { |
27 | 22 auto it = name_to_binder_.find(name); |
28 mojom::InterfaceProviderPtr InterfaceRegistry::TakeClientHandle() { | 23 if (it != name_to_binder_.end()) |
29 return std::move(client_handle_); | 24 name_to_binder_.erase(it); |
30 } | |
31 | |
32 mojom::InterfaceProvider* InterfaceRegistry::GetRemoteInterfaces() { | |
33 return remote_interfaces_.get(); | |
34 } | |
35 | |
36 void InterfaceRegistry::SetRemoteInterfacesConnectionLostClosure( | |
37 const base::Closure& connection_lost_closure) { | |
38 remote_interfaces_.set_connection_error_handler(connection_lost_closure); | |
39 } | 25 } |
40 | 26 |
41 // mojom::InterfaceProvider: | 27 // mojom::InterfaceProvider: |
42 void InterfaceRegistry::GetInterface(const mojo::String& interface_name, | 28 void InterfaceRegistry::GetInterface(const mojo::String& interface_name, |
43 mojo::ScopedMessagePipeHandle handle) { | 29 mojo::ScopedMessagePipeHandle handle) { |
44 auto iter = name_to_binder_.find(interface_name); | 30 auto iter = name_to_binder_.find(interface_name); |
45 if (iter != name_to_binder_.end()) { | 31 if (iter != name_to_binder_.end()) { |
46 iter->second->BindInterface(connection_, interface_name, std::move(handle)); | 32 iter->second->BindInterface(connection_, interface_name, std::move(handle)); |
47 } else if (connection_ && connection_->AllowsInterface(interface_name)) { | 33 } else if (connection_ && connection_->AllowsInterface(interface_name)) { |
48 LOG(ERROR) << "Connection CapabilityFilter prevented binding to " | 34 LOG(ERROR) << "Connection CapabilityFilter prevented binding to " |
49 << "interface: " << interface_name << " connection_name:" | 35 << "interface: " << interface_name << " connection_name:" |
50 << connection_->GetConnectionName() << " remote_name:" | 36 << connection_->GetConnectionName() << " remote_name:" |
51 << connection_->GetRemoteIdentity().name(); | 37 << connection_->GetRemoteIdentity().name(); |
52 } | 38 } |
53 } | 39 } |
54 | 40 |
55 bool InterfaceRegistry::SetInterfaceBinderForName( | 41 bool InterfaceRegistry::SetInterfaceBinderForName( |
56 std::unique_ptr<InterfaceBinder> binder, | 42 std::unique_ptr<InterfaceBinder> binder, |
57 const std::string& interface_name) { | 43 const std::string& interface_name) { |
58 if (!connection_ || | 44 if (!connection_ || |
59 (connection_ && connection_->AllowsInterface(interface_name))) { | 45 (connection_ && connection_->AllowsInterface(interface_name))) { |
60 RemoveInterfaceBinderForName(interface_name); | 46 RemoveInterface(interface_name); |
61 name_to_binder_[interface_name] = std::move(binder); | 47 name_to_binder_[interface_name] = std::move(binder); |
62 return true; | 48 return true; |
63 } | 49 } |
64 return false; | 50 return false; |
65 } | 51 } |
66 | 52 |
67 void InterfaceRegistry::RemoveInterfaceBinderForName( | |
68 const std::string& interface_name) { | |
69 NameToInterfaceBinderMap::iterator it = name_to_binder_.find(interface_name); | |
70 if (it == name_to_binder_.end()) | |
71 return; | |
72 name_to_binder_.erase(it); | |
73 } | |
74 | |
75 } // namespace shell | 53 } // namespace shell |
OLD | NEW |