1 // Copyright (c) 2012 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 "android_webview/browser/aw_content_browser_client.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/browser/aw_browser_main_parts.h"
9 #include "android_webview/browser/aw_contents_client_bridge_base.h"
10 #include "android_webview/browser/aw_contents_io_thread_client.h"
11 #include "android_webview/browser/aw_cookie_access_policy.h"
12 #include "android_webview/browser/aw_locale_manager.h"
13 #include "android_webview/browser/aw_printing_message_filter.h"
14 #include "android_webview/browser/aw_quota_permission_context.h"
15 #include "android_webview/browser/aw_web_preferences_populater.h"
16 #include "android_webview/browser/jni_dependency_factory.h"
17 #include "android_webview/browser/net/aw_url_request_context_getter.h"
18 #include "android_webview/browser/net_disk_cache_remover.h"
19 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
20 #include "android_webview/common/aw_descriptors.h"
21 #include "android_webview/common/render_view_messages.h"
22 #include "android_webview/common/url_constants.h"
23 #include "base/android/locale_utils.h"
24 #include "base/base_paths_android.h"
25 #include "base/path_service.h"
26 #include "components/cdm/browser/cdm_message_filter_android.h"
27 #include "content/public/browser/access_token_store.h"
28 #include "content/public/browser/browser_message_filter.h"
29 #include "content/public/browser/child_process_security_policy.h"
30 #include "content/public/browser/client_certificate_delegate.h"
31 #include "content/public/browser/render_frame_host.h"
32 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/render_view_host.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/url_constants.h"
36 #include "content/public/common/web_preferences.h"
37 #include "net/android/network_library.h"
38 #include "net/ssl/ssl_cert_request_info.h"
39 #include "net/ssl/ssl_info.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/base/resource/resource_bundle_android.h"
42 #include "ui/resources/grit/ui_resources.h"
44 using content::ResourceType
;
46 namespace android_webview
{
49 // TODO(sgurun) move this to its own file.
50 // This class filters out incoming aw_contents related IPC messages for the
51 // renderer process on the IPC thread.
52 class AwContentsMessageFilter
: public content::BrowserMessageFilter
{
54 explicit AwContentsMessageFilter(int process_id
);
56 // BrowserMessageFilter methods.
57 bool OnMessageReceived(const IPC::Message
& message
) override
;
59 void OnSubFrameCreated(int parent_render_frame_id
, int child_render_frame_id
);
62 ~AwContentsMessageFilter() override
;
66 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter
);
69 AwContentsMessageFilter::AwContentsMessageFilter(int process_id
)
70 : BrowserMessageFilter(AndroidWebViewMsgStart
),
71 process_id_(process_id
) {
74 AwContentsMessageFilter::~AwContentsMessageFilter() {
77 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message
& message
) {
79 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter
, message
)
80 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated
, OnSubFrameCreated
)
81 IPC_MESSAGE_UNHANDLED(handled
= false)
86 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id
,
87 int child_render_frame_id
) {
88 AwContentsIoThreadClient::SubFrameCreated(
89 process_id_
, parent_render_frame_id
, child_render_frame_id
);
92 class AwAccessTokenStore
: public content::AccessTokenStore
{
94 AwAccessTokenStore() { }
96 // content::AccessTokenStore implementation
97 void LoadAccessTokens(const LoadAccessTokensCallbackType
& request
) override
{
98 AccessTokenStore::AccessTokenSet access_token_set
;
99 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
100 // but Run needs to be called to finish the geolocation setup.
101 request
.Run(access_token_set
, NULL
);
103 void SaveAccessToken(const GURL
& server_url
,
104 const base::string16
& access_token
) override
{}
107 ~AwAccessTokenStore() override
{}
109 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore
);
112 AwLocaleManager
* g_locale_manager
= NULL
;
114 } // anonymous namespace
117 std::string
AwContentBrowserClient::GetAcceptLangsImpl() {
118 // Start with the current locale.
119 std::string langs
= g_locale_manager
->GetLocale();
121 // If we're not en-US, add in en-US which will be
122 // used with a lower q-value.
123 if (base::ToLowerASCII(langs
) != "en-us") {
130 AwBrowserContext
* AwContentBrowserClient::GetAwBrowserContext() {
131 return AwBrowserContext::GetDefault();
134 AwContentBrowserClient::AwContentBrowserClient(
135 JniDependencyFactory
* native_factory
)
136 : native_factory_(native_factory
) {
137 base::FilePath user_data_dir
;
138 if (!PathService::Get(base::DIR_ANDROID_APP_DATA
, &user_data_dir
)) {
139 NOTREACHED() << "Failed to get app data directory for Android WebView";
141 browser_context_
.reset(
142 new AwBrowserContext(user_data_dir
, native_factory_
));
143 g_locale_manager
= native_factory
->CreateAwLocaleManager();
146 AwContentBrowserClient::~AwContentBrowserClient() {
147 delete g_locale_manager
;
148 g_locale_manager
= NULL
;
151 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type
,
152 const void* cert_data
,
154 int render_process_id
,
155 int render_frame_id
) {
157 net::android::StoreCertificate(cert_type
, cert_data
, cert_size
);
160 content::BrowserMainParts
* AwContentBrowserClient::CreateBrowserMainParts(
161 const content::MainFunctionParams
& parameters
) {
162 return new AwBrowserMainParts(browser_context_
.get());
165 content::WebContentsViewDelegate
*
166 AwContentBrowserClient::GetWebContentsViewDelegate(
167 content::WebContents
* web_contents
) {
168 return native_factory_
->CreateViewDelegate(web_contents
);
171 void AwContentBrowserClient::RenderProcessWillLaunch(
172 content::RenderProcessHost
* host
) {
173 // Grant content: scheme access to the whole renderer process, since we impose
174 // per-view access checks, and access is granted by default (see
175 // AwSettings.mAllowContentUrlAccess).
176 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
177 host
->GetID(), url::kContentScheme
);
179 host
->AddFilter(new AwContentsMessageFilter(host
->GetID()));
180 host
->AddFilter(new cdm::CdmMessageFilterAndroid());
181 host
->AddFilter(new AwPrintingMessageFilter(host
->GetID()));
184 net::URLRequestContextGetter
* AwContentBrowserClient::CreateRequestContext(
185 content::BrowserContext
* browser_context
,
186 content::ProtocolHandlerMap
* protocol_handlers
,
187 content::URLRequestInterceptorScopedVector request_interceptors
) {
188 DCHECK_EQ(browser_context_
.get(), browser_context
);
189 return browser_context_
->CreateRequestContext(protocol_handlers
,
190 request_interceptors
.Pass());
193 net::URLRequestContextGetter
*
194 AwContentBrowserClient::CreateRequestContextForStoragePartition(
195 content::BrowserContext
* browser_context
,
196 const base::FilePath
& partition_path
,
198 content::ProtocolHandlerMap
* protocol_handlers
,
199 content::URLRequestInterceptorScopedVector request_interceptors
) {
200 DCHECK_EQ(browser_context_
.get(), browser_context
);
201 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
202 // downstream. (crbug.com/350286)
203 return browser_context_
->CreateRequestContextForStoragePartition(
204 partition_path
, in_memory
, protocol_handlers
,
205 request_interceptors
.Pass());
208 bool AwContentBrowserClient::IsHandledURL(const GURL
& url
) {
209 if (!url
.is_valid()) {
210 // We handle error cases.
214 const std::string scheme
= url
.scheme();
215 DCHECK_EQ(scheme
, base::ToLowerASCII(scheme
));
216 // See CreateJobFactory in aw_url_request_context_getter.cc for the
217 // list of protocols that are handled.
218 // TODO(mnaganov): Make this automatic.
219 static const char* const kProtocolList
[] = {
222 url::kFileSystemScheme
,
223 content::kChromeUIScheme
,
224 content::kChromeDevToolsScheme
,
227 if (scheme
== url::kFileScheme
) {
228 // Return false for the "special" file URLs, so they can be loaded
229 // even if access to file: scheme is not granted to the child process.
230 return !IsAndroidSpecialFileUrl(url
);
232 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
233 if (scheme
== kProtocolList
[i
])
236 return net::URLRequest::IsHandledProtocol(scheme
);
239 std::string
AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
240 const std::string
& alias_name
) {
244 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
245 base::CommandLine
* command_line
,
246 int child_process_id
) {
247 NOTREACHED() << "Android WebView does not support multi-process yet";
250 std::string
AwContentBrowserClient::GetApplicationLocale() {
251 return base::android::GetDefaultLocale();
254 std::string
AwContentBrowserClient::GetAcceptLangs(
255 content::BrowserContext
* context
) {
256 return GetAcceptLangsImpl();
259 const gfx::ImageSkia
* AwContentBrowserClient::GetDefaultFavicon() {
260 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
261 // TODO(boliu): Bundle our own default favicon?
262 return rb
.GetImageSkiaNamed(IDR_DEFAULT_FAVICON
);
265 bool AwContentBrowserClient::AllowAppCache(const GURL
& manifest_url
,
266 const GURL
& first_party
,
267 content::ResourceContext
* context
) {
268 // WebView doesn't have a per-site policy for locally stored data,
269 // instead AppCache can be disabled for individual WebViews.
274 bool AwContentBrowserClient::AllowGetCookie(const GURL
& url
,
275 const GURL
& first_party
,
276 const net::CookieList
& cookie_list
,
277 content::ResourceContext
* context
,
278 int render_process_id
,
279 int render_frame_id
) {
280 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url
,
288 bool AwContentBrowserClient::AllowSetCookie(const GURL
& url
,
289 const GURL
& first_party
,
290 const std::string
& cookie_line
,
291 content::ResourceContext
* context
,
292 int render_process_id
,
294 net::CookieOptions
* options
) {
295 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url
,
304 bool AwContentBrowserClient::AllowWorkerDatabase(
306 const base::string16
& name
,
307 const base::string16
& display_name
,
308 unsigned long estimated_size
,
309 content::ResourceContext
* context
,
310 const std::vector
<std::pair
<int, int> >& render_frames
) {
311 // Android WebView does not yet support web workers.
315 void AwContentBrowserClient::AllowWorkerFileSystem(
317 content::ResourceContext
* context
,
318 const std::vector
<std::pair
<int, int> >& render_frames
,
319 base::Callback
<void(bool)> callback
) {
320 // Android WebView does not yet support web workers.
324 bool AwContentBrowserClient::AllowWorkerIndexedDB(
326 const base::string16
& name
,
327 content::ResourceContext
* context
,
328 const std::vector
<std::pair
<int, int> >& render_frames
) {
329 // Android WebView does not yet support web workers.
333 content::QuotaPermissionContext
*
334 AwContentBrowserClient::CreateQuotaPermissionContext() {
335 return new AwQuotaPermissionContext
;
338 void AwContentBrowserClient::AllowCertificateError(
339 int render_process_id
,
342 const net::SSLInfo
& ssl_info
,
343 const GURL
& request_url
,
344 ResourceType resource_type
,
346 bool strict_enforcement
,
347 bool expired_previous_decision
,
348 const base::Callback
<void(bool)>& callback
,
349 content::CertificateRequestResultType
* result
) {
350 AwContentsClientBridgeBase
* client
=
351 AwContentsClientBridgeBase::FromID(render_process_id
, render_frame_id
);
352 bool cancel_request
= true;
354 client
->AllowCertificateError(cert_error
,
360 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY
;
363 void AwContentBrowserClient::SelectClientCertificate(
364 content::WebContents
* web_contents
,
365 net::SSLCertRequestInfo
* cert_request_info
,
366 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
367 AwContentsClientBridgeBase
* client
=
368 AwContentsClientBridgeBase::FromWebContents(web_contents
);
370 client
->SelectClientCertificate(cert_request_info
, delegate
.Pass());
373 bool AwContentBrowserClient::CanCreateWindow(
374 const GURL
& opener_url
,
375 const GURL
& opener_top_level_frame_url
,
376 const GURL
& source_origin
,
377 WindowContainerType container_type
,
378 const GURL
& target_url
,
379 const content::Referrer
& referrer
,
380 WindowOpenDisposition disposition
,
381 const blink::WebWindowFeatures
& features
,
383 bool opener_suppressed
,
384 content::ResourceContext
* context
,
385 int render_process_id
,
386 int opener_render_view_id
,
387 int opener_render_frame_id
,
388 bool* no_javascript_access
) {
389 // We unconditionally allow popup windows at this stage and will give
390 // the embedder the opporunity to handle displaying of the popup in
391 // WebContentsDelegate::AddContents (via the
392 // AwContentsClient.onCreateWindow callback).
393 // Note that if the embedder has blocked support for creating popup
394 // windows through AwSettings, then we won't get to this point as
395 // the popup creation will have been blocked at the WebKit level.
396 if (no_javascript_access
) {
397 *no_javascript_access
= false;
402 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
403 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
406 net::NetLog
* AwContentBrowserClient::GetNetLog() {
407 return browser_context_
->GetAwURLRequestContext()->GetNetLog();
410 content::AccessTokenStore
* AwContentBrowserClient::CreateAccessTokenStore() {
411 return new AwAccessTokenStore();
414 bool AwContentBrowserClient::IsFastShutdownPossible() {
415 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
416 << " should never be called";
420 void AwContentBrowserClient::ClearCache(content::RenderFrameHost
* rfh
) {
421 RemoveHttpDiskCache(rfh
->GetProcess()->GetBrowserContext(),
422 rfh
->GetProcess()->GetID());
425 void AwContentBrowserClient::ClearCookies(content::RenderFrameHost
* rfh
) {
426 // TODO(boliu): Implement.
430 base::FilePath
AwContentBrowserClient::GetDefaultDownloadDirectory() {
431 // Android WebView does not currently use the Chromium downloads system.
432 // Download requests are cancelled immedately when recognized; see
433 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
434 // download system still tries to start up and calls this before recognizing
435 // the request has been cancelled.
436 return base::FilePath();
439 std::string
AwContentBrowserClient::GetDefaultDownloadName() {
440 NOTREACHED() << "Android WebView does not use chromium downloads";
441 return std::string();
444 void AwContentBrowserClient::DidCreatePpapiPlugin(
445 content::BrowserPpapiHost
* browser_host
) {
446 NOTREACHED() << "Android WebView does not support plugins";
449 bool AwContentBrowserClient::AllowPepperSocketAPI(
450 content::BrowserContext
* browser_context
,
453 const content::SocketPermissionRequest
* params
) {
454 NOTREACHED() << "Android WebView does not support plugins";
458 void AwContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
459 const base::CommandLine
& command_line
,
460 int child_process_id
,
461 content::FileDescriptorInfo
* mappings
,
462 std::map
<int, base::MemoryMappedFile::Region
>* regions
) {
463 int fd
= ui::GetMainAndroidPackFd(
464 &(*regions
)[kAndroidWebViewMainPakDescriptor
]);
465 mappings
->Share(kAndroidWebViewMainPakDescriptor
, fd
);
467 fd
= ui::GetLocalePackFd(&(*regions
)[kAndroidWebViewLocalePakDescriptor
]);
468 mappings
->Share(kAndroidWebViewLocalePakDescriptor
, fd
);
471 void AwContentBrowserClient::OverrideWebkitPrefs(
472 content::RenderViewHost
* rvh
,
473 content::WebPreferences
* web_prefs
) {
474 if (!preferences_populater_
.get()) {
475 preferences_populater_
= make_scoped_ptr(native_factory_
->
476 CreateWebPreferencesPopulater());
478 preferences_populater_
->PopulateFor(
479 content::WebContents::FromRenderViewHost(rvh
), web_prefs
);
482 #if defined(VIDEO_HOLE)
483 content::ExternalVideoSurfaceContainer
*
484 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
485 content::WebContents
* web_contents
) {
486 return native_factory_
->CreateExternalVideoSurfaceContainer(web_contents
);
490 } // namespace android_webview