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/run_loop.h" |
11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
12 #include "base/threading/thread_local.h" | 13 #include "base/threading/thread_local.h" |
| 14 #include "content/public/common/content_switches.h" |
13 #include "mojo/converters/network/network_type_converters.h" | 15 #include "mojo/converters/network/network_type_converters.h" |
14 #include "mojo/edk/embedder/embedder.h" | 16 #include "mojo/edk/embedder/embedder.h" |
| 17 #include "mojo/edk/embedder/platform_channel_pair.h" |
15 #include "mojo/shell/public/cpp/shell_client.h" | 18 #include "mojo/shell/public/cpp/shell_client.h" |
16 #include "mojo/shell/public/cpp/shell_connection.h" | 19 #include "mojo/shell/public/cpp/shell_connection.h" |
17 #include "mojo/shell/runner/common/client_util.h" | 20 #include "mojo/shell/runner/common/client_util.h" |
18 | 21 |
19 namespace content { | 22 namespace content { |
20 namespace { | 23 namespace { |
21 | 24 |
22 using MojoShellConnectionPtr = | 25 using MojoShellConnectionPtr = |
23 base::ThreadLocalPointer<MojoShellConnectionImpl>; | 26 base::ThreadLocalPointer<MojoShellConnectionImpl>; |
24 | 27 |
25 // Env is thread local so that aura may be used on multiple threads. | 28 // Env is thread local so that aura may be used on multiple threads. |
26 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = | 29 base::LazyInstance<MojoShellConnectionPtr>::Leaky lazy_tls_ptr = |
27 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
28 | 31 |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 bool IsRunningInMojoShell() { | 34 bool IsRunningInMojoShell() { |
32 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 35 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
33 "mojo-platform-channel-handle"); | 36 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch); |
| 37 } |
| 38 |
| 39 bool ShouldWaitForShell() { |
| 40 return IsRunningInMojoShell() && |
| 41 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 42 switches::kWaitForMojoShell); |
34 } | 43 } |
35 | 44 |
36 // static | 45 // static |
37 void MojoShellConnectionImpl::Create() { | 46 void MojoShellConnectionImpl::Create() { |
38 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 47 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
39 MojoShellConnectionImpl* connection = | 48 MojoShellConnectionImpl* connection = |
40 new MojoShellConnectionImpl(true /* external */); | 49 new MojoShellConnectionImpl(true /* external */); |
41 lazy_tls_ptr.Pointer()->Set(connection); | 50 lazy_tls_ptr.Pointer()->Set(connection); |
42 } | 51 } |
43 | 52 |
(...skipping 10 matching lines...) Expand all Loading... |
54 | 63 |
55 // static | 64 // static |
56 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { | 65 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { |
57 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); | 66 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); |
58 } | 67 } |
59 | 68 |
60 void MojoShellConnectionImpl::BindToRequestFromCommandLine() { | 69 void MojoShellConnectionImpl::BindToRequestFromCommandLine() { |
61 DCHECK(!shell_connection_); | 70 DCHECK(!shell_connection_); |
62 shell_connection_.reset(new mojo::ShellConnection( | 71 shell_connection_.reset(new mojo::ShellConnection( |
63 this, mojo::shell::GetShellClientRequestFromCommandLine())); | 72 this, mojo::shell::GetShellClientRequestFromCommandLine())); |
| 73 |
| 74 // TODO(rockot): Remove this. http://crbug.com/594852. |
| 75 if (ShouldWaitForShell()) { |
| 76 base::RunLoop wait_loop; |
| 77 shell_connection_->set_initialize_handler(wait_loop.QuitClosure()); |
| 78 wait_loop.Run(); |
| 79 } |
64 } | 80 } |
65 | 81 |
66 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : | 82 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : |
67 external_(external) {} | 83 external_(external) {} |
68 | 84 |
69 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 85 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
70 STLDeleteElements(&listeners_); | 86 STLDeleteElements(&listeners_); |
71 } | 87 } |
72 | 88 |
73 void MojoShellConnectionImpl::Initialize(mojo::Connector* connector, | 89 void MojoShellConnectionImpl::Initialize(mojo::Connector* connector, |
74 const mojo::Identity& identity, | 90 const mojo::Identity& identity, |
75 uint32_t id) { | 91 uint32_t id) { |
76 } | 92 } |
77 | 93 |
78 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { | 94 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { |
79 bool found = false; | 95 bool found = false; |
80 for (auto listener : listeners_) | 96 for (auto listener : listeners_) |
81 found |= listener->AcceptConnection(connection); | 97 found |= listener->AcceptConnection(connection); |
82 return found; | 98 return found; |
83 } | 99 } |
84 | 100 |
| 101 void MojoShellConnectionImpl::ShellConnectionLost() { |
| 102 LOG(ERROR) << "Shell connection lost."; |
| 103 } |
| 104 |
85 mojo::Connector* MojoShellConnectionImpl::GetConnector() { | 105 mojo::Connector* MojoShellConnectionImpl::GetConnector() { |
86 DCHECK(shell_connection_); | 106 DCHECK(shell_connection_); |
87 return shell_connection_->connector(); | 107 return shell_connection_->connector(); |
88 } | 108 } |
89 | 109 |
90 bool MojoShellConnectionImpl::UsingExternalShell() const { | 110 bool MojoShellConnectionImpl::UsingExternalShell() const { |
91 return external_; | 111 return external_; |
92 } | 112 } |
93 | 113 |
94 void MojoShellConnectionImpl::AddListener(Listener* listener) { | 114 void MojoShellConnectionImpl::AddListener(Listener* listener) { |
(...skipping 16 matching lines...) Expand all Loading... |
111 // static | 131 // static |
112 void MojoShellConnection::Destroy() { | 132 void MojoShellConnection::Destroy() { |
113 // This joins the shell controller thread. | 133 // This joins the shell controller thread. |
114 delete Get(); | 134 delete Get(); |
115 lazy_tls_ptr.Pointer()->Set(nullptr); | 135 lazy_tls_ptr.Pointer()->Set(nullptr); |
116 } | 136 } |
117 | 137 |
118 MojoShellConnection::~MojoShellConnection() {} | 138 MojoShellConnection::~MojoShellConnection() {} |
119 | 139 |
120 } // namespace content | 140 } // namespace content |
OLD | NEW |