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_browser_permission_request_delegate.h"
10 #include "android_webview/browser/aw_contents_client_bridge_base.h"
11 #include "android_webview/browser/aw_contents_io_thread_client.h"
12 #include "android_webview/browser/aw_cookie_access_policy.h"
13 #include "android_webview/browser/aw_quota_permission_context.h"
14 #include "android_webview/browser/aw_web_preferences_populater.h"
15 #include "android_webview/browser/jni_dependency_factory.h"
16 #include "android_webview/browser/net_disk_cache_remover.h"
17 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
18 #include "android_webview/common/render_view_messages.h"
19 #include "android_webview/common/url_constants.h"
20 #include "base/base_paths_android.h"
21 #include "base/path_service.h"
22 #include "components/cdm/browser/cdm_message_filter_android.h"
23 #include "content/public/browser/access_token_store.h"
24 #include "content/public/browser/browser_message_filter.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/child_process_security_policy.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/url_constants.h"
31 #include "grit/ui_resources.h"
32 #include "net/android/network_library.h"
33 #include "net/ssl/ssl_cert_request_info.h"
34 #include "net/ssl/ssl_info.h"
35 #include "ui/base/l10n/l10n_util_android.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "webkit/common/webpreferences.h"
39 using content::BrowserThread
;
41 namespace android_webview
{
44 // TODO(sgurun) move this to its own file.
45 // This class filters out incoming aw_contents related IPC messages for the
46 // renderer process on the IPC thread.
47 class AwContentsMessageFilter
: public content::BrowserMessageFilter
{
49 explicit AwContentsMessageFilter(int process_id
);
51 // BrowserMessageFilter methods.
52 virtual void OverrideThreadForMessage(
53 const IPC::Message
& message
,
54 BrowserThread::ID
* thread
) OVERRIDE
;
55 virtual bool OnMessageReceived(
56 const IPC::Message
& message
) OVERRIDE
;
58 void OnShouldOverrideUrlLoading(int routing_id
,
59 const base::string16
& url
,
60 bool* ignore_navigation
);
61 void OnSubFrameCreated(int parent_render_frame_id
, int child_render_frame_id
);
64 virtual ~AwContentsMessageFilter();
68 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter
);
71 AwContentsMessageFilter::AwContentsMessageFilter(int process_id
)
72 : BrowserMessageFilter(AndroidWebViewMsgStart
),
73 process_id_(process_id
) {
76 AwContentsMessageFilter::~AwContentsMessageFilter() {
79 void AwContentsMessageFilter::OverrideThreadForMessage(
80 const IPC::Message
& message
, BrowserThread::ID
* thread
) {
81 if (message
.type() == AwViewHostMsg_ShouldOverrideUrlLoading::ID
) {
82 *thread
= BrowserThread::UI
;
86 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message
& message
) {
88 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter
, message
)
89 IPC_MESSAGE_HANDLER(AwViewHostMsg_ShouldOverrideUrlLoading
,
90 OnShouldOverrideUrlLoading
)
91 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated
, OnSubFrameCreated
)
92 IPC_MESSAGE_UNHANDLED(handled
= false)
97 void AwContentsMessageFilter::OnShouldOverrideUrlLoading(
99 const base::string16
& url
,
100 bool* ignore_navigation
) {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
102 *ignore_navigation
= false;
103 AwContentsClientBridgeBase
* client
=
104 AwContentsClientBridgeBase::FromID(process_id_
, render_frame_id
);
106 *ignore_navigation
= client
->ShouldOverrideUrlLoading(url
);
108 LOG(WARNING
) << "Failed to find the associated render view host for url: "
113 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id
,
114 int child_render_frame_id
) {
115 AwContentsIoThreadClient::SubFrameCreated(
116 process_id_
, parent_render_frame_id
, child_render_frame_id
);
119 class AwAccessTokenStore
: public content::AccessTokenStore
{
121 AwAccessTokenStore() { }
123 // content::AccessTokenStore implementation
124 virtual void LoadAccessTokens(
125 const LoadAccessTokensCallbackType
& request
) OVERRIDE
{
126 AccessTokenStore::AccessTokenSet access_token_set
;
127 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
128 // but Run needs to be called to finish the geolocation setup.
129 request
.Run(access_token_set
, NULL
);
131 virtual void SaveAccessToken(const GURL
& server_url
,
132 const base::string16
& access_token
) OVERRIDE
{ }
135 virtual ~AwAccessTokenStore() { }
137 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore
);
140 void CancelProtectedMediaIdentifierPermissionRequests(
141 int render_process_id
,
143 const GURL
& origin
) {
144 AwBrowserPermissionRequestDelegate
* delegate
=
145 AwBrowserPermissionRequestDelegate::FromID(render_process_id
,
148 delegate
->CancelProtectedMediaIdentifierPermissionRequests(origin
);
153 std::string
AwContentBrowserClient::GetAcceptLangsImpl() {
154 // Start with the currnet locale.
155 std::string langs
= l10n_util::GetDefaultLocale();
157 // If we're not en-US, add in en-US which will be
158 // used with a lower q-value.
159 if (StringToLowerASCII(langs
) != "en-us") {
165 AwBrowserContext
* AwContentBrowserClient::GetAwBrowserContext() {
166 return AwBrowserContext::GetDefault();
169 AwContentBrowserClient::AwContentBrowserClient(
170 JniDependencyFactory
* native_factory
)
171 : native_factory_(native_factory
) {
172 base::FilePath user_data_dir
;
173 if (!PathService::Get(base::DIR_ANDROID_APP_DATA
, &user_data_dir
)) {
174 NOTREACHED() << "Failed to get app data directory for Android WebView";
176 browser_context_
.reset(
177 new AwBrowserContext(user_data_dir
, native_factory_
));
180 AwContentBrowserClient::~AwContentBrowserClient() {
183 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type
,
184 const void* cert_data
,
186 int render_process_id
,
187 int render_frame_id
) {
189 net::android::StoreCertificate(cert_type
, cert_data
, cert_size
);
192 content::BrowserMainParts
* AwContentBrowserClient::CreateBrowserMainParts(
193 const content::MainFunctionParams
& parameters
) {
194 return new AwBrowserMainParts(browser_context_
.get());
197 content::WebContentsViewDelegate
*
198 AwContentBrowserClient::GetWebContentsViewDelegate(
199 content::WebContents
* web_contents
) {
200 return native_factory_
->CreateViewDelegate(web_contents
);
203 void AwContentBrowserClient::RenderProcessWillLaunch(
204 content::RenderProcessHost
* host
) {
205 // If WebView becomes multi-process capable, this may be insecure.
206 // More benefit can be derived from the ChildProcessSecurotyPolicy by
207 // deferring the GrantScheme calls until we know that a given child process
208 // really does need that priviledge. Check here to ensure we rethink this
209 // when the time comes. See crbug.com/156062.
210 CHECK(content::RenderProcessHost::run_renderer_in_process());
212 // Grant content: and file: scheme to the whole process, since we impose
213 // per-view access checks.
214 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
215 host
->GetID(), android_webview::kContentScheme
);
216 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
217 host
->GetID(), url::kFileScheme
);
219 host
->AddFilter(new AwContentsMessageFilter(host
->GetID()));
220 host
->AddFilter(new cdm::CdmMessageFilterAndroid());
223 net::URLRequestContextGetter
* AwContentBrowserClient::CreateRequestContext(
224 content::BrowserContext
* browser_context
,
225 content::ProtocolHandlerMap
* protocol_handlers
,
226 content::URLRequestInterceptorScopedVector request_interceptors
) {
227 DCHECK(browser_context_
.get() == browser_context
);
228 return browser_context_
->CreateRequestContext(protocol_handlers
,
229 request_interceptors
.Pass());
232 net::URLRequestContextGetter
*
233 AwContentBrowserClient::CreateRequestContextForStoragePartition(
234 content::BrowserContext
* browser_context
,
235 const base::FilePath
& partition_path
,
237 content::ProtocolHandlerMap
* protocol_handlers
,
238 content::URLRequestInterceptorScopedVector request_interceptors
) {
239 DCHECK(browser_context_
.get() == browser_context
);
240 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
241 // downstream. (crbug.com/350286)
242 return browser_context_
->CreateRequestContextForStoragePartition(
243 partition_path
, in_memory
, protocol_handlers
,
244 request_interceptors
.Pass());
247 std::string
AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
248 const std::string
& alias_name
) {
252 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
253 base::CommandLine
* command_line
,
254 int child_process_id
) {
255 NOTREACHED() << "Android WebView does not support multi-process yet";
258 std::string
AwContentBrowserClient::GetApplicationLocale() {
259 return l10n_util::GetDefaultLocale();
262 std::string
AwContentBrowserClient::GetAcceptLangs(
263 content::BrowserContext
* context
) {
264 return GetAcceptLangsImpl();
267 const gfx::ImageSkia
* AwContentBrowserClient::GetDefaultFavicon() {
268 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
269 // TODO(boliu): Bundle our own default favicon?
270 return rb
.GetImageSkiaNamed(IDR_DEFAULT_FAVICON
);
273 bool AwContentBrowserClient::AllowAppCache(const GURL
& manifest_url
,
274 const GURL
& first_party
,
275 content::ResourceContext
* context
) {
276 // WebView doesn't have a per-site policy for locally stored data,
277 // instead AppCache can be disabled for individual WebViews.
282 bool AwContentBrowserClient::AllowGetCookie(const GURL
& url
,
283 const GURL
& first_party
,
284 const net::CookieList
& cookie_list
,
285 content::ResourceContext
* context
,
286 int render_process_id
,
287 int render_frame_id
) {
288 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url
,
296 bool AwContentBrowserClient::AllowSetCookie(const GURL
& url
,
297 const GURL
& first_party
,
298 const std::string
& cookie_line
,
299 content::ResourceContext
* context
,
300 int render_process_id
,
302 net::CookieOptions
* options
) {
303 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url
,
312 bool AwContentBrowserClient::AllowWorkerDatabase(
314 const base::string16
& name
,
315 const base::string16
& display_name
,
316 unsigned long estimated_size
,
317 content::ResourceContext
* context
,
318 const std::vector
<std::pair
<int, int> >& render_frames
) {
319 // Android WebView does not yet support web workers.
323 bool AwContentBrowserClient::AllowWorkerFileSystem(
325 content::ResourceContext
* context
,
326 const std::vector
<std::pair
<int, int> >& render_frames
) {
327 // Android WebView does not yet support web workers.
331 bool AwContentBrowserClient::AllowWorkerIndexedDB(
333 const base::string16
& name
,
334 content::ResourceContext
* context
,
335 const std::vector
<std::pair
<int, int> >& render_frames
) {
336 // Android WebView does not yet support web workers.
340 content::QuotaPermissionContext
*
341 AwContentBrowserClient::CreateQuotaPermissionContext() {
342 return new AwQuotaPermissionContext
;
345 void AwContentBrowserClient::AllowCertificateError(
346 int render_process_id
,
349 const net::SSLInfo
& ssl_info
,
350 const GURL
& request_url
,
351 ResourceType::Type resource_type
,
353 bool strict_enforcement
,
354 const base::Callback
<void(bool)>& callback
,
355 content::CertificateRequestResultType
* result
) {
356 AwContentsClientBridgeBase
* client
=
357 AwContentsClientBridgeBase::FromID(render_process_id
, render_frame_id
);
358 bool cancel_request
= true;
360 client
->AllowCertificateError(cert_error
,
366 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY
;
369 void AwContentBrowserClient::SelectClientCertificate(
370 int render_process_id
,
372 const net::HttpNetworkSession
* network_session
,
373 net::SSLCertRequestInfo
* cert_request_info
,
374 const base::Callback
<void(net::X509Certificate
*)>& callback
) {
375 AwContentsClientBridgeBase
* client
=
376 AwContentsClientBridgeBase::FromID(render_process_id
, render_frame_id
);
378 client
->SelectClientCertificate(cert_request_info
, callback
);
384 blink::WebNotificationPresenter::Permission
385 AwContentBrowserClient::CheckDesktopNotificationPermission(
386 const GURL
& source_url
,
387 content::ResourceContext
* context
,
388 int render_process_id
) {
389 // Android WebView does not support notifications, so return Denied here.
390 return blink::WebNotificationPresenter::PermissionDenied
;
393 void AwContentBrowserClient::ShowDesktopNotification(
394 const content::ShowDesktopNotificationHostMsgParams
& params
,
395 content::RenderFrameHost
* render_frame_host
,
396 content::DesktopNotificationDelegate
* delegate
,
397 base::Closure
* cancel_callback
) {
398 NOTREACHED() << "Android WebView does not support desktop notifications.";
401 void AwContentBrowserClient::RequestGeolocationPermission(
402 content::WebContents
* web_contents
,
404 const GURL
& requesting_frame
,
406 base::Callback
<void(bool)> result_callback
,
407 base::Closure
* cancel_callback
) {
408 AwContentsClientBridgeBase
* client
=
409 AwContentsClientBridgeBase::FromWebContents(web_contents
);
411 client
->RequestGeolocationPermission(
412 web_contents
, requesting_frame
, result_callback
, cancel_callback
);
414 LOG(WARNING
) << "Failed to find the associated bridge for geolocation "
415 << "permission request.";
416 result_callback
.Run(false);
420 void AwContentBrowserClient::RequestMidiSysExPermission(
421 content::WebContents
* web_contents
,
423 const GURL
& requesting_frame
,
425 base::Callback
<void(bool)> result_callback
,
426 base::Closure
* cancel_callback
) {
427 // TODO(toyoshim): Android WebView is not supported yet.
428 // See http://crbug.com/339767.
429 result_callback
.Run(false);
432 void AwContentBrowserClient::RequestProtectedMediaIdentifierPermission(
433 content::WebContents
* web_contents
,
435 base::Callback
<void(bool)> result_callback
,
436 base::Closure
* cancel_callback
) {
437 int render_process_id
= web_contents
->GetRenderProcessHost()->GetID();
438 int render_view_id
= web_contents
->GetRenderViewHost()->GetRoutingID();
439 AwBrowserPermissionRequestDelegate
* delegate
=
440 AwBrowserPermissionRequestDelegate::FromID(render_process_id
,
442 if (delegate
== NULL
) {
443 DVLOG(0) << "Dropping ProtectedMediaIdentifierPermission request";
444 result_callback
.Run(false);
448 if (cancel_callback
) {
449 *cancel_callback
= base::Bind(
450 CancelProtectedMediaIdentifierPermissionRequests
,
451 render_process_id
, render_view_id
, origin
);
453 delegate
->RequestProtectedMediaIdentifierPermission(origin
, result_callback
);
456 bool AwContentBrowserClient::CanCreateWindow(
457 const GURL
& opener_url
,
458 const GURL
& opener_top_level_frame_url
,
459 const GURL
& source_origin
,
460 WindowContainerType container_type
,
461 const GURL
& target_url
,
462 const content::Referrer
& referrer
,
463 WindowOpenDisposition disposition
,
464 const blink::WebWindowFeatures
& features
,
466 bool opener_suppressed
,
467 content::ResourceContext
* context
,
468 int render_process_id
,
470 bool* no_javascript_access
) {
471 // We unconditionally allow popup windows at this stage and will give
472 // the embedder the opporunity to handle displaying of the popup in
473 // WebContentsDelegate::AddContents (via the
474 // AwContentsClient.onCreateWindow callback).
475 // Note that if the embedder has blocked support for creating popup
476 // windows through AwSettings, then we won't get to this point as
477 // the popup creation will have been blocked at the WebKit level.
478 if (no_javascript_access
) {
479 *no_javascript_access
= false;
484 std::string
AwContentBrowserClient::GetWorkerProcessTitle(const GURL
& url
,
485 content::ResourceContext
* context
) {
486 NOTREACHED() << "Android WebView does not yet support web workers.";
487 return std::string();
491 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
492 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
495 net::NetLog
* AwContentBrowserClient::GetNetLog() {
496 // TODO(boliu): Implement AwNetLog.
500 content::AccessTokenStore
* AwContentBrowserClient::CreateAccessTokenStore() {
501 return new AwAccessTokenStore();
504 bool AwContentBrowserClient::IsFastShutdownPossible() {
505 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
506 << " should never be called";
510 void AwContentBrowserClient::UpdateInspectorSetting(
511 content::RenderViewHost
* rvh
,
512 const std::string
& key
,
513 const std::string
& value
) {
514 // TODO(boliu): Implement persisting inspector settings.
518 void AwContentBrowserClient::ClearCache(content::RenderViewHost
* rvh
) {
519 RemoveHttpDiskCache(rvh
->GetProcess()->GetBrowserContext(),
520 rvh
->GetProcess()->GetID());
523 void AwContentBrowserClient::ClearCookies(content::RenderViewHost
* rvh
) {
524 // TODO(boliu): Implement.
528 base::FilePath
AwContentBrowserClient::GetDefaultDownloadDirectory() {
529 // Android WebView does not currently use the Chromium downloads system.
530 // Download requests are cancelled immedately when recognized; see
531 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
532 // download system still tries to start up and calls this before recognizing
533 // the request has been cancelled.
534 return base::FilePath();
537 std::string
AwContentBrowserClient::GetDefaultDownloadName() {
538 NOTREACHED() << "Android WebView does not use chromium downloads";
539 return std::string();
542 void AwContentBrowserClient::DidCreatePpapiPlugin(
543 content::BrowserPpapiHost
* browser_host
) {
544 NOTREACHED() << "Android WebView does not support plugins";
547 bool AwContentBrowserClient::AllowPepperSocketAPI(
548 content::BrowserContext
* browser_context
,
551 const content::SocketPermissionRequest
* params
) {
552 NOTREACHED() << "Android WebView does not support plugins";
556 void AwContentBrowserClient::OverrideWebkitPrefs(content::RenderViewHost
* rvh
,
558 WebPreferences
* web_prefs
) {
559 if (!preferences_populater_
.get()) {
560 preferences_populater_
= make_scoped_ptr(native_factory_
->
561 CreateWebPreferencesPopulater());
563 preferences_populater_
->PopulateFor(
564 content::WebContents::FromRenderViewHost(rvh
), web_prefs
);
567 #if defined(VIDEO_HOLE)
568 content::ExternalVideoSurfaceContainer
*
569 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
570 content::WebContents
* web_contents
) {
571 return native_factory_
->CreateExternalVideoSurfaceContainer(web_contents
);
575 } // namespace android_webview