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/file_util.h"
10 #include "base/path_service.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/resource_dispatcher_host.h"
13 #include "content/public/browser/storage_partition.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/url_constants.h"
16 #include "content/shell/browser/shell.h"
17 #include "content/shell/browser/shell_browser_context.h"
18 #include "content/shell/browser/shell_browser_main_parts.h"
19 #include "content/shell/browser/shell_devtools_delegate.h"
20 #include "content/shell/browser/shell_message_filter.h"
21 #include "content/shell/browser/shell_net_log.h"
22 #include "content/shell/browser/shell_quota_permission_context.h"
23 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
24 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
25 #include "content/shell/browser/webkit_test_controller.h"
26 #include "content/shell/common/shell_messages.h"
27 #include "content/shell/common/shell_switches.h"
28 #include "content/shell/common/webkit_test_helpers.h"
29 #include "content/shell/geolocation/shell_access_token_store.h"
30 #include "net/url_request/url_request_context_getter.h"
32 #include "webkit/common/webpreferences.h"
34 #if defined(OS_ANDROID)
35 #include "base/android/path_utils.h"
36 #include "base/path_service.h"
37 #include "components/breakpad/browser/crash_dump_manager_android.h"
38 #include "content/shell/android/shell_descriptors.h"
41 #if defined(OS_POSIX) && !defined(OS_MACOSX)
42 #include "base/debug/leak_annotations.h"
43 #include "base/platform_file.h"
44 #include "components/breakpad/app/breakpad_linux.h"
45 #include "components/breakpad/browser/crash_handler_host_linux.h"
46 #include "content/public/common/content_descriptors.h"
53 ShellContentBrowserClient
* g_browser_client
;
54 bool g_swap_processes_for_redirect
= false;
56 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
57 breakpad::CrashHandlerHostLinux
* CreateCrashHandlerHost(
58 const std::string
& process_type
) {
59 base::FilePath dumps_path
=
60 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
61 switches::kCrashDumpsDir
);
63 ANNOTATE_SCOPED_MEMORY_LEAK
;
64 breakpad::CrashHandlerHostLinux
* crash_handler
=
65 new breakpad::CrashHandlerHostLinux(
66 process_type
, dumps_path
, false);
67 crash_handler
->StartUploaderThread();
72 int GetCrashSignalFD(const CommandLine
& command_line
) {
73 if (!breakpad::IsCrashReporterEnabled())
76 std::string process_type
=
77 command_line
.GetSwitchValueASCII(switches::kProcessType
);
79 if (process_type
== switches::kRendererProcess
) {
80 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
82 crash_handler
= CreateCrashHandlerHost(process_type
);
83 return crash_handler
->GetDeathSignalSocket();
86 if (process_type
== switches::kPluginProcess
) {
87 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
89 crash_handler
= CreateCrashHandlerHost(process_type
);
90 return crash_handler
->GetDeathSignalSocket();
93 if (process_type
== switches::kPpapiPluginProcess
) {
94 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
96 crash_handler
= CreateCrashHandlerHost(process_type
);
97 return crash_handler
->GetDeathSignalSocket();
100 if (process_type
== switches::kGpuProcess
) {
101 static breakpad::CrashHandlerHostLinux
* crash_handler
= NULL
;
103 crash_handler
= CreateCrashHandlerHost(process_type
);
104 return crash_handler
->GetDeathSignalSocket();
109 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
113 ShellContentBrowserClient
* ShellContentBrowserClient::Get() {
114 return g_browser_client
;
117 void ShellContentBrowserClient::SetSwapProcessesForRedirect(bool swap
) {
118 g_swap_processes_for_redirect
= swap
;
121 ShellContentBrowserClient::ShellContentBrowserClient()
122 : shell_browser_main_parts_(NULL
) {
123 DCHECK(!g_browser_client
);
124 g_browser_client
= this;
125 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
127 webkit_source_dir_
= GetWebKitRootDirFilePath();
130 ShellContentBrowserClient::~ShellContentBrowserClient() {
131 g_browser_client
= NULL
;
134 BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
135 const MainFunctionParams
& parameters
) {
136 shell_browser_main_parts_
= new ShellBrowserMainParts(parameters
);
137 return shell_browser_main_parts_
;
140 void ShellContentBrowserClient::RenderProcessWillLaunch(
141 RenderProcessHost
* host
) {
142 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
144 host
->AddFilter(new ShellMessageFilter(
146 BrowserContext::GetDefaultStoragePartition(browser_context())
147 ->GetDatabaseTracker(),
148 BrowserContext::GetDefaultStoragePartition(browser_context())
150 BrowserContext::GetDefaultStoragePartition(browser_context())
151 ->GetURLRequestContext()));
152 host
->Send(new ShellViewMsg_SetWebKitSourceDir(webkit_source_dir_
));
155 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
156 BrowserContext
* content_browser_context
,
157 ProtocolHandlerMap
* protocol_handlers
) {
158 ShellBrowserContext
* shell_browser_context
=
159 ShellBrowserContextForBrowserContext(content_browser_context
);
160 return shell_browser_context
->CreateRequestContext(protocol_handlers
);
163 net::URLRequestContextGetter
*
164 ShellContentBrowserClient::CreateRequestContextForStoragePartition(
165 BrowserContext
* content_browser_context
,
166 const base::FilePath
& partition_path
,
168 ProtocolHandlerMap
* protocol_handlers
) {
169 ShellBrowserContext
* shell_browser_context
=
170 ShellBrowserContextForBrowserContext(content_browser_context
);
171 return shell_browser_context
->CreateRequestContextForStoragePartition(
172 partition_path
, in_memory
, protocol_handlers
);
175 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
178 DCHECK_EQ(url
.scheme(), StringToLowerASCII(url
.scheme()));
179 // Keep in sync with ProtocolHandlers added by
180 // ShellURLRequestContextGetter::GetURLRequestContext().
181 static const char* const kProtocolList
[] = {
184 chrome::kChromeUIScheme
,
185 chrome::kChromeDevToolsScheme
,
189 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
190 if (url
.scheme() == kProtocolList
[i
])
196 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
197 CommandLine
* command_line
, int child_process_id
) {
198 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
199 command_line
->AppendSwitch(switches::kDumpRenderTree
);
200 if (CommandLine::ForCurrentProcess()->HasSwitch(
201 switches::kExposeInternalsForTesting
))
202 command_line
->AppendSwitch(switches::kExposeInternalsForTesting
);
203 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStableReleaseMode
))
204 command_line
->AppendSwitch(switches::kStableReleaseMode
);
205 if (CommandLine::ForCurrentProcess()->HasSwitch(
206 switches::kEnableCrashReporter
)) {
207 command_line
->AppendSwitch(switches::kEnableCrashReporter
);
209 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kCrashDumpsDir
)) {
210 command_line
->AppendSwitchPath(
211 switches::kCrashDumpsDir
,
212 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
213 switches::kCrashDumpsDir
));
217 void ShellContentBrowserClient::OverrideWebkitPrefs(
218 RenderViewHost
* render_view_host
,
220 WebPreferences
* prefs
) {
221 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
223 WebKitTestController::Get()->OverrideWebkitPrefs(prefs
);
226 void ShellContentBrowserClient::ResourceDispatcherHostCreated() {
227 resource_dispatcher_host_delegate_
.reset(
228 new ShellResourceDispatcherHostDelegate());
229 ResourceDispatcherHost::Get()->SetDelegate(
230 resource_dispatcher_host_delegate_
.get());
233 std::string
ShellContentBrowserClient::GetDefaultDownloadName() {
237 bool ShellContentBrowserClient::SupportsBrowserPlugin(
238 content::BrowserContext
* browser_context
, const GURL
& url
) {
239 return CommandLine::ForCurrentProcess()->HasSwitch(
240 switches::kEnableBrowserPluginForAllViewTypes
);
243 WebContentsViewDelegate
* ShellContentBrowserClient::GetWebContentsViewDelegate(
244 WebContents
* web_contents
) {
245 #if !defined(USE_AURA)
246 return CreateShellWebContentsViewDelegate(web_contents
);
252 QuotaPermissionContext
*
253 ShellContentBrowserClient::CreateQuotaPermissionContext() {
254 return new ShellQuotaPermissionContext();
257 SpeechRecognitionManagerDelegate
*
258 ShellContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
259 return new ShellSpeechRecognitionManagerDelegate();
262 net::NetLog
* ShellContentBrowserClient::GetNetLog() {
263 return shell_browser_main_parts_
->net_log();
266 bool ShellContentBrowserClient::ShouldSwapProcessesForRedirect(
267 ResourceContext
* resource_context
,
268 const GURL
& current_url
,
269 const GURL
& new_url
) {
270 return g_swap_processes_for_redirect
;
273 #if defined(OS_POSIX) && !defined(OS_MACOSX)
274 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
275 const CommandLine
& command_line
,
276 int child_process_id
,
277 std::vector
<content::FileDescriptorInfo
>* mappings
) {
278 #if defined(OS_ANDROID)
279 int flags
= base::PLATFORM_FILE_OPEN
| base::PLATFORM_FILE_READ
;
280 base::FilePath pak_file
;
281 bool r
= PathService::Get(base::DIR_ANDROID_APP_DATA
, &pak_file
);
283 pak_file
= pak_file
.Append(FILE_PATH_LITERAL("paks"));
284 pak_file
= pak_file
.Append(FILE_PATH_LITERAL("content_shell.pak"));
286 base::PlatformFile f
=
287 base::CreatePlatformFile(pak_file
, flags
, NULL
, NULL
);
288 if (f
== base::kInvalidPlatformFileValue
) {
289 NOTREACHED() << "Failed to open file when creating renderer process: "
290 << "content_shell.pak";
293 content::FileDescriptorInfo(kShellPakDescriptor
,
294 base::FileDescriptor(f
, true)));
296 if (breakpad::IsCrashReporterEnabled()) {
297 f
= breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
299 if (f
== base::kInvalidPlatformFileValue
) {
300 LOG(ERROR
) << "Failed to create file for minidump, crash reporting will "
301 << "be disabled for this process.";
303 mappings
->push_back(FileDescriptorInfo(kAndroidMinidumpDescriptor
,
304 base::FileDescriptor(f
, true)));
307 #else // !defined(OS_ANDROID)
308 int crash_signal_fd
= GetCrashSignalFD(command_line
);
309 if (crash_signal_fd
>= 0) {
310 mappings
->push_back(FileDescriptorInfo(
311 kCrashDumpSignal
, base::FileDescriptor(crash_signal_fd
, false)));
313 #endif // defined(OS_ANDROID)
315 #endif // defined(OS_POSIX) && !defined(OS_MACOSX)
317 ShellBrowserContext
* ShellContentBrowserClient::browser_context() {
318 return shell_browser_main_parts_
->browser_context();
322 ShellContentBrowserClient::off_the_record_browser_context() {
323 return shell_browser_main_parts_
->off_the_record_browser_context();
326 AccessTokenStore
* ShellContentBrowserClient::CreateAccessTokenStore() {
327 return new ShellAccessTokenStore(browser_context());
331 ShellContentBrowserClient::ShellBrowserContextForBrowserContext(
332 BrowserContext
* content_browser_context
) {
333 if (content_browser_context
== browser_context())
334 return browser_context();
335 DCHECK_EQ(content_browser_context
, off_the_record_browser_context());
336 return off_the_record_browser_context();
339 } // namespace content