Chromium Code Reviews| 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/mojo_shell_context.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "content/common/process_control.mojom.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/browser/content_browser_client.h" | |
| 15 #include "content/public/browser/utility_process_host.h" | |
| 16 #include "content/public/browser/utility_process_host_client.h" | |
| 17 #include "content/public/common/content_client.h" | |
| 18 #include "content/public/common/service_registry.h" | |
| 19 #include "mojo/application/public/cpp/application_delegate.h" | |
| 20 #include "mojo/common/url_type_converters.h" | |
| 21 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | |
| 22 #include "mojo/shell/application_loader.h" | |
| 23 #include "mojo/shell/static_application_loader.h" | |
| 24 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | |
| 25 #include "third_party/mojo/src/mojo/public/cpp/bindings/string.h" | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // Virtual app URL to use as the requestor identity when connecting from browser | |
| 32 // code to a Mojo app via the shell proxy. | |
| 33 const char kBrowserAppUrl[] = "system:content_browser"; | |
| 34 | |
| 35 // An extra set of apps to register on initialization, if set by a test. | |
| 36 const MojoShellContext::StaticApplicationMap* g_applications_for_test; | |
|
Lei Zhang
2015/08/25 03:16:03
Should this be explicitly initialized to nullptr?
Ken Rockot(use gerrit already)
2015/08/25 03:54:51
There's no need to do this as C++ guarantees zero-
| |
| 37 | |
| 38 void StartProcessOnIOThread(mojo::InterfaceRequest<ProcessControl> request) { | |
| 39 UtilityProcessHost* process_host = | |
| 40 UtilityProcessHost::Create(nullptr, nullptr); | |
| 41 // TODO(rockot): Make it possible for the embedder to associate app URLs with | |
| 42 // app names so we can have more meaningful process names here. | |
| 43 process_host->SetName(base::UTF8ToUTF16("Mojo Application")); | |
|
Lei Zhang
2015/08/25 03:16:04
I'm assuming because this is in mojo_shell, it doe
Ken Rockot(use gerrit already)
2015/08/25 03:54:51
This is not in Mojo shell. This code is shipping t
| |
| 44 process_host->StartMojoMode(); | |
| 45 ServiceRegistry* services = process_host->GetServiceRegistry(); | |
| 46 services->ConnectToRemoteService(request.Pass()); | |
| 47 } | |
| 48 | |
| 49 void OnApplicationLoaded(const GURL& url, bool success) { | |
| 50 if (!success) | |
| 51 LOG(ERROR) << "Failed to launch Mojo application for " << url.spec(); | |
| 52 } | |
| 53 | |
| 54 // The default loader to use for all applications. This launches a utility | |
| 55 // process and forwards the Load request the ProcessControl service there. | |
| 56 class UtilityProcessLoader : public mojo::shell::ApplicationLoader { | |
| 57 public: | |
| 58 UtilityProcessLoader() {} | |
| 59 ~UtilityProcessLoader() override {} | |
| 60 | |
| 61 private: | |
| 62 // mojo::shell::ApplicationLoader: | |
| 63 void Load( | |
| 64 const GURL& url, | |
| 65 mojo::InterfaceRequest<mojo::Application> application_request) override { | |
| 66 ProcessControlPtr process_control; | |
| 67 auto process_request = mojo::GetProxy(&process_control); | |
| 68 BrowserThread::PostTask( | |
| 69 BrowserThread::IO, FROM_HERE, | |
| 70 base::Bind(&StartProcessOnIOThread, base::Passed(&process_request))); | |
| 71 process_control->LoadApplication(url.spec(), application_request.Pass(), | |
| 72 base::Bind(&OnApplicationLoaded, url)); | |
| 73 } | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(UtilityProcessLoader); | |
| 76 }; | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 // Thread-safe proxy providing access to the shell context from any thread. | |
| 81 class MojoShellContext::Proxy { | |
| 82 public: | |
| 83 Proxy(MojoShellContext* shell_context) | |
|
Lei Zhang
2015/08/25 03:16:04
nit: explicit
Ken Rockot(use gerrit already)
2015/08/25 03:54:51
Acknowledged.
| |
| 84 : shell_context_(shell_context), | |
| 85 task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
| 86 | |
| 87 ~Proxy() {} | |
| 88 | |
| 89 void ConnectToApplication( | |
| 90 const GURL& url, | |
| 91 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
| 92 if (task_runner_ == base::ThreadTaskRunnerHandle::Get()) { | |
| 93 if (shell_context_) | |
|
Lei Zhang
2015/08/25 03:16:04
Here you are checking for |shell_context_|, but in
Ken Rockot(use gerrit already)
2015/08/25 03:54:51
This is a pretty subtle thing, but I believe it's
| |
| 94 shell_context_->ConnectToApplicationOnOwnThread(url, request.Pass()); | |
| 95 } else { | |
| 96 // |shell_context_| outlives the main MessageLoop, so it's safe for it to | |
| 97 // be unretained here. | |
| 98 task_runner_->PostTask( | |
| 99 FROM_HERE, | |
| 100 base::Bind(&MojoShellContext::ConnectToApplicationOnOwnThread, | |
| 101 base::Unretained(shell_context_), url, | |
| 102 base::Passed(&request))); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 MojoShellContext* shell_context_; | |
| 108 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(Proxy); | |
| 111 }; | |
| 112 | |
| 113 // static | |
| 114 base::LazyInstance<scoped_ptr<MojoShellContext::Proxy>> | |
| 115 MojoShellContext::proxy_ = LAZY_INSTANCE_INITIALIZER; | |
| 116 | |
| 117 void MojoShellContext::SetApplicationsForTest( | |
| 118 const StaticApplicationMap* apps) { | |
| 119 g_applications_for_test = apps; | |
| 120 } | |
| 121 | |
| 122 MojoShellContext::MojoShellContext() | |
| 123 : application_manager_(new mojo::shell::ApplicationManager(this)) { | |
| 124 proxy_.Get().reset(new Proxy(this)); | |
| 125 application_manager_->set_default_loader( | |
| 126 scoped_ptr<mojo::shell::ApplicationLoader>(new UtilityProcessLoader)); | |
| 127 | |
| 128 StaticApplicationMap apps; | |
| 129 GetContentClient()->browser()->RegisterMojoApplications(&apps); | |
| 130 if (g_applications_for_test) { | |
| 131 // Add testing apps to the map, potentially overwriting whatever the | |
| 132 // browser client registered. | |
| 133 for (const auto& entry : *g_applications_for_test) | |
| 134 apps[entry.first] = entry.second; | |
| 135 } | |
| 136 for (const auto& entry : apps) { | |
| 137 application_manager_->SetLoaderForURL( | |
| 138 scoped_ptr<mojo::shell::ApplicationLoader>( | |
| 139 new mojo::shell::StaticApplicationLoader(entry.second)), | |
| 140 entry.first); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 MojoShellContext::~MojoShellContext() { | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 void MojoShellContext::ConnectToApplication( | |
| 149 const GURL& url, | |
| 150 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
| 151 proxy_.Get()->ConnectToApplication(url, request.Pass()); | |
| 152 } | |
| 153 | |
| 154 void MojoShellContext::ConnectToApplicationOnOwnThread( | |
| 155 const GURL& url, | |
| 156 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
| 157 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | |
| 158 url_request->url = mojo::String::From(url); | |
| 159 application_manager_->ConnectToApplication( | |
| 160 url_request.Pass(), GURL(kBrowserAppUrl), request.Pass(), | |
| 161 mojo::ServiceProviderPtr(), base::Bind(&base::DoNothing)); | |
| 162 } | |
| 163 | |
| 164 GURL MojoShellContext::ResolveMappings(const GURL& url) { | |
| 165 return url; | |
| 166 } | |
| 167 | |
| 168 GURL MojoShellContext::ResolveMojoURL(const GURL& url) { | |
| 169 return url; | |
| 170 } | |
| 171 | |
| 172 bool MojoShellContext::CreateFetcher( | |
| 173 const GURL& url, | |
| 174 const mojo::shell::Fetcher::FetchCallback& loader_callback) { | |
| 175 return false; | |
| 176 } | |
| 177 | |
| 178 } // namespace content | |
| OLD | NEW |