Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / shell / browser / shell_content_browser_client.cc
blob2f928a92d6b18fde6d714d7181ead7a587f13524
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/public/test/test_mojo_app.h"
22 #include "content/shell/browser/blink_test_controller.h"
23 #include "content/shell/browser/ipc_echo_message_filter.h"
24 #include "content/shell/browser/layout_test/layout_test_browser_main_parts.h"
25 #include "content/shell/browser/layout_test/layout_test_resource_dispatcher_host_delegate.h"
26 #include "content/shell/browser/shell.h"
27 #include "content/shell/browser/shell_access_token_store.h"
28 #include "content/shell/browser/shell_browser_context.h"
29 #include "content/shell/browser/shell_browser_main_parts.h"
30 #include "content/shell/browser/shell_devtools_manager_delegate.h"
31 #include "content/shell/browser/shell_net_log.h"
32 #include "content/shell/browser/shell_quota_permission_context.h"
33 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
34 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
35 #include "content/shell/common/shell_messages.h"
36 #include "content/shell/common/shell_switches.h"
37 #include "net/url_request/url_request_context_getter.h"
38 #include "url/gurl.h"
40 #if defined(OS_ANDROID)
41 #include "base/android/apk_assets.h"
42 #include "base/android/path_utils.h"
43 #include "components/crash/content/browser/crash_dump_manager_android.h"
44 #include "content/shell/android/shell_descriptors.h"
45 #endif
47 #if defined(OS_POSIX) && !defined(OS_MACOSX)
48 #include "base/debug/leak_annotations.h"
49 #include "components/crash/content/app/breakpad_linux.h"
50 #include "components/crash/content/browser/crash_handler_host_linux.h"
51 #include "content/public/common/content_descriptors.h"
52 #endif
54 #if defined(OS_WIN)
55 #include "content/common/sandbox_win.h"
56 #include "sandbox/win/src/sandbox.h"
57 #endif
59 namespace content {
61 namespace {
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();
78 return crash_handler;
82 int GetCrashSignalFD(const base::CommandLine& command_line) {
83 if (!breakpad::IsCrashReporterEnabled())
84 return -1;
86 std::string process_type =
87 command_line.GetSwitchValueASCII(switches::kProcessType);
89 if (process_type == switches::kRendererProcess) {
90 static breakpad::CrashHandlerHostLinux* crash_handler = NULL;
91 if (!crash_handler)
92 crash_handler = CreateCrashHandlerHost(process_type);
93 return crash_handler->GetDeathSignalSocket();
96 if (process_type == switches::kPluginProcess) {
97 static breakpad::CrashHandlerHostLinux* crash_handler = NULL;
98 if (!crash_handler)
99 crash_handler = CreateCrashHandlerHost(process_type);
100 return crash_handler->GetDeathSignalSocket();
103 if (process_type == switches::kPpapiPluginProcess) {
104 static breakpad::CrashHandlerHostLinux* crash_handler = NULL;
105 if (!crash_handler)
106 crash_handler = CreateCrashHandlerHost(process_type);
107 return crash_handler->GetDeathSignalSocket();
110 if (process_type == switches::kGpuProcess) {
111 static breakpad::CrashHandlerHostLinux* crash_handler = NULL;
112 if (!crash_handler)
113 crash_handler = CreateCrashHandlerHost(process_type);
114 return crash_handler->GetDeathSignalSocket();
117 return -1;
119 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
121 } // namespace
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,
171 bool in_memory,
172 ProtocolHandlerMap* protocol_handlers,
173 URLRequestInterceptorScopedVector request_interceptors) {
174 ShellBrowserContext* shell_browser_context =
175 ShellBrowserContextForBrowserContext(content_browser_context);
176 return shell_browser_context->CreateRequestContextForStoragePartition(
177 partition_path,
178 in_memory,
179 protocol_handlers,
180 request_interceptors.Pass());
183 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
184 if (!url.is_valid())
185 return false;
186 // Keep in sync with ProtocolHandlers added by
187 // ShellURLRequestContextGetter::GetURLRequestContext().
188 static const char* const kProtocolList[] = {
189 url::kBlobScheme,
190 url::kFileSystemScheme,
191 kChromeUIScheme,
192 kChromeDevToolsScheme,
193 url::kDataScheme,
194 url::kFileScheme,
196 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
197 if (url.scheme() == kProtocolList[i])
198 return true;
200 return false;
203 bool ShellContentBrowserClient::IsNPAPIEnabled() {
204 #if defined(OS_WIN) || defined(OS_MACOSX)
205 return true;
206 #else
207 return false;
208 #endif
211 void ShellContentBrowserClient::RegisterOutOfProcessMojoApplications(
212 OutOfProcessMojoApplicationMap* apps) {
213 apps->insert(std::make_pair(GURL(kTestMojoAppUrl),
214 base::UTF8ToUTF16("Test Mojo App")));
217 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
218 base::CommandLine* command_line,
219 int child_process_id) {
220 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
221 switches::kRunLayoutTest)) {
222 command_line->AppendSwitch(switches::kRunLayoutTest);
224 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
225 switches::kDumpLineBoxTrees)) {
226 command_line->AppendSwitch(switches::kDumpLineBoxTrees);
228 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
229 switches::kEnableFontAntialiasing)) {
230 command_line->AppendSwitch(switches::kEnableFontAntialiasing);
232 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
233 switches::kExposeInternalsForTesting)) {
234 command_line->AppendSwitch(switches::kExposeInternalsForTesting);
236 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
237 switches::kExposeIpcEcho)) {
238 command_line->AppendSwitch(switches::kExposeIpcEcho);
240 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
241 switches::kStableReleaseMode)) {
242 command_line->AppendSwitch(switches::kStableReleaseMode);
244 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
245 switches::kEnableCrashReporter)) {
246 command_line->AppendSwitch(switches::kEnableCrashReporter);
248 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
249 switches::kCrashDumpsDir)) {
250 command_line->AppendSwitchPath(
251 switches::kCrashDumpsDir,
252 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
253 switches::kCrashDumpsDir));
255 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
256 switches::kEnableLeakDetection)) {
257 command_line->AppendSwitchASCII(
258 switches::kEnableLeakDetection,
259 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
260 switches::kEnableLeakDetection));
262 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
263 switches::kRegisterFontFiles)) {
264 command_line->AppendSwitchASCII(
265 switches::kRegisterFontFiles,
266 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
267 switches::kRegisterFontFiles));
271 void ShellContentBrowserClient::OverrideWebkitPrefs(
272 RenderViewHost* render_view_host,
273 WebPreferences* prefs) {
274 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
275 switches::kRunLayoutTest))
276 return;
277 BlinkTestController::Get()->OverrideWebkitPrefs(prefs);
280 void ShellContentBrowserClient::ResourceDispatcherHostCreated() {
281 resource_dispatcher_host_delegate_.reset(
282 base::CommandLine::ForCurrentProcess()->HasSwitch(
283 switches::kRunLayoutTest)
284 ? new LayoutTestResourceDispatcherHostDelegate
285 : new ShellResourceDispatcherHostDelegate);
286 ResourceDispatcherHost::Get()->SetDelegate(
287 resource_dispatcher_host_delegate_.get());
290 std::string ShellContentBrowserClient::GetDefaultDownloadName() {
291 return "download";
294 WebContentsViewDelegate* ShellContentBrowserClient::GetWebContentsViewDelegate(
295 WebContents* web_contents) {
296 #if !defined(USE_AURA)
297 return CreateShellWebContentsViewDelegate(web_contents);
298 #else
299 return NULL;
300 #endif
303 QuotaPermissionContext*
304 ShellContentBrowserClient::CreateQuotaPermissionContext() {
305 return new ShellQuotaPermissionContext();
308 void ShellContentBrowserClient::SelectClientCertificate(
309 WebContents* web_contents,
310 net::SSLCertRequestInfo* cert_request_info,
311 scoped_ptr<ClientCertificateDelegate> delegate) {
312 if (!select_client_certificate_callback_.is_null())
313 select_client_certificate_callback_.Run();
316 SpeechRecognitionManagerDelegate*
317 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
318 return new ShellSpeechRecognitionManagerDelegate();
321 net::NetLog* ShellContentBrowserClient::GetNetLog() {
322 return shell_browser_main_parts_->net_log();
325 bool ShellContentBrowserClient::ShouldSwapProcessesForRedirect(
326 ResourceContext* resource_context,
327 const GURL& current_url,
328 const GURL& new_url) {
329 return g_swap_processes_for_redirect;
332 DevToolsManagerDelegate*
333 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
334 return new ShellDevToolsManagerDelegate(browser_context());
337 void ShellContentBrowserClient::OpenURL(
338 BrowserContext* browser_context,
339 const OpenURLParams& params,
340 const base::Callback<void(WebContents*)>& callback) {
341 callback.Run(Shell::CreateNewWindow(browser_context,
342 params.url,
343 nullptr,
344 gfx::Size())->web_contents());
347 #if defined(OS_ANDROID)
348 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
349 const base::CommandLine& command_line,
350 int child_process_id,
351 content::FileDescriptorInfo* mappings,
352 std::map<int, base::MemoryMappedFile::Region>* regions) {
353 mappings->Share(
354 kShellPakDescriptor,
355 base::GlobalDescriptors::GetInstance()->Get(kShellPakDescriptor));
356 regions->insert(std::make_pair(
357 kShellPakDescriptor,
358 base::GlobalDescriptors::GetInstance()->GetRegion(kShellPakDescriptor)));
360 if (breakpad::IsCrashReporterEnabled()) {
361 base::File f(breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
362 child_process_id));
363 if (!f.IsValid()) {
364 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
365 << "be disabled for this process.";
366 } else {
367 mappings->Transfer(kAndroidMinidumpDescriptor,
368 base::ScopedFD(f.TakePlatformFile()));
372 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
373 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
374 const base::CommandLine& command_line,
375 int child_process_id,
376 content::FileDescriptorInfo* mappings) {
377 int crash_signal_fd = GetCrashSignalFD(command_line);
378 if (crash_signal_fd >= 0) {
379 mappings->Share(kCrashDumpSignal, crash_signal_fd);
382 #endif // defined(OS_ANDROID)
384 #if defined(OS_WIN)
385 void ShellContentBrowserClient::PreSpawnRenderer(sandbox::TargetPolicy* policy,
386 bool* success) {
387 // Add sideloaded font files for testing. See also DIR_WINDOWS_FONTS
388 // addition in |StartSandboxedProcess|.
389 std::vector<std::string> font_files = switches::GetSideloadFontFiles();
390 for (std::vector<std::string>::const_iterator i(font_files.begin());
391 i != font_files.end();
392 ++i) {
393 policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
394 sandbox::TargetPolicy::FILES_ALLOW_READONLY,
395 base::UTF8ToWide(*i).c_str());
398 #endif // OS_WIN
400 ShellBrowserContext* ShellContentBrowserClient::browser_context() {
401 return shell_browser_main_parts_->browser_context();
404 ShellBrowserContext*
405 ShellContentBrowserClient::off_the_record_browser_context() {
406 return shell_browser_main_parts_->off_the_record_browser_context();
409 AccessTokenStore* ShellContentBrowserClient::CreateAccessTokenStore() {
410 return new ShellAccessTokenStore(browser_context());
413 ShellBrowserContext*
414 ShellContentBrowserClient::ShellBrowserContextForBrowserContext(
415 BrowserContext* content_browser_context) {
416 if (content_browser_context == browser_context())
417 return browser_context();
418 DCHECK_EQ(content_browser_context, off_the_record_browser_context());
419 return off_the_record_browser_context();
422 } // namespace content