| 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 "content/common/mojo/mojo_shell_connection_impl.h" | 5 #include "content/common/mojo/mojo_shell_connection_impl.h" |
| 6 | 6 |
| 7 #include <queue> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | 7 #include "base/bind.h" |
| 12 #include "base/callback_helpers.h" | |
| 13 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 16 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_local.h" |
| 17 #include "content/common/mojo/embedded_application_runner.h" | 11 #include "content/common/mojo/embedded_application_runner.h" |
| 18 #include "content/public/common/connection_filter.h" | |
| 19 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 20 #include "mojo/public/cpp/system/message_pipe.h" | |
| 21 #include "services/shell/public/cpp/service.h" | 12 #include "services/shell/public/cpp/service.h" |
| 22 #include "services/shell/public/cpp/service_context.h" | 13 #include "services/shell/public/cpp/service_context.h" |
| 23 #include "services/shell/public/interfaces/service_factory.mojom.h" | |
| 24 #include "services/shell/runner/common/client_util.h" | 14 #include "services/shell/runner/common/client_util.h" |
| 25 | 15 |
| 26 namespace content { | 16 namespace content { |
| 27 namespace { | 17 namespace { |
| 28 | 18 |
| 29 base::LazyInstance<std::unique_ptr<MojoShellConnection>>::Leaky | 19 using MojoShellConnectionPtr = |
| 30 g_connection_for_process = LAZY_INSTANCE_INITIALIZER; | 20 base::ThreadLocalPointer<MojoShellConnection>; |
| 21 |
| 22 // Env is thread local so that aura may be used on multiple threads. |
| 23 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = |
| 24 LAZY_INSTANCE_INITIALIZER; |
| 31 | 25 |
| 32 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; | 26 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; |
| 33 | 27 |
| 34 } // namespace | 28 } // namespace |
| 35 | 29 |
| 36 // A ref-counted object which owns the IO thread state of a | |
| 37 // MojoShellConnectionImpl. This includes Service and ServiceFactory | |
| 38 // bindings. | |
| 39 class MojoShellConnectionImpl::IOThreadContext | |
| 40 : public base::RefCountedThreadSafe<IOThreadContext>, | |
| 41 public shell::Service, | |
| 42 public shell::InterfaceFactory<shell::mojom::ServiceFactory>, | |
| 43 public shell::mojom::ServiceFactory { | |
| 44 public: | |
| 45 using InitializeCallback = base::Callback<void(const shell::Identity&)>; | |
| 46 using ServiceFactoryCallback = | |
| 47 base::Callback<void(shell::mojom::ServiceRequest, const mojo::String&)>; | |
| 48 | |
| 49 IOThreadContext(shell::mojom::ServiceRequest service_request, | |
| 50 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | |
| 51 std::unique_ptr<shell::Connector> io_thread_connector, | |
| 52 shell::mojom::ConnectorRequest connector_request) | |
| 53 : pending_service_request_(std::move(service_request)), | |
| 54 io_task_runner_(io_task_runner), | |
| 55 io_thread_connector_(std::move(io_thread_connector)), | |
| 56 pending_connector_request_(std::move(connector_request)) { | |
| 57 // This will be reattached by any of the IO thread functions on first call. | |
| 58 io_thread_checker_.DetachFromThread(); | |
| 59 } | |
| 60 | |
| 61 // Safe to call from any thread. | |
| 62 void Start(const InitializeCallback& initialize_callback, | |
| 63 const ServiceFactoryCallback& create_service_callback, | |
| 64 const base::Closure& stop_callback) { | |
| 65 DCHECK(!started_); | |
| 66 | |
| 67 started_ = true; | |
| 68 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 69 initialize_handler_ = initialize_callback; | |
| 70 create_service_callback_ = create_service_callback; | |
| 71 stop_callback_ = stop_callback; | |
| 72 io_task_runner_->PostTask( | |
| 73 FROM_HERE, base::Bind(&IOThreadContext::StartOnIOThread, this)); | |
| 74 } | |
| 75 | |
| 76 // Safe to call from whichever thread called Start() (or may have called | |
| 77 // Start()). Must be called before IO thread shutdown. | |
| 78 void ShutDown() { | |
| 79 if (!started_) | |
| 80 return; | |
| 81 | |
| 82 bool posted = io_task_runner_->PostTask( | |
| 83 FROM_HERE, base::Bind(&IOThreadContext::ShutDownOnIOThread, this)); | |
| 84 DCHECK(posted); | |
| 85 } | |
| 86 | |
| 87 // Safe to call any time before Start() is called. | |
| 88 void AddConnectionFilter(std::unique_ptr<ConnectionFilter> filter) { | |
| 89 DCHECK(!started_); | |
| 90 connection_filters_.emplace_back(std::move(filter)); | |
| 91 } | |
| 92 | |
| 93 // Safe to call any time before Start() is called. | |
| 94 void SetDefaultBinderForBrowserConnection( | |
| 95 const shell::InterfaceRegistry::Binder& binder) { | |
| 96 DCHECK(!started_); | |
| 97 default_browser_binder_ = base::Bind( | |
| 98 &IOThreadContext::CallBinderOnTaskRunner, | |
| 99 base::ThreadTaskRunnerHandle::Get(), binder); | |
| 100 } | |
| 101 | |
| 102 // Safe to call any time after Start() is called. | |
| 103 void GetRemoteInterface(const mojo::String& interface_name, | |
| 104 mojo::ScopedMessagePipeHandle request_handle) { | |
| 105 DCHECK(started_); | |
| 106 io_task_runner_->PostTask( | |
| 107 FROM_HERE, | |
| 108 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, this, | |
| 109 interface_name, base::Passed(&request_handle))); | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 friend class base::RefCountedThreadSafe<IOThreadContext>; | |
| 114 | |
| 115 ~IOThreadContext() override {} | |
| 116 | |
| 117 void StartOnIOThread() { | |
| 118 // Should bind |io_thread_checker_| to the context's thread. | |
| 119 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 120 service_context_.reset(new shell::ServiceContext( | |
| 121 this, std::move(pending_service_request_), | |
| 122 std::move(io_thread_connector_), | |
| 123 std::move(pending_connector_request_))); | |
| 124 } | |
| 125 | |
| 126 void ShutDownOnIOThread() { | |
| 127 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 128 service_context_.reset(); | |
| 129 factory_bindings_.CloseAllBindings(); | |
| 130 } | |
| 131 | |
| 132 void GetRemoteInterfaceOnIOThread( | |
| 133 const mojo::String& interface_name, | |
| 134 mojo::ScopedMessagePipeHandle request_handle) { | |
| 135 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 136 if (browser_connection_) { | |
| 137 browser_connection_->GetRemoteInterfaces()->GetInterface( | |
| 138 interface_name, std::move(request_handle)); | |
| 139 } else { | |
| 140 pending_remote_interface_requests_.emplace(interface_name, | |
| 141 std::move(request_handle)); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 void OnBrowserConnectionLost() { | |
| 146 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 147 browser_connection_ = nullptr; | |
| 148 } | |
| 149 | |
| 150 ///////////////////////////////////////////////////////////////////////////// | |
| 151 // shell::Service implementation | |
| 152 | |
| 153 void OnStart(shell::Connector* connector, | |
| 154 const shell::Identity& identity, | |
| 155 uint32_t id) override { | |
| 156 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 157 DCHECK(!initialize_handler_.is_null()); | |
| 158 | |
| 159 InitializeCallback handler = base::ResetAndReturn(&initialize_handler_); | |
| 160 callback_task_runner_->PostTask(FROM_HERE, base::Bind(handler, identity)); | |
| 161 } | |
| 162 | |
| 163 bool OnConnect(shell::Connection* connection) override { | |
| 164 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 165 std::string remote_app = connection->GetRemoteIdentity().name(); | |
| 166 if (remote_app == "mojo:shell") { | |
| 167 // Only expose the SCF interface to the shell. | |
| 168 connection->AddInterface<shell::mojom::ServiceFactory>(this); | |
| 169 return true; | |
| 170 } | |
| 171 | |
| 172 bool accept = false; | |
| 173 for (auto& filter : connection_filters_) { | |
| 174 accept = accept || | |
| 175 filter->OnConnect(connection, service_context_->connector()); | |
| 176 } | |
| 177 | |
| 178 // Capture the browser connection if possible. | |
| 179 if (remote_app == "exe:content_browser" && !browser_connection_) { | |
| 180 browser_connection_ = connection; | |
| 181 connection->GetInterfaceRegistry()->set_default_binder( | |
| 182 default_browser_binder_); | |
| 183 connection->SetConnectionLostClosure( | |
| 184 base::Bind(&IOThreadContext::OnBrowserConnectionLost, this)); | |
| 185 shell::InterfaceProvider* remote_interfaces = | |
| 186 connection->GetRemoteInterfaces(); | |
| 187 | |
| 188 // Flush any pending outgoing interface requests. | |
| 189 while (!pending_remote_interface_requests_.empty()) { | |
| 190 auto& request = pending_remote_interface_requests_.front(); | |
| 191 remote_interfaces->GetInterface( | |
| 192 request.first, std::move(request.second)); | |
| 193 pending_remote_interface_requests_.pop(); | |
| 194 } | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 // If no filters were interested, reject the connection. | |
| 199 return accept; | |
| 200 } | |
| 201 | |
| 202 bool OnStop() override { | |
| 203 callback_task_runner_->PostTask(FROM_HERE, stop_callback_); | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 ///////////////////////////////////////////////////////////////////////////// | |
| 208 // shell::InterfaceFactory<shell::mojom::ServiceFactory> implementation | |
| 209 | |
| 210 void Create(shell::Connection* connection, | |
| 211 shell::mojom::ServiceFactoryRequest request) override { | |
| 212 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 213 factory_bindings_.AddBinding(this, std::move(request)); | |
| 214 } | |
| 215 | |
| 216 ///////////////////////////////////////////////////////////////////////////// | |
| 217 // shell::mojom::ServiceFactory implementation | |
| 218 | |
| 219 void CreateService(shell::mojom::ServiceRequest request, | |
| 220 const mojo::String& name) override { | |
| 221 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 222 callback_task_runner_->PostTask( | |
| 223 FROM_HERE, | |
| 224 base::Bind(create_service_callback_, base::Passed(&request), name)); | |
| 225 } | |
| 226 | |
| 227 static void CallBinderOnTaskRunner( | |
| 228 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
| 229 const shell::InterfaceRegistry::Binder& binder, | |
| 230 const mojo::String& interface_name, | |
| 231 mojo::ScopedMessagePipeHandle request_handle) { | |
| 232 task_runner->PostTask(FROM_HERE, base::Bind(binder, interface_name, | |
| 233 base::Passed(&request_handle))); | |
| 234 } | |
| 235 | |
| 236 base::ThreadChecker io_thread_checker_; | |
| 237 bool started_ = false; | |
| 238 | |
| 239 // Temporary state established on construction and consumed on the IO thread | |
| 240 // once the connection is started. | |
| 241 shell::mojom::ServiceRequest pending_service_request_; | |
| 242 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | |
| 243 std::unique_ptr<shell::Connector> io_thread_connector_; | |
| 244 shell::mojom::ConnectorRequest pending_connector_request_; | |
| 245 | |
| 246 // TaskRunner on which to run our owner's callbacks, i.e. the ones passed to | |
| 247 // Start(). | |
| 248 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; | |
| 249 | |
| 250 // Callback to run once Service::OnStart is invoked. | |
| 251 InitializeCallback initialize_handler_; | |
| 252 | |
| 253 // Callback to run when a new Service request is received. | |
| 254 ServiceFactoryCallback create_service_callback_; | |
| 255 | |
| 256 // Callback to run if the service is stopped by the service manager. | |
| 257 base::Closure stop_callback_; | |
| 258 | |
| 259 // The incoming Connection from the browser process. This is captured the | |
| 260 // first time the browser connects to this Service and persists until shutdown | |
| 261 // or a connection error is detected. This connection is used to fulfill | |
| 262 // remote interface requests from legacy code which does not use | |
| 263 // shell::Connector. | |
| 264 // | |
| 265 // TODO(rockot): Remove this once all child-to-browser interface connections | |
| 266 // are made via a Connector rather than directly through an InterfaceProvider | |
| 267 // and all interfaces exposed to the browser are exposed via a | |
| 268 // ConnectionFilter. | |
| 269 shell::Connection* browser_connection_ = nullptr; | |
| 270 | |
| 271 // A queue of remote interface requests destined for the browser, which will | |
| 272 // remain pending until an incoming connection is accepted from the browser. | |
| 273 // | |
| 274 // TODO(rockot): Remove this once all child-to-browser interface connections | |
| 275 // are made via a Connector rather than directly through an InterfaceProvider. | |
| 276 std::queue<std::pair<mojo::String, mojo::ScopedMessagePipeHandle>> | |
| 277 pending_remote_interface_requests_; | |
| 278 | |
| 279 // Default binder callback used for the browser connection's | |
| 280 // InterfaceRegistry. | |
| 281 // | |
| 282 // TODO(rockot): Remove this once all interfaces exposed to the browser are | |
| 283 // exposed via a ConnectionFilter. | |
| 284 shell::InterfaceRegistry::Binder default_browser_binder_; | |
| 285 | |
| 286 std::unique_ptr<shell::ServiceContext> service_context_; | |
| 287 mojo::BindingSet<shell::mojom::ServiceFactory> factory_bindings_; | |
| 288 std::vector<std::unique_ptr<ConnectionFilter>> connection_filters_; | |
| 289 | |
| 290 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | |
| 291 }; | |
| 292 | |
| 293 //////////////////////////////////////////////////////////////////////////////// | 30 //////////////////////////////////////////////////////////////////////////////// |
| 294 // MojoShellConnection, public: | 31 // MojoShellConnection, public: |
| 295 | 32 |
| 296 // static | 33 // static |
| 297 void MojoShellConnection::SetForProcess( | 34 void MojoShellConnection::SetForProcess( |
| 298 std::unique_ptr<MojoShellConnection> connection) { | 35 std::unique_ptr<MojoShellConnection> connection) { |
| 299 DCHECK(!g_connection_for_process.Get()); | 36 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 300 g_connection_for_process.Get() = std::move(connection); | 37 lazy_tls_ptr.Pointer()->Set(connection.release()); |
| 301 } | 38 } |
| 302 | 39 |
| 303 // static | 40 // static |
| 304 MojoShellConnection* MojoShellConnection::GetForProcess() { | 41 MojoShellConnection* MojoShellConnection::GetForProcess() { |
| 305 return g_connection_for_process.Get().get(); | 42 return lazy_tls_ptr.Pointer()->Get(); |
| 306 } | 43 } |
| 307 | 44 |
| 308 // static | 45 // static |
| 309 void MojoShellConnection::DestroyForProcess() { | 46 void MojoShellConnection::DestroyForProcess() { |
| 310 // This joins the shell controller thread. | 47 // This joins the shell controller thread. |
| 311 g_connection_for_process.Get().reset(); | 48 delete GetForProcess(); |
| 49 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 312 } | 50 } |
| 313 | 51 |
| 314 // static | 52 // static |
| 315 void MojoShellConnection::SetFactoryForTest(Factory* factory) { | 53 void MojoShellConnection::SetFactoryForTest(Factory* factory) { |
| 316 DCHECK(!g_connection_for_process.Get()); | 54 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 317 mojo_shell_connection_factory = factory; | 55 mojo_shell_connection_factory = factory; |
| 318 } | 56 } |
| 319 | 57 |
| 320 // static | 58 // static |
| 321 std::unique_ptr<MojoShellConnection> MojoShellConnection::Create( | 59 std::unique_ptr<MojoShellConnection> MojoShellConnection::Create( |
| 322 shell::mojom::ServiceRequest request, | 60 shell::mojom::ServiceRequest request) { |
| 323 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { | |
| 324 if (mojo_shell_connection_factory) | 61 if (mojo_shell_connection_factory) |
| 325 return mojo_shell_connection_factory->Run(); | 62 return mojo_shell_connection_factory->Run(); |
| 326 return base::MakeUnique<MojoShellConnectionImpl>( | 63 return base::WrapUnique(new MojoShellConnectionImpl(std::move(request))); |
| 327 std::move(request), io_task_runner); | |
| 328 } | 64 } |
| 329 | 65 |
| 330 MojoShellConnection::~MojoShellConnection() {} | 66 MojoShellConnection::~MojoShellConnection() {} |
| 331 | 67 |
| 332 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
| 333 // MojoShellConnectionImpl, public: | 69 // MojoShellConnectionImpl, public: |
| 334 | 70 |
| 335 MojoShellConnectionImpl::MojoShellConnectionImpl( | 71 MojoShellConnectionImpl::MojoShellConnectionImpl( |
| 336 shell::mojom::ServiceRequest request, | 72 shell::mojom::ServiceRequest request) |
| 337 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 73 : shell_connection_(new shell::ServiceContext(this, std::move(request))) {} |
| 338 : weak_factory_(this) { | |
| 339 shell::mojom::ConnectorRequest connector_request; | |
| 340 connector_ = shell::Connector::Create(&connector_request); | |
| 341 | 74 |
| 342 std::unique_ptr<shell::Connector> io_thread_connector = connector_->Clone(); | 75 MojoShellConnectionImpl::~MojoShellConnectionImpl() {} |
| 343 context_ = new IOThreadContext( | 76 |
| 344 std::move(request), io_task_runner, std::move(io_thread_connector), | 77 //////////////////////////////////////////////////////////////////////////////// |
| 345 std::move(connector_request)); | 78 // MojoShellConnectionImpl, shell::Service implementation: |
| 79 |
| 80 void MojoShellConnectionImpl::OnStart(shell::Connector* connector, |
| 81 const shell::Identity& identity, |
| 82 uint32_t id) { |
| 83 for (auto& client : embedded_services_) |
| 84 client->OnStart(connector, identity, id); |
| 346 } | 85 } |
| 347 | 86 |
| 348 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 87 bool MojoShellConnectionImpl::OnConnect(shell::Connection* connection) { |
| 349 context_->ShutDown(); | 88 std::string remote_app = connection->GetRemoteIdentity().name(); |
| 89 if (remote_app == "mojo:shell") { |
| 90 // Only expose the SCF interface to the shell. |
| 91 connection->AddInterface<shell::mojom::ServiceFactory>(this); |
| 92 return true; |
| 93 } |
| 94 |
| 95 bool accept = false; |
| 96 for (auto& client : embedded_services_) |
| 97 accept |= client->OnConnect(connection); |
| 98 |
| 99 // Reject all other connections to this application. |
| 100 return accept; |
| 101 } |
| 102 |
| 103 shell::InterfaceRegistry* |
| 104 MojoShellConnectionImpl::GetInterfaceRegistryForConnection() { |
| 105 // TODO(beng): This is really horrible since obviously subject to issues |
| 106 // of ordering, but is no more horrible than this API is in general. |
| 107 shell::InterfaceRegistry* registry = nullptr; |
| 108 for (auto& client : embedded_services_) { |
| 109 registry = client->GetInterfaceRegistryForConnection(); |
| 110 if (registry) |
| 111 return registry; |
| 112 } |
| 113 return nullptr; |
| 114 } |
| 115 |
| 116 shell::InterfaceProvider* |
| 117 MojoShellConnectionImpl::GetInterfaceProviderForConnection() { |
| 118 // TODO(beng): This is really horrible since obviously subject to issues |
| 119 // of ordering, but is no more horrible than this API is in general. |
| 120 shell::InterfaceProvider* provider = nullptr; |
| 121 for (auto& client : embedded_services_) { |
| 122 provider = client->GetInterfaceProviderForConnection(); |
| 123 if (provider) |
| 124 return provider; |
| 125 } |
| 126 return nullptr; |
| 127 } |
| 128 |
| 129 //////////////////////////////////////////////////////////////////////////////// |
| 130 // MojoShellConnectionImpl, |
| 131 // shell::InterfaceFactory<shell::mojom::ServiceFactory> implementation: |
| 132 |
| 133 void MojoShellConnectionImpl::Create( |
| 134 shell::Connection* connection, |
| 135 shell::mojom::ServiceFactoryRequest request) { |
| 136 factory_bindings_.AddBinding(this, std::move(request)); |
| 137 } |
| 138 |
| 139 //////////////////////////////////////////////////////////////////////////////// |
| 140 // MojoShellConnectionImpl, shell::mojom::ServiceFactory implementation: |
| 141 |
| 142 void MojoShellConnectionImpl::CreateService( |
| 143 shell::mojom::ServiceRequest request, |
| 144 const mojo::String& name) { |
| 145 auto it = request_handlers_.find(name); |
| 146 if (it != request_handlers_.end()) |
| 147 it->second.Run(std::move(request)); |
| 350 } | 148 } |
| 351 | 149 |
| 352 //////////////////////////////////////////////////////////////////////////////// | 150 //////////////////////////////////////////////////////////////////////////////// |
| 353 // MojoShellConnectionImpl, MojoShellConnection implementation: | 151 // MojoShellConnectionImpl, MojoShellConnection implementation: |
| 354 | 152 |
| 355 void MojoShellConnectionImpl::Start() { | 153 shell::ServiceContext* MojoShellConnectionImpl::GetShellConnection() { |
| 356 context_->Start( | 154 return shell_connection_.get(); |
| 357 base::Bind(&MojoShellConnectionImpl::OnContextInitialized, | |
| 358 weak_factory_.GetWeakPtr()), | |
| 359 base::Bind(&MojoShellConnectionImpl::CreateService, | |
| 360 weak_factory_.GetWeakPtr()), | |
| 361 base::Bind(&MojoShellConnectionImpl::OnConnectionLost, | |
| 362 weak_factory_.GetWeakPtr())); | |
| 363 } | |
| 364 | |
| 365 void MojoShellConnectionImpl::SetInitializeHandler( | |
| 366 const base::Closure& handler) { | |
| 367 DCHECK(initialize_handler_.is_null()); | |
| 368 initialize_handler_ = handler; | |
| 369 } | 155 } |
| 370 | 156 |
| 371 shell::Connector* MojoShellConnectionImpl::GetConnector() { | 157 shell::Connector* MojoShellConnectionImpl::GetConnector() { |
| 372 return connector_.get(); | 158 DCHECK(shell_connection_); |
| 159 return shell_connection_->connector(); |
| 373 } | 160 } |
| 374 | 161 |
| 375 const shell::Identity& MojoShellConnectionImpl::GetIdentity() const { | 162 const shell::Identity& MojoShellConnectionImpl::GetIdentity() const { |
| 376 return identity_; | 163 DCHECK(shell_connection_); |
| 164 return shell_connection_->identity(); |
| 377 } | 165 } |
| 378 | 166 |
| 379 void MojoShellConnectionImpl::SetConnectionLostClosure( | 167 void MojoShellConnectionImpl::SetConnectionLostClosure( |
| 380 const base::Closure& closure) { | 168 const base::Closure& closure) { |
| 381 connection_lost_handler_ = closure; | 169 shell_connection_->SetConnectionLostClosure(closure); |
| 382 } | 170 } |
| 383 | 171 |
| 384 void MojoShellConnectionImpl::SetupInterfaceRequestProxies( | 172 void MojoShellConnectionImpl::MergeService( |
| 385 shell::InterfaceRegistry* registry, | 173 std::unique_ptr<shell::Service> service) { |
| 386 shell::InterfaceProvider* provider) { | 174 embedded_services_.push_back(service.get()); |
| 387 // It's safe to bind |registry| as a raw pointer because the caller must | 175 owned_services_.push_back(std::move(service)); |
| 388 // guarantee that it outlives |this|, and |this| is bound as a weak ptr here. | |
| 389 context_->SetDefaultBinderForBrowserConnection( | |
| 390 base::Bind(&MojoShellConnectionImpl::GetInterface, | |
| 391 weak_factory_.GetWeakPtr(), registry)); | |
| 392 | |
| 393 // Forward all remote interface requests on |provider| to our IO-thread | |
| 394 // context. This will ensure they're forwarded to the provider on the | |
| 395 // incoming browser connection. | |
| 396 provider->Forward(base::Bind(&IOThreadContext::GetRemoteInterface, context_)); | |
| 397 } | 176 } |
| 398 | 177 |
| 399 void MojoShellConnectionImpl::AddConnectionFilter( | 178 void MojoShellConnectionImpl::MergeService( |
| 400 std::unique_ptr<ConnectionFilter> filter) { | 179 shell::Service* service) { |
| 401 context_->AddConnectionFilter(std::move(filter)); | 180 embedded_services_.push_back(service); |
| 402 } | 181 } |
| 403 | 182 |
| 404 void MojoShellConnectionImpl::AddEmbeddedService( | 183 void MojoShellConnectionImpl::AddEmbeddedService( |
| 405 const std::string& name, | 184 const std::string& name, |
| 406 const MojoApplicationInfo& info) { | 185 const MojoApplicationInfo& info) { |
| 407 std::unique_ptr<EmbeddedApplicationRunner> app( | 186 std::unique_ptr<EmbeddedApplicationRunner> app( |
| 408 new EmbeddedApplicationRunner(name, info)); | 187 new EmbeddedApplicationRunner(name, info)); |
| 409 AddServiceRequestHandler( | 188 AddServiceRequestHandler( |
| 410 name, base::Bind(&EmbeddedApplicationRunner::BindServiceRequest, | 189 name, base::Bind(&EmbeddedApplicationRunner::BindServiceRequest, |
| 411 base::Unretained(app.get()))); | 190 base::Unretained(app.get()))); |
| 412 auto result = embedded_apps_.insert(std::make_pair(name, std::move(app))); | 191 auto result = embedded_apps_.insert(std::make_pair(name, std::move(app))); |
| 413 DCHECK(result.second); | 192 DCHECK(result.second); |
| 414 } | 193 } |
| 415 | 194 |
| 416 void MojoShellConnectionImpl::AddServiceRequestHandler( | 195 void MojoShellConnectionImpl::AddServiceRequestHandler( |
| 417 const std::string& name, | 196 const std::string& name, |
| 418 const ServiceRequestHandler& handler) { | 197 const ServiceRequestHandler& handler) { |
| 419 auto result = request_handlers_.insert(std::make_pair(name, handler)); | 198 auto result = request_handlers_.insert(std::make_pair(name, handler)); |
| 420 DCHECK(result.second); | 199 DCHECK(result.second); |
| 421 } | 200 } |
| 422 | 201 |
| 423 void MojoShellConnectionImpl::CreateService( | |
| 424 shell::mojom::ServiceRequest request, | |
| 425 const mojo::String& name) { | |
| 426 auto it = request_handlers_.find(name); | |
| 427 if (it != request_handlers_.end()) | |
| 428 it->second.Run(std::move(request)); | |
| 429 } | |
| 430 | |
| 431 void MojoShellConnectionImpl::OnContextInitialized( | |
| 432 const shell::Identity& identity) { | |
| 433 identity_ = identity; | |
| 434 if (!initialize_handler_.is_null()) | |
| 435 base::ResetAndReturn(&initialize_handler_).Run(); | |
| 436 } | |
| 437 | |
| 438 void MojoShellConnectionImpl::OnConnectionLost() { | |
| 439 if (!connection_lost_handler_.is_null()) | |
| 440 connection_lost_handler_.Run(); | |
| 441 } | |
| 442 | |
| 443 void MojoShellConnectionImpl::GetInterface( | |
| 444 shell::mojom::InterfaceProvider* provider, | |
| 445 const mojo::String& interface_name, | |
| 446 mojo::ScopedMessagePipeHandle request_handle) { | |
| 447 provider->GetInterface(interface_name, std::move(request_handle)); | |
| 448 } | |
| 449 | |
| 450 } // namespace content | 202 } // namespace content |
| OLD | NEW |