Update broken references to image assets
[chromium-blink-merge.git] / content / shell / browser / shell_content_browser_client.cc
blob433ec23dccdfabc0dde699e8222e302adc0fcf6a
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"
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/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/app/breakpad_linux.h"
50 #include "components/crash/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::AppendExtraCommandLineSwitches(
212 base::CommandLine* command_line,
213 int child_process_id) {
214 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
215 switches::kRunLayoutTest)) {
216 command_line->AppendSwitch(switches::kRunLayoutTest);
218 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
219 switches::kDumpLineBoxTrees)) {
220 command_line->AppendSwitch(switches::kDumpLineBoxTrees);
222 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
223 switches::kEnableFontAntialiasing)) {
224 command_line->AppendSwitch(switches::kEnableFontAntialiasing);
226 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
227 switches::kExposeInternalsForTesting)) {
228 command_line->AppendSwitch(switches::kExposeInternalsForTesting);
230 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
231 switches::kExposeIpcEcho)) {
232 command_line->AppendSwitch(switches::kExposeIpcEcho);
234 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
235 switches::kStableReleaseMode)) {
236 command_line->AppendSwitch(switches::kStableReleaseMode);
238 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
239 switches::kEnableCrashReporter)) {
240 command_line->AppendSwitch(switches::kEnableCrashReporter);
242 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
243 switches::kCrashDumpsDir)) {
244 command_line->AppendSwitchPath(
245 switches::kCrashDumpsDir,
246 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
247 switches::kCrashDumpsDir));
249 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
250 switches::kEnableLeakDetection)) {
251 command_line->AppendSwitchASCII(
252 switches::kEnableLeakDetection,
253 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
254 switches::kEnableLeakDetection));
256 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
257 switches::kRegisterFontFiles)) {
258 command_line->AppendSwitchASCII(
259 switches::kRegisterFontFiles,
260 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
261 switches::kRegisterFontFiles));
265 void ShellContentBrowserClient::OverrideWebkitPrefs(
266 RenderViewHost* render_view_host,
267 WebPreferences* prefs) {
268 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
269 switches::kRunLayoutTest))
270 return;
271 BlinkTestController::Get()->OverrideWebkitPrefs(prefs);
274 void ShellContentBrowserClient::ResourceDispatcherHostCreated() {
275 resource_dispatcher_host_delegate_.reset(
276 base::CommandLine::ForCurrentProcess()->HasSwitch(
277 switches::kRunLayoutTest)
278 ? new LayoutTestResourceDispatcherHostDelegate
279 : new ShellResourceDispatcherHostDelegate);
280 ResourceDispatcherHost::Get()->SetDelegate(
281 resource_dispatcher_host_delegate_.get());
284 std::string ShellContentBrowserClient::GetDefaultDownloadName() {
285 return "download";
288 WebContentsViewDelegate* ShellContentBrowserClient::GetWebContentsViewDelegate(
289 WebContents* web_contents) {
290 #if !defined(USE_AURA)
291 return CreateShellWebContentsViewDelegate(web_contents);
292 #else
293 return NULL;
294 #endif
297 QuotaPermissionContext*
298 ShellContentBrowserClient::CreateQuotaPermissionContext() {
299 return new ShellQuotaPermissionContext();
302 void ShellContentBrowserClient::SelectClientCertificate(
303 WebContents* web_contents,
304 net::SSLCertRequestInfo* cert_request_info,
305 scoped_ptr<ClientCertificateDelegate> delegate) {
306 if (!select_client_certificate_callback_.is_null())
307 select_client_certificate_callback_.Run();
310 SpeechRecognitionManagerDelegate*
311 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
312 return new ShellSpeechRecognitionManagerDelegate();
315 net::NetLog* ShellContentBrowserClient::GetNetLog() {
316 return shell_browser_main_parts_->net_log();
319 bool ShellContentBrowserClient::ShouldSwapProcessesForRedirect(
320 ResourceContext* resource_context,
321 const GURL& current_url,
322 const GURL& new_url) {
323 return g_swap_processes_for_redirect;
326 DevToolsManagerDelegate*
327 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
328 return new ShellDevToolsManagerDelegate(browser_context());
331 void ShellContentBrowserClient::OpenURL(
332 BrowserContext* browser_context,
333 const OpenURLParams& params,
334 const base::Callback<void(WebContents*)>& callback) {
335 callback.Run(Shell::CreateNewWindow(browser_context,
336 params.url,
337 nullptr,
338 gfx::Size())->web_contents());
341 #if defined(OS_ANDROID)
342 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
343 const base::CommandLine& command_line,
344 int child_process_id,
345 content::FileDescriptorInfo* mappings,
346 std::map<int, base::MemoryMappedFile::Region>* regions) {
347 mappings->Share(
348 kShellPakDescriptor,
349 base::GlobalDescriptors::GetInstance()->Get(kShellPakDescriptor));
350 regions->insert(std::make_pair(
351 kShellPakDescriptor,
352 base::GlobalDescriptors::GetInstance()->GetRegion(kShellPakDescriptor)));
354 if (breakpad::IsCrashReporterEnabled()) {
355 base::File f(breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
356 child_process_id));
357 if (!f.IsValid()) {
358 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
359 << "be disabled for this process.";
360 } else {
361 mappings->Transfer(kAndroidMinidumpDescriptor,
362 base::ScopedFD(f.TakePlatformFile()));
366 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
367 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
368 const base::CommandLine& command_line,
369 int child_process_id,
370 content::FileDescriptorInfo* mappings) {
371 int crash_signal_fd = GetCrashSignalFD(command_line);
372 if (crash_signal_fd >= 0) {
373 mappings->Share(kCrashDumpSignal, crash_signal_fd);
376 #endif // defined(OS_ANDROID)
378 #if defined(OS_WIN)
379 void ShellContentBrowserClient::PreSpawnRenderer(sandbox::TargetPolicy* policy,
380 bool* success) {
381 // Add sideloaded font files for testing. See also DIR_WINDOWS_FONTS
382 // addition in |StartSandboxedProcess|.
383 std::vector<std::string> font_files = GetSideloadFontFiles();
384 for (std::vector<std::string>::const_iterator i(font_files.begin());
385 i != font_files.end();
386 ++i) {
387 policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
388 sandbox::TargetPolicy::FILES_ALLOW_READONLY,
389 base::UTF8ToWide(*i).c_str());
392 #endif // OS_WIN
394 ShellBrowserContext* ShellContentBrowserClient::browser_context() {
395 return shell_browser_main_parts_->browser_context();
398 ShellBrowserContext*
399 ShellContentBrowserClient::off_the_record_browser_context() {
400 return shell_browser_main_parts_->off_the_record_browser_context();
403 AccessTokenStore* ShellContentBrowserClient::CreateAccessTokenStore() {
404 return new ShellAccessTokenStore(browser_context());
407 ShellBrowserContext*
408 ShellContentBrowserClient::ShellBrowserContextForBrowserContext(
409 BrowserContext* content_browser_context) {
410 if (content_browser_context == browser_context())
411 return browser_context();
412 DCHECK_EQ(content_browser_context, off_the_record_browser_context());
413 return off_the_record_browser_context();
416 } // namespace content