1 // Copyright 2013 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 "content/shell/browser/shell_content_browser_client.h"
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/files/file.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/client_certificate_delegate.h"
14 #include "content/public/browser/page_navigator.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/resource_dispatcher_host.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/url_constants.h"
20 #include "content/public/common/web_preferences.h"
21 #include "content/shell/browser/blink_test_controller.h"
22 #include "content/shell/browser/ipc_echo_message_filter.h"
23 #include "content/shell/browser/layout_test/layout_test_browser_main_parts.h"
24 #include "content/shell/browser/layout_test/layout_test_resource_dispatcher_host_delegate.h"
25 #include "content/shell/browser/shell.h"
26 #include "content/shell/browser/shell_access_token_store.h"
27 #include "content/shell/browser/shell_browser_context.h"
28 #include "content/shell/browser/shell_browser_main_parts.h"
29 #include "content/shell/browser/shell_devtools_manager_delegate.h"
30 #include "content/shell/browser/shell_net_log.h"
31 #include "content/shell/browser/shell_quota_permission_context.h"
32 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
33 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
34 #include "content/shell/common/shell_messages.h"
35 #include "content/shell/common/shell_switches.h"
36 #include "content/shell/renderer/layout_test/blink_test_helpers.h"
37 #include "net/url_request/url_request_context_getter.h"
40 #if defined(OS_ANDROID)
41 #include "base/android/apk_assets.h"
42 #include "base/android/path_utils.h"
43 #include "components/crash/browser/crash_dump_manager_android.h"
44 #include "content/shell/android/shell_descriptors.h"
47 #if defined(OS_POSIX) && !defined(OS_MACOSX)
48 #include "base/debug/leak_annotations.h"
49 #include "components/crash/app/breakpad_linux.h"
50 #include "components/crash/browser/crash_handler_host_linux.h"
51 #include "content/public/common/content_descriptors.h"
55 #include "content/common/sandbox_win.h"
56 #include "sandbox/win/src/sandbox.h"
63 ShellContentBrowserClient
* g_browser_client
;
64 bool g_swap_processes_for_redirect
= false;
66 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
67 breakpad::CrashHandlerHostLinux
* CreateCrashHandlerHost(
68 const std::string
& process_type
) {
69 base::FilePath dumps_path
=
70 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
71 switches::kCrashDumpsDir
);
73 ANNOTATE_SCOPED_MEMORY_LEAK
;
74 breakpad::CrashHandlerHostLinux
* crash_handler
=
75 new breakpad::CrashHandlerHostLinux(
76 process_type
, dumps_path
, false);
77 crash_handler
->StartUploaderThread();
82 int GetCrashSignalFD(const base::CommandLine
& command_line
) {
83 if (!breakpad::IsCrashReporterEnabled())
86 std::string process_type
=
87 command_line
.GetSwitchValueASCII(switches::kProcessType
);
89 if (process_type
== switches::kRendererProcess
) {
90 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
92 crash_handler
= CreateCrashHandlerHost(process_type
);
93 return crash_handler
->GetDeathSignalSocket();
96 if (process_type
== switches::kPluginProcess
) {
97 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
99 crash_handler
= CreateCrashHandlerHost(process_type
);
100 return crash_handler
->GetDeathSignalSocket();
103 if (process_type
== switches::kPpapiPluginProcess
) {
104 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
106 crash_handler
= CreateCrashHandlerHost(process_type
);
107 return crash_handler
->GetDeathSignalSocket();
110 if (process_type
== switches::kGpuProcess
) {
111 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
113 crash_handler
= CreateCrashHandlerHost(process_type
);
114 return crash_handler
->GetDeathSignalSocket();
119 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
123 ShellContentBrowserClient
* ShellContentBrowserClient::Get() {
124 return g_browser_client
;
127 void ShellContentBrowserClient::SetSwapProcessesForRedirect(bool swap
) {
128 g_swap_processes_for_redirect
= swap
;
131 ShellContentBrowserClient::ShellContentBrowserClient()
132 : shell_browser_main_parts_(NULL
) {
133 DCHECK(!g_browser_client
);
134 g_browser_client
= this;
137 ShellContentBrowserClient::~ShellContentBrowserClient() {
138 g_browser_client
= NULL
;
141 BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
142 const MainFunctionParams
& parameters
) {
143 shell_browser_main_parts_
= base::CommandLine::ForCurrentProcess()->HasSwitch(
144 switches::kRunLayoutTest
)
145 ? new LayoutTestBrowserMainParts(parameters
)
146 : new ShellBrowserMainParts(parameters
);
147 return shell_browser_main_parts_
;
150 void ShellContentBrowserClient::RenderProcessWillLaunch(
151 RenderProcessHost
* host
) {
152 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kExposeIpcEcho
))
154 host
->AddFilter(new IPCEchoMessageFilter());
157 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
158 BrowserContext
* content_browser_context
,
159 ProtocolHandlerMap
* protocol_handlers
,
160 URLRequestInterceptorScopedVector request_interceptors
) {
161 ShellBrowserContext
* shell_browser_context
=
162 ShellBrowserContextForBrowserContext(content_browser_context
);
163 return shell_browser_context
->CreateRequestContext(
164 protocol_handlers
, request_interceptors
.Pass());
167 net::URLRequestContextGetter
*
168 ShellContentBrowserClient::CreateRequestContextForStoragePartition(
169 BrowserContext
* content_browser_context
,
170 const base::FilePath
& partition_path
,
172 ProtocolHandlerMap
* protocol_handlers
,
173 URLRequestInterceptorScopedVector request_interceptors
) {
174 ShellBrowserContext
* shell_browser_context
=
175 ShellBrowserContextForBrowserContext(content_browser_context
);
176 return shell_browser_context
->CreateRequestContextForStoragePartition(
180 request_interceptors
.Pass());
183 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
186 DCHECK_EQ(url
.scheme(), base::StringToLowerASCII(url
.scheme()));
187 // Keep in sync with ProtocolHandlers added by
188 // ShellURLRequestContextGetter::GetURLRequestContext().
189 static const char* const kProtocolList
[] = {
191 url::kFileSystemScheme
,
193 kChromeDevToolsScheme
,
197 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
198 if (url
.scheme() == kProtocolList
[i
])
204 bool ShellContentBrowserClient::IsNPAPIEnabled() {
205 #if defined(OS_WIN) || defined(OS_MACOSX)
212 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
213 base::CommandLine
* command_line
,
214 int child_process_id
) {
215 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
216 switches::kRunLayoutTest
)) {
217 command_line
->AppendSwitch(switches::kRunLayoutTest
);
219 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
220 switches::kDumpLineBoxTrees
)) {
221 command_line
->AppendSwitch(switches::kDumpLineBoxTrees
);
223 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
224 switches::kEnableFontAntialiasing
)) {
225 command_line
->AppendSwitch(switches::kEnableFontAntialiasing
);
227 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
228 switches::kExposeInternalsForTesting
)) {
229 command_line
->AppendSwitch(switches::kExposeInternalsForTesting
);
231 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
232 switches::kExposeIpcEcho
)) {
233 command_line
->AppendSwitch(switches::kExposeIpcEcho
);
235 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
236 switches::kStableReleaseMode
)) {
237 command_line
->AppendSwitch(switches::kStableReleaseMode
);
239 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
240 switches::kEnableCrashReporter
)) {
241 command_line
->AppendSwitch(switches::kEnableCrashReporter
);
243 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
244 switches::kCrashDumpsDir
)) {
245 command_line
->AppendSwitchPath(
246 switches::kCrashDumpsDir
,
247 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
248 switches::kCrashDumpsDir
));
250 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
251 switches::kEnableLeakDetection
)) {
252 command_line
->AppendSwitchASCII(
253 switches::kEnableLeakDetection
,
254 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
255 switches::kEnableLeakDetection
));
257 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
258 switches::kRegisterFontFiles
)) {
259 command_line
->AppendSwitchASCII(
260 switches::kRegisterFontFiles
,
261 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
262 switches::kRegisterFontFiles
));
266 void ShellContentBrowserClient::OverrideWebkitPrefs(
267 RenderViewHost
* render_view_host
,
268 WebPreferences
* prefs
) {
269 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
270 switches::kRunLayoutTest
))
272 BlinkTestController::Get()->OverrideWebkitPrefs(prefs
);
275 void ShellContentBrowserClient::ResourceDispatcherHostCreated() {
276 resource_dispatcher_host_delegate_
.reset(
277 base::CommandLine::ForCurrentProcess()->HasSwitch(
278 switches::kRunLayoutTest
)
279 ? new LayoutTestResourceDispatcherHostDelegate
280 : new ShellResourceDispatcherHostDelegate
);
281 ResourceDispatcherHost::Get()->SetDelegate(
282 resource_dispatcher_host_delegate_
.get());
285 std::string
ShellContentBrowserClient::GetDefaultDownloadName() {
289 WebContentsViewDelegate
* ShellContentBrowserClient::GetWebContentsViewDelegate(
290 WebContents
* web_contents
) {
291 #if !defined(USE_AURA)
292 return CreateShellWebContentsViewDelegate(web_contents
);
298 QuotaPermissionContext
*
299 ShellContentBrowserClient::CreateQuotaPermissionContext() {
300 return new ShellQuotaPermissionContext();
303 void ShellContentBrowserClient::SelectClientCertificate(
304 WebContents
* web_contents
,
305 net::SSLCertRequestInfo
* cert_request_info
,
306 scoped_ptr
<ClientCertificateDelegate
> delegate
) {
307 if (!select_client_certificate_callback_
.is_null())
308 select_client_certificate_callback_
.Run();
311 SpeechRecognitionManagerDelegate
*
312 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
313 return new ShellSpeechRecognitionManagerDelegate();
316 net::NetLog
* ShellContentBrowserClient::GetNetLog() {
317 return shell_browser_main_parts_
->net_log();
320 bool ShellContentBrowserClient::ShouldSwapProcessesForRedirect(
321 ResourceContext
* resource_context
,
322 const GURL
& current_url
,
323 const GURL
& new_url
) {
324 return g_swap_processes_for_redirect
;
327 DevToolsManagerDelegate
*
328 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
329 return new ShellDevToolsManagerDelegate(browser_context());
332 void ShellContentBrowserClient::OpenURL(
333 BrowserContext
* browser_context
,
334 const OpenURLParams
& params
,
335 const base::Callback
<void(WebContents
*)>& callback
) {
336 callback
.Run(Shell::CreateNewWindow(browser_context
,
339 gfx::Size())->web_contents());
342 #if defined(OS_ANDROID)
343 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
344 const base::CommandLine
& command_line
,
345 int child_process_id
,
346 content::FileDescriptorInfo
* mappings
,
347 std::map
<int, base::MemoryMappedFile::Region
>* regions
) {
350 base::GlobalDescriptors::GetInstance()->Get(kShellPakDescriptor
));
351 regions
->insert(std::make_pair(
353 base::GlobalDescriptors::GetInstance()->GetRegion(kShellPakDescriptor
)));
355 if (breakpad::IsCrashReporterEnabled()) {
356 base::File
f(breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
359 LOG(ERROR
) << "Failed to create file for minidump, crash reporting will "
360 << "be disabled for this process.";
362 mappings
->Transfer(kAndroidMinidumpDescriptor
,
363 base::ScopedFD(f
.TakePlatformFile()));
367 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
368 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
369 const base::CommandLine
& command_line
,
370 int child_process_id
,
371 content::FileDescriptorInfo
* mappings
) {
372 int crash_signal_fd
= GetCrashSignalFD(command_line
);
373 if (crash_signal_fd
>= 0) {
374 mappings
->Share(kCrashDumpSignal
, crash_signal_fd
);
377 #endif // defined(OS_ANDROID)
380 void ShellContentBrowserClient::PreSpawnRenderer(sandbox::TargetPolicy
* policy
,
382 // Add sideloaded font files for testing. See also DIR_WINDOWS_FONTS
383 // addition in |StartSandboxedProcess|.
384 std::vector
<std::string
> font_files
= GetSideloadFontFiles();
385 for (std::vector
<std::string
>::const_iterator
i(font_files
.begin());
386 i
!= font_files
.end();
388 policy
->AddRule(sandbox::TargetPolicy::SUBSYS_FILES
,
389 sandbox::TargetPolicy::FILES_ALLOW_READONLY
,
390 base::UTF8ToWide(*i
).c_str());
395 ShellBrowserContext
* ShellContentBrowserClient::browser_context() {
396 return shell_browser_main_parts_
->browser_context();
400 ShellContentBrowserClient::off_the_record_browser_context() {
401 return shell_browser_main_parts_
->off_the_record_browser_context();
404 AccessTokenStore
* ShellContentBrowserClient::CreateAccessTokenStore() {
405 return new ShellAccessTokenStore(browser_context());
409 ShellContentBrowserClient::ShellBrowserContextForBrowserContext(
410 BrowserContext
* content_browser_context
) {
411 if (content_browser_context
== browser_context())
412 return browser_context();
413 DCHECK_EQ(content_browser_context
, off_the_record_browser_context());
414 return off_the_record_browser_context();
417 } // namespace content