| 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/edk/embedder/embedder.h" |
| 14 #include "mojo/shell/public/cpp/shell_client.h" | 15 #include "mojo/shell/public/cpp/shell_client.h" |
| 15 #include "mojo/shell/public/cpp/shell_connection.h" | 16 #include "mojo/shell/public/cpp/shell_connection.h" |
| 16 #include "mojo/shell/runner/child/runner_connection.h" | 17 #include "mojo/shell/runner/child/runner_connection.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 using MojoShellConnectionPtr = | 22 using MojoShellConnectionPtr = |
| 22 base::ThreadLocalPointer<MojoShellConnectionImpl>; | 23 base::ThreadLocalPointer<MojoShellConnectionImpl>; |
| 23 | 24 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 lazy_tls_ptr.Pointer()->Set(connection); | 41 lazy_tls_ptr.Pointer()->Set(connection); |
| 41 } | 42 } |
| 42 | 43 |
| 43 // static | 44 // static |
| 44 void MojoShellConnectionImpl::Create( | 45 void MojoShellConnectionImpl::Create( |
| 45 mojo::shell::mojom::ShellClientRequest request) { | 46 mojo::shell::mojom::ShellClientRequest request) { |
| 46 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 47 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 47 MojoShellConnectionImpl* connection = | 48 MojoShellConnectionImpl* connection = |
| 48 new MojoShellConnectionImpl(false /* external */); | 49 new MojoShellConnectionImpl(false /* external */); |
| 49 lazy_tls_ptr.Pointer()->Set(connection); | 50 lazy_tls_ptr.Pointer()->Set(connection); |
| 50 | |
| 51 connection->shell_connection_.reset( | 51 connection->shell_connection_.reset( |
| 52 new mojo::ShellConnection(connection, std::move(request))); | 52 new mojo::ShellConnection(connection, std::move(request))); |
| 53 connection->shell_connection_->WaitForInitialize(); | |
| 54 } | 53 } |
| 55 | 54 |
| 56 // static | 55 // static |
| 57 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { | 56 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { |
| 58 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); | 57 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void MojoShellConnectionImpl::BindToCommandLinePlatformChannel() { | 60 void MojoShellConnectionImpl::BindToRequestFromCommandLine() { |
| 62 DCHECK(IsRunningInMojoShell()); | 61 DCHECK(!shell_connection_); |
| 63 if (initialized_) | |
| 64 return; | |
| 65 WaitForShell(mojo::ScopedMessagePipeHandle()); | |
| 66 } | |
| 67 | 62 |
| 68 void MojoShellConnectionImpl::BindToMessagePipe( | 63 shell_connection_.reset(new mojo::ShellConnection(this)); |
| 69 mojo::ScopedMessagePipeHandle handle) { | 64 runner_connection_ = |
| 70 if (initialized_) | 65 mojo::shell::RunnerConnection::Create(shell_connection_.get(), |
| 71 return; | 66 false /* exit_on_error */); |
| 72 WaitForShell(std::move(handle)); | 67 if (!runner_connection_) { |
| 68 delete this; |
| 69 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 70 } |
| 73 } | 71 } |
| 74 | 72 |
| 75 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : | 73 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : |
| 76 external_(external), initialized_(false) {} | 74 external_(external) {} |
| 77 | 75 |
| 78 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 76 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
| 79 STLDeleteElements(&listeners_); | 77 STLDeleteElements(&listeners_); |
| 80 } | 78 } |
| 81 | 79 |
| 82 void MojoShellConnectionImpl::WaitForShell( | |
| 83 mojo::ScopedMessagePipeHandle handle) { | |
| 84 mojo::shell::mojom::ShellClientRequest request; | |
| 85 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( | |
| 86 &request, std::move(handle), false /* exit_on_error */)); | |
| 87 if (!runner_connection_) { | |
| 88 DLOG(ERROR) << "Unable to connect to the Mojo shell."; | |
| 89 delete this; | |
| 90 lazy_tls_ptr.Pointer()->Set(nullptr); | |
| 91 return; | |
| 92 } | |
| 93 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); | |
| 94 shell_connection_->WaitForInitialize(); | |
| 95 } | |
| 96 | |
| 97 void MojoShellConnectionImpl::Initialize(mojo::Connector* connector, | 80 void MojoShellConnectionImpl::Initialize(mojo::Connector* connector, |
| 98 const mojo::Identity& identity, | 81 const mojo::Identity& identity, |
| 99 uint32_t id) { | 82 uint32_t id) { |
| 100 initialized_ = true; | |
| 101 } | 83 } |
| 102 | 84 |
| 103 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { | 85 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { |
| 104 bool found = false; | 86 bool found = false; |
| 105 for (auto listener : listeners_) | 87 for (auto listener : listeners_) |
| 106 found |= listener->AcceptConnection(connection); | 88 found |= listener->AcceptConnection(connection); |
| 107 return found; | 89 return found; |
| 108 } | 90 } |
| 109 | 91 |
| 110 mojo::Connector* MojoShellConnectionImpl::GetConnector() { | 92 mojo::Connector* MojoShellConnectionImpl::GetConnector() { |
| 111 DCHECK(initialized_); | 93 DCHECK(shell_connection_); |
| 112 return shell_connection_->connector(); | 94 return shell_connection_->connector(); |
| 113 } | 95 } |
| 114 | 96 |
| 115 bool MojoShellConnectionImpl::UsingExternalShell() const { | 97 bool MojoShellConnectionImpl::UsingExternalShell() const { |
| 116 return external_; | 98 return external_; |
| 117 } | 99 } |
| 118 | 100 |
| 119 void MojoShellConnectionImpl::AddListener(Listener* listener) { | 101 void MojoShellConnectionImpl::AddListener(Listener* listener) { |
| 120 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == | 102 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
| 121 listeners_.end()); | 103 listeners_.end()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 136 // static | 118 // static |
| 137 void MojoShellConnection::Destroy() { | 119 void MojoShellConnection::Destroy() { |
| 138 // This joins the shell controller thread. | 120 // This joins the shell controller thread. |
| 139 delete Get(); | 121 delete Get(); |
| 140 lazy_tls_ptr.Pointer()->Set(nullptr); | 122 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 141 } | 123 } |
| 142 | 124 |
| 143 MojoShellConnection::~MojoShellConnection() {} | 125 MojoShellConnection::~MojoShellConnection() {} |
| 144 | 126 |
| 145 } // namespace content | 127 } // namespace content |
| OLD | NEW |