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_shell_context_impl.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/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/content_browser_client.h" | |
| 16 #include "content/public/browser/mojo_app_connection.h" | |
| 17 #include "content/public/browser/utility_process_host.h" | |
| 18 #include "content/public/browser/utility_process_host_client.h" | |
| 19 #include "content/public/common/content_client.h" | |
| 20 #include "content/public/common/process_control.mojom.h" | |
| 21 #include "content/public/common/service_registry.h" | |
| 22 #include "content/public/common/static_mojo_application_loader.h" | |
| 23 #include "mojo/application/public/cpp/application_delegate.h" | |
| 24 #include "mojo/common/url_type_converters.h" | |
| 25 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | |
| 26 #include "mojo/shell/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"; | |
|
jam
2015/05/27 16:05:36
i'm confused, how come this string is only referen
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
It would make sense for this to be a public consta
| |
| 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")); | |
|
jam
2015/05/27 16:05:36
all the other process names are localized..
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
It didn't seem worthwhile to localize a placeholde
| |
| 48 if (process_host->StartMojoMode()) { | |
| 49 ServiceRegistry* services = process_host->GetServiceRegistry(); | |
| 50 services->ConnectToRemoteService(request.Pass()); | |
| 51 } else { | |
| 52 LOG(ERROR) << "Unable to start utility process."; | |
|
jam
2015/05/27 16:05:36
do we really need all this error handling? i looke
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
Ah, you're right. I removed the error checking. I
| |
| 53 } | |
| 54 } | |
| 55 | |
| 56 void OnApplicationLoaded( | |
| 57 const GURL& url, LoadApplicationResult result) { | |
| 58 if (result != LOAD_APPLICATION_RESULT_OK) { | |
| 59 LOG(ERROR) << "Failed to launch Mojo application for " << url.spec(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 // The default loader to use for all applications. This launches a utility | |
| 64 // process and forwards the Load request the ProcessControl service there. | |
| 65 class UtilityProcessLoader : public mojo::shell::ApplicationLoader { | |
| 66 public: | |
| 67 UtilityProcessLoader() {} | |
| 68 ~UtilityProcessLoader() override {} | |
| 69 | |
| 70 private: | |
| 71 // mojo::shell::ApplicationLoader: | |
| 72 void Load( | |
| 73 const GURL& url, | |
| 74 mojo::InterfaceRequest<mojo::Application> application_request) override { | |
| 75 ProcessControlPtr process_control; | |
| 76 auto process_request = mojo::GetProxy(&process_control); | |
| 77 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) | |
|
jam
2015/05/27 16:05:36
nit:BrowserThread::PostTask(BrowserThread::IO, FRO
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
Done.
| |
| 78 ->PostTask(FROM_HERE, | |
| 79 base::Bind(&StartProcessOnIOThread, | |
| 80 base::Passed(&process_request))); | |
| 81 process_control->LoadApplication( | |
| 82 mojo::String::From(url.spec()), application_request.Pass(), | |
|
jam
2015/05/27 16:05:36
nit: I think url.spec() is enough since mojo::Stri
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
Done.
| |
| 83 base::Bind(&OnApplicationLoaded, url)); | |
| 84 } | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(UtilityProcessLoader); | |
| 87 }; | |
| 88 | |
| 89 } // namespace | |
| 90 | |
| 91 // Thread-safe proxy providing global browser access to the shell context. | |
|
jam
2015/05/27 16:05:36
what does "global browser access" mean? do you mea
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
Yes. Clarified.
| |
| 92 class MojoShellContextImpl::Proxy { | |
| 93 public: | |
| 94 Proxy() {} | |
| 95 ~Proxy() {} | |
| 96 | |
| 97 void Reset(base::WeakPtr<MojoShellContextImpl> weak_shell_context); | |
|
jam
2015/05/27 16:05:36
nit: IMO this would be more readable if they were
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
Done.
| |
| 98 void ConnectToApplication( | |
| 99 const GURL& url, | |
| 100 mojo::InterfaceRequest<mojo::ServiceProvider> request); | |
| 101 | |
| 102 private: | |
| 103 base::Lock lock_; | |
|
jam
2015/05/27 16:05:35
I don't understand why this locking is needed. i.e
Ken Rockot(use gerrit already)
2015/05/27 19:36:16
You're right, no need for the weakptr either. Thou
| |
| 104 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 105 base::WeakPtr<MojoShellContextImpl> weak_shell_context_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(Proxy); | |
| 108 }; | |
| 109 | |
| 110 // static | |
| 111 base::LazyInstance<MojoShellContextImpl::Proxy> MojoShellContextImpl::proxy_ = | |
| 112 LAZY_INSTANCE_INITIALIZER; | |
| 113 | |
| 114 void MojoShellContextImpl::Proxy::Reset( | |
| 115 base::WeakPtr<MojoShellContextImpl> weak_shell_context) { | |
| 116 base::AutoLock locker(lock_); | |
| 117 DCHECK(!weak_shell_context_ || !weak_shell_context); | |
| 118 weak_shell_context_ = weak_shell_context; | |
| 119 if (weak_shell_context_) { | |
| 120 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 121 } else { | |
| 122 task_runner_ = nullptr; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void MojoShellContextImpl::Proxy::ConnectToApplication( | |
| 127 const GURL& url, | |
| 128 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
| 129 base::AutoLock locker(lock_); | |
| 130 if (!task_runner_) { | |
| 131 LOG(ERROR) << "No shell context available to connect to " << url.spec(); | |
| 132 return; | |
| 133 } | |
| 134 if (task_runner_ == base::ThreadTaskRunnerHandle::Get()) { | |
| 135 if (weak_shell_context_) { | |
| 136 weak_shell_context_->ConnectToApplicationOnOwnThread( | |
| 137 url, request.Pass()); | |
| 138 } | |
| 139 } else { | |
| 140 task_runner_->PostTask( | |
| 141 FROM_HERE, | |
| 142 base::Bind(&MojoShellContextImpl::ConnectToApplicationOnOwnThread, | |
| 143 weak_shell_context_, url, base::Passed(&request))); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 scoped_ptr<MojoShellContext> MojoShellContext::Create() { | |
| 149 return scoped_ptr<MojoShellContext>(new MojoShellContextImpl); | |
| 150 } | |
| 151 | |
| 152 void MojoShellContext::SetApplicationRegistryInitializerForTesting( | |
| 153 const base::Callback<void(ApplicationRegistry*)>& registry) { | |
| 154 g_application_registry_initializer_for_testing.Get() = registry; | |
| 155 } | |
| 156 | |
| 157 MojoShellContextImpl::MojoShellContextImpl() | |
| 158 : application_manager_(new mojo::shell::ApplicationManager(this)), | |
| 159 weak_factory_(this) { | |
| 160 proxy_.Get().Reset(weak_factory_.GetWeakPtr()); | |
| 161 application_manager_->set_default_loader( | |
| 162 scoped_ptr<mojo::shell::ApplicationLoader>(new UtilityProcessLoader)); | |
| 163 GetContentClient()->browser()->RegisterMojoApplications(this); | |
| 164 if (!g_application_registry_initializer_for_testing.Get().is_null()) | |
| 165 g_application_registry_initializer_for_testing.Get().Run(this); | |
| 166 } | |
| 167 | |
| 168 MojoShellContextImpl::~MojoShellContextImpl() { | |
| 169 proxy_.Get().Reset(base::WeakPtr<MojoShellContextImpl>()); | |
| 170 } | |
| 171 | |
| 172 // static | |
| 173 void MojoShellContextImpl::ConnectToApplication( | |
| 174 const GURL& url, | |
| 175 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
| 176 proxy_.Get().ConnectToApplication(url, request.Pass()); | |
| 177 } | |
| 178 | |
| 179 void MojoShellContextImpl::RegisterStaticAppForURL( | |
| 180 const GURL& url, | |
| 181 const StaticAppFactory& factory) { | |
| 182 application_manager_->SetLoaderForURL( | |
| 183 scoped_ptr<mojo::shell::ApplicationLoader>( | |
| 184 new StaticMojoApplicationLoader(factory)), | |
| 185 url); | |
| 186 } | |
| 187 | |
| 188 void MojoShellContextImpl::ConnectToApplicationOnOwnThread( | |
| 189 const GURL& url, | |
| 190 mojo::InterfaceRequest<mojo::ServiceProvider> request) { | |
| 191 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | |
| 192 url_request->url = mojo::String::From(url); | |
| 193 application_manager_->ConnectToApplication( | |
| 194 url_request.Pass(), GURL(kBrowserAppUrl), request.Pass(), | |
| 195 mojo::ServiceProviderPtr(), base::Bind(&base::DoNothing)); | |
| 196 } | |
| 197 | |
| 198 GURL MojoShellContextImpl::ResolveMappings(const GURL& url) { | |
| 199 return url; | |
| 200 } | |
| 201 | |
| 202 GURL MojoShellContextImpl::ResolveMojoURL(const GURL& url) { | |
| 203 return url; | |
| 204 } | |
| 205 | |
| 206 bool MojoShellContextImpl::CreateFetcher( | |
| 207 const GURL& url, | |
| 208 const mojo::shell::Fetcher::FetchCallback& loader_callback) { | |
| 209 return false; | |
| 210 } | |
| 211 | |
| 212 | |
| 213 } // namespace content | |
| OLD | NEW |