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 */,
126 extension_info_map
));
127 return browser_main_parts_
->browser_context()->CreateRequestContext(
128 protocol_handlers
, request_interceptors
.Pass(), extension_info_map
);
131 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
134 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
135 // content::ShellURLRequestContextGetter::GetURLRequestContext().
136 static const char* const kProtocolList
[] = {
138 content::kChromeDevToolsScheme
,
139 content::kChromeUIScheme
,
142 url::kFileSystemScheme
,
144 kExtensionResourceScheme
,
146 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
147 if (url
.scheme() == kProtocolList
[i
])
153 void ShellContentBrowserClient::SiteInstanceGotProcess(
154 content::SiteInstance
* site_instance
) {
155 // If this isn't an extension renderer there's nothing to do.
156 const Extension
* extension
= GetExtension(site_instance
);
160 ProcessMap::Get(browser_main_parts_
->browser_context())
161 ->Insert(extension
->id(),
162 site_instance
->GetProcess()->GetID(),
163 site_instance
->GetId());
165 BrowserThread::PostTask(
168 base::Bind(&InfoMap::RegisterExtensionProcess
,
169 browser_main_parts_
->extension_system()->info_map(),
171 site_instance
->GetProcess()->GetID(),
172 site_instance
->GetId()));
175 void ShellContentBrowserClient::SiteInstanceDeleting(
176 content::SiteInstance
* site_instance
) {
177 // If this isn't an extension renderer there's nothing to do.
178 const Extension
* extension
= GetExtension(site_instance
);
182 ProcessMap::Get(browser_main_parts_
->browser_context())
183 ->Remove(extension
->id(),
184 site_instance
->GetProcess()->GetID(),
185 site_instance
->GetId());
187 BrowserThread::PostTask(
190 base::Bind(&InfoMap::UnregisterExtensionProcess
,
191 browser_main_parts_
->extension_system()->info_map(),
193 site_instance
->GetProcess()->GetID(),
194 site_instance
->GetId()));
197 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
198 base::CommandLine
* command_line
,
199 int child_process_id
) {
200 std::string process_type
=
201 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
202 if (process_type
== ::switches::kRendererProcess
)
203 AppendRendererSwitches(command_line
);
206 content::SpeechRecognitionManagerDelegate
*
207 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
208 return new speech::ShellSpeechRecognitionManagerDelegate();
211 content::BrowserPpapiHost
*
212 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id
) {
213 #if !defined(DISABLE_NACL)
214 content::BrowserChildProcessHostIterator
iter(PROCESS_TYPE_NACL_LOADER
);
215 while (!iter
.Done()) {
216 nacl::NaClProcessHost
* host
= static_cast<nacl::NaClProcessHost
*>(
218 if (host
->process() &&
219 host
->process()->GetData().id
== plugin_process_id
) {
221 return host
->browser_ppapi_host();
229 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
230 std::vector
<std::string
>* additional_allowed_schemes
) {
231 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
232 additional_allowed_schemes
);
233 additional_allowed_schemes
->push_back(kExtensionScheme
);
236 content::DevToolsManagerDelegate
*
237 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
238 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
241 ShellBrowserMainParts
* ShellContentBrowserClient::CreateShellBrowserMainParts(
242 const content::MainFunctionParams
& parameters
,
243 ShellBrowserMainDelegate
* browser_main_delegate
) {
244 return new ShellBrowserMainParts(parameters
, browser_main_delegate
);
247 void ShellContentBrowserClient::AppendRendererSwitches(
248 base::CommandLine
* command_line
) {
249 // TODO(jamescook): Should we check here if the process is in the extension
250 // service process map, or can we assume all renderers are extension
252 command_line
->AppendSwitch(switches::kExtensionProcess
);
254 #if !defined(DISABLE_NACL)
255 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
256 // SFI switches either here or for the zygote process.
257 static const char* const kSwitchNames
[] = {
258 ::switches::kEnableNaClDebug
,
260 command_line
->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
261 kSwitchNames
, arraysize(kSwitchNames
));
262 #endif // !defined(DISABLE_NACL)
265 const Extension
* ShellContentBrowserClient::GetExtension(
266 content::SiteInstance
* site_instance
) {
267 ExtensionRegistry
* registry
=
268 ExtensionRegistry::Get(site_instance
->GetBrowserContext());
269 return registry
->enabled_extensions().GetExtensionOrAppByURL(
270 site_instance
->GetSiteURL());
273 } // namespace extensions