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(mojo::ShellClientRequest request) { |
| 45 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 46 MojoShellConnectionImpl* connection = |
| 47 new MojoShellConnectionImpl(false /* external */); |
| 48 lazy_tls_ptr.Pointer()->Set(connection); |
| 49 |
| 50 connection->shell_connection_.reset( |
| 51 new mojo::ShellConnection(connection, std::move(request))); |
| 52 connection->shell_connection_->WaitForInitialize(); |
| 53 } |
| 54 |
| 55 // static |
42 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { | 56 MojoShellConnectionImpl* MojoShellConnectionImpl::Get() { |
43 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); | 57 return static_cast<MojoShellConnectionImpl*>(MojoShellConnection::Get()); |
44 } | 58 } |
45 | 59 |
46 void MojoShellConnectionImpl::BindToCommandLinePlatformChannel() { | 60 void MojoShellConnectionImpl::BindToCommandLinePlatformChannel() { |
47 DCHECK(IsRunningInMojoShell()); | 61 DCHECK(IsRunningInMojoShell()); |
48 if (initialized_) | 62 if (initialized_) |
49 return; | 63 return; |
50 WaitForShell(mojo::ScopedMessagePipeHandle()); | 64 WaitForShell(mojo::ScopedMessagePipeHandle()); |
51 } | 65 } |
52 | 66 |
53 void MojoShellConnectionImpl::BindToMessagePipe( | 67 void MojoShellConnectionImpl::BindToMessagePipe( |
54 mojo::ScopedMessagePipeHandle handle) { | 68 mojo::ScopedMessagePipeHandle handle) { |
55 if (initialized_) | 69 if (initialized_) |
56 return; | 70 return; |
57 WaitForShell(std::move(handle)); | 71 WaitForShell(std::move(handle)); |
58 } | 72 } |
59 | 73 |
60 MojoShellConnectionImpl::MojoShellConnectionImpl() : initialized_(false) {} | 74 MojoShellConnectionImpl::MojoShellConnectionImpl(bool external) : |
| 75 external_(external), initialized_(false) {} |
| 76 |
61 MojoShellConnectionImpl::~MojoShellConnectionImpl() { | 77 MojoShellConnectionImpl::~MojoShellConnectionImpl() { |
62 STLDeleteElements(&listeners_); | 78 STLDeleteElements(&listeners_); |
63 } | 79 } |
64 | 80 |
65 void MojoShellConnectionImpl::WaitForShell( | 81 void MojoShellConnectionImpl::WaitForShell( |
66 mojo::ScopedMessagePipeHandle handle) { | 82 mojo::ScopedMessagePipeHandle handle) { |
67 mojo::ShellClientRequest request; | 83 mojo::ShellClientRequest request; |
68 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( | 84 runner_connection_.reset(mojo::shell::RunnerConnection::ConnectToRunner( |
69 &request, std::move(handle))); | 85 &request, std::move(handle), false /* exit_on_error */)); |
| 86 if (!runner_connection_) { |
| 87 delete this; |
| 88 lazy_tls_ptr.Pointer()->Set(nullptr); |
| 89 return; |
| 90 } |
70 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); | 91 shell_connection_.reset(new mojo::ShellConnection(this, std::move(request))); |
71 shell_connection_->WaitForInitialize(); | 92 shell_connection_->WaitForInitialize(); |
72 } | 93 } |
73 | 94 |
74 void MojoShellConnectionImpl::Initialize(mojo::Shell* shell, | 95 void MojoShellConnectionImpl::Initialize(mojo::Shell* shell, |
75 const std::string& url, | 96 const std::string& url, |
76 uint32_t id, | 97 uint32_t id, |
77 uint32_t user_id) { | 98 uint32_t user_id) { |
78 initialized_ = true; | 99 initialized_ = true; |
79 } | 100 } |
80 | 101 |
81 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { | 102 bool MojoShellConnectionImpl::AcceptConnection(mojo::Connection* connection) { |
82 bool found = false; | 103 bool found = false; |
83 for (auto listener : listeners_) | 104 for (auto listener : listeners_) |
84 found |= listener->AcceptConnection(connection); | 105 found |= listener->AcceptConnection(connection); |
85 return found; | 106 return found; |
86 } | 107 } |
87 | 108 |
88 mojo::Shell* MojoShellConnectionImpl::GetShell() { | 109 mojo::Shell* MojoShellConnectionImpl::GetShell() { |
89 DCHECK(initialized_); | 110 DCHECK(initialized_); |
90 return shell_connection_.get(); | 111 return shell_connection_.get(); |
91 } | 112 } |
92 | 113 |
93 bool MojoShellConnectionImpl::UsingExternalShell() const { | 114 bool MojoShellConnectionImpl::UsingExternalShell() const { |
94 return true; | 115 return external_; |
95 } | 116 } |
96 | 117 |
97 void MojoShellConnectionImpl::AddListener(Listener* listener) { | 118 void MojoShellConnectionImpl::AddListener(Listener* listener) { |
98 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == | 119 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
99 listeners_.end()); | 120 listeners_.end()); |
100 listeners_.push_back(listener); | 121 listeners_.push_back(listener); |
101 } | 122 } |
102 | 123 |
103 void MojoShellConnectionImpl::RemoveListener(Listener* listener) { | 124 void MojoShellConnectionImpl::RemoveListener(Listener* listener) { |
104 auto it = std::find(listeners_.begin(), listeners_.end(), listener); | 125 auto it = std::find(listeners_.begin(), listeners_.end(), listener); |
105 DCHECK(it != listeners_.end()); | 126 DCHECK(it != listeners_.end()); |
106 listeners_.erase(it); | 127 listeners_.erase(it); |
107 } | 128 } |
108 | 129 |
109 // static | 130 // static |
110 MojoShellConnection* MojoShellConnection::Get() { | 131 MojoShellConnection* MojoShellConnection::Get() { |
111 return lazy_tls_ptr.Pointer()->Get(); | 132 return lazy_tls_ptr.Pointer()->Get(); |
112 } | 133 } |
113 | 134 |
114 // static | 135 // static |
115 void MojoShellConnection::Destroy() { | 136 void MojoShellConnection::Destroy() { |
116 // This joins the shell controller thread. | 137 // This joins the shell controller thread. |
117 delete Get(); | 138 delete Get(); |
118 lazy_tls_ptr.Pointer()->Set(nullptr); | 139 lazy_tls_ptr.Pointer()->Set(nullptr); |
119 } | 140 } |
120 | 141 |
121 MojoShellConnection::~MojoShellConnection() {} | 142 MojoShellConnection::~MojoShellConnection() {} |
122 | 143 |
123 } // namespace content | 144 } // namespace content |
OLD | NEW |