| 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/interface_binder.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 InterfaceProviderPtr remote_interfaces, |
| 27 InterfaceRequest<ServiceProvider> local_services, | 27 InterfaceRequest<InterfaceProvider> local_interfaces, |
| 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_interfaces_(std::move(remote_interfaces)), |
| 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_binder_(nullptr), | 39 default_binder_(nullptr), |
| 40 weak_factory_(this) { | 40 weak_factory_(this) { |
| 41 if (local_services.is_pending()) | 41 if (local_interfaces.is_pending()) |
| 42 local_binding_.Bind(std::move(local_services)); | 42 local_binding_.Bind(std::move(local_interfaces)); |
| 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_binder_(nullptr), | 51 default_binder_(nullptr), |
| 52 weak_factory_(this) { | 52 weak_factory_(this) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 87 } |
| 88 | 88 |
| 89 const std::string& ConnectionImpl::GetConnectionURL() { | 89 const std::string& ConnectionImpl::GetConnectionURL() { |
| 90 return connection_url_; | 90 return connection_url_; |
| 91 } | 91 } |
| 92 | 92 |
| 93 const std::string& ConnectionImpl::GetRemoteApplicationURL() { | 93 const std::string& ConnectionImpl::GetRemoteApplicationURL() { |
| 94 return remote_url_; | 94 return remote_url_; |
| 95 } | 95 } |
| 96 | 96 |
| 97 ServiceProvider* ConnectionImpl::GetRemoteInterfaces() { | 97 InterfaceProvider* ConnectionImpl::GetRemoteInterfaces() { |
| 98 return remote_service_provider_.get(); | 98 return remote_interfaces_.get(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 ServiceProvider* ConnectionImpl::GetLocalInterfaces() { | 101 InterfaceProvider* ConnectionImpl::GetLocalInterfaces() { |
| 102 return this; | 102 return this; |
| 103 } | 103 } |
| 104 | 104 |
| 105 void ConnectionImpl::SetRemoteServiceProviderConnectionErrorHandler( | 105 void ConnectionImpl::SetRemoteInterfaceProviderConnectionErrorHandler( |
| 106 const Closure& handler) { | 106 const Closure& handler) { |
| 107 remote_service_provider_.set_connection_error_handler(handler); | 107 remote_interfaces_.set_connection_error_handler(handler); |
| 108 } | 108 } |
| 109 | 109 |
| 110 bool ConnectionImpl::GetRemoteApplicationID(uint32_t* remote_id) const { | 110 bool ConnectionImpl::GetRemoteApplicationID(uint32_t* remote_id) const { |
| 111 if (!remote_ids_valid_) | 111 if (!remote_ids_valid_) |
| 112 return false; | 112 return false; |
| 113 | 113 |
| 114 *remote_id = remote_id_; | 114 *remote_id = remote_id_; |
| 115 return true; | 115 return true; |
| 116 } | 116 } |
| 117 | 117 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 130 return; | 130 return; |
| 131 } | 131 } |
| 132 remote_id_callbacks_.push_back(callback); | 132 remote_id_callbacks_.push_back(callback); |
| 133 } | 133 } |
| 134 | 134 |
| 135 base::WeakPtr<Connection> ConnectionImpl::GetWeakPtr() { | 135 base::WeakPtr<Connection> ConnectionImpl::GetWeakPtr() { |
| 136 return weak_factory_.GetWeakPtr(); | 136 return weak_factory_.GetWeakPtr(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 //////////////////////////////////////////////////////////////////////////////// | 139 //////////////////////////////////////////////////////////////////////////////// |
| 140 // ConnectionImpl, ServiceProvider implementation: | 140 // ConnectionImpl, InterfaceProvider implementation: |
| 141 | 141 |
| 142 void ConnectionImpl::ConnectToService(const mojo::String& interface_name, | 142 void ConnectionImpl::GetInterface(const mojo::String& interface_name, |
| 143 ScopedMessagePipeHandle handle) { | 143 ScopedMessagePipeHandle handle) { |
| 144 auto iter = name_to_binder_.find(interface_name); | 144 auto iter = name_to_binder_.find(interface_name); |
| 145 if (iter != name_to_binder_.end()) | 145 InterfaceBinder* binder = iter != name_to_binder_.end() ? iter->second : |
| 146 iter->second->BindInterface(this, interface_name, std::move(handle)); | 146 default_binder_; |
| 147 else if (default_binder_) | 147 if (binder) |
| 148 default_binder_->BindInterface(this, interface_name, std::move(handle)); | 148 binder->BindInterface(this, interface_name, std::move(handle)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 //////////////////////////////////////////////////////////////////////////////// | 151 //////////////////////////////////////////////////////////////////////////////// |
| 152 // ConnectionImpl, private: | 152 // ConnectionImpl, private: |
| 153 | 153 |
| 154 void ConnectionImpl::RemoveInterfaceBinderForName( | 154 void ConnectionImpl::RemoveInterfaceBinderForName( |
| 155 const std::string& interface_name) { | 155 const std::string& interface_name) { |
| 156 NameToInterfaceBinderMap::iterator it = name_to_binder_.find(interface_name); | 156 NameToInterfaceBinderMap::iterator it = name_to_binder_.find(interface_name); |
| 157 if (it == name_to_binder_.end()) | 157 if (it == name_to_binder_.end()) |
| 158 return; | 158 return; |
| 159 delete it->second; | 159 delete it->second; |
| 160 name_to_binder_.erase(it); | 160 name_to_binder_.erase(it); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void ConnectionImpl::OnGotRemoteIDs(uint32_t target_application_id, | 163 void ConnectionImpl::OnGotRemoteIDs(uint32_t target_application_id, |
| 164 uint32_t content_handler_id) { | 164 uint32_t content_handler_id) { |
| 165 DCHECK(!remote_ids_valid_); | 165 DCHECK(!remote_ids_valid_); |
| 166 remote_ids_valid_ = true; | 166 remote_ids_valid_ = true; |
| 167 | 167 |
| 168 remote_id_ = target_application_id; | 168 remote_id_ = target_application_id; |
| 169 content_handler_id_ = content_handler_id; | 169 content_handler_id_ = content_handler_id; |
| 170 std::vector<Closure> callbacks; | 170 std::vector<Closure> callbacks; |
| 171 callbacks.swap(remote_id_callbacks_); | 171 callbacks.swap(remote_id_callbacks_); |
| 172 for (auto callback : callbacks) | 172 for (auto callback : callbacks) |
| 173 callback.Run(); | 173 callback.Run(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 } // namespace internal | 176 } // namespace internal |
| 177 } // namespace mojo | 177 } // namespace mojo |
| OLD | NEW |