API migration: picture.draw -> picture.playback
[chromium-blink-merge.git] / android_webview / browser / aw_content_browser_client.cc
blob8af695fb76d44e340c68422753d79263de764e58
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/android/locale_utils.h"
23 #include "base/base_paths_android.h"
24 #include "base/path_service.h"
25 #include "components/cdm/browser/cdm_message_filter_android.h"
26 #include "content/public/browser/access_token_store.h"
27 #include "content/public/browser/browser_message_filter.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/child_process_security_policy.h"
30 #include "content/public/browser/permission_type.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/url_constants.h"
35 #include "content/public/common/web_preferences.h"
36 #include "net/android/network_library.h"
37 #include "net/ssl/ssl_cert_request_info.h"
38 #include "net/ssl/ssl_info.h"
39 #include "ui/base/resource/resource_bundle.h"
40 #include "ui/resources/grit/ui_resources.h"
42 using content::BrowserThread;
43 using content::ResourceType;
45 namespace android_webview {
46 namespace {
48 // TODO(sgurun) move this to its own file.
49 // This class filters out incoming aw_contents related IPC messages for the
50 // renderer process on the IPC thread.
51 class AwContentsMessageFilter : public content::BrowserMessageFilter {
52 public:
53 explicit AwContentsMessageFilter(int process_id);
55 // BrowserMessageFilter methods.
56 virtual void OverrideThreadForMessage(
57 const IPC::Message& message,
58 BrowserThread::ID* thread) override;
59 virtual bool OnMessageReceived(
60 const IPC::Message& message) override;
62 void OnShouldOverrideUrlLoading(int routing_id,
63 const base::string16& url,
64 bool* ignore_navigation);
65 void OnSubFrameCreated(int parent_render_frame_id, int child_render_frame_id);
67 private:
68 virtual ~AwContentsMessageFilter();
70 int process_id_;
72 DISALLOW_COPY_AND_ASSIGN(AwContentsMessageFilter);
75 AwContentsMessageFilter::AwContentsMessageFilter(int process_id)
76 : BrowserMessageFilter(AndroidWebViewMsgStart),
77 process_id_(process_id) {
80 AwContentsMessageFilter::~AwContentsMessageFilter() {
83 void AwContentsMessageFilter::OverrideThreadForMessage(
84 const IPC::Message& message, BrowserThread::ID* thread) {
85 if (message.type() == AwViewHostMsg_ShouldOverrideUrlLoading::ID) {
86 *thread = BrowserThread::UI;
90 bool AwContentsMessageFilter::OnMessageReceived(const IPC::Message& message) {
91 bool handled = true;
92 IPC_BEGIN_MESSAGE_MAP(AwContentsMessageFilter, message)
93 IPC_MESSAGE_HANDLER(AwViewHostMsg_ShouldOverrideUrlLoading,
94 OnShouldOverrideUrlLoading)
95 IPC_MESSAGE_HANDLER(AwViewHostMsg_SubFrameCreated, OnSubFrameCreated)
96 IPC_MESSAGE_UNHANDLED(handled = false)
97 IPC_END_MESSAGE_MAP()
98 return handled;
101 void AwContentsMessageFilter::OnShouldOverrideUrlLoading(
102 int render_frame_id,
103 const base::string16& url,
104 bool* ignore_navigation) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
106 *ignore_navigation = false;
107 AwContentsClientBridgeBase* client =
108 AwContentsClientBridgeBase::FromID(process_id_, render_frame_id);
109 if (client) {
110 *ignore_navigation = client->ShouldOverrideUrlLoading(url);
111 } else {
112 LOG(WARNING) << "Failed to find the associated render view host for url: "
113 << url;
117 void AwContentsMessageFilter::OnSubFrameCreated(int parent_render_frame_id,
118 int child_render_frame_id) {
119 AwContentsIoThreadClient::SubFrameCreated(
120 process_id_, parent_render_frame_id, child_render_frame_id);
123 class AwAccessTokenStore : public content::AccessTokenStore {
124 public:
125 AwAccessTokenStore() { }
127 // content::AccessTokenStore implementation
128 virtual void LoadAccessTokens(
129 const LoadAccessTokensCallbackType& request) override {
130 AccessTokenStore::AccessTokenSet access_token_set;
131 // AccessTokenSet and net::URLRequestContextGetter not used on Android,
132 // but Run needs to be called to finish the geolocation setup.
133 request.Run(access_token_set, NULL);
135 virtual void SaveAccessToken(const GURL& server_url,
136 const base::string16& access_token) override { }
138 private:
139 virtual ~AwAccessTokenStore() { }
141 DISALLOW_COPY_AND_ASSIGN(AwAccessTokenStore);
144 } // namespace
146 std::string AwContentBrowserClient::GetAcceptLangsImpl() {
147 // Start with the currnet locale.
148 std::string langs = base::android::GetDefaultLocale();
150 // If we're not en-US, add in en-US which will be
151 // used with a lower q-value.
152 if (base::StringToLowerASCII(langs) != "en-us") {
153 langs += ",en-US";
155 return langs;
158 AwBrowserContext* AwContentBrowserClient::GetAwBrowserContext() {
159 return AwBrowserContext::GetDefault();
162 AwContentBrowserClient::AwContentBrowserClient(
163 JniDependencyFactory* native_factory)
164 : native_factory_(native_factory) {
165 base::FilePath user_data_dir;
166 if (!PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir)) {
167 NOTREACHED() << "Failed to get app data directory for Android WebView";
169 browser_context_.reset(
170 new AwBrowserContext(user_data_dir, native_factory_));
173 AwContentBrowserClient::~AwContentBrowserClient() {
176 void AwContentBrowserClient::AddCertificate(net::CertificateMimeType cert_type,
177 const void* cert_data,
178 size_t cert_size,
179 int render_process_id,
180 int render_frame_id) {
181 if (cert_size > 0)
182 net::android::StoreCertificate(cert_type, cert_data, cert_size);
185 content::BrowserMainParts* AwContentBrowserClient::CreateBrowserMainParts(
186 const content::MainFunctionParams& parameters) {
187 return new AwBrowserMainParts(browser_context_.get());
190 content::WebContentsViewDelegate*
191 AwContentBrowserClient::GetWebContentsViewDelegate(
192 content::WebContents* web_contents) {
193 return native_factory_->CreateViewDelegate(web_contents);
196 void AwContentBrowserClient::RenderProcessWillLaunch(
197 content::RenderProcessHost* host) {
198 // If WebView becomes multi-process capable, this may be insecure.
199 // More benefit can be derived from the ChildProcessSecurotyPolicy by
200 // deferring the GrantScheme calls until we know that a given child process
201 // really does need that priviledge. Check here to ensure we rethink this
202 // when the time comes. See crbug.com/156062.
203 CHECK(content::RenderProcessHost::run_renderer_in_process());
205 // Grant content: and file: scheme to the whole process, since we impose
206 // per-view access checks.
207 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
208 host->GetID(), android_webview::kContentScheme);
209 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
210 host->GetID(), url::kFileScheme);
212 host->AddFilter(new AwContentsMessageFilter(host->GetID()));
213 host->AddFilter(new cdm::CdmMessageFilterAndroid());
216 net::URLRequestContextGetter* AwContentBrowserClient::CreateRequestContext(
217 content::BrowserContext* browser_context,
218 content::ProtocolHandlerMap* protocol_handlers,
219 content::URLRequestInterceptorScopedVector request_interceptors) {
220 DCHECK_EQ(browser_context_.get(), browser_context);
221 return browser_context_->CreateRequestContext(protocol_handlers,
222 request_interceptors.Pass());
225 net::URLRequestContextGetter*
226 AwContentBrowserClient::CreateRequestContextForStoragePartition(
227 content::BrowserContext* browser_context,
228 const base::FilePath& partition_path,
229 bool in_memory,
230 content::ProtocolHandlerMap* protocol_handlers,
231 content::URLRequestInterceptorScopedVector request_interceptors) {
232 DCHECK_EQ(browser_context_.get(), browser_context);
233 // TODO(mkosiba,kinuko): request_interceptors should be hooked up in the
234 // downstream. (crbug.com/350286)
235 return browser_context_->CreateRequestContextForStoragePartition(
236 partition_path, in_memory, protocol_handlers,
237 request_interceptors.Pass());
240 std::string AwContentBrowserClient::GetCanonicalEncodingNameByAliasName(
241 const std::string& alias_name) {
242 return alias_name;
245 void AwContentBrowserClient::AppendExtraCommandLineSwitches(
246 base::CommandLine* command_line,
247 int child_process_id) {
248 NOTREACHED() << "Android WebView does not support multi-process yet";
251 std::string AwContentBrowserClient::GetApplicationLocale() {
252 return base::android::GetDefaultLocale();
255 std::string AwContentBrowserClient::GetAcceptLangs(
256 content::BrowserContext* context) {
257 return GetAcceptLangsImpl();
260 const gfx::ImageSkia* AwContentBrowserClient::GetDefaultFavicon() {
261 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
262 // TODO(boliu): Bundle our own default favicon?
263 return rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
266 bool AwContentBrowserClient::AllowAppCache(const GURL& manifest_url,
267 const GURL& first_party,
268 content::ResourceContext* context) {
269 // WebView doesn't have a per-site policy for locally stored data,
270 // instead AppCache can be disabled for individual WebViews.
271 return true;
275 bool AwContentBrowserClient::AllowGetCookie(const GURL& url,
276 const GURL& first_party,
277 const net::CookieList& cookie_list,
278 content::ResourceContext* context,
279 int render_process_id,
280 int render_frame_id) {
281 return AwCookieAccessPolicy::GetInstance()->AllowGetCookie(url,
282 first_party,
283 cookie_list,
284 context,
285 render_process_id,
286 render_frame_id);
289 bool AwContentBrowserClient::AllowSetCookie(const GURL& url,
290 const GURL& first_party,
291 const std::string& cookie_line,
292 content::ResourceContext* context,
293 int render_process_id,
294 int render_frame_id,
295 net::CookieOptions* options) {
296 return AwCookieAccessPolicy::GetInstance()->AllowSetCookie(url,
297 first_party,
298 cookie_line,
299 context,
300 render_process_id,
301 render_frame_id,
302 options);
305 bool AwContentBrowserClient::AllowWorkerDatabase(
306 const GURL& url,
307 const base::string16& name,
308 const base::string16& display_name,
309 unsigned long estimated_size,
310 content::ResourceContext* context,
311 const std::vector<std::pair<int, int> >& render_frames) {
312 // Android WebView does not yet support web workers.
313 return false;
316 void AwContentBrowserClient::AllowWorkerFileSystem(
317 const GURL& url,
318 content::ResourceContext* context,
319 const std::vector<std::pair<int, int> >& render_frames,
320 base::Callback<void(bool)> callback) {
321 // Android WebView does not yet support web workers.
322 callback.Run(false);
325 bool AwContentBrowserClient::AllowWorkerIndexedDB(
326 const GURL& url,
327 const base::string16& name,
328 content::ResourceContext* context,
329 const std::vector<std::pair<int, int> >& render_frames) {
330 // Android WebView does not yet support web workers.
331 return false;
334 content::QuotaPermissionContext*
335 AwContentBrowserClient::CreateQuotaPermissionContext() {
336 return new AwQuotaPermissionContext;
339 void AwContentBrowserClient::AllowCertificateError(
340 int render_process_id,
341 int render_frame_id,
342 int cert_error,
343 const net::SSLInfo& ssl_info,
344 const GURL& request_url,
345 ResourceType resource_type,
346 bool overridable,
347 bool strict_enforcement,
348 bool expired_previous_decision,
349 const base::Callback<void(bool)>& callback,
350 content::CertificateRequestResultType* result) {
351 AwContentsClientBridgeBase* client =
352 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
353 bool cancel_request = true;
354 if (client)
355 client->AllowCertificateError(cert_error,
356 ssl_info.cert.get(),
357 request_url,
358 callback,
359 &cancel_request);
360 if (cancel_request)
361 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
364 void AwContentBrowserClient::SelectClientCertificate(
365 int render_process_id,
366 int render_frame_id,
367 net::SSLCertRequestInfo* cert_request_info,
368 const base::Callback<void(net::X509Certificate*)>& callback) {
369 AwContentsClientBridgeBase* client =
370 AwContentsClientBridgeBase::FromID(render_process_id, render_frame_id);
371 if (client) {
372 client->SelectClientCertificate(cert_request_info, callback);
373 } else {
374 callback.Run(NULL);
378 void AwContentBrowserClient::RequestPermission(
379 content::PermissionType permission,
380 content::WebContents* web_contents,
381 int bridge_id,
382 const GURL& requesting_frame,
383 bool user_gesture,
384 const base::Callback<void(bool)>& result_callback) {
385 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
386 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
387 GURL origin = requesting_frame.GetOrigin();
388 AwBrowserPermissionRequestDelegate* delegate =
389 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
390 render_view_id);
391 switch (permission) {
392 case content::PERMISSION_GEOLOCATION:
393 if (!delegate) {
394 DVLOG(0) << "Dropping GeolocationPermission request";
395 result_callback.Run(false);
396 return;
398 delegate->RequestGeolocationPermission(origin, result_callback);
399 break;
400 case content::PERMISSION_PROTECTED_MEDIA:
401 if (!delegate) {
402 DVLOG(0) << "Dropping ProtectedMediaIdentifierPermission request";
403 result_callback.Run(false);
404 return;
406 delegate->RequestProtectedMediaIdentifierPermission(origin,
407 result_callback);
408 break;
409 case content::PERMISSION_MIDI_SYSEX:
410 case content::PERMISSION_NOTIFICATIONS:
411 case content::PERMISSION_PUSH_MESSAGING:
412 NOTIMPLEMENTED() << "RequestPermission not implemented for "
413 << permission;
414 break;
415 case content::PERMISSION_NUM:
416 NOTREACHED() << "Invalid RequestPermission for " << permission;
417 break;
421 void AwContentBrowserClient::CancelPermissionRequest(
422 content::PermissionType permission,
423 content::WebContents* web_contents,
424 int bridge_id,
425 const GURL& origin) {
426 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
427 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
428 AwBrowserPermissionRequestDelegate* delegate =
429 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
430 render_view_id);
431 if (!delegate)
432 return;
433 switch (permission) {
434 case content::PERMISSION_GEOLOCATION:
435 delegate->CancelGeolocationPermissionRequests(origin);
436 break;
437 case content::PERMISSION_PROTECTED_MEDIA:
438 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin);
439 break;
440 case content::PERMISSION_MIDI_SYSEX:
441 case content::PERMISSION_NOTIFICATIONS:
442 case content::PERMISSION_PUSH_MESSAGING:
443 NOTIMPLEMENTED() << "CancelPermission not implemented for " << permission;
444 break;
445 case content::PERMISSION_NUM:
446 NOTREACHED() << "Invalid CancelPermission for " << permission;
447 break;
451 bool AwContentBrowserClient::CanCreateWindow(
452 const GURL& opener_url,
453 const GURL& opener_top_level_frame_url,
454 const GURL& source_origin,
455 WindowContainerType container_type,
456 const GURL& target_url,
457 const content::Referrer& referrer,
458 WindowOpenDisposition disposition,
459 const blink::WebWindowFeatures& features,
460 bool user_gesture,
461 bool opener_suppressed,
462 content::ResourceContext* context,
463 int render_process_id,
464 int opener_id,
465 bool* no_javascript_access) {
466 // We unconditionally allow popup windows at this stage and will give
467 // the embedder the opporunity to handle displaying of the popup in
468 // WebContentsDelegate::AddContents (via the
469 // AwContentsClient.onCreateWindow callback).
470 // Note that if the embedder has blocked support for creating popup
471 // windows through AwSettings, then we won't get to this point as
472 // the popup creation will have been blocked at the WebKit level.
473 if (no_javascript_access) {
474 *no_javascript_access = false;
476 return true;
479 void AwContentBrowserClient::ResourceDispatcherHostCreated() {
480 AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated();
483 net::NetLog* AwContentBrowserClient::GetNetLog() {
484 return browser_context_->GetAwURLRequestContext()->GetNetLog();
487 content::AccessTokenStore* AwContentBrowserClient::CreateAccessTokenStore() {
488 return new AwAccessTokenStore();
491 bool AwContentBrowserClient::IsFastShutdownPossible() {
492 NOTREACHED() << "Android WebView is single process, so IsFastShutdownPossible"
493 << " should never be called";
494 return false;
497 void AwContentBrowserClient::ClearCache(content::RenderViewHost* rvh) {
498 RemoveHttpDiskCache(rvh->GetProcess()->GetBrowserContext(),
499 rvh->GetProcess()->GetID());
502 void AwContentBrowserClient::ClearCookies(content::RenderViewHost* rvh) {
503 // TODO(boliu): Implement.
504 NOTIMPLEMENTED();
507 base::FilePath AwContentBrowserClient::GetDefaultDownloadDirectory() {
508 // Android WebView does not currently use the Chromium downloads system.
509 // Download requests are cancelled immedately when recognized; see
510 // AwResourceDispatcherHost::CreateResourceHandlerForDownload. However the
511 // download system still tries to start up and calls this before recognizing
512 // the request has been cancelled.
513 return base::FilePath();
516 std::string AwContentBrowserClient::GetDefaultDownloadName() {
517 NOTREACHED() << "Android WebView does not use chromium downloads";
518 return std::string();
521 void AwContentBrowserClient::DidCreatePpapiPlugin(
522 content::BrowserPpapiHost* browser_host) {
523 NOTREACHED() << "Android WebView does not support plugins";
526 bool AwContentBrowserClient::AllowPepperSocketAPI(
527 content::BrowserContext* browser_context,
528 const GURL& url,
529 bool private_api,
530 const content::SocketPermissionRequest* params) {
531 NOTREACHED() << "Android WebView does not support plugins";
532 return false;
535 void AwContentBrowserClient::OverrideWebkitPrefs(
536 content::RenderViewHost* rvh,
537 const GURL& url,
538 content::WebPreferences* web_prefs) {
539 if (!preferences_populater_.get()) {
540 preferences_populater_ = make_scoped_ptr(native_factory_->
541 CreateWebPreferencesPopulater());
543 preferences_populater_->PopulateFor(
544 content::WebContents::FromRenderViewHost(rvh), web_prefs);
547 #if defined(VIDEO_HOLE)
548 content::ExternalVideoSurfaceContainer*
549 AwContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
550 content::WebContents* web_contents) {
551 return native_factory_->CreateExternalVideoSurfaceContainer(web_contents);
553 #endif
555 content::DevToolsManagerDelegate*
556 AwContentBrowserClient::GetDevToolsManagerDelegate() {
557 return new AwDevToolsManagerDelegate();
560 } // namespace android_webview