OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/shell/public/cpp/interface_registry.h" | |
6 | |
7 #include <sstream> | |
8 | |
9 #include "mojo/public/cpp/bindings/message.h" | |
10 #include "services/shell/public/cpp/connection.h" | |
11 | |
12 namespace shell { | |
13 | |
14 InterfaceRegistry::InterfaceRegistry() | |
15 : binding_(this), allow_all_interfaces_(true), weak_factory_(this) {} | |
16 | |
17 InterfaceRegistry::InterfaceRegistry( | |
18 const Identity& local_identity, | |
19 const Identity& remote_identity, | |
20 const Interfaces& allowed_interfaces) | |
21 : binding_(this), | |
22 local_identity_(local_identity), | |
23 remote_identity_(remote_identity), | |
24 allowed_interfaces_(allowed_interfaces), | |
25 allow_all_interfaces_(allowed_interfaces_.size() == 1 && | |
26 allowed_interfaces_.count("*") == 1), | |
27 weak_factory_(this) {} | |
28 | |
29 InterfaceRegistry::~InterfaceRegistry() {} | |
30 | |
31 void InterfaceRegistry::Bind( | |
32 mojom::InterfaceProviderRequest local_interfaces_request) { | |
33 DCHECK(!binding_.is_bound()); | |
34 binding_.Bind(std::move(local_interfaces_request)); | |
35 } | |
36 | |
37 base::WeakPtr<InterfaceRegistry> InterfaceRegistry::GetWeakPtr() { | |
38 return weak_factory_.GetWeakPtr(); | |
39 } | |
40 | |
41 bool InterfaceRegistry::AddInterface( | |
42 const std::string& name, | |
43 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& callback, | |
44 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | |
45 return SetInterfaceBinderForName( | |
46 base::MakeUnique<internal::GenericCallbackBinder>(callback, task_runner), | |
47 name); | |
48 } | |
49 | |
50 void InterfaceRegistry::RemoveInterface(const std::string& name) { | |
51 auto it = name_to_binder_.find(name); | |
52 if (it != name_to_binder_.end()) | |
53 name_to_binder_.erase(it); | |
54 } | |
55 | |
56 void InterfaceRegistry::PauseBinding() { | |
57 DCHECK(!is_paused_); | |
58 is_paused_ = true; | |
59 } | |
60 | |
61 void InterfaceRegistry::ResumeBinding() { | |
62 DCHECK(is_paused_); | |
63 is_paused_ = false; | |
64 | |
65 while (!pending_interface_requests_.empty()) { | |
66 auto& request = pending_interface_requests_.front(); | |
67 GetInterface(request.first, std::move(request.second)); | |
68 pending_interface_requests_.pop(); | |
69 } | |
70 } | |
71 | |
72 void InterfaceRegistry::GetInterfaceNames( | |
73 std::set<std::string>* interface_names) { | |
74 DCHECK(interface_names); | |
75 for (auto& entry : name_to_binder_) | |
76 interface_names->insert(entry.first); | |
77 } | |
78 | |
79 void InterfaceRegistry::SetConnectionLostClosure( | |
80 const base::Closure& connection_lost_closure) { | |
81 binding_.set_connection_error_handler(connection_lost_closure); | |
82 } | |
83 | |
84 // mojom::InterfaceProvider: | |
85 void InterfaceRegistry::GetInterface(const std::string& interface_name, | |
86 mojo::ScopedMessagePipeHandle handle) { | |
87 if (is_paused_) { | |
88 pending_interface_requests_.emplace(interface_name, std::move(handle)); | |
89 return; | |
90 } | |
91 | |
92 auto iter = name_to_binder_.find(interface_name); | |
93 if (iter != name_to_binder_.end()) { | |
94 iter->second->BindInterface(remote_identity_, | |
95 interface_name, | |
96 std::move(handle)); | |
97 } else if (!CanBindRequestForInterface(interface_name)) { | |
98 std::stringstream ss; | |
99 ss << "Capability spec prevented service " << remote_identity_.name() | |
100 << " from binding interface: " << interface_name | |
101 << " exposed by: " << local_identity_.name(); | |
102 LOG(ERROR) << ss.str(); | |
103 mojo::ReportBadMessage(ss.str()); | |
104 } else if (!default_binder_.is_null()) { | |
105 default_binder_.Run(interface_name, std::move(handle)); | |
106 } else { | |
107 LOG(ERROR) << "Failed to locate a binder for interface: " << interface_name | |
108 << " requested by: " << remote_identity_.name() | |
109 << " exposed by: " << local_identity_.name(); | |
110 } | |
111 } | |
112 | |
113 bool InterfaceRegistry::SetInterfaceBinderForName( | |
114 std::unique_ptr<InterfaceBinder> binder, | |
115 const std::string& interface_name) { | |
116 if (CanBindRequestForInterface(interface_name)) { | |
117 RemoveInterface(interface_name); | |
118 name_to_binder_[interface_name] = std::move(binder); | |
119 return true; | |
120 } | |
121 return false; | |
122 } | |
123 | |
124 bool InterfaceRegistry::CanBindRequestForInterface( | |
125 const std::string& interface_name) const { | |
126 return allow_all_interfaces_ || allowed_interfaces_.count(interface_name); | |
127 } | |
128 | |
129 } // namespace shell | |
OLD | NEW |