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 ExtensionsGuestViewMessageFilter(
98 render_process_id
, browser_context
));
99 // PluginInfoMessageFilter is not required because app_shell does not have
100 // the concept of disabled plugins.
101 #if !defined(DISABLE_NACL)
102 host
->AddFilter(new nacl::NaClHostMessageFilter(
104 browser_context
->IsOffTheRecord(),
105 browser_context
->GetPath(),
106 browser_context
->GetRequestContextForRenderProcess(render_process_id
)));
110 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
111 content::BrowserContext
* browser_context
,
112 const GURL
& effective_url
) {
113 // This ensures that all render views created for a single app will use the
114 // same render process (see content::SiteInstance::GetProcess). Otherwise the
115 // default behavior of ContentBrowserClient will lead to separate render
116 // processes for the background page and each app window view.
120 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
121 content::BrowserContext
* content_browser_context
,
122 content::ProtocolHandlerMap
* protocol_handlers
,
123 content::URLRequestInterceptorScopedVector request_interceptors
) {
124 // Handle only chrome-extension:// requests. app_shell does not support
125 // chrome-extension-resource:// requests (it does not store shared extension
126 // data in its installation directory).
127 InfoMap
* extension_info_map
=
128 browser_main_parts_
->extension_system()->info_map();
129 (*protocol_handlers
)[kExtensionScheme
] =
130 linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
131 CreateExtensionProtocolHandler(false /* is_incognito */,
132 extension_info_map
));
133 return browser_main_parts_
->browser_context()->CreateRequestContext(
134 protocol_handlers
, request_interceptors
.Pass(), extension_info_map
);
137 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
140 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
141 // content::ShellURLRequestContextGetter::GetURLRequestContext().
142 static const char* const kProtocolList
[] = {
144 content::kChromeDevToolsScheme
,
145 content::kChromeUIScheme
,
148 url::kFileSystemScheme
,
150 kExtensionResourceScheme
,
152 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
153 if (url
.scheme() == kProtocolList
[i
])
159 void ShellContentBrowserClient::SiteInstanceGotProcess(
160 content::SiteInstance
* site_instance
) {
161 // If this isn't an extension renderer there's nothing to do.
162 const Extension
* extension
= GetExtension(site_instance
);
166 ProcessMap::Get(browser_main_parts_
->browser_context())
167 ->Insert(extension
->id(),
168 site_instance
->GetProcess()->GetID(),
169 site_instance
->GetId());
171 BrowserThread::PostTask(
174 base::Bind(&InfoMap::RegisterExtensionProcess
,
175 browser_main_parts_
->extension_system()->info_map(),
177 site_instance
->GetProcess()->GetID(),
178 site_instance
->GetId()));
181 void ShellContentBrowserClient::SiteInstanceDeleting(
182 content::SiteInstance
* site_instance
) {
183 // If this isn't an extension renderer there's nothing to do.
184 const Extension
* extension
= GetExtension(site_instance
);
188 ProcessMap::Get(browser_main_parts_
->browser_context())
189 ->Remove(extension
->id(),
190 site_instance
->GetProcess()->GetID(),
191 site_instance
->GetId());
193 BrowserThread::PostTask(
196 base::Bind(&InfoMap::UnregisterExtensionProcess
,
197 browser_main_parts_
->extension_system()->info_map(),
199 site_instance
->GetProcess()->GetID(),
200 site_instance
->GetId()));
203 void ShellContentBrowserClient::AppendMappedFileCommandLineSwitches(
204 base::CommandLine
* command_line
) {
205 std::string process_type
=
206 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
208 #if defined(OS_POSIX) && !defined(OS_MACOSX)
209 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
210 DCHECK(natives_fd_exists());
211 command_line
->AppendSwitch(::switches::kV8NativesPassedByFD
);
212 if (snapshot_fd_exists())
213 command_line
->AppendSwitch(::switches::kV8SnapshotPassedByFD
);
214 #endif // V8_USE_EXTERNAL_STARTUP_DATA
215 #endif // OS_POSIX && !OS_MACOSX
218 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
219 base::CommandLine
* command_line
,
220 int child_process_id
) {
221 std::string process_type
=
222 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
223 if (process_type
== ::switches::kRendererProcess
)
224 AppendRendererSwitches(command_line
);
227 content::SpeechRecognitionManagerDelegate
*
228 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
229 return new speech::ShellSpeechRecognitionManagerDelegate();
232 content::BrowserPpapiHost
*
233 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id
) {
234 #if !defined(DISABLE_NACL)
235 content::BrowserChildProcessHostIterator
iter(PROCESS_TYPE_NACL_LOADER
);
236 while (!iter
.Done()) {
237 nacl::NaClProcessHost
* host
= static_cast<nacl::NaClProcessHost
*>(
239 if (host
->process() &&
240 host
->process()->GetData().id
== plugin_process_id
) {
242 return host
->browser_ppapi_host();
250 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
251 std::vector
<std::string
>* additional_allowed_schemes
) {
252 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
253 additional_allowed_schemes
);
254 additional_allowed_schemes
->push_back(kExtensionScheme
);
257 #if defined(OS_POSIX) && !defined(OS_MACOSX)
258 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
259 const base::CommandLine
& command_line
,
260 int child_process_id
,
261 content::FileDescriptorInfo
* mappings
) {
262 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
263 if (!natives_fd_exists()) {
264 int v8_natives_fd
= -1;
265 int v8_snapshot_fd
= -1;
266 if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd
,
268 v8_natives_fd_
.reset(v8_natives_fd
);
269 v8_snapshot_fd_
.reset(v8_snapshot_fd
);
272 // V8 can't start up without the source of the natives, but it can
273 // start up (slower) without the snapshot.
274 DCHECK(natives_fd_exists());
275 mappings
->Share(kV8NativesDataDescriptor
, v8_natives_fd_
.get());
276 mappings
->Share(kV8SnapshotDataDescriptor
, v8_snapshot_fd_
.get());
277 #endif // V8_USE_EXTERNAL_STARTUP_DATA
279 #endif // OS_POSIX && !OS_MACOSX
281 content::DevToolsManagerDelegate
*
282 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
283 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
286 ShellBrowserMainParts
* ShellContentBrowserClient::CreateShellBrowserMainParts(
287 const content::MainFunctionParams
& parameters
,
288 ShellBrowserMainDelegate
* browser_main_delegate
) {
289 return new ShellBrowserMainParts(parameters
, browser_main_delegate
);
292 void ShellContentBrowserClient::AppendRendererSwitches(
293 base::CommandLine
* command_line
) {
294 // TODO(jamescook): Should we check here if the process is in the extension
295 // service process map, or can we assume all renderers are extension
297 command_line
->AppendSwitch(switches::kExtensionProcess
);
299 #if !defined(DISABLE_NACL)
300 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
301 // SFI switches either here or for the zygote process.
302 static const char* const kSwitchNames
[] = {
303 ::switches::kEnableNaClDebug
,
305 command_line
->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
306 kSwitchNames
, arraysize(kSwitchNames
));
307 #endif // !defined(DISABLE_NACL)
310 const Extension
* ShellContentBrowserClient::GetExtension(
311 content::SiteInstance
* site_instance
) {
312 ExtensionRegistry
* registry
=
313 ExtensionRegistry::Get(site_instance
->GetBrowserContext());
314 return registry
->enabled_extensions().GetExtensionOrAppByURL(
315 site_instance
->GetSiteURL());
318 } // namespace extensions