| OLD | NEW |
| 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 "mojo/shell/public/cpp/lib/connection_impl.h" | 5 #include "mojo/shell/public/cpp/lib/connection_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "mojo/shell/public/cpp/connection.h" | 13 #include "mojo/shell/public/cpp/connection.h" |
| 14 #include "mojo/shell/public/cpp/service_connector.h" | 14 #include "mojo/shell/public/cpp/interface_binder.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace internal { | 17 namespace internal { |
| 18 | 18 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
| 20 // ConnectionImpl, public: | 20 // ConnectionImpl, public: |
| 21 | 21 |
| 22 ConnectionImpl::ConnectionImpl( | 22 ConnectionImpl::ConnectionImpl( |
| 23 const std::string& connection_url, | 23 const std::string& connection_url, |
| 24 const std::string& remote_url, | 24 const std::string& remote_url, |
| 25 uint32_t remote_id, | 25 uint32_t remote_id, |
| 26 ServiceProviderPtr remote_services, | 26 ServiceProviderPtr remote_services, |
| 27 InterfaceRequest<ServiceProvider> local_services, | 27 InterfaceRequest<ServiceProvider> local_services, |
| 28 const std::set<std::string>& allowed_interfaces) | 28 const std::set<std::string>& allowed_interfaces) |
| 29 : connection_url_(connection_url), | 29 : connection_url_(connection_url), |
| 30 remote_url_(remote_url), | 30 remote_url_(remote_url), |
| 31 remote_id_(remote_id), | 31 remote_id_(remote_id), |
| 32 content_handler_id_(0u), | 32 content_handler_id_(0u), |
| 33 remote_ids_valid_(false), | 33 remote_ids_valid_(false), |
| 34 local_binding_(this), | 34 local_binding_(this), |
| 35 remote_service_provider_(std::move(remote_services)), | 35 remote_service_provider_(std::move(remote_services)), |
| 36 allowed_interfaces_(allowed_interfaces), | 36 allowed_interfaces_(allowed_interfaces), |
| 37 allow_all_interfaces_(allowed_interfaces_.size() == 1 && | 37 allow_all_interfaces_(allowed_interfaces_.size() == 1 && |
| 38 allowed_interfaces_.count("*") == 1), | 38 allowed_interfaces_.count("*") == 1), |
| 39 default_connector_(nullptr), | 39 default_binder_(nullptr), |
| 40 weak_factory_(this) { | 40 weak_factory_(this) { |
| 41 if (local_services.is_pending()) | 41 if (local_services.is_pending()) |
| 42 local_binding_.Bind(std::move(local_services)); | 42 local_binding_.Bind(std::move(local_services)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 ConnectionImpl::ConnectionImpl() | 45 ConnectionImpl::ConnectionImpl() |
| 46 : remote_id_(shell::mojom::Shell::kInvalidApplicationID), | 46 : remote_id_(shell::mojom::Shell::kInvalidApplicationID), |
| 47 content_handler_id_(shell::mojom::Shell::kInvalidApplicationID), | 47 content_handler_id_(shell::mojom::Shell::kInvalidApplicationID), |
| 48 remote_ids_valid_(false), | 48 remote_ids_valid_(false), |
| 49 local_binding_(this), | 49 local_binding_(this), |
| 50 allow_all_interfaces_(true), | 50 allow_all_interfaces_(true), |
| 51 default_connector_(nullptr), | 51 default_binder_(nullptr), |
| 52 weak_factory_(this) { | 52 weak_factory_(this) { |
| 53 } | 53 } |
| 54 | 54 |
| 55 ConnectionImpl::~ConnectionImpl() { | 55 ConnectionImpl::~ConnectionImpl() { |
| 56 for (NameToServiceConnectorMap::iterator i = | 56 for (auto& i : name_to_binder_) |
| 57 name_to_service_connector_.begin(); | 57 delete i.second; |
| 58 i != name_to_service_connector_.end(); ++i) { | 58 name_to_binder_.clear(); |
| 59 delete i->second; | |
| 60 } | |
| 61 name_to_service_connector_.clear(); | |
| 62 } | 59 } |
| 63 | 60 |
| 64 shell::mojom::Shell::ConnectToApplicationCallback | 61 shell::mojom::Shell::ConnectToApplicationCallback |
| 65 ConnectionImpl::GetConnectToApplicationCallback() { | 62 ConnectionImpl::GetConnectToApplicationCallback() { |
| 66 return base::Bind(&ConnectionImpl::OnGotRemoteIDs, | 63 return base::Bind(&ConnectionImpl::OnGotRemoteIDs, |
| 67 weak_factory_.GetWeakPtr()); | 64 weak_factory_.GetWeakPtr()); |
| 68 } | 65 } |
| 69 | 66 |
| 70 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
| 71 // ConnectionImpl, Connection implementation: | 68 // ConnectionImpl, Connection implementation: |
| 72 | 69 |
| 73 void ConnectionImpl::SetServiceConnector(ServiceConnector* service_connector) { | 70 void ConnectionImpl::SetDefaultInterfaceBinder(InterfaceBinder* binder) { |
| 74 default_connector_ = service_connector; | 71 default_binder_ = binder; |
| 75 } | 72 } |
| 76 | 73 |
| 77 bool ConnectionImpl::SetServiceConnectorForName( | 74 bool ConnectionImpl::SetInterfaceBinderForName( |
| 78 ServiceConnector* service_connector, | 75 InterfaceBinder* binder, |
| 79 const std::string& interface_name) { | 76 const std::string& interface_name) { |
| 80 if (allow_all_interfaces_ || | 77 if (allow_all_interfaces_ || |
| 81 allowed_interfaces_.count(interface_name)) { | 78 allowed_interfaces_.count(interface_name)) { |
| 82 RemoveServiceConnectorForName(interface_name); | 79 RemoveInterfaceBinderForName(interface_name); |
| 83 name_to_service_connector_[interface_name] = service_connector; | 80 name_to_binder_[interface_name] = binder; |
| 84 return true; | 81 return true; |
| 85 } | 82 } |
| 86 LOG(WARNING) << "CapabilityFilter prevented connection to interface: " | 83 LOG(WARNING) << "CapabilityFilter prevented connection to interface: " |
| 87 << interface_name << " connection_url:" << connection_url_ | 84 << interface_name << " connection_url:" << connection_url_ |
| 88 << " remote_url:" << remote_url_; | 85 << " remote_url:" << remote_url_; |
| 89 return false; | 86 return false; |
| 90 } | 87 } |
| 91 | 88 |
| 92 const std::string& ConnectionImpl::GetConnectionURL() { | 89 const std::string& ConnectionImpl::GetConnectionURL() { |
| 93 return connection_url_; | 90 return connection_url_; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 } | 133 } |
| 137 | 134 |
| 138 base::WeakPtr<Connection> ConnectionImpl::GetWeakPtr() { | 135 base::WeakPtr<Connection> ConnectionImpl::GetWeakPtr() { |
| 139 return weak_factory_.GetWeakPtr(); | 136 return weak_factory_.GetWeakPtr(); |
| 140 } | 137 } |
| 141 | 138 |
| 142 //////////////////////////////////////////////////////////////////////////////// | 139 //////////////////////////////////////////////////////////////////////////////// |
| 143 // ConnectionImpl, ServiceProvider implementation: | 140 // ConnectionImpl, ServiceProvider implementation: |
| 144 | 141 |
| 145 void ConnectionImpl::ConnectToService(const mojo::String& interface_name, | 142 void ConnectionImpl::ConnectToService(const mojo::String& interface_name, |
| 146 ScopedMessagePipeHandle client_handle) { | 143 ScopedMessagePipeHandle handle) { |
| 147 auto iter = name_to_service_connector_.find(interface_name); | 144 auto iter = name_to_binder_.find(interface_name); |
| 148 if (iter != name_to_service_connector_.end()) { | 145 if (iter != name_to_binder_.end()) |
| 149 iter->second->ConnectToService(this, interface_name, | 146 iter->second->BindInterface(this, interface_name, std::move(handle)); |
| 150 std::move(client_handle)); | 147 else if (default_binder_) |
| 151 } | 148 default_binder_->BindInterface(this, interface_name, std::move(handle)); |
| 152 if (default_connector_) { | |
| 153 default_connector_->ConnectToService(this, interface_name, | |
| 154 std::move(client_handle)); | |
| 155 } | |
| 156 } | 149 } |
| 157 | 150 |
| 158 //////////////////////////////////////////////////////////////////////////////// | 151 //////////////////////////////////////////////////////////////////////////////// |
| 159 // ConnectionImpl, private: | 152 // ConnectionImpl, private: |
| 160 | 153 |
| 161 void ConnectionImpl::RemoveServiceConnectorForName( | 154 void ConnectionImpl::RemoveInterfaceBinderForName( |
| 162 const std::string& interface_name) { | 155 const std::string& interface_name) { |
| 163 NameToServiceConnectorMap::iterator it = | 156 NameToInterfaceBinderMap::iterator it = name_to_binder_.find(interface_name); |
| 164 name_to_service_connector_.find(interface_name); | 157 if (it == name_to_binder_.end()) |
| 165 if (it == name_to_service_connector_.end()) | |
| 166 return; | 158 return; |
| 167 delete it->second; | 159 delete it->second; |
| 168 name_to_service_connector_.erase(it); | 160 name_to_binder_.erase(it); |
| 169 } | 161 } |
| 170 | 162 |
| 171 void ConnectionImpl::OnGotRemoteIDs(uint32_t target_application_id, | 163 void ConnectionImpl::OnGotRemoteIDs(uint32_t target_application_id, |
| 172 uint32_t content_handler_id) { | 164 uint32_t content_handler_id) { |
| 173 DCHECK(!remote_ids_valid_); | 165 DCHECK(!remote_ids_valid_); |
| 174 remote_ids_valid_ = true; | 166 remote_ids_valid_ = true; |
| 175 | 167 |
| 176 remote_id_ = target_application_id; | 168 remote_id_ = target_application_id; |
| 177 content_handler_id_ = content_handler_id; | 169 content_handler_id_ = content_handler_id; |
| 178 std::vector<Closure> callbacks; | 170 std::vector<Closure> callbacks; |
| 179 callbacks.swap(remote_id_callbacks_); | 171 callbacks.swap(remote_id_callbacks_); |
| 180 for (auto callback : callbacks) | 172 for (auto callback : callbacks) |
| 181 callback.Run(); | 173 callback.Run(); |
| 182 } | 174 } |
| 183 | 175 |
| 184 } // namespace internal | 176 } // namespace internal |
| 185 } // namespace mojo | 177 } // namespace mojo |
| OLD | NEW |