Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: content/browser/mojo/mojo_shell_context.cc

Issue 1871223003: Use ShellClientFactory interface to load mojo:profile (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/mojo/mojo_shell_context.h ('k') | content/browser/storage_partition_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <utility> 8 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/command_line.h" 11 #include "base/command_line.h"
11 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/path_service.h" 15 #include "base/path_service.h"
15 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
16 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
17 #include "components/profile_service/profile_app.h" 18 #include "components/profile_service/public/cpp/constants.h"
18 #include "content/browser/gpu/gpu_process_host.h" 19 #include "content/browser/gpu/gpu_process_host.h"
19 #include "content/browser/mojo/constants.h" 20 #include "content/browser/mojo/constants.h"
20 #include "content/common/gpu_process_launch_causes.h" 21 #include "content/common/gpu_process_launch_causes.h"
21 #include "content/common/mojo/current_thread_loader.h"
22 #include "content/common/mojo/mojo_shell_connection_impl.h" 22 #include "content/common/mojo/mojo_shell_connection_impl.h"
23 #include "content/common/mojo/static_loader.h" 23 #include "content/common/mojo/static_loader.h"
24 #include "content/common/process_control.mojom.h" 24 #include "content/common/process_control.mojom.h"
25 #include "content/grit/content_resources.h" 25 #include "content/grit/content_resources.h"
26 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/content_browser_client.h" 27 #include "content/public/browser/content_browser_client.h"
28 #include "content/public/browser/utility_process_host.h" 28 #include "content/public/browser/utility_process_host.h"
29 #include "content/public/browser/utility_process_host_client.h" 29 #include "content/public/browser/utility_process_host_client.h"
30 #include "content/public/common/content_client.h" 30 #include "content/public/common/content_client.h"
31 #include "content/public/common/content_switches.h" 31 #include "content/public/common/content_switches.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 ServiceRegistry* services = process_host->GetServiceRegistry(); 64 ServiceRegistry* services = process_host->GetServiceRegistry();
65 services->ConnectToRemoteService(std::move(request)); 65 services->ConnectToRemoteService(std::move(request));
66 } 66 }
67 67
68 void OnApplicationLoaded(const std::string& name, bool success) { 68 void OnApplicationLoaded(const std::string& name, bool success) {
69 if (!success) 69 if (!success)
70 LOG(ERROR) << "Failed to launch Mojo application for " << name; 70 LOG(ERROR) << "Failed to launch Mojo application for " << name;
71 } 71 }
72 72
73 // The default loader to use for all applications. This does nothing but drop
74 // the Application request.
75 class DefaultLoader : public mojo::shell::Loader {
76 public:
77 DefaultLoader() {}
78 ~DefaultLoader() override {}
79
80 private:
81 // mojo::shell::Loader:
82 void Load(const std::string& name,
83 mojo::shell::mojom::ShellClientRequest request) override {}
84
85 DISALLOW_COPY_AND_ASSIGN(DefaultLoader);
86 };
87
88 // This launches a utility process and forwards the Load request the 73 // This launches a utility process and forwards the Load request the
89 // mojom::ProcessControl service there. The utility process is sandboxed iff 74 // mojom::ProcessControl service there. The utility process is sandboxed iff
90 // |use_sandbox| is true. 75 // |use_sandbox| is true.
91 class UtilityProcessLoader : public mojo::shell::Loader { 76 class UtilityProcessLoader : public mojo::shell::Loader {
92 public: 77 public:
93 UtilityProcessLoader(const base::string16& process_name, bool use_sandbox) 78 UtilityProcessLoader(const base::string16& process_name, bool use_sandbox)
94 : process_name_(process_name), use_sandbox_(use_sandbox) {} 79 : process_name_(process_name), use_sandbox_(use_sandbox) {}
95 ~UtilityProcessLoader() override {} 80 ~UtilityProcessLoader() override {}
96 81
97 private: 82 private:
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 BrowserThread::PostTask( 135 BrowserThread::PostTask(
151 BrowserThread::IO, FROM_HERE, 136 BrowserThread::IO, FROM_HERE,
152 base::Bind(&RequestGpuProcessControl, base::Passed(&process_request))); 137 base::Bind(&RequestGpuProcessControl, base::Passed(&process_request)));
153 process_control->LoadApplication(name, std::move(request), 138 process_control->LoadApplication(name, std::move(request),
154 base::Bind(&OnApplicationLoaded, name)); 139 base::Bind(&OnApplicationLoaded, name));
155 } 140 }
156 141
157 DISALLOW_COPY_AND_ASSIGN(GpuProcessLoader); 142 DISALLOW_COPY_AND_ASSIGN(GpuProcessLoader);
158 }; 143 };
159 144
160 std::string GetStringResource(int id) {
161 return GetContentClient()->GetDataResource(
162 id, ui::ScaleFactor::SCALE_FACTOR_NONE).as_string();
163 }
164
165 } // namespace 145 } // namespace
166 146
167 // A ManifestProvider which resolves application names to builtin manifest 147 // A ManifestProvider which resolves application names to builtin manifest
168 // resources for the catalog service to consume. 148 // resources for the catalog service to consume.
169 class MojoShellContext::BuiltinManifestProvider 149 class MojoShellContext::BuiltinManifestProvider
170 : public catalog::ManifestProvider { 150 : public catalog::ManifestProvider {
171 public: 151 public:
172 BuiltinManifestProvider() {} 152 BuiltinManifestProvider() {}
173 ~BuiltinManifestProvider() override {} 153 ~BuiltinManifestProvider() override {}
174 154
155 void AddManifestResource(const std::string& name, int resource_id) {
156 auto result = manifest_resources_.insert(
157 std::make_pair(name, resource_id));
158 DCHECK(result.second);
159 }
160
175 private: 161 private:
176 // catalog::ManifestProvider: 162 // catalog::ManifestProvider:
177 bool GetApplicationManifest(const base::StringPiece& name, 163 bool GetApplicationManifest(const base::StringPiece& name,
178 std::string* manifest_contents) override { 164 std::string* manifest_contents) override {
179 if (name == "mojo:catalog") { 165 auto it = manifest_resources_.find(name.as_string());
180 *manifest_contents = GetStringResource(IDR_MOJO_CATALOG_MANIFEST); 166 if (it == manifest_resources_.end())
181 return true; 167 return false;
182 } else if (name == kBrowserMojoApplicationName) { 168 *manifest_contents = GetContentClient()->GetDataResource(
183 *manifest_contents = GetStringResource(IDR_MOJO_CONTENT_BROWSER_MANIFEST); 169 it->second, ui::ScaleFactor::SCALE_FACTOR_NONE).as_string();
184 return true; 170 return true;
185 } else if (name == kRendererMojoApplicationName) { 171 }
186 *manifest_contents =
187 GetStringResource(IDR_MOJO_CONTENT_RENDERER_MANIFEST);
188 return true;
189 }
190 172
191 return false; 173 std::unordered_map<std::string, int> manifest_resources_;
192 }
193 174
194 DISALLOW_COPY_AND_ASSIGN(BuiltinManifestProvider); 175 DISALLOW_COPY_AND_ASSIGN(BuiltinManifestProvider);
195 }; 176 };
196 177
197 // Thread-safe proxy providing access to the shell context from any thread. 178 // Thread-safe proxy providing access to the shell context from any thread.
198 class MojoShellContext::Proxy { 179 class MojoShellContext::Proxy {
199 public: 180 public:
200 Proxy(MojoShellContext* shell_context) 181 Proxy(MojoShellContext* shell_context)
201 : shell_context_(shell_context), 182 : shell_context_(shell_context),
202 task_runner_(base::ThreadTaskRunnerHandle::Get()) {} 183 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 218
238 // static 219 // static
239 base::LazyInstance<std::unique_ptr<MojoShellContext::Proxy>> 220 base::LazyInstance<std::unique_ptr<MojoShellContext::Proxy>>
240 MojoShellContext::proxy_ = LAZY_INSTANCE_INITIALIZER; 221 MojoShellContext::proxy_ = LAZY_INSTANCE_INITIALIZER;
241 222
242 void MojoShellContext::SetApplicationsForTest( 223 void MojoShellContext::SetApplicationsForTest(
243 const StaticApplicationMap* apps) { 224 const StaticApplicationMap* apps) {
244 g_applications_for_test = apps; 225 g_applications_for_test = apps;
245 } 226 }
246 227
247 MojoShellContext::MojoShellContext( 228 MojoShellContext::MojoShellContext() {
248 scoped_refptr<base::SingleThreadTaskRunner> file_thread,
249 scoped_refptr<base::SingleThreadTaskRunner> db_thread) {
250 proxy_.Get().reset(new Proxy(this)); 229 proxy_.Get().reset(new Proxy(this));
251 230
252 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner = 231 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner =
253 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); 232 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
254 std::unique_ptr<mojo::shell::NativeRunnerFactory> native_runner_factory( 233 std::unique_ptr<mojo::shell::NativeRunnerFactory> native_runner_factory(
255 new mojo::shell::InProcessNativeRunnerFactory( 234 new mojo::shell::InProcessNativeRunnerFactory(
256 BrowserThread::GetBlockingPool())); 235 BrowserThread::GetBlockingPool()));
236
257 manifest_provider_.reset(new BuiltinManifestProvider); 237 manifest_provider_.reset(new BuiltinManifestProvider);
238 manifest_provider_->AddManifestResource(kBrowserMojoApplicationName,
239 IDR_MOJO_CONTENT_BROWSER_MANIFEST);
240 manifest_provider_->AddManifestResource(kRendererMojoApplicationName,
241 IDR_MOJO_CONTENT_RENDERER_MANIFEST);
242 manifest_provider_->AddManifestResource("mojo:catalog",
243 IDR_MOJO_CATALOG_MANIFEST);
244 manifest_provider_->AddManifestResource(profile::kProfileMojoApplicationName,
245 IDR_MOJO_PROFILE_MANIFEST);
246
258 catalog_.reset(new catalog::Factory(file_task_runner.get(), nullptr, 247 catalog_.reset(new catalog::Factory(file_task_runner.get(), nullptr,
259 manifest_provider_.get())); 248 manifest_provider_.get()));
260 shell_.reset(new mojo::shell::Shell(std::move(native_runner_factory), 249 shell_.reset(new mojo::shell::Shell(std::move(native_runner_factory),
261 catalog_->TakeShellClient())); 250 catalog_->TakeShellClient()));
262 shell_->set_default_loader(
263 std::unique_ptr<mojo::shell::Loader>(new DefaultLoader));
264 251
265 StaticApplicationMap apps; 252 StaticApplicationMap apps;
266 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps); 253 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps);
267 if (g_applications_for_test) { 254 if (g_applications_for_test) {
268 // Add testing apps to the map, potentially overwriting whatever the 255 // Add testing apps to the map, potentially overwriting whatever the
269 // browser client registered. 256 // browser client registered.
270 for (const auto& entry : *g_applications_for_test) 257 for (const auto& entry : *g_applications_for_test)
271 apps[entry.first] = entry.second; 258 apps[entry.first] = entry.second;
272 } 259 }
273 for (const auto& entry : apps) { 260 for (const auto& entry : apps) {
(...skipping 19 matching lines...) Expand all
293 shell_->SetLoaderForName(base::WrapUnique(new UtilityProcessLoader( 280 shell_->SetLoaderForName(base::WrapUnique(new UtilityProcessLoader(
294 app.second, false /* use_sandbox */)), 281 app.second, false /* use_sandbox */)),
295 app.first); 282 app.first);
296 } 283 }
297 284
298 #if (ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) 285 #if (ENABLE_MOJO_MEDIA_IN_GPU_PROCESS)
299 shell_->SetLoaderForName(base::WrapUnique(new GpuProcessLoader), 286 shell_->SetLoaderForName(base::WrapUnique(new GpuProcessLoader),
300 "mojo:media"); 287 "mojo:media");
301 #endif 288 #endif
302 289
303 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
304 switches::kMojoLocalStorage)) {
305 base::Callback<std::unique_ptr<mojo::ShellClient>()> profile_callback =
306 base::Bind(&profile::CreateProfileApp, file_thread, db_thread);
307 shell_->SetLoaderForName(
308 base::WrapUnique(new CurrentThreadLoader(profile_callback)),
309 "mojo:profile");
310 }
311
312 if (!IsRunningInMojoShell()) { 290 if (!IsRunningInMojoShell()) {
313 MojoShellConnection::Create( 291 MojoShellConnection::Create(
314 shell_->InitInstanceForEmbedder(kBrowserMojoApplicationName), 292 shell_->InitInstanceForEmbedder(kBrowserMojoApplicationName),
315 false /* is_external */); 293 false /* is_external */);
316 } 294 }
317 } 295 }
318 296
319 MojoShellContext::~MojoShellContext() { 297 MojoShellContext::~MojoShellContext() {
320 if (!IsRunningInMojoShell()) 298 if (!IsRunningInMojoShell())
321 MojoShellConnectionImpl::Destroy(); 299 MojoShellConnectionImpl::Destroy();
(...skipping 24 matching lines...) Expand all
346 mojo::Identity source_id(requestor_name, user_id); 324 mojo::Identity source_id(requestor_name, user_id);
347 params->set_source(source_id); 325 params->set_source(source_id);
348 params->set_target(mojo::Identity(name, user_id)); 326 params->set_target(mojo::Identity(name, user_id));
349 params->set_remote_interfaces(std::move(request)); 327 params->set_remote_interfaces(std::move(request));
350 params->set_local_interfaces(std::move(exposed_services)); 328 params->set_local_interfaces(std::move(exposed_services));
351 params->set_connect_callback(callback); 329 params->set_connect_callback(callback);
352 shell_->Connect(std::move(params)); 330 shell_->Connect(std::move(params));
353 } 331 }
354 332
355 } // namespace content 333 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/mojo/mojo_shell_context.h ('k') | content/browser/storage_partition_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698