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 "components/guest_view/browser/guest_view_message_filter.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/site_instance.h"
12 #include "content/public/common/content_descriptors.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/url_constants.h"
15 #include "content/shell/browser/shell_browser_context.h"
16 #include "content/shell/browser/shell_devtools_manager_delegate.h"
17 #include "extensions/browser/extension_message_filter.h"
18 #include "extensions/browser/extension_protocols.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/guest_view/extensions_guest_view_message_filter.h"
21 #include "extensions/browser/info_map.h"
22 #include "extensions/browser/io_thread_extension_message_filter.h"
23 #include "extensions/browser/process_map.h"
24 #include "extensions/common/constants.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/switches.h"
27 #include "extensions/shell/browser/shell_browser_context.h"
28 #include "extensions/shell/browser/shell_browser_main_parts.h"
29 #include "extensions/shell/browser/shell_extension_system.h"
30 #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.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"
43 using base::CommandLine
;
44 using content::BrowserContext
;
45 using content::BrowserThread
;
47 namespace extensions
{
50 ShellContentBrowserClient
* g_instance
= nullptr;
54 ShellContentBrowserClient::ShellContentBrowserClient(
55 ShellBrowserMainDelegate
* browser_main_delegate
)
56 : browser_main_parts_(nullptr),
57 browser_main_delegate_(browser_main_delegate
) {
62 ShellContentBrowserClient::~ShellContentBrowserClient() {
67 ShellContentBrowserClient
* ShellContentBrowserClient::Get() {
71 content::BrowserContext
* ShellContentBrowserClient::GetBrowserContext() {
72 return browser_main_parts_
->browser_context();
75 content::BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
76 const content::MainFunctionParams
& parameters
) {
78 CreateShellBrowserMainParts(parameters
, browser_main_delegate_
);
79 return browser_main_parts_
;
82 void ShellContentBrowserClient::RenderProcessWillLaunch(
83 content::RenderProcessHost
* host
) {
84 int render_process_id
= host
->GetID();
85 BrowserContext
* browser_context
= browser_main_parts_
->browser_context();
87 new ExtensionMessageFilter(render_process_id
, browser_context
));
89 new IOThreadExtensionMessageFilter(render_process_id
, browser_context
));
91 new ExtensionsGuestViewMessageFilter(
92 render_process_id
, browser_context
));
93 // PluginInfoMessageFilter is not required because app_shell does not have
94 // the concept of disabled plugins.
95 #if !defined(DISABLE_NACL)
96 host
->AddFilter(new nacl::NaClHostMessageFilter(
98 browser_context
->IsOffTheRecord(),
99 browser_context
->GetPath(),
100 browser_context
->GetRequestContextForRenderProcess(render_process_id
)));
104 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
105 content::BrowserContext
* browser_context
,
106 const GURL
& effective_url
) {
107 // This ensures that all render views created for a single app will use the
108 // same render process (see content::SiteInstance::GetProcess). Otherwise the
109 // default behavior of ContentBrowserClient will lead to separate render
110 // processes for the background page and each app window view.
114 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
115 content::BrowserContext
* content_browser_context
,
116 content::ProtocolHandlerMap
* protocol_handlers
,
117 content::URLRequestInterceptorScopedVector request_interceptors
) {
118 // Handle only chrome-extension:// requests. app_shell does not support
119 // chrome-extension-resource:// requests (it does not store shared extension
120 // data in its installation directory).
121 InfoMap
* extension_info_map
=
122 browser_main_parts_
->extension_system()->info_map();
123 (*protocol_handlers
)[kExtensionScheme
] =
124 linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
125 CreateExtensionProtocolHandler(false /* is_incognito */,
128 return browser_main_parts_
->browser_context()->CreateRequestContext(
129 protocol_handlers
, request_interceptors
.Pass(), extension_info_map
);
132 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
135 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
136 // content::ShellURLRequestContextGetter::GetURLRequestContext().
137 static const char* const kProtocolList
[] = {
139 content::kChromeDevToolsScheme
,
140 content::kChromeUIScheme
,
143 url::kFileSystemScheme
,
145 kExtensionResourceScheme
,
147 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
148 if (url
.scheme() == kProtocolList
[i
])
154 void ShellContentBrowserClient::SiteInstanceGotProcess(
155 content::SiteInstance
* site_instance
) {
156 // If this isn't an extension renderer there's nothing to do.
157 const Extension
* extension
= GetExtension(site_instance
);
161 ProcessMap::Get(browser_main_parts_
->browser_context())
162 ->Insert(extension
->id(),
163 site_instance
->GetProcess()->GetID(),
164 site_instance
->GetId());
166 BrowserThread::PostTask(
169 base::Bind(&InfoMap::RegisterExtensionProcess
,
170 browser_main_parts_
->extension_system()->info_map(),
172 site_instance
->GetProcess()->GetID(),
173 site_instance
->GetId()));
176 void ShellContentBrowserClient::SiteInstanceDeleting(
177 content::SiteInstance
* site_instance
) {
178 // If this isn't an extension renderer there's nothing to do.
179 const Extension
* extension
= GetExtension(site_instance
);
183 ProcessMap::Get(browser_main_parts_
->browser_context())
184 ->Remove(extension
->id(),
185 site_instance
->GetProcess()->GetID(),
186 site_instance
->GetId());
188 BrowserThread::PostTask(
191 base::Bind(&InfoMap::UnregisterExtensionProcess
,
192 browser_main_parts_
->extension_system()->info_map(),
194 site_instance
->GetProcess()->GetID(),
195 site_instance
->GetId()));
198 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
199 base::CommandLine
* command_line
,
200 int child_process_id
) {
201 std::string process_type
=
202 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
203 if (process_type
== ::switches::kRendererProcess
)
204 AppendRendererSwitches(command_line
);
207 content::SpeechRecognitionManagerDelegate
*
208 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
209 return new speech::ShellSpeechRecognitionManagerDelegate();
212 content::BrowserPpapiHost
*
213 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id
) {
214 #if !defined(DISABLE_NACL)
215 content::BrowserChildProcessHostIterator
iter(PROCESS_TYPE_NACL_LOADER
);
216 while (!iter
.Done()) {
217 nacl::NaClProcessHost
* host
= static_cast<nacl::NaClProcessHost
*>(
219 if (host
->process() &&
220 host
->process()->GetData().id
== plugin_process_id
) {
222 return host
->browser_ppapi_host();
230 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
231 std::vector
<std::string
>* additional_allowed_schemes
) {
232 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
233 additional_allowed_schemes
);
234 additional_allowed_schemes
->push_back(kExtensionScheme
);
237 content::DevToolsManagerDelegate
*
238 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
239 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
242 ShellBrowserMainParts
* ShellContentBrowserClient::CreateShellBrowserMainParts(
243 const content::MainFunctionParams
& parameters
,
244 ShellBrowserMainDelegate
* browser_main_delegate
) {
245 return new ShellBrowserMainParts(parameters
, browser_main_delegate
);
248 void ShellContentBrowserClient::AppendRendererSwitches(
249 base::CommandLine
* command_line
) {
250 // TODO(jamescook): Should we check here if the process is in the extension
251 // service process map, or can we assume all renderers are extension
253 command_line
->AppendSwitch(switches::kExtensionProcess
);
255 #if !defined(DISABLE_NACL)
256 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
257 // SFI switches either here or for the zygote process.
258 static const char* const kSwitchNames
[] = {
259 ::switches::kEnableNaClDebug
,
261 command_line
->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
262 kSwitchNames
, arraysize(kSwitchNames
));
263 #endif // !defined(DISABLE_NACL)
266 const Extension
* ShellContentBrowserClient::GetExtension(
267 content::SiteInstance
* site_instance
) {
268 ExtensionRegistry
* registry
=
269 ExtensionRegistry::Get(site_instance
->GetBrowserContext());
270 return registry
->enabled_extensions().GetExtensionOrAppByURL(
271 site_instance
->GetSiteURL());
274 } // namespace extensions