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/browser/mojo/mojo_shell_context.h" | 5 #include "content/browser/mojo/mojo_shell_context.h" |
6 | 6 |
7 #include <unordered_map> | 7 #include <unordered_map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 166 } |
167 return false; | 167 return false; |
168 } | 168 } |
169 | 169 |
170 std::unordered_map<std::string, int> manifest_resources_; | 170 std::unordered_map<std::string, int> manifest_resources_; |
171 std::unique_ptr<ContentBrowserClient::MojoApplicationManifestMap> manifests_; | 171 std::unique_ptr<ContentBrowserClient::MojoApplicationManifestMap> manifests_; |
172 | 172 |
173 DISALLOW_COPY_AND_ASSIGN(BuiltinManifestProvider); | 173 DISALLOW_COPY_AND_ASSIGN(BuiltinManifestProvider); |
174 }; | 174 }; |
175 | 175 |
176 // Thread-safe proxy providing access to the shell context from any thread. | |
177 class MojoShellContext::Proxy { | |
178 public: | |
179 Proxy(MojoShellContext* shell_context) | |
180 : shell_context_(shell_context), | |
181 task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
182 | |
183 ~Proxy() {} | |
184 | |
185 void ConnectToApplication( | |
186 const std::string& user_id, | |
187 const std::string& name, | |
188 const std::string& requestor_name, | |
189 shell::mojom::InterfaceProviderRequest request, | |
190 shell::mojom::InterfaceProviderPtr exposed_services, | |
191 const shell::mojom::Connector::ConnectCallback& callback) { | |
192 if (task_runner_ == base::ThreadTaskRunnerHandle::Get()) { | |
193 if (shell_context_) { | |
194 shell_context_->ConnectToApplicationOnOwnThread( | |
195 user_id, name, requestor_name, std::move(request), | |
196 std::move(exposed_services), callback); | |
197 } | |
198 } else { | |
199 // |shell_context_| outlives the main MessageLoop, so it's safe for it to | |
200 // be unretained here. | |
201 task_runner_->PostTask( | |
202 FROM_HERE, | |
203 base::Bind(&MojoShellContext::ConnectToApplicationOnOwnThread, | |
204 base::Unretained(shell_context_), user_id, name, | |
205 requestor_name, base::Passed(&request), | |
206 base::Passed(&exposed_services), callback)); | |
207 } | |
208 } | |
209 | |
210 private: | |
211 MojoShellContext* shell_context_; | |
212 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
213 | |
214 DISALLOW_COPY_AND_ASSIGN(Proxy); | |
215 }; | |
216 | |
217 // static | |
218 base::LazyInstance<std::unique_ptr<MojoShellContext::Proxy>> | |
219 MojoShellContext::proxy_ = LAZY_INSTANCE_INITIALIZER; | |
220 | |
221 MojoShellContext::MojoShellContext() { | 176 MojoShellContext::MojoShellContext() { |
222 proxy_.Get().reset(new Proxy(this)); | |
223 | |
224 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner = | 177 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner = |
225 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE); | 178 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE); |
226 std::unique_ptr<shell::NativeRunnerFactory> native_runner_factory( | 179 std::unique_ptr<shell::NativeRunnerFactory> native_runner_factory( |
227 new shell::InProcessNativeRunnerFactory( | 180 new shell::InProcessNativeRunnerFactory( |
228 BrowserThread::GetBlockingPool())); | 181 BrowserThread::GetBlockingPool())); |
229 | 182 |
230 // Allow the embedder to register additional Mojo application manifests | 183 // Allow the embedder to register additional Mojo application manifests |
231 // beyond the default ones below. | 184 // beyond the default ones below. |
232 std::unique_ptr<ContentBrowserClient::MojoApplicationManifestMap> manifests( | 185 std::unique_ptr<ContentBrowserClient::MojoApplicationManifestMap> manifests( |
233 new ContentBrowserClient::MojoApplicationManifestMap); | 186 new ContentBrowserClient::MojoApplicationManifestMap); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 | 263 |
311 MojoShellContext::~MojoShellContext() { | 264 MojoShellContext::~MojoShellContext() { |
312 if (MojoShellConnection::GetForProcess()) | 265 if (MojoShellConnection::GetForProcess()) |
313 MojoShellConnection::DestroyForProcess(); | 266 MojoShellConnection::DestroyForProcess(); |
314 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 267 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
315 base::Bind(&DestroyConnectorOnIOThread)); | 268 base::Bind(&DestroyConnectorOnIOThread)); |
316 catalog_.reset(); | 269 catalog_.reset(); |
317 } | 270 } |
318 | 271 |
319 // static | 272 // static |
320 void MojoShellContext::ConnectToApplication( | |
321 const std::string& user_id, | |
322 const std::string& name, | |
323 const std::string& requestor_name, | |
324 shell::mojom::InterfaceProviderRequest request, | |
325 shell::mojom::InterfaceProviderPtr exposed_services, | |
326 const shell::mojom::Connector::ConnectCallback& callback) { | |
327 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name, | |
328 std::move(request), | |
329 std::move(exposed_services), callback); | |
330 } | |
331 | |
332 // static | |
333 shell::Connector* MojoShellContext::GetConnectorForIOThread() { | 273 shell::Connector* MojoShellContext::GetConnectorForIOThread() { |
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
335 return g_io_thread_connector.Get().get(); | 275 return g_io_thread_connector.Get().get(); |
336 } | 276 } |
337 | 277 |
338 void MojoShellContext::ConnectToApplicationOnOwnThread( | |
339 const std::string& user_id, | |
340 const std::string& name, | |
341 const std::string& requestor_name, | |
342 shell::mojom::InterfaceProviderRequest request, | |
343 shell::mojom::InterfaceProviderPtr exposed_services, | |
344 const shell::mojom::Connector::ConnectCallback& callback) { | |
345 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); | |
346 shell::Identity source_id(requestor_name, user_id); | |
347 params->set_source(source_id); | |
348 params->set_target(shell::Identity(name, user_id)); | |
349 params->set_remote_interfaces(std::move(request)); | |
350 params->set_local_interfaces(std::move(exposed_services)); | |
351 params->set_connect_callback(callback); | |
352 service_manager_->Connect(std::move(params)); | |
353 } | |
354 | |
355 } // namespace content | 278 } // namespace content |
OLD | NEW |