Remove unused parameter.
[chromium-blink-merge.git] / extensions / shell / browser / shell_content_browser_client.cc
blob3971f0ca86683b7def20bf56af7d9f6047ea9091
1 // Copyright 2014 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.
5 #include "extensions/shell/browser/shell_content_browser_client.h"
7 #include "base/command_line.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/common/content_descriptors.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/common/url_constants.h"
14 #include "content/shell/browser/shell_browser_context.h"
15 #include "content/shell/browser/shell_devtools_manager_delegate.h"
16 #include "extensions/browser/extension_message_filter.h"
17 #include "extensions/browser/extension_protocols.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/guest_view/guest_view_message_filter.h"
20 #include "extensions/browser/info_map.h"
21 #include "extensions/browser/io_thread_extension_message_filter.h"
22 #include "extensions/browser/process_map.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/switches.h"
26 #include "extensions/shell/browser/shell_browser_context.h"
27 #include "extensions/shell/browser/shell_browser_main_parts.h"
28 #include "extensions/shell/browser/shell_extension_system.h"
29 #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
30 #include "gin/v8_initializer.h"
31 #include "url/gurl.h"
33 #if !defined(DISABLE_NACL)
34 #include "components/nacl/browser/nacl_browser.h"
35 #include "components/nacl/browser/nacl_host_message_filter.h"
36 #include "components/nacl/browser/nacl_process_host.h"
37 #include "components/nacl/common/nacl_process_type.h"
38 #include "components/nacl/common/nacl_switches.h"
39 #include "content/public/browser/browser_child_process_host.h"
40 #include "content/public/browser/child_process_data.h"
41 #endif
43 using base::CommandLine;
44 using content::BrowserContext;
45 using content::BrowserThread;
47 namespace extensions {
48 namespace {
50 ShellContentBrowserClient* g_instance = nullptr;
52 } // namespace
54 ShellContentBrowserClient::ShellContentBrowserClient(
55 ShellBrowserMainDelegate* browser_main_delegate)
57 #if defined(OS_POSIX) && !defined(OS_MACOSX)
58 v8_natives_fd_(-1),
59 v8_snapshot_fd_(-1),
60 #endif // OS_POSIX && !OS_MACOSX
61 browser_main_parts_(nullptr),
62 browser_main_delegate_(browser_main_delegate) {
63 DCHECK(!g_instance);
64 g_instance = this;
67 ShellContentBrowserClient::~ShellContentBrowserClient() {
68 g_instance = nullptr;
71 // static
72 ShellContentBrowserClient* ShellContentBrowserClient::Get() {
73 return g_instance;
76 content::BrowserContext* ShellContentBrowserClient::GetBrowserContext() {
77 return browser_main_parts_->browser_context();
80 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
81 const content::MainFunctionParams& parameters) {
82 browser_main_parts_ =
83 CreateShellBrowserMainParts(parameters, browser_main_delegate_);
84 return browser_main_parts_;
87 void ShellContentBrowserClient::RenderProcessWillLaunch(
88 content::RenderProcessHost* host) {
89 int render_process_id = host->GetID();
90 BrowserContext* browser_context = browser_main_parts_->browser_context();
91 host->AddFilter(
92 new ExtensionMessageFilter(render_process_id, browser_context));
93 host->AddFilter(
94 new IOThreadExtensionMessageFilter(render_process_id, browser_context));
95 host->AddFilter(
96 new GuestViewMessageFilter(render_process_id, browser_context));
97 // PluginInfoMessageFilter is not required because app_shell does not have
98 // the concept of disabled plugins.
99 #if !defined(DISABLE_NACL)
100 host->AddFilter(new nacl::NaClHostMessageFilter(
101 render_process_id,
102 browser_context->IsOffTheRecord(),
103 browser_context->GetPath(),
104 browser_context->GetRequestContextForRenderProcess(render_process_id)));
105 #endif
108 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
109 content::BrowserContext* browser_context,
110 const GURL& effective_url) {
111 // This ensures that all render views created for a single app will use the
112 // same render process (see content::SiteInstance::GetProcess). Otherwise the
113 // default behavior of ContentBrowserClient will lead to separate render
114 // processes for the background page and each app window view.
115 return true;
118 net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext(
119 content::BrowserContext* content_browser_context,
120 content::ProtocolHandlerMap* protocol_handlers,
121 content::URLRequestInterceptorScopedVector request_interceptors) {
122 // Handle only chrome-extension:// requests. app_shell does not support
123 // chrome-extension-resource:// requests (it does not store shared extension
124 // data in its installation directory).
125 InfoMap* extension_info_map =
126 browser_main_parts_->extension_system()->info_map();
127 (*protocol_handlers)[kExtensionScheme] =
128 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
129 CreateExtensionProtocolHandler(false /* is_incognito */,
130 extension_info_map));
131 return browser_main_parts_->browser_context()->CreateRequestContext(
132 protocol_handlers, request_interceptors.Pass(), extension_info_map);
135 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
136 if (!url.is_valid())
137 return false;
138 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
139 // content::ShellURLRequestContextGetter::GetURLRequestContext().
140 static const char* const kProtocolList[] = {
141 url::kBlobScheme,
142 content::kChromeDevToolsScheme,
143 content::kChromeUIScheme,
144 url::kDataScheme,
145 url::kFileScheme,
146 url::kFileSystemScheme,
147 kExtensionScheme,
148 kExtensionResourceScheme,
150 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
151 if (url.scheme() == kProtocolList[i])
152 return true;
154 return false;
157 void ShellContentBrowserClient::SiteInstanceGotProcess(
158 content::SiteInstance* site_instance) {
159 // If this isn't an extension renderer there's nothing to do.
160 const Extension* extension = GetExtension(site_instance);
161 if (!extension)
162 return;
164 ProcessMap::Get(browser_main_parts_->browser_context())
165 ->Insert(extension->id(),
166 site_instance->GetProcess()->GetID(),
167 site_instance->GetId());
169 BrowserThread::PostTask(
170 BrowserThread::IO,
171 FROM_HERE,
172 base::Bind(&InfoMap::RegisterExtensionProcess,
173 browser_main_parts_->extension_system()->info_map(),
174 extension->id(),
175 site_instance->GetProcess()->GetID(),
176 site_instance->GetId()));
179 void ShellContentBrowserClient::SiteInstanceDeleting(
180 content::SiteInstance* site_instance) {
181 // If this isn't an extension renderer there's nothing to do.
182 const Extension* extension = GetExtension(site_instance);
183 if (!extension)
184 return;
186 ProcessMap::Get(browser_main_parts_->browser_context())
187 ->Remove(extension->id(),
188 site_instance->GetProcess()->GetID(),
189 site_instance->GetId());
191 BrowserThread::PostTask(
192 BrowserThread::IO,
193 FROM_HERE,
194 base::Bind(&InfoMap::UnregisterExtensionProcess,
195 browser_main_parts_->extension_system()->info_map(),
196 extension->id(),
197 site_instance->GetProcess()->GetID(),
198 site_instance->GetId()));
201 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
202 base::CommandLine* command_line,
203 int child_process_id) {
204 std::string process_type =
205 command_line->GetSwitchValueASCII(::switches::kProcessType);
206 if (process_type == ::switches::kRendererProcess)
207 AppendRendererSwitches(command_line);
210 content::SpeechRecognitionManagerDelegate*
211 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
212 return new speech::ShellSpeechRecognitionManagerDelegate();
215 content::BrowserPpapiHost*
216 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id) {
217 #if !defined(DISABLE_NACL)
218 content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
219 while (!iter.Done()) {
220 nacl::NaClProcessHost* host = static_cast<nacl::NaClProcessHost*>(
221 iter.GetDelegate());
222 if (host->process() &&
223 host->process()->GetData().id == plugin_process_id) {
224 // Found the plugin.
225 return host->browser_ppapi_host();
227 ++iter;
229 #endif
230 return nullptr;
233 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
234 std::vector<std::string>* additional_allowed_schemes) {
235 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
236 additional_allowed_schemes);
237 additional_allowed_schemes->push_back(kExtensionScheme);
240 #if defined(OS_POSIX) && !defined(OS_MACOSX)
241 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
242 const base::CommandLine& command_line,
243 int child_process_id,
244 content::FileDescriptorInfo* mappings) {
245 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
246 if (v8_natives_fd_.get() == -1 || v8_snapshot_fd_.get() == -1) {
247 int v8_natives_fd = -1;
248 int v8_snapshot_fd = -1;
249 if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd,
250 &v8_snapshot_fd)) {
251 v8_natives_fd_.reset(v8_natives_fd);
252 v8_snapshot_fd_.reset(v8_snapshot_fd);
255 DCHECK(v8_natives_fd_.get() != -1 && v8_snapshot_fd_.get() != -1);
256 mappings->Share(kV8NativesDataDescriptor, v8_natives_fd_.get());
257 mappings->Share(kV8SnapshotDataDescriptor, v8_snapshot_fd_.get());
258 #endif // V8_USE_EXTERNAL_STARTUP_DATA
260 #endif // OS_POSIX && !OS_MACOSX
262 content::DevToolsManagerDelegate*
263 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
264 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
267 ShellBrowserMainParts* ShellContentBrowserClient::CreateShellBrowserMainParts(
268 const content::MainFunctionParams& parameters,
269 ShellBrowserMainDelegate* browser_main_delegate) {
270 return new ShellBrowserMainParts(parameters, browser_main_delegate);
273 void ShellContentBrowserClient::AppendRendererSwitches(
274 base::CommandLine* command_line) {
275 // TODO(jamescook): Should we check here if the process is in the extension
276 // service process map, or can we assume all renderers are extension
277 // renderers?
278 command_line->AppendSwitch(switches::kExtensionProcess);
280 #if !defined(DISABLE_NACL)
281 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
282 // SFI switches either here or for the zygote process.
283 static const char* const kSwitchNames[] = {
284 ::switches::kEnableNaClDebug,
286 command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
287 kSwitchNames, arraysize(kSwitchNames));
288 #endif // !defined(DISABLE_NACL)
291 const Extension* ShellContentBrowserClient::GetExtension(
292 content::SiteInstance* site_instance) {
293 ExtensionRegistry* registry =
294 ExtensionRegistry::Get(site_instance->GetBrowserContext());
295 return registry->enabled_extensions().GetExtensionOrAppByURL(
296 site_instance->GetSiteURL());
299 } // namespace extensions