| 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" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 // static | 43 // static |
| 44 void MojoShellConnectionImpl::Create( | 44 void MojoShellConnectionImpl::Create( |
| 45 mojo::shell::mojom::ShellClientRequest request) { | 45 mojo::shell::mojom::ShellClientRequest request) { |
| 46 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 46 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 47 MojoShellConnectionImpl* connection = | 47 MojoShellConnectionImpl* connection = |
| 48 new MojoShellConnectionImpl(false /* external */); | 48 new MojoShellConnectionImpl(false /* external */); |
| 49 lazy_tls_ptr.Pointer()->Set(connection); | 49 lazy_tls_ptr.Pointer()->Set(connection); |
| 50 | 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::BindToCommandLinePlatformChannel() { |
| 62 DCHECK(IsRunningInMojoShell()); | 61 DCHECK(IsRunningInMojoShell()); |
| 63 if (initialized_) | 62 ConnectToShell(mojo::ScopedMessagePipeHandle()); |
| 64 return; | |
| 65 WaitForShell(mojo::ScopedMessagePipeHandle()); | |
| 66 } | 63 } |
| 67 | 64 |
| 68 void MojoShellConnectionImpl::BindToMessagePipe( | 65 void MojoShellConnectionImpl::BindToMessagePipe( |
| 69 mojo::ScopedMessagePipeHandle handle) { | 66 mojo::ScopedMessagePipeHandle handle) { |
| 70 if (initialized_) | 67 ConnectToShell(std::move(handle)); |
| 71 return; | |
| 72 WaitForShell(std::move(handle)); | |
| 73 } | 68 } |
| 74 | 69 |
| 75 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : | 70 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) |
| 76 external_(external), initialized_(false) {} | 71 : external_(external) {} |
| 77 | 72 |
| 78 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 73 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
| 79 STLDeleteElements(&listeners_); | 74 STLDeleteElements(&listeners_); |
| 80 } | 75 } |
| 81 | 76 |
| 82 void MojoShellConnectionImpl::WaitForShell( | 77 void MojoShellConnectionImpl::ConnectToShell( |
| 83 mojo::ScopedMessagePipeHandle handle) { | 78 mojo::ScopedMessagePipeHandle handle) { |
| 84 mojo::shell::mojom::ShellClientRequest request; | 79 mojo::shell::mojom::ShellClientRequest request; |
| 85 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( | 80 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( |
| 86 &request, std::move(handle), false /* exit_on_error */)); | 81 &request, std::move(handle), false /* exit_on_error */)); |
| 87 if (!runner_connection_) { | 82 if (!runner_connection_) { |
| 88 DLOG(ERROR) << "Unable to connect to the Mojo shell."; | 83 DLOG(ERROR) << "Unable to connect to the Mojo shell."; |
| 89 delete this; | 84 delete this; |
| 90 lazy_tls_ptr.Pointer()->Set(nullptr); | 85 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 91 return; | 86 return; |
| 92 } | 87 } |
| 93 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); | 88 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); |
| 94 shell_connection_->WaitForInitialize(); | |
| 95 } | 89 } |
| 96 | 90 |
| 97 void MojoShellConnectionImpl::Initialize(mojo::Connector* connector, | 91 void MojoShellConnectionImpl::Initialize(mojo::Connector* connector, |
| 98 const mojo::Identity& identity, | 92 const mojo::Identity& identity, |
| 99 uint32_t id) { | 93 uint32_t id) { |
| 100 initialized_ = true; | |
| 101 } | 94 } |
| 102 | 95 |
| 103 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { | 96 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { |
| 104 bool found = false; | 97 bool found = false; |
| 105 for (auto listener : listeners_) | 98 for (auto listener : listeners_) |
| 106 found |= listener->AcceptConnection(connection); | 99 found |= listener->AcceptConnection(connection); |
| 107 return found; | 100 return found; |
| 108 } | 101 } |
| 109 | 102 |
| 110 mojo::Connector* MojoShellConnectionImpl::GetConnector() { | 103 mojo::Connector* MojoShellConnectionImpl::GetConnector() { |
| 111 DCHECK(initialized_); | |
| 112 return shell_connection_->connector(); | 104 return shell_connection_->connector(); |
| 113 } | 105 } |
| 114 | 106 |
| 115 bool MojoShellConnectionImpl::UsingExternalShell() const { | 107 bool MojoShellConnectionImpl::UsingExternalShell() const { |
| 116 return external_; | 108 return external_; |
| 117 } | 109 } |
| 118 | 110 |
| 119 void MojoShellConnectionImpl::AddListener(Listener* listener) { | 111 void MojoShellConnectionImpl::AddListener(Listener* listener) { |
| 120 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == | 112 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
| 121 listeners_.end()); | 113 listeners_.end()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 136 // static | 128 // static |
| 137 void MojoShellConnection::Destroy() { | 129 void MojoShellConnection::Destroy() { |
| 138 // This joins the shell controller thread. | 130 // This joins the shell controller thread. |
| 139 delete Get(); | 131 delete Get(); |
| 140 lazy_tls_ptr.Pointer()->Set(nullptr); | 132 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 141 } | 133 } |
| 142 | 134 |
| 143 MojoShellConnection::~MojoShellConnection() {} | 135 MojoShellConnection::~MojoShellConnection() {} |
| 144 | 136 |
| 145 } // namespace content | 137 } // namespace content |
| OLD | NEW |