OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "content/common/mojo/mojo_shell_connection_impl.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/lazy_instance.h" |
| 9 #include "base/threading/thread_local.h" |
| 10 #include "mojo/application/public/cpp/application_delegate.h" |
| 11 #include "mojo/application/public/cpp/application_impl.h" |
| 12 #include "mojo/converters/network/network_type_converters.h" |
| 13 #include "mojo/runner/child/runner_connection.h" |
| 14 |
| 15 namespace content { |
| 16 namespace { |
| 17 using MojoShellConnectionPtr = |
| 18 base::ThreadLocalPointer<MojoShellConnectionImpl>; |
| 19 |
| 20 // Env is thread local so that aura may be used on multiple threads. |
| 21 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = |
| 22 LAZY_INSTANCE_INITIALIZER; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 bool IsRunningInMojoShell() { |
| 27 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 28 "mojo-platform-channel-handle"); |
| 29 } |
| 30 |
| 31 // static |
| 32 void MojoShellConnectionImpl::Create() { |
| 33 DCHECK(IsRunningInMojoShell()); |
| 34 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 35 MojoShellConnectionImpl* connection = new MojoShellConnectionImpl; |
| 36 lazy_tls_ptr.Pointer()->Set(connection); |
| 37 connection->WaitForShell(); |
| 38 } |
| 39 |
| 40 MojoShellConnectionImpl::MojoShellConnectionImpl() : initialized_(false) {} |
| 41 MojoShellConnectionImpl::~MojoShellConnectionImpl() {} |
| 42 |
| 43 void MojoShellConnectionImpl::WaitForShell() { |
| 44 mojo::InterfaceRequest<mojo::Application> application_request; |
| 45 runner_connection_.reset( |
| 46 mojo::runner::RunnerConnection::ConnectToRunner(&application_request)); |
| 47 application_impl_.reset(new mojo::ApplicationImpl( |
| 48 this, application_request.Pass())); |
| 49 application_impl_->WaitForInitialize(); |
| 50 } |
| 51 |
| 52 void MojoShellConnectionImpl::Initialize(mojo::ApplicationImpl* application) { |
| 53 initialized_ = true; |
| 54 } |
| 55 |
| 56 bool MojoShellConnectionImpl::ConfigureIncomingConnection( |
| 57 mojo::ApplicationConnection* connection) { |
| 58 bool found = false; |
| 59 for (auto listener : listeners_) |
| 60 found |= listener->ConfigureIncomingConnection(connection); |
| 61 return found; |
| 62 } |
| 63 |
| 64 mojo::ApplicationImpl* MojoShellConnectionImpl::GetApplication() { |
| 65 DCHECK(initialized_); |
| 66 return application_impl_.get(); |
| 67 } |
| 68 |
| 69 void MojoShellConnectionImpl::AddListener(Listener* listener) { |
| 70 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
| 71 listeners_.end()); |
| 72 listeners_.push_back(listener); |
| 73 } |
| 74 |
| 75 void MojoShellConnectionImpl::RemoveListener(Listener* listener) { |
| 76 auto it = std::find(listeners_.begin(), listeners_.end(), listener); |
| 77 DCHECK(it != listeners_.end()); |
| 78 listeners_.erase(it); |
| 79 } |
| 80 |
| 81 // static |
| 82 MojoShellConnection* MojoShellConnection::Get() { |
| 83 return lazy_tls_ptr.Pointer()->Get(); |
| 84 } |
| 85 |
| 86 // static |
| 87 void MojoShellConnection::Destroy() { |
| 88 // This joins the shell controller thread. |
| 89 delete Get(); |
| 90 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 91 } |
| 92 |
| 93 MojoShellConnection::~MojoShellConnection() {} |
| 94 |
| 95 } // namespace content |
OLD | NEW |