| 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 21 matching lines...) Expand all Loading... |
| 32 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; | 32 MojoShellConnection::Factory* mojo_shell_connection_factory = nullptr; |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 bool IsRunningInMojoShell() { | 36 bool IsRunningInMojoShell() { |
| 37 return mojo_shell_connection_factory || | 37 return mojo_shell_connection_factory || |
| 38 base::CommandLine::ForCurrentProcess()->HasSwitch( | 38 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 39 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch); | 39 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch); |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool ShouldWaitForShell() { | |
| 43 return mojo_shell_connection_factory || | |
| 44 (IsRunningInMojoShell() && | |
| 45 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 46 switches::kWaitForMojoShell)); | |
| 47 } | |
| 48 | |
| 49 // static | 42 // static |
| 50 bool MojoShellConnectionImpl::CreateUsingFactory() { | 43 bool MojoShellConnectionImpl::CreateUsingFactory() { |
| 51 if (mojo_shell_connection_factory) { | 44 if (mojo_shell_connection_factory) { |
| 52 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 45 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 53 mojo_shell_connection_factory->Run(); | 46 mojo_shell_connection_factory->Run(); |
| 54 DCHECK(lazy_tls_ptr.Pointer()->Get()); | 47 DCHECK(lazy_tls_ptr.Pointer()->Get()); |
| 55 return true; | 48 return true; |
| 56 } | 49 } |
| 57 return false; | 50 return false; |
| 58 } | 51 } |
| 59 | 52 |
| 60 // static | 53 // static |
| 61 void MojoShellConnectionImpl::Create() { | 54 void MojoShellConnectionImpl::Create() { |
| 62 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 55 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 63 MojoShellConnectionImpl* connection = | 56 MojoShellConnectionImpl* connection = |
| 64 new MojoShellConnectionImpl(true /* external */); | 57 new MojoShellConnectionImpl(true /* external */); |
| 65 lazy_tls_ptr.Pointer()->Set(connection); | 58 lazy_tls_ptr.Pointer()->Set(connection); |
| 66 } | 59 } |
| 67 | 60 |
| 68 // static | 61 // static |
| 69 void MojoShellConnection::Create(shell::mojom::ShellClientRequest request, | 62 void MojoShellConnection::Create(shell::mojom::ShellClientRequest request, |
| 70 bool is_external) { | 63 bool is_external) { |
| 71 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 64 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 72 MojoShellConnectionImpl* connection = | 65 MojoShellConnectionImpl* connection = |
| 73 new MojoShellConnectionImpl(is_external); | 66 new MojoShellConnectionImpl(is_external); |
| 74 lazy_tls_ptr.Pointer()->Set(connection); | 67 lazy_tls_ptr.Pointer()->Set(connection); |
| 75 connection->shell_connection_.reset( | 68 connection->shell_connection_.reset( |
| 76 new shell::ShellConnection(connection, std::move(request))); | 69 new shell::ShellConnection(connection, std::move(request))); |
| 77 if (is_external) | |
| 78 connection->WaitForShellIfNecessary(); | |
| 79 } | 70 } |
| 80 | 71 |
| 81 // static | 72 // static |
| 82 void MojoShellConnection::SetFactoryForTest(Factory* factory) { | 73 void MojoShellConnection::SetFactoryForTest(Factory* factory) { |
| 83 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 74 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 84 mojo_shell_connection_factory = factory; | 75 mojo_shell_connection_factory = factory; |
| 85 } | 76 } |
| 86 | 77 |
| 87 // static | 78 // static |
| 88 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { | 79 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { |
| 89 // Assume that if a mojo_shell_connection_factory was set that it did not | 80 // Assume that if a mojo_shell_connection_factory was set that it did not |
| 90 // create a MojoShellConnectionImpl. | 81 // create a MojoShellConnectionImpl. |
| 91 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); | 82 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); |
| 92 } | 83 } |
| 93 | 84 |
| 94 void MojoShellConnectionImpl::BindToRequestFromCommandLine() { | 85 void MojoShellConnectionImpl::BindToRequestFromCommandLine() { |
| 95 DCHECK(!shell_connection_); | 86 DCHECK(!shell_connection_); |
| 96 shell_connection_.reset(new shell::ShellConnection( | 87 shell_connection_.reset(new shell::ShellConnection( |
| 97 this, shell::GetShellClientRequestFromCommandLine())); | 88 this, shell::GetShellClientRequestFromCommandLine())); |
| 98 WaitForShellIfNecessary(); | |
| 99 } | 89 } |
| 100 | 90 |
| 101 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) | 91 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) |
| 102 : external_(external) {} | 92 : external_(external) {} |
| 103 | 93 |
| 104 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 94 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
| 105 STLDeleteElements(&listeners_); | 95 STLDeleteElements(&listeners_); |
| 106 } | 96 } |
| 107 | 97 |
| 108 void MojoShellConnectionImpl::WaitForShellIfNecessary() { | |
| 109 // TODO(rockot): Remove this. http://crbug.com/594852. | |
| 110 if (ShouldWaitForShell()) { | |
| 111 base::RunLoop wait_loop; | |
| 112 shell_connection_->set_initialize_handler(wait_loop.QuitClosure()); | |
| 113 wait_loop.Run(); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void MojoShellConnectionImpl::Initialize(shell::Connector* connector, | 98 void MojoShellConnectionImpl::Initialize(shell::Connector* connector, |
| 118 const shell::Identity& identity, | 99 const shell::Identity& identity, |
| 119 uint32_t id) {} | 100 uint32_t id) {} |
| 120 | 101 |
| 121 bool MojoShellConnectionImpl::AcceptConnection(shell::Connection* connection) { | 102 bool MojoShellConnectionImpl::AcceptConnection(shell::Connection* connection) { |
| 122 bool found = false; | 103 bool found = false; |
| 123 for (auto listener : listeners_) | 104 for (auto listener : listeners_) |
| 124 found |= listener->AcceptConnection(connection); | 105 found |= listener->AcceptConnection(connection); |
| 125 return found; | 106 return found; |
| 126 } | 107 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // static | 147 // static |
| 167 void MojoShellConnection::Destroy() { | 148 void MojoShellConnection::Destroy() { |
| 168 // This joins the shell controller thread. | 149 // This joins the shell controller thread. |
| 169 delete Get(); | 150 delete Get(); |
| 170 lazy_tls_ptr.Pointer()->Set(nullptr); | 151 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 171 } | 152 } |
| 172 | 153 |
| 173 MojoShellConnection::~MojoShellConnection() {} | 154 MojoShellConnection::~MojoShellConnection() {} |
| 174 | 155 |
| 175 } // namespace content | 156 } // namespace content |
| OLD | NEW |