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_dev_tools_manager_delegate.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/render_view_messages.h"
21 #include "android_webview/common/url_constants.h"
22 #include "base/base_paths_android.h"
23 #include "base/path_service.h"
24 #include "components/cdm/browser/cdm_message_filter_android.h"
25 #include "content/public/browser/access_token_store.h"
26 #include "content/public/browser/browser_message_filter.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/child_process_security_policy.h"
29 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/render_view_host.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/url_constants.h"
33 #include "content/public/common/web_preferences.h"
34 #include "net/android/network_library.h"
35 #include "net/ssl/ssl_cert_request_info.h"
36 #include "net/ssl/ssl_info.h"
37 #include "ui/base/l10n/l10n_util_android.h"
38 #include "ui/base/resource/resource_bundle.h"
39 #include "ui/resources/grit/ui_resources.h"
41 using content::BrowserThread;
42 using content::ResourceType;
44 namespace android_webview {
47 // TODO(sgurun) move this to its own file.
48 // This class filters out incoming aw_contents related IPC messages for the
49 // renderer process on the IPC thread.
50 class AwContentsMessageFilter : public content::BrowserMessageFilter {
52 explicit AwContentsMessageFilter(int process_id);
54 // BrowserMessageFilter methods.
55 virtual void OverrideThreadForMessage(
56 const IPC::Message& message,
57 BrowserThread::ID* thread) OVERRIDE;
58 virtual bool OnMessageReceived(
59 const IPC::Message& message) OVERRIDE;
61 void OnShouldOverrideUrlLoading(int routing_id,
62 const base::string16& url,
63 bool* ignore_navigation);
64 void OnSubFrameCreated(int parent_render_frame_id, int child_render_frame_id);
67 virtual ~AwContentsMessageFilter();
71 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter);
74 AwContentsMessageFilter::AwContentsMessageFilter(int process_id)
75 : BrowserMessageFilter(AndroidWebViewMsgStart),
76 process_id_(process_id) {
79 AwContentsMessageFilter::~AwContentsMessageFilter() {
82 void AwContentsMessageFilter::OverrideThreadForMessage(
83 const IPC::Message& message, BrowserThread::ID* thread) {
84 if (message.type() == AwViewHostMsg_ShouldOverrideUrlLoading::ID) {
85 *thread = BrowserThread::UI;
89 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message& message) {
91 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter, message)
92 IPC_MESSAGE_HANDLER(AwViewHostMsg_ShouldOverrideUrlLoading,
93 OnShouldOverrideUrlLoading)
94 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated, OnSubFrameCreated)
95 IPC_MESSAGE_UNHANDLED(handled = false)
100 void AwContentsMessageFilter::OnShouldOverrideUrlLoading(
102 const base::string16& url,
103 bool* ignore_navigation) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
105 *ignore_navigation = false;
106 AwContentsClientBridgeBase* client =
107 AwContentsClientBridgeBase::FromID(process_id_, render_frame_id);
109 *ignore_navigation = client->ShouldOverrideUrlLoading(url);
111 LOG(WARNING) << "Failed to find the associated render view host for url: "
116 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id,
117 int child_render_frame_id) {
118 AwContentsIoThreadClient::SubFrameCreated(
119 process_id_, parent_render_frame_id, child_render_frame_id);
122 class AwAccessTokenStore : public content::AccessTokenStore {
124 AwAccessTokenStore() { }
126 // content::AccessTokenStore implementation
127 virtual void LoadAccessTokens(
128 const LoadAccessTokensCallbackType& request) OVERRIDE {
129 AccessTokenStore::AccessTokenSet access_token_set;
130 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
131 // but Run needs to be called to finish the geolocation setup.
132 request.Run(access_token_set, NULL);
134 virtual void SaveAccessToken(const GURL& server_url,
135 const base::string16& access_token) OVERRIDE { }
138 virtual ~AwAccessTokenStore() { }
140 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore);
143 void CancelProtectedMediaIdentifierPermissionRequests(
144 int render_process_id,
146 const GURL& origin) {
147 AwBrowserPermissionRequestDelegate* delegate =
148 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
151 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin);
154 void CancelGeolocationPermissionRequests(
155 int render_process_id,
157 const GURL& origin) {
158 AwBrowserPermissionRequestDelegate* delegate =
159 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
162 delegate->CancelGeolocationPermissionRequests(origin);
167 std::string AwContentBrowserClient::GetAcceptLangsImpl() {
168 // Start with the currnet locale.
169 std::string langs = l10n_util::GetDefaultLocale();
171 // If we're not en-US, add in en-US which will be
172 // used with a lower q-value.
173 if (base::StringToLowerASCII(langs) != "en-us") {
179 AwBrowserContext* AwContentBrowserClient::GetAwBrowserContext() {
180 return AwBrowserContext::GetDefault();
183 AwContentBrowserClient::AwContentBrowserClient(
184 JniDependencyFactory* native_factory)
185 : native_factory_(native_factory) {
186 base::FilePath user_data_dir;
187 if (!PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir)) {
188 NOTREACHED() << "Failed to get app data directory for Android WebView";
190 browser_context_.reset(
191 new AwBrowserContext(user_data_dir, native_factory_));
194 AwContentBrowserClient::~AwContentBrowserClient() {
197 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type,
198 const void* cert_data,
200 int render_process_id,
201 int render_frame_id) {
203 net::android::StoreCertificate(cert_type, cert_data, cert_size);
206 content::BrowserMainParts* AwContentBrowserClient::CreateBrowserMainParts(
207 const content::MainFunctionParams& parameters) {
208 return new AwBrowserMainParts(browser_context_.get());
211 content::WebContentsViewDelegate*
212 AwContentBrowserClient::GetWebContentsViewDelegate(
213 content::WebContents* web_contents) {
214 return native_factory_->CreateViewDelegate(web_contents);
217 void AwContentBrowserClient::RenderProcessWillLaunch(
218 content::RenderProcessHost* host) {
219 // If WebView becomes multi-process capable, this may be insecure.
220 // More benefit can be derived from the ChildProcessSecurotyPolicy by
221 // deferring the GrantScheme calls until we know that a given child process
222 // really does need that priviledge. Check here to ensure we rethink this
223 // when the time comes. See crbug.com/156062.
224 CHECK(content::RenderProcessHost::run_renderer_in_process());
226 // Grant content: and file: scheme to the whole process, since we impose
227 // per-view access checks.
228 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
229 host->GetID(), android_webview::kContentScheme);
230 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
231 host->GetID(), url::kFileScheme);
233 host->AddFilter(new AwContentsMessageFilter(host->GetID()));
234 host->AddFilter(new cdm::CdmMessageFilterAndroid());
237 net::URLRequestContextGetter* AwContentBrowserClient::CreateRequestContext(
238 content::BrowserContext* browser_context,
239 content::ProtocolHandlerMap* protocol_handlers,
240 content::URLRequestInterceptorScopedVector request_interceptors) {
241 DCHECK_EQ(browser_context_.get(), browser_context);
242 return browser_context_->CreateRequestContext(protocol_handlers,
243 request_interceptors.Pass());
246 net::URLRequestContextGetter*
247 AwContentBrowserClient::CreateRequestContextForStoragePartition(
248 content::BrowserContext* browser_context,
249 const base::FilePath& partition_path,
251 content::ProtocolHandlerMap* protocol_handlers,
252 content::URLRequestInterceptorScopedVector request_interceptors) {
253 DCHECK_EQ(browser_context_.get(), browser_context);
254 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
255 // downstream. (crbug.com/350286)
256 return browser_context_->CreateRequestContextForStoragePartition(
257 partition_path, in_memory, protocol_handlers,
258 request_interceptors.Pass());
261 std::string AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
262 const std::string& alias_name) {
266 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
267 base::CommandLine* command_line,
268 int child_process_id) {
269 NOTREACHED() << "Android WebView does not support multi-process yet";
272 std::string AwContentBrowserClient::GetApplicationLocale() {
273 return l10n_util::GetDefaultLocale();
276 std::string AwContentBrowserClient::GetAcceptLangs(
277 content::BrowserContext* context) {
278 return GetAcceptLangsImpl();
281 const gfx::ImageSkia* AwContentBrowserClient::GetDefaultFavicon() {
282 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
283 // TODO(boliu): Bundle our own default favicon?
284 return rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
287 bool AwContentBrowserClient::AllowAppCache(const GURL& manifest_url,
288 const GURL& first_party,
289 content::ResourceContext* context) {
290 // WebView doesn't have a per-site policy for locally stored data,
291 // instead AppCache can be disabled for individual WebViews.
296 bool AwContentBrowserClient::AllowGetCookie(const GURL& url,
297 const GURL& first_party,
298 const net::CookieList& cookie_list,
299 content::ResourceContext* context,
300 int render_process_id,
301 int render_frame_id) {
302 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url,
310 bool AwContentBrowserClient::AllowSetCookie(const GURL& url,
311 const GURL& first_party,
312 const std::string& cookie_line,
313 content::ResourceContext* context,
314 int render_process_id,
316 net::CookieOptions* options) {
317 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url,
326 bool AwContentBrowserClient::AllowWorkerDatabase(
328 const base::string16& name,
329 const base::string16& display_name,
330 unsigned long estimated_size,
331 content::ResourceContext* context,
332 const std::vector<std::pair<int, int> >& render_frames) {
333 // Android WebView does not yet support web workers.
337 void AwContentBrowserClient::AllowWorkerFileSystem(
339 content::ResourceContext* context,
340 const std::vector<std::pair<int, int> >& render_frames,
341 base::Callback<void(bool)> callback) {
342 // Android WebView does not yet support web workers.
346 bool AwContentBrowserClient::AllowWorkerIndexedDB(
348 const base::string16& name,
349 content::ResourceContext* context,
350 const std::vector<std::pair<int, int> >& render_frames) {
351 // Android WebView does not yet support web workers.
355 content::QuotaPermissionContext*
356 AwContentBrowserClient::CreateQuotaPermissionContext() {
357 return new AwQuotaPermissionContext;
360 void AwContentBrowserClient::AllowCertificateError(
361 int render_process_id,
364 const net::SSLInfo& ssl_info,
365 const GURL& request_url,
366 ResourceType resource_type,
368 bool strict_enforcement,
369 bool expired_previous_decision,
370 const base::Callback<void(bool)>& callback,
371 content::CertificateRequestResultType* result) {
372 AwContentsClientBridgeBase* client =
373 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
374 bool cancel_request = true;
376 client->AllowCertificateError(cert_error,
382 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
385 void AwContentBrowserClient::SelectClientCertificate(
386 int render_process_id,
388 const net::HttpNetworkSession* network_session,
389 net::SSLCertRequestInfo* cert_request_info,
390 const base::Callback<void(net::X509Certificate*)>& callback) {
391 AwContentsClientBridgeBase* client =
392 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
394 client->SelectClientCertificate(cert_request_info, callback);
400 blink::WebNotificationPermission
401 AwContentBrowserClient::CheckDesktopNotificationPermission(
402 const GURL& source_url,
403 content::ResourceContext* context,
404 int render_process_id) {
405 // Android WebView does not support notifications, so return Denied here.
406 return blink::WebNotificationPermissionDenied;
409 void AwContentBrowserClient::ShowDesktopNotification(
410 const content::ShowDesktopNotificationHostMsgParams& params,
411 content::RenderFrameHost* render_frame_host,
412 scoped_ptr<content::DesktopNotificationDelegate> delegate,
413 base::Closure* cancel_callback) {
414 NOTREACHED() << "Android WebView does not support desktop notifications.";
417 void AwContentBrowserClient::RequestGeolocationPermission(
418 content::WebContents* web_contents,
420 const GURL& requesting_frame,
422 base::Callback<void(bool)> result_callback,
423 base::Closure* cancel_callback) {
424 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
425 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
426 AwBrowserPermissionRequestDelegate* delegate =
427 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
429 if (delegate == NULL) {
430 DVLOG(0) << "Dropping GeolocationPermission request";
431 result_callback.Run(false);
435 GURL origin = requesting_frame.GetOrigin();
436 if (cancel_callback) {
437 *cancel_callback = base::Bind(
438 CancelGeolocationPermissionRequests, render_process_id, render_view_id,
441 delegate->RequestGeolocationPermission(origin, result_callback);
444 void AwContentBrowserClient::RequestMidiSysExPermission(
445 content::WebContents* web_contents,
447 const GURL& requesting_frame,
449 base::Callback<void(bool)> result_callback,
450 base::Closure* cancel_callback) {
451 // TODO(toyoshim): Android WebView is not supported yet.
452 // See http://crbug.com/339767.
453 result_callback.Run(false);
456 void AwContentBrowserClient::RequestProtectedMediaIdentifierPermission(
457 content::WebContents* web_contents,
459 base::Callback<void(bool)> result_callback,
460 base::Closure* cancel_callback) {
461 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
462 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
463 AwBrowserPermissionRequestDelegate* delegate =
464 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
466 if (delegate == NULL) {
467 DVLOG(0) << "Dropping ProtectedMediaIdentifierPermission request";
468 result_callback.Run(false);
472 if (cancel_callback) {
473 *cancel_callback = base::Bind(
474 CancelProtectedMediaIdentifierPermissionRequests,
475 render_process_id, render_view_id, origin);
477 delegate->RequestProtectedMediaIdentifierPermission(origin, result_callback);
480 bool AwContentBrowserClient::CanCreateWindow(
481 const GURL& opener_url,
482 const GURL& opener_top_level_frame_url,
483 const GURL& source_origin,
484 WindowContainerType container_type,
485 const GURL& target_url,
486 const content::Referrer& referrer,
487 WindowOpenDisposition disposition,
488 const blink::WebWindowFeatures& features,
490 bool opener_suppressed,
491 content::ResourceContext* context,
492 int render_process_id,
494 bool* no_javascript_access) {
495 // We unconditionally allow popup windows at this stage and will give
496 // the embedder the opporunity to handle displaying of the popup in
497 // WebContentsDelegate::AddContents (via the
498 // AwContentsClient.onCreateWindow callback).
499 // Note that if the embedder has blocked support for creating popup
500 // windows through AwSettings, then we won't get to this point as
501 // the popup creation will have been blocked at the WebKit level.
502 if (no_javascript_access) {
503 *no_javascript_access = false;
508 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
509 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
512 net::NetLog* AwContentBrowserClient::GetNetLog() {
513 return browser_context_->GetAwURLRequestContext()->GetNetLog();
516 content::AccessTokenStore* AwContentBrowserClient::CreateAccessTokenStore() {
517 return new AwAccessTokenStore();
520 bool AwContentBrowserClient::IsFastShutdownPossible() {
521 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
522 << " should never be called";
526 void AwContentBrowserClient::ClearCache(content::RenderViewHost* rvh) {
527 RemoveHttpDiskCache(rvh->GetProcess()->GetBrowserContext(),
528 rvh->GetProcess()->GetID());
531 void AwContentBrowserClient::ClearCookies(content::RenderViewHost* rvh) {
532 // TODO(boliu): Implement.
536 base::FilePath AwContentBrowserClient::GetDefaultDownloadDirectory() {
537 // Android WebView does not currently use the Chromium downloads system.
538 // Download requests are cancelled immedately when recognized; see
539 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
540 // download system still tries to start up and calls this before recognizing
541 // the request has been cancelled.
542 return base::FilePath();
545 std::string AwContentBrowserClient::GetDefaultDownloadName() {
546 NOTREACHED() << "Android WebView does not use chromium downloads";
547 return std::string();
550 void AwContentBrowserClient::DidCreatePpapiPlugin(
551 content::BrowserPpapiHost* browser_host) {
552 NOTREACHED() << "Android WebView does not support plugins";
555 bool AwContentBrowserClient::AllowPepperSocketAPI(
556 content::BrowserContext* browser_context,
559 const content::SocketPermissionRequest* params) {
560 NOTREACHED() << "Android WebView does not support plugins";
564 void AwContentBrowserClient::OverrideWebkitPrefs(
565 content::RenderViewHost* rvh,
567 content::WebPreferences* web_prefs) {
568 if (!preferences_populater_.get()) {
569 preferences_populater_ = make_scoped_ptr(native_factory_->
570 CreateWebPreferencesPopulater());
572 preferences_populater_->PopulateFor(
573 content::WebContents::FromRenderViewHost(rvh), web_prefs);
576 #if defined(VIDEO_HOLE)
577 content::ExternalVideoSurfaceContainer*
578 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
579 content::WebContents* web_contents) {
580 return native_factory_->CreateExternalVideoSurfaceContainer(web_contents);
584 content::DevToolsManagerDelegate*
585 AwContentBrowserClient::GetDevToolsManagerDelegate() {
586 return new AwDevToolsManagerDelegate();
589 } // namespace android_webview