OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/mojo_shell_context.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "base/macros.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "content/common/process_control.mojom.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/content_browser_client.h" | |
17 #include "content/public/browser/mojo_app_connection.h" | |
18 #include "content/public/browser/utility_process_host.h" | |
19 #include "content/public/browser/utility_process_host_client.h" | |
jam
2015/05/29 01:48:54
i don't think this is needed
there are a lot of i
Ken Rockot(use gerrit already)
2015/05/29 03:20:40
Cleaned up some headers left behind, but this one
| |
20 #include "content/public/common/content_client.h" | |
21 #include "content/public/common/service_registry.h" | |
22 #include "mojo/application/public/cpp/application_delegate.h" | |
23 #include "mojo/common/url_type_converters.h" | |
24 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | |
25 #include "mojo/shell/application_loader.h" | |
26 #include "mojo/shell/static_application_loader.h" | |
27 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | |
28 #include "third_party/mojo/src/mojo/public/cpp/bindings/string.h" | |
29 | |
30 namespace content { | |
31 | |
32 namespace { | |
33 | |
34 // Virtual app URL to use as the requestor identity when connecting from browser | |
35 // code to a Mojo app via the shell proxy. | |
36 const char kBrowserAppUrl[] = "system:content_browser"; | |
37 | |
38 // An ApplicationRegistry instance used for testing to override builtin loaders. | |
39 base::LazyInstance<base::Callback<void(ApplicationRegistry*)>> | |
40 g_application_registry_initializer_for_testing; | |
41 | |
42 void StartProcessOnIOThread(mojo::InterfaceRequest<ProcessControl> request) { | |
43 UtilityProcessHost* process_host = | |
44 UtilityProcessHost::Create(nullptr, nullptr); | |
45 // TODO(rockot): Make it possible for the embedder to associate app URLs with | |
46 // app names so we can have more meaningful process names here. | |
47 process_host->SetName(base::UTF8ToUTF16("Mojo Application")); | |
48 process_host->StartMojoMode(); | |
49 ServiceRegistry* services = process_host->GetServiceRegistry(); | |
50 services->ConnectToRemoteService(request.Pass()); | |
51 } | |
52 | |
53 void OnApplicationLoaded(const GURL& url, LoadApplicationResult result) { | |
54 if (result != LOAD_APPLICATION_RESULT_OK) { | |
55 LOG(ERROR) << "Failed to launch Mojo application for " << url.spec(); | |
56 } | |
57 } | |
58 | |
59 // The default loader to use for all applications. This launches a utility | |
60 // process and forwards the Load request the ProcessControl service there. | |
61 class UtilityProcessLoader : public mojo::shell::ApplicationLoader { | |
62 public: | |
63 UtilityProcessLoader() {} | |
64 ~UtilityProcessLoader() override {} | |
65 | |
66 private: | |
67 // mojo::shell::ApplicationLoader: | |
68 void Load( | |
69 const GURL& url, | |
70 mojo::InterfaceRequest<mojo::Application> application_request) override { | |
71 ProcessControlPtr process_control; | |
72 auto process_request = mojo::GetProxy(&process_control); | |
73 BrowserThread::PostTask( | |
74 BrowserThread::IO, FROM_HERE, | |
75 base::Bind(&StartProcessOnIOThread, base::Passed(&process_request))); | |
76 process_control->LoadApplication(url.spec(), application_request.Pass(), | |
77 base::Bind(&OnApplicationLoaded, url)); | |
78 } | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(UtilityProcessLoader); | |
81 }; | |
82 | |
83 } // namespace | |
84 | |
85 // Thread-safe proxy providing access to the shell context from any thread. | |
86 class MojoShellContext::Proxy { | |
87 public: | |
88 Proxy(MojoShellContext* shell_context) | |
89 : shell_context_(shell_context), | |
90 task_runner_(base::ThreadTaskRunnerHandle::Get()) { | |
91 } | |
92 | |
93 ~Proxy() {} | |
94 | |
95 void ConnectToApplication( | |
96 const GURL& url, | |
97 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
98 if (task_runner_ == base::ThreadTaskRunnerHandle::Get()) { | |
99 if (shell_context_) | |
100 shell_context_->ConnectToApplicationOnOwnThread(url, request.Pass()); | |
101 } else { | |
102 // |shell_context_| outlives the main MessageLoop, so it's safe for it to | |
103 // be unretained here. | |
104 task_runner_->PostTask( | |
105 FROM_HERE, | |
106 base::Bind(&MojoShellContext::ConnectToApplicationOnOwnThread, | |
107 base::Unretained(shell_context_), url, | |
108 base::Passed(&request))); | |
109 } | |
110 } | |
111 | |
112 private: | |
113 MojoShellContext* shell_context_; | |
114 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(Proxy); | |
117 }; | |
118 | |
119 // static | |
120 base::LazyInstance<scoped_ptr<MojoShellContext::Proxy>> | |
121 MojoShellContext::proxy_ = LAZY_INSTANCE_INITIALIZER; | |
122 | |
123 void MojoShellContext::SetApplicationRegistryInitializerForTesting( | |
124 const base::Callback<void(ApplicationRegistry*)>& registry) { | |
125 g_application_registry_initializer_for_testing.Get() = registry; | |
126 } | |
127 | |
128 MojoShellContext::MojoShellContext() | |
129 : application_manager_(new mojo::shell::ApplicationManager(this)) { | |
130 proxy_.Get().reset(new Proxy(this)); | |
131 application_manager_->set_default_loader( | |
132 scoped_ptr<mojo::shell::ApplicationLoader>(new UtilityProcessLoader)); | |
133 GetContentClient()->browser()->RegisterMojoApplications(this); | |
134 if (!g_application_registry_initializer_for_testing.Get().is_null()) | |
135 g_application_registry_initializer_for_testing.Get().Run(this); | |
136 } | |
137 | |
138 MojoShellContext::~MojoShellContext() { | |
139 } | |
140 | |
141 // static | |
142 void MojoShellContext::ConnectToApplication( | |
143 const GURL& url, | |
144 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
145 proxy_.Get()->ConnectToApplication(url, request.Pass()); | |
146 } | |
147 | |
148 void MojoShellContext::ConnectToApplicationOnOwnThread( | |
149 const GURL& url, | |
150 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
151 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | |
152 url_request->url = mojo::String::From(url); | |
153 application_manager_->ConnectToApplication( | |
154 url_request.Pass(), GURL(kBrowserAppUrl), request.Pass(), | |
155 mojo::ServiceProviderPtr(), base::Bind(&base::DoNothing)); | |
156 } | |
157 | |
158 void MojoShellContext::RegisterStaticAppForURL( | |
159 const GURL& url, | |
160 const StaticAppFactory& factory) { | |
161 application_manager_->SetLoaderForURL( | |
162 scoped_ptr<mojo::shell::ApplicationLoader>( | |
163 new mojo::shell::StaticApplicationLoader(factory)), | |
164 url); | |
165 } | |
166 | |
167 GURL MojoShellContext::ResolveMappings(const GURL& url) { | |
168 return url; | |
169 } | |
170 | |
171 GURL MojoShellContext::ResolveMojoURL(const GURL& url) { | |
172 return url; | |
173 } | |
174 | |
175 bool MojoShellContext::CreateFetcher( | |
176 const GURL& url, | |
177 const mojo::shell::Fetcher::FetchCallback& loader_callback) { | |
178 return false; | |
179 } | |
180 | |
181 } // namespace content | |
OLD | NEW |