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/command_line.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_types.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/resource_dispatcher_host.h"
14 #include "content/public/browser/storage_partition.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/url_constants.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/shell/browser/shell_browser_context.h"
19 #include "content/shell/browser/shell_browser_main_parts.h"
20 #include "content/shell/browser/shell_devtools_delegate.h"
21 #include "content/shell/browser/shell_message_filter.h"
22 #include "content/shell/browser/shell_net_log.h"
23 #include "content/shell/browser/shell_quota_permission_context.h"
24 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
25 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
26 #include "content/shell/browser/webkit_test_controller.h"
27 #include "content/shell/common/shell_messages.h"
28 #include "content/shell/common/shell_switches.h"
29 #include "content/shell/common/webkit_test_helpers.h"
30 #include "content/shell/geolocation/shell_access_token_store.h"
31 #include "net/url_request/url_request_context_getter.h"
33 #include "webkit/common/webpreferences.h"
35 #if defined(OS_ANDROID)
36 #include "base/android/path_utils.h"
37 #include "base/path_service.h"
38 #include "base/platform_file.h"
39 #include "content/shell/android/shell_descriptors.h"
46 ShellContentBrowserClient
* g_browser_client
;
47 bool g_swap_processes_for_redirect
= false;
51 ShellContentBrowserClient
* ShellContentBrowserClient::Get() {
52 return g_browser_client
;
55 void ShellContentBrowserClient::SetSwapProcessesForRedirect(bool swap
) {
56 g_swap_processes_for_redirect
= swap
;
59 ShellContentBrowserClient::ShellContentBrowserClient()
60 : shell_browser_main_parts_(NULL
) {
61 DCHECK(!g_browser_client
);
62 g_browser_client
= this;
63 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
65 webkit_source_dir_
= GetWebKitRootDirFilePath();
68 ShellContentBrowserClient::~ShellContentBrowserClient() {
69 g_browser_client
= NULL
;
72 BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
73 const MainFunctionParams
& parameters
) {
74 shell_browser_main_parts_
= new ShellBrowserMainParts(parameters
);
75 return shell_browser_main_parts_
;
78 void ShellContentBrowserClient::RenderProcessHostCreated(
79 RenderProcessHost
* host
) {
80 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
82 host
->GetChannel()->AddFilter(new ShellMessageFilter(
84 BrowserContext::GetDefaultStoragePartition(browser_context())
85 ->GetDatabaseTracker(),
86 BrowserContext::GetDefaultStoragePartition(browser_context())
88 BrowserContext::GetDefaultStoragePartition(browser_context())
89 ->GetURLRequestContext()));
90 host
->Send(new ShellViewMsg_SetWebKitSourceDir(webkit_source_dir_
));
92 NOTIFICATION_RENDERER_PROCESS_CREATED
,
93 Source
<RenderProcessHost
>(host
));
95 NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
96 Source
<RenderProcessHost
>(host
));
99 net::URLRequestContextGetter
* ShellContentBrowserClient::CreateRequestContext(
100 BrowserContext
* content_browser_context
,
101 ProtocolHandlerMap
* protocol_handlers
) {
102 ShellBrowserContext
* shell_browser_context
=
103 ShellBrowserContextForBrowserContext(content_browser_context
);
104 return shell_browser_context
->CreateRequestContext(protocol_handlers
);
107 net::URLRequestContextGetter
*
108 ShellContentBrowserClient::CreateRequestContextForStoragePartition(
109 BrowserContext
* content_browser_context
,
110 const base::FilePath
& partition_path
,
112 ProtocolHandlerMap
* protocol_handlers
) {
113 ShellBrowserContext
* shell_browser_context
=
114 ShellBrowserContextForBrowserContext(content_browser_context
);
115 return shell_browser_context
->CreateRequestContextForStoragePartition(
116 partition_path
, in_memory
, protocol_handlers
);
119 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
122 DCHECK_EQ(url
.scheme(), StringToLowerASCII(url
.scheme()));
123 // Keep in sync with ProtocolHandlers added by
124 // ShellURLRequestContextGetter::GetURLRequestContext().
125 static const char* const kProtocolList
[] = {
127 chrome::kFileSystemScheme
,
128 chrome::kChromeUIScheme
,
129 chrome::kChromeDevToolsScheme
,
133 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
134 if (url
.scheme() == kProtocolList
[i
])
140 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
141 CommandLine
* command_line
, int child_process_id
) {
142 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
143 command_line
->AppendSwitch(switches::kDumpRenderTree
);
144 if (CommandLine::ForCurrentProcess()->HasSwitch(
145 switches::kExposeInternalsForTesting
))
146 command_line
->AppendSwitch(switches::kExposeInternalsForTesting
);
147 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kStableReleaseMode
))
148 command_line
->AppendSwitch(switches::kStableReleaseMode
);
151 void ShellContentBrowserClient::OverrideWebkitPrefs(
152 RenderViewHost
* render_view_host
,
154 WebPreferences
* prefs
) {
155 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
157 WebKitTestController::Get()->OverrideWebkitPrefs(prefs
);
160 void ShellContentBrowserClient::ResourceDispatcherHostCreated() {
161 resource_dispatcher_host_delegate_
.reset(
162 new ShellResourceDispatcherHostDelegate());
163 ResourceDispatcherHost::Get()->SetDelegate(
164 resource_dispatcher_host_delegate_
.get());
167 std::string
ShellContentBrowserClient::GetDefaultDownloadName() {
171 bool ShellContentBrowserClient::SupportsBrowserPlugin(
172 content::BrowserContext
* browser_context
, const GURL
& url
) {
173 return CommandLine::ForCurrentProcess()->HasSwitch(
174 switches::kEnableBrowserPluginForAllViewTypes
);
177 WebContentsViewDelegate
* ShellContentBrowserClient::GetWebContentsViewDelegate(
178 WebContents
* web_contents
) {
179 #if !defined(USE_AURA)
180 return CreateShellWebContentsViewDelegate(web_contents
);
186 QuotaPermissionContext
*
187 ShellContentBrowserClient::CreateQuotaPermissionContext() {
188 return new ShellQuotaPermissionContext();
191 SpeechRecognitionManagerDelegate
*
192 ShellContentBrowserClient::GetSpeechRecognitionManagerDelegate() {
193 return new ShellSpeechRecognitionManagerDelegate();
196 net::NetLog
* ShellContentBrowserClient::GetNetLog() {
197 return shell_browser_main_parts_
->net_log();
200 bool ShellContentBrowserClient::ShouldSwapProcessesForRedirect(
201 ResourceContext
* resource_context
,
202 const GURL
& current_url
,
203 const GURL
& new_url
) {
204 return g_swap_processes_for_redirect
;
207 #if defined(OS_ANDROID)
208 void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
209 const CommandLine
& command_line
,
210 int child_process_id
,
211 std::vector
<content::FileDescriptorInfo
>* mappings
) {
212 int flags
= base::PLATFORM_FILE_OPEN
| base::PLATFORM_FILE_READ
;
213 base::FilePath pak_file
;
214 bool r
= PathService::Get(base::DIR_ANDROID_APP_DATA
, &pak_file
);
216 pak_file
= pak_file
.Append(FILE_PATH_LITERAL("paks"));
217 pak_file
= pak_file
.Append(FILE_PATH_LITERAL("content_shell.pak"));
219 base::PlatformFile f
=
220 base::CreatePlatformFile(pak_file
, flags
, NULL
, NULL
);
221 if (f
== base::kInvalidPlatformFileValue
) {
222 NOTREACHED() << "Failed to open file when creating renderer process: "
223 << "content_shell.pak";
226 content::FileDescriptorInfo(kShellPakDescriptor
,
227 base::FileDescriptor(f
, true)));
231 void ShellContentBrowserClient::Observe(int type
,
232 const NotificationSource
& source
,
233 const NotificationDetails
& details
) {
235 case NOTIFICATION_RENDERER_PROCESS_CREATED
: {
236 registrar_
.Remove(this,
237 NOTIFICATION_RENDERER_PROCESS_CREATED
,
239 registrar_
.Remove(this,
240 NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
245 case NOTIFICATION_RENDERER_PROCESS_TERMINATED
: {
246 registrar_
.Remove(this,
247 NOTIFICATION_RENDERER_PROCESS_CREATED
,
249 registrar_
.Remove(this,
250 NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
260 ShellBrowserContext
* ShellContentBrowserClient::browser_context() {
261 return shell_browser_main_parts_
->browser_context();
265 ShellContentBrowserClient::off_the_record_browser_context() {
266 return shell_browser_main_parts_
->off_the_record_browser_context();
269 AccessTokenStore
* ShellContentBrowserClient::CreateAccessTokenStore() {
270 return new ShellAccessTokenStore(browser_context());
274 ShellContentBrowserClient::ShellBrowserContextForBrowserContext(
275 BrowserContext
* content_browser_context
) {
276 if (content_browser_context
== browser_context())
277 return browser_context();
278 DCHECK_EQ(content_browser_context
, off_the_record_browser_context());
279 return off_the_record_browser_context();
282 } // namespace content