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_switches.h"
12 #include "content/public/common/url_constants.h"
13 #include "content/shell/browser/shell_browser_context.h"
14 #include "content/shell/browser/shell_devtools_manager_delegate.h"
15 #include "extensions/browser/extension_message_filter.h"
16 #include "extensions/browser/extension_protocols.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/guest_view/guest_view_message_filter.h"
19 #include "extensions/browser/info_map.h"
20 #include "extensions/browser/process_map.h"
21 #include "extensions/common/constants.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/switches.h"
24 #include "extensions/shell/browser/shell_browser_context.h"
25 #include "extensions/shell/browser/shell_browser_main_parts.h"
26 #include "extensions/shell/browser/shell_extension_system.h"
27 #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
30 #if !defined(DISABLE_NACL)
31 #include "components/nacl/browser/nacl_browser.h"
32 #include "components/nacl/browser/nacl_host_message_filter.h"
33 #include "components/nacl/browser/nacl_process_host.h"
34 #include "components/nacl/common/nacl_process_type.h"
35 #include "components/nacl/common/nacl_switches.h"
36 #include "content/public/browser/browser_child_process_host.h"
37 #include "content/public/browser/child_process_data.h"
40 using base::CommandLine
;
41 using content::BrowserContext
;
42 using content::BrowserThread
;
44 namespace extensions
{
47 ShellContentBrowserClient
* g_instance
= NULL
;
51 ShellContentBrowserClient::ShellContentBrowserClient(
52 ShellBrowserMainDelegate
* browser_main_delegate
)
53 : browser_main_parts_(NULL
), browser_main_delegate_(browser_main_delegate
) {
58 ShellContentBrowserClient::~ShellContentBrowserClient() {
63 ShellContentBrowserClient
* ShellContentBrowserClient::Get() {
67 content::BrowserContext
* ShellContentBrowserClient::GetBrowserContext() {
68 return browser_main_parts_
->browser_context();
71 content::BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
72 const content::MainFunctionParams
& parameters
) {
74 CreateShellBrowserMainParts(parameters
, browser_main_delegate_
);
75 return browser_main_parts_
;
78 void ShellContentBrowserClient::RenderProcessWillLaunch(
79 content::RenderProcessHost
* host
) {
80 int render_process_id
= host
->GetID();
81 BrowserContext
* browser_context
= browser_main_parts_
->browser_context();
83 new ExtensionMessageFilter(render_process_id
, browser_context
));
85 new GuestViewMessageFilter(render_process_id
, browser_context
));
86 // PluginInfoMessageFilter is not required because app_shell does not have
87 // the concept of disabled plugins.
88 #if !defined(DISABLE_NACL)
89 host
->AddFilter(new nacl::NaClHostMessageFilter(
91 browser_context
->IsOffTheRecord(),
92 browser_context
->GetPath(),
93 browser_context
->GetRequestContextForRenderProcess(render_process_id
)));
97 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
98 content::BrowserContext
* browser_context
,
99 const GURL
& effective_url
) {
100 // This ensures that all render views created for a single app will use the
101 // same render process (see content::SiteInstance::GetProcess). Otherwise the
102 // default behavior of ContentBrowserClient will lead to separate render
103 // processes for the background page and each app window view.
107 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
108 content::BrowserContext
* content_browser_context
,
109 content::ProtocolHandlerMap
* protocol_handlers
,
110 content::URLRequestInterceptorScopedVector request_interceptors
) {
111 // Handle only chrome-extension:// requests. app_shell does not support
112 // chrome-extension-resource:// requests (it does not store shared extension
113 // data in its installation directory).
114 InfoMap
* extension_info_map
=
115 browser_main_parts_
->extension_system()->info_map();
116 (*protocol_handlers
)[kExtensionScheme
] =
117 linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
118 CreateExtensionProtocolHandler(false /* is_incognito */,
119 extension_info_map
));
120 return browser_main_parts_
->browser_context()->CreateRequestContext(
121 protocol_handlers
, request_interceptors
.Pass(), extension_info_map
);
124 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
127 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
128 // content::ShellURLRequestContextGetter::GetURLRequestContext().
129 static const char* const kProtocolList
[] = {
131 content::kChromeDevToolsScheme
,
132 content::kChromeUIScheme
,
135 url::kFileSystemScheme
,
137 kExtensionResourceScheme
,
139 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
140 if (url
.scheme() == kProtocolList
[i
])
146 void ShellContentBrowserClient::SiteInstanceGotProcess(
147 content::SiteInstance
* site_instance
) {
148 // If this isn't an extension renderer there's nothing to do.
149 const Extension
* extension
= GetExtension(site_instance
);
153 ProcessMap::Get(browser_main_parts_
->browser_context())
154 ->Insert(extension
->id(),
155 site_instance
->GetProcess()->GetID(),
156 site_instance
->GetId());
158 BrowserThread::PostTask(
161 base::Bind(&InfoMap::RegisterExtensionProcess
,
162 browser_main_parts_
->extension_system()->info_map(),
164 site_instance
->GetProcess()->GetID(),
165 site_instance
->GetId()));
168 void ShellContentBrowserClient::SiteInstanceDeleting(
169 content::SiteInstance
* site_instance
) {
170 // If this isn't an extension renderer there's nothing to do.
171 const Extension
* extension
= GetExtension(site_instance
);
175 ProcessMap::Get(browser_main_parts_
->browser_context())
176 ->Remove(extension
->id(),
177 site_instance
->GetProcess()->GetID(),
178 site_instance
->GetId());
180 BrowserThread::PostTask(
183 base::Bind(&InfoMap::UnregisterExtensionProcess
,
184 browser_main_parts_
->extension_system()->info_map(),
186 site_instance
->GetProcess()->GetID(),
187 site_instance
->GetId()));
190 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
191 base::CommandLine
* command_line
,
192 int child_process_id
) {
193 std::string process_type
=
194 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
195 if (process_type
== ::switches::kRendererProcess
)
196 AppendRendererSwitches(command_line
);
199 content::SpeechRecognitionManagerDelegate
*
200 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
201 return new speech::ShellSpeechRecognitionManagerDelegate();
204 content::BrowserPpapiHost
*
205 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id
) {
206 #if !defined(DISABLE_NACL)
207 content::BrowserChildProcessHostIterator
iter(PROCESS_TYPE_NACL_LOADER
);
208 while (!iter
.Done()) {
209 nacl::NaClProcessHost
* host
= static_cast<nacl::NaClProcessHost
*>(
211 if (host
->process() &&
212 host
->process()->GetData().id
== plugin_process_id
) {
214 return host
->browser_ppapi_host();
222 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
223 std::vector
<std::string
>* additional_allowed_schemes
) {
224 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
225 additional_allowed_schemes
);
226 additional_allowed_schemes
->push_back(kExtensionScheme
);
229 content::DevToolsManagerDelegate
*
230 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
231 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
234 ShellBrowserMainParts
* ShellContentBrowserClient::CreateShellBrowserMainParts(
235 const content::MainFunctionParams
& parameters
,
236 ShellBrowserMainDelegate
* browser_main_delegate
) {
237 return new ShellBrowserMainParts(parameters
, browser_main_delegate
);
240 void ShellContentBrowserClient::AppendRendererSwitches(
241 base::CommandLine
* command_line
) {
242 // TODO(jamescook): Should we check here if the process is in the extension
243 // service process map, or can we assume all renderers are extension
245 command_line
->AppendSwitch(switches::kExtensionProcess
);
247 #if !defined(DISABLE_NACL)
248 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
249 // SFI switches either here or for the zygote process.
250 static const char* const kSwitchNames
[] = {
251 ::switches::kEnableNaClDebug
,
253 command_line
->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
254 kSwitchNames
, arraysize(kSwitchNames
));
255 #endif // !defined(DISABLE_NACL)
258 const Extension
* ShellContentBrowserClient::GetExtension(
259 content::SiteInstance
* site_instance
) {
260 ExtensionRegistry
* registry
=
261 ExtensionRegistry::Get(site_instance
->GetBrowserContext());
262 return registry
->enabled_extensions().GetExtensionOrAppByURL(
263 site_instance
->GetSiteURL());
266 } // namespace extensions