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"
31 #include "gin/v8_initializer.h"
34 #if !defined(DISABLE_NACL)
35 #include "components/nacl/browser/nacl_browser.h"
36 #include "components/nacl/browser/nacl_host_message_filter.h"
37 #include "components/nacl/browser/nacl_process_host.h"
38 #include "components/nacl/common/nacl_process_type.h"
39 #include "components/nacl/common/nacl_switches.h"
40 #include "content/public/browser/browser_child_process_host.h"
41 #include "content/public/browser/child_process_data.h"
44 using base::CommandLine
;
45 using content::BrowserContext
;
46 using content::BrowserThread
;
48 namespace extensions
{
51 ShellContentBrowserClient
* g_instance
= nullptr;
55 ShellContentBrowserClient::ShellContentBrowserClient(
56 ShellBrowserMainDelegate
* browser_main_delegate
)
58 #if defined(OS_POSIX) && !defined(OS_MACOSX)
61 #endif // OS_POSIX && !OS_MACOSX
62 browser_main_parts_(nullptr),
63 browser_main_delegate_(browser_main_delegate
) {
68 ShellContentBrowserClient::~ShellContentBrowserClient() {
73 ShellContentBrowserClient
* ShellContentBrowserClient::Get() {
77 content::BrowserContext
* ShellContentBrowserClient::GetBrowserContext() {
78 return browser_main_parts_
->browser_context();
81 content::BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
82 const content::MainFunctionParams
& parameters
) {
84 CreateShellBrowserMainParts(parameters
, browser_main_delegate_
);
85 return browser_main_parts_
;
88 void ShellContentBrowserClient::RenderProcessWillLaunch(
89 content::RenderProcessHost
* host
) {
90 int render_process_id
= host
->GetID();
91 BrowserContext
* browser_context
= browser_main_parts_
->browser_context();
93 new ExtensionMessageFilter(render_process_id
, browser_context
));
95 new IOThreadExtensionMessageFilter(render_process_id
, browser_context
));
97 new guest_view::GuestViewMessageFilter(
98 render_process_id
, browser_context
));
100 new ExtensionsGuestViewMessageFilter(
101 render_process_id
, browser_context
));
102 // PluginInfoMessageFilter is not required because app_shell does not have
103 // the concept of disabled plugins.
104 #if !defined(DISABLE_NACL)
105 host
->AddFilter(new nacl::NaClHostMessageFilter(
107 browser_context
->IsOffTheRecord(),
108 browser_context
->GetPath(),
109 browser_context
->GetRequestContextForRenderProcess(render_process_id
)));
113 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
114 content::BrowserContext
* browser_context
,
115 const GURL
& effective_url
) {
116 // This ensures that all render views created for a single app will use the
117 // same render process (see content::SiteInstance::GetProcess). Otherwise the
118 // default behavior of ContentBrowserClient will lead to separate render
119 // processes for the background page and each app window view.
123 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
124 content::BrowserContext
* content_browser_context
,
125 content::ProtocolHandlerMap
* protocol_handlers
,
126 content::URLRequestInterceptorScopedVector request_interceptors
) {
127 // Handle only chrome-extension:// requests. app_shell does not support
128 // chrome-extension-resource:// requests (it does not store shared extension
129 // data in its installation directory).
130 InfoMap
* extension_info_map
=
131 browser_main_parts_
->extension_system()->info_map();
132 (*protocol_handlers
)[kExtensionScheme
] =
133 linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
134 CreateExtensionProtocolHandler(false /* is_incognito */,
135 extension_info_map
));
136 return browser_main_parts_
->browser_context()->CreateRequestContext(
137 protocol_handlers
, request_interceptors
.Pass(), extension_info_map
);
140 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
143 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
144 // content::ShellURLRequestContextGetter::GetURLRequestContext().
145 static const char* const kProtocolList
[] = {
147 content::kChromeDevToolsScheme
,
148 content::kChromeUIScheme
,
151 url::kFileSystemScheme
,
153 kExtensionResourceScheme
,
155 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
156 if (url
.scheme() == kProtocolList
[i
])
162 void ShellContentBrowserClient::SiteInstanceGotProcess(
163 content::SiteInstance
* site_instance
) {
164 // If this isn't an extension renderer there's nothing to do.
165 const Extension
* extension
= GetExtension(site_instance
);
169 ProcessMap::Get(browser_main_parts_
->browser_context())
170 ->Insert(extension
->id(),
171 site_instance
->GetProcess()->GetID(),
172 site_instance
->GetId());
174 BrowserThread::PostTask(
177 base::Bind(&InfoMap::RegisterExtensionProcess
,
178 browser_main_parts_
->extension_system()->info_map(),
180 site_instance
->GetProcess()->GetID(),
181 site_instance
->GetId()));
184 void ShellContentBrowserClient::SiteInstanceDeleting(
185 content::SiteInstance
* site_instance
) {
186 // If this isn't an extension renderer there's nothing to do.
187 const Extension
* extension
= GetExtension(site_instance
);
191 ProcessMap::Get(browser_main_parts_
->browser_context())
192 ->Remove(extension
->id(),
193 site_instance
->GetProcess()->GetID(),
194 site_instance
->GetId());
196 BrowserThread::PostTask(
199 base::Bind(&InfoMap::UnregisterExtensionProcess
,
200 browser_main_parts_
->extension_system()->info_map(),
202 site_instance
->GetProcess()->GetID(),
203 site_instance
->GetId()));
206 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
207 base::CommandLine
* command_line
,
208 int child_process_id
) {
209 std::string process_type
=
210 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
212 #if defined(OS_POSIX) && !defined(OS_MACOSX)
213 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
214 if (process_type
!= ::switches::kZygoteProcess
) {
215 command_line
->AppendSwitch(::switches::kV8NativesPassedByFD
);
216 command_line
->AppendSwitch(::switches::kV8SnapshotPassedByFD
);
218 #endif // V8_USE_EXTERNAL_STARTUP_DATA
219 #endif // OS_POSIX && !OS_MACOSX
221 if (process_type
== ::switches::kRendererProcess
)
222 AppendRendererSwitches(command_line
);
225 content::SpeechRecognitionManagerDelegate
*
226 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
227 return new speech::ShellSpeechRecognitionManagerDelegate();
230 content::BrowserPpapiHost
*
231 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id
) {
232 #if !defined(DISABLE_NACL)
233 content::BrowserChildProcessHostIterator
iter(PROCESS_TYPE_NACL_LOADER
);
234 while (!iter
.Done()) {
235 nacl::NaClProcessHost
* host
= static_cast<nacl::NaClProcessHost
*>(
237 if (host
->process() &&
238 host
->process()->GetData().id
== plugin_process_id
) {
240 return host
->browser_ppapi_host();
248 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
249 std::vector
<std::string
>* additional_allowed_schemes
) {
250 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
251 additional_allowed_schemes
);
252 additional_allowed_schemes
->push_back(kExtensionScheme
);
255 #if defined(OS_POSIX) && !defined(OS_MACOSX)
256 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
257 const base::CommandLine
& command_line
,
258 int child_process_id
,
259 content::FileDescriptorInfo
* mappings
) {
260 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
261 if (v8_natives_fd_
.get() == -1 || v8_snapshot_fd_
.get() == -1) {
262 int v8_natives_fd
= -1;
263 int v8_snapshot_fd
= -1;
264 if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd
,
266 v8_natives_fd_
.reset(v8_natives_fd
);
267 v8_snapshot_fd_
.reset(v8_snapshot_fd
);
270 DCHECK(v8_natives_fd_
.get() != -1 && v8_snapshot_fd_
.get() != -1);
271 mappings
->Share(kV8NativesDataDescriptor
, v8_natives_fd_
.get());
272 mappings
->Share(kV8SnapshotDataDescriptor
, v8_snapshot_fd_
.get());
273 #endif // V8_USE_EXTERNAL_STARTUP_DATA
275 #endif // OS_POSIX && !OS_MACOSX
277 content::DevToolsManagerDelegate
*
278 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
279 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
282 ShellBrowserMainParts
* ShellContentBrowserClient::CreateShellBrowserMainParts(
283 const content::MainFunctionParams
& parameters
,
284 ShellBrowserMainDelegate
* browser_main_delegate
) {
285 return new ShellBrowserMainParts(parameters
, browser_main_delegate
);
288 void ShellContentBrowserClient::AppendRendererSwitches(
289 base::CommandLine
* command_line
) {
290 // TODO(jamescook): Should we check here if the process is in the extension
291 // service process map, or can we assume all renderers are extension
293 command_line
->AppendSwitch(switches::kExtensionProcess
);
295 #if !defined(DISABLE_NACL)
296 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
297 // SFI switches either here or for the zygote process.
298 static const char* const kSwitchNames
[] = {
299 ::switches::kEnableNaClDebug
,
301 command_line
->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
302 kSwitchNames
, arraysize(kSwitchNames
));
303 #endif // !defined(DISABLE_NACL)
306 const Extension
* ShellContentBrowserClient::GetExtension(
307 content::SiteInstance
* site_instance
) {
308 ExtensionRegistry
* registry
=
309 ExtensionRegistry::Get(site_instance
->GetBrowserContext());
310 return registry
->enabled_extensions().GetExtensionOrAppByURL(
311 site_instance
->GetSiteURL());
314 } // namespace extensions