| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/threading/thread_local.h" | 12 #include "base/threading/thread_local.h" |
| 13 #include "mojo/converters/network/network_type_converters.h" | 13 #include "mojo/converters/network/network_type_converters.h" |
| 14 #include "mojo/shell/public/cpp/shell_client.h" | 14 #include "mojo/shell/public/cpp/shell_client.h" |
| 15 #include "mojo/shell/public/cpp/shell_connection.h" | 15 #include "mojo/shell/public/cpp/shell_connection.h" |
| 16 #include "mojo/shell/runner/child/runner_connection.h" | 16 #include "mojo/shell/runner/child/runner_connection.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 namespace { | 19 namespace { |
| 20 |
| 20 using MojoShellConnectionPtr = | 21 using MojoShellConnectionPtr = |
| 21 base::ThreadLocalPointer<MojoShellConnectionImpl>; | 22 base::ThreadLocalPointer<MojoShellConnectionImpl>; |
| 22 | 23 |
| 23 // Env is thread local so that aura may be used on multiple threads. | 24 // Env is thread local so that aura may be used on multiple threads. |
| 24 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = | 25 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = |
| 25 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 bool IsRunningInMojoShell() { | 30 bool IsRunningInMojoShell() { |
| 30 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 31 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 31 "mojo-platform-channel-handle"); | 32 "mojo-platform-channel-handle"); |
| 32 } | 33 } |
| 33 | 34 |
| 34 // static | 35 // static |
| 35 void MojoShellConnectionImpl::Create() { | 36 void MojoShellConnectionImpl::Create() { |
| 36 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 37 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 37 MojoShellConnectionImpl* connection = new MojoShellConnectionImpl; | 38 MojoShellConnectionImpl* connection = |
| 39 new MojoShellConnectionImpl(true /* external */); |
| 38 lazy_tls_ptr.Pointer()->Set(connection); | 40 lazy_tls_ptr.Pointer()->Set(connection); |
| 39 } | 41 } |
| 40 | 42 |
| 41 // static | 43 // static |
| 44 void MojoShellConnectionImpl::Create( |
| 45 mojo::shell::mojom::ShellClientRequest request) { |
| 46 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 47 MojoShellConnectionImpl* connection = |
| 48 new MojoShellConnectionImpl(false /* external */); |
| 49 lazy_tls_ptr.Pointer()->Set(connection); |
| 50 |
| 51 connection->shell_connection_.reset( |
| 52 new mojo::ShellConnection(connection, std::move(request))); |
| 53 connection->shell_connection_->WaitForInitialize(); |
| 54 } |
| 55 |
| 56 // static |
| 42 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { | 57 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { |
| 43 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); | 58 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); |
| 44 } | 59 } |
| 45 | 60 |
| 46 void MojoShellConnectionImpl::BindToCommandLinePlatformChannel() { | 61 void MojoShellConnectionImpl::BindToCommandLinePlatformChannel() { |
| 47 DCHECK(IsRunningInMojoShell()); | 62 DCHECK(IsRunningInMojoShell()); |
| 48 if (initialized_) | 63 if (initialized_) |
| 49 return; | 64 return; |
| 50 WaitForShell(mojo::ScopedMessagePipeHandle()); | 65 WaitForShell(mojo::ScopedMessagePipeHandle()); |
| 51 } | 66 } |
| 52 | 67 |
| 53 void MojoShellConnectionImpl::BindToMessagePipe( | 68 void MojoShellConnectionImpl::BindToMessagePipe( |
| 54 mojo::ScopedMessagePipeHandle handle) { | 69 mojo::ScopedMessagePipeHandle handle) { |
| 55 if (initialized_) | 70 if (initialized_) |
| 56 return; | 71 return; |
| 57 WaitForShell(std::move(handle)); | 72 WaitForShell(std::move(handle)); |
| 58 } | 73 } |
| 59 | 74 |
| 60 MojoShellConnectionImpl::MojoShellConnectionImpl() : initialized_(false) {} | 75 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : |
| 76 external_(external), initialized_(false) {} |
| 77 |
| 61 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 78 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
| 62 STLDeleteElements(&listeners_); | 79 STLDeleteElements(&listeners_); |
| 63 } | 80 } |
| 64 | 81 |
| 65 void MojoShellConnectionImpl::WaitForShell( | 82 void MojoShellConnectionImpl::WaitForShell( |
| 66 mojo::ScopedMessagePipeHandle handle) { | 83 mojo::ScopedMessagePipeHandle handle) { |
| 67 mojo::shell::mojom::ShellClientRequest request; | 84 mojo::shell::mojom::ShellClientRequest request; |
| 68 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( | 85 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( |
| 69 &request, std::move(handle))); | 86 &request, std::move(handle), false /* exit_on_error */)); |
| 87 if (!runner_connection_) { |
| 88 delete this; |
| 89 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 90 return; |
| 91 } |
| 70 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); | 92 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); |
| 71 shell_connection_->WaitForInitialize(); | 93 shell_connection_->WaitForInitialize(); |
| 72 } | 94 } |
| 73 | 95 |
| 74 void MojoShellConnectionImpl::Initialize(mojo::Shell* shell, | 96 void MojoShellConnectionImpl::Initialize(mojo::Shell* shell, |
| 75 const std::string& url, | 97 const std::string& url, |
| 76 uint32_t id, | 98 uint32_t id, |
| 77 uint32_t user_id) { | 99 uint32_t user_id) { |
| 78 initialized_ = true; | 100 initialized_ = true; |
| 79 } | 101 } |
| 80 | 102 |
| 81 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { | 103 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { |
| 82 bool found = false; | 104 bool found = false; |
| 83 for (auto listener : listeners_) | 105 for (auto listener : listeners_) |
| 84 found |= listener->AcceptConnection(connection); | 106 found |= listener->AcceptConnection(connection); |
| 85 return found; | 107 return found; |
| 86 } | 108 } |
| 87 | 109 |
| 88 mojo::Shell* MojoShellConnectionImpl::GetShell() { | 110 mojo::Shell* MojoShellConnectionImpl::GetShell() { |
| 89 DCHECK(initialized_); | 111 DCHECK(initialized_); |
| 90 return shell_connection_.get(); | 112 return shell_connection_.get(); |
| 91 } | 113 } |
| 92 | 114 |
| 93 bool MojoShellConnectionImpl::UsingExternalShell() const { | 115 bool MojoShellConnectionImpl::UsingExternalShell() const { |
| 94 return true; | 116 return external_; |
| 95 } | 117 } |
| 96 | 118 |
| 97 void MojoShellConnectionImpl::AddListener(Listener* listener) { | 119 void MojoShellConnectionImpl::AddListener(Listener* listener) { |
| 98 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == | 120 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
| 99 listeners_.end()); | 121 listeners_.end()); |
| 100 listeners_.push_back(listener); | 122 listeners_.push_back(listener); |
| 101 } | 123 } |
| 102 | 124 |
| 103 void MojoShellConnectionImpl::RemoveListener(Listener* listener) { | 125 void MojoShellConnectionImpl::RemoveListener(Listener* listener) { |
| 104 auto it = std::find(listeners_.begin(), listeners_.end(), listener); | 126 auto it = std::find(listeners_.begin(), listeners_.end(), listener); |
| 105 DCHECK(it != listeners_.end()); | 127 DCHECK(it != listeners_.end()); |
| 106 listeners_.erase(it); | 128 listeners_.erase(it); |
| 107 } | 129 } |
| 108 | 130 |
| 109 // static | 131 // static |
| 110 MojoShellConnection* MojoShellConnection::Get() { | 132 MojoShellConnection* MojoShellConnection::Get() { |
| 111 return lazy_tls_ptr.Pointer()->Get(); | 133 return lazy_tls_ptr.Pointer()->Get(); |
| 112 } | 134 } |
| 113 | 135 |
| 114 // static | 136 // static |
| 115 void MojoShellConnection::Destroy() { | 137 void MojoShellConnection::Destroy() { |
| 116 // This joins the shell controller thread. | 138 // This joins the shell controller thread. |
| 117 delete Get(); | 139 delete Get(); |
| 118 lazy_tls_ptr.Pointer()->Set(nullptr); | 140 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 119 } | 141 } |
| 120 | 142 |
| 121 MojoShellConnection::~MojoShellConnection() {} | 143 MojoShellConnection::~MojoShellConnection() {} |
| 122 | 144 |
| 123 } // namespace content | 145 } // namespace content |
| OLD | NEW |