| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/android/download_controller_android_impl.h" | 5 #include "content/browser/android/download_controller_android_impl.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "net/http/http_response_headers.h" | 36 #include "net/http/http_response_headers.h" |
| 37 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
| 38 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
| 39 | 39 |
| 40 using base::android::ConvertUTF8ToJavaString; | 40 using base::android::ConvertUTF8ToJavaString; |
| 41 using base::android::ScopedJavaLocalRef; | 41 using base::android::ScopedJavaLocalRef; |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 // Guards download_controller_ | 44 // Guards download_controller_ |
| 45 base::LazyInstance<base::Lock> g_download_controller_lock_; | 45 base::LazyInstance<base::Lock> g_download_controller_lock_; |
| 46 |
| 47 content::WebContents* GetWebContents(int render_process_id, |
| 48 int render_view_id) { |
| 49 content::RenderViewHost* render_view_host = |
| 50 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 51 |
| 52 if (!render_view_host) |
| 53 return nullptr; |
| 54 |
| 55 return content::WebContents::FromRenderViewHost(render_view_host); |
| 46 } | 56 } |
| 47 | 57 |
| 58 void CreateContextMenuDownload(int render_process_id, |
| 59 int render_view_id, |
| 60 const content::ContextMenuParams& params, |
| 61 bool is_link, |
| 62 const std::string& extra_headers, |
| 63 bool granted) { |
| 64 if (!granted) |
| 65 return; |
| 66 |
| 67 content::WebContents* web_contents = |
| 68 GetWebContents(render_process_id, render_view_id); |
| 69 if (!web_contents) |
| 70 return; |
| 71 |
| 72 const GURL& url = is_link ? params.link_url : params.src_url; |
| 73 const GURL& referring_url = |
| 74 params.frame_url.is_empty() ? params.page_url : params.frame_url; |
| 75 content::DownloadManagerImpl* dlm = |
| 76 static_cast<content::DownloadManagerImpl*>( |
| 77 content::BrowserContext::GetDownloadManager( |
| 78 web_contents->GetBrowserContext())); |
| 79 scoped_ptr<content::DownloadUrlParameters> dl_params( |
| 80 content::DownloadUrlParameters::FromWebContents(web_contents, url)); |
| 81 content::Referrer referrer = content::Referrer::SanitizeForRequest( |
| 82 url, |
| 83 content::Referrer(referring_url.GetAsReferrer(), params.referrer_policy)); |
| 84 dl_params->set_referrer(referrer); |
| 85 if (is_link) |
| 86 dl_params->set_referrer_encoding(params.frame_charset); |
| 87 net::HttpRequestHeaders headers; |
| 88 headers.AddHeadersFromString(extra_headers); |
| 89 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) |
| 90 dl_params->add_request_header(it.name(), it.value()); |
| 91 if (!is_link && extra_headers.empty()) |
| 92 dl_params->set_prefer_cache(true); |
| 93 dl_params->set_prompt(false); |
| 94 dlm->DownloadUrl(dl_params.Pass()); |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 |
| 48 namespace content { | 99 namespace content { |
| 49 | 100 |
| 50 // JNI methods | 101 // JNI methods |
| 51 static void Init(JNIEnv* env, jobject obj) { | 102 static void Init(JNIEnv* env, jobject obj) { |
| 52 DownloadControllerAndroidImpl::GetInstance()->Init(env, obj); | 103 DownloadControllerAndroidImpl::GetInstance()->Init(env, obj); |
| 53 } | 104 } |
| 54 | 105 |
| 55 static void OnRequestFileAccessResult( | 106 static void OnRequestFileAccessResult( |
| 56 JNIEnv* env, jobject obj, jlong callback_id, jboolean granted) { | 107 JNIEnv* env, jobject obj, jlong callback_id, jboolean granted) { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 for (auto iter = deferred_downloads_.begin(); | 172 for (auto iter = deferred_downloads_.begin(); |
| 122 iter != deferred_downloads_.end(); ++iter) { | 173 iter != deferred_downloads_.end(); ++iter) { |
| 123 if (*iter == observer) { | 174 if (*iter == observer) { |
| 124 deferred_downloads_.erase(iter); | 175 deferred_downloads_.erase(iter); |
| 125 return; | 176 return; |
| 126 } | 177 } |
| 127 } | 178 } |
| 128 } | 179 } |
| 129 | 180 |
| 130 void DownloadControllerAndroidImpl::AcquireFileAccessPermission( | 181 void DownloadControllerAndroidImpl::AcquireFileAccessPermission( |
| 131 WebContents* web_contents, | 182 int render_process_id, |
| 183 int render_view_id, |
| 132 const DownloadControllerAndroid::AcquireFileAccessPermissionCallback& cb) { | 184 const DownloadControllerAndroid::AcquireFileAccessPermissionCallback& cb) { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 186 WebContents* web_contents = GetWebContents(render_process_id, render_view_id); |
| 134 if (!web_contents) { | 187 if (!web_contents) { |
| 135 BrowserThread::PostTask( | 188 BrowserThread::PostTask( |
| 136 BrowserThread::UI, FROM_HERE, base::Bind(cb, false)); | 189 BrowserThread::UI, FROM_HERE, base::Bind(cb, false)); |
| 137 return; | 190 return; |
| 138 } | 191 } |
| 139 | 192 |
| 140 ScopedJavaLocalRef<jobject> view = | 193 ScopedJavaLocalRef<jobject> view = |
| 141 GetContentViewCoreFromWebContents(web_contents); | 194 GetContentViewCoreFromWebContents(web_contents); |
| 142 if (view.is_null()) { | 195 if (view.is_null()) { |
| 143 BrowserThread::PostTask( | 196 BrowserThread::PostTask( |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 // ContentViewCore is created. | 351 // ContentViewCore is created. |
| 299 deferred_downloads_.push_back(new DeferredDownloadObserver( | 352 deferred_downloads_.push_back(new DeferredDownloadObserver( |
| 300 web_contents, | 353 web_contents, |
| 301 base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownload, | 354 base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownload, |
| 302 base::Unretained(this), render_process_id, render_view_id, | 355 base::Unretained(this), render_process_id, render_view_id, |
| 303 info))); | 356 info))); |
| 304 return; | 357 return; |
| 305 } | 358 } |
| 306 | 359 |
| 307 AcquireFileAccessPermission( | 360 AcquireFileAccessPermission( |
| 308 web_contents, | 361 render_process_id, render_view_id, |
| 309 base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownloadInternal, | 362 base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownloadInternal, |
| 310 base::Unretained(this), render_process_id, render_view_id, | 363 base::Unretained(this), render_process_id, render_view_id, |
| 311 info)); | 364 info)); |
| 312 } | 365 } |
| 313 | 366 |
| 314 void DownloadControllerAndroidImpl::StartAndroidDownloadInternal( | 367 void DownloadControllerAndroidImpl::StartAndroidDownloadInternal( |
| 315 int render_process_id, int render_view_id, | 368 int render_process_id, int render_view_id, |
| 316 const DownloadInfoAndroid& info, bool allowed) { | 369 const DownloadInfoAndroid& info, bool allowed) { |
| 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 370 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 318 if (!allowed) | 371 if (!allowed) |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 env, item->GetTargetFilePath().BaseName().value()); | 495 env, item->GetTargetFilePath().BaseName().value()); |
| 443 ScopedJavaLocalRef<jobject> view_core = GetContentViewCoreFromWebContents( | 496 ScopedJavaLocalRef<jobject> view_core = GetContentViewCoreFromWebContents( |
| 444 item->GetWebContents()); | 497 item->GetWebContents()); |
| 445 if (!view_core.is_null()) { | 498 if (!view_core.is_null()) { |
| 446 Java_DownloadController_onDangerousDownload( | 499 Java_DownloadController_onDangerousDownload( |
| 447 env, GetJavaObject()->Controller(env).obj(), view_core.obj(), | 500 env, GetJavaObject()->Controller(env).obj(), view_core.obj(), |
| 448 jfilename.obj(), item->GetId()); | 501 jfilename.obj(), item->GetId()); |
| 449 } | 502 } |
| 450 } | 503 } |
| 451 | 504 |
| 452 WebContents* DownloadControllerAndroidImpl::GetWebContents( | |
| 453 int render_process_id, int render_view_id) { | |
| 454 RenderViewHost* render_view_host = | |
| 455 RenderViewHost::FromID(render_process_id, render_view_id); | |
| 456 | |
| 457 if (!render_view_host) | |
| 458 return NULL; | |
| 459 | |
| 460 return render_view_host->GetDelegate()->GetAsWebContents(); | |
| 461 } | |
| 462 | |
| 463 ScopedJavaLocalRef<jobject> | 505 ScopedJavaLocalRef<jobject> |
| 464 DownloadControllerAndroidImpl::GetContentViewCoreFromWebContents( | 506 DownloadControllerAndroidImpl::GetContentViewCoreFromWebContents( |
| 465 WebContents* web_contents) { | 507 WebContents* web_contents) { |
| 466 if (!web_contents) | 508 if (!web_contents) |
| 467 return ScopedJavaLocalRef<jobject>(); | 509 return ScopedJavaLocalRef<jobject>(); |
| 468 | 510 |
| 469 ContentViewCore* view_core = ContentViewCore::FromWebContents(web_contents); | 511 ContentViewCore* view_core = ContentViewCore::FromWebContents(web_contents); |
| 470 return view_core ? view_core->GetJavaObject() : | 512 return view_core ? view_core->GetJavaObject() : |
| 471 ScopedJavaLocalRef<jobject>(); | 513 ScopedJavaLocalRef<jobject>(); |
| 472 } | 514 } |
| 473 | 515 |
| 474 DownloadControllerAndroidImpl::JavaObject* | 516 DownloadControllerAndroidImpl::JavaObject* |
| 475 DownloadControllerAndroidImpl::GetJavaObject() { | 517 DownloadControllerAndroidImpl::GetJavaObject() { |
| 476 if (!java_object_) { | 518 if (!java_object_) { |
| 477 // Initialize Java DownloadController by calling | 519 // Initialize Java DownloadController by calling |
| 478 // DownloadController.getInstance(), which will call Init() | 520 // DownloadController.getInstance(), which will call Init() |
| 479 // if Java DownloadController is not instantiated already. | 521 // if Java DownloadController is not instantiated already. |
| 480 JNIEnv* env = base::android::AttachCurrentThread(); | 522 JNIEnv* env = base::android::AttachCurrentThread(); |
| 481 Java_DownloadController_getInstance(env); | 523 Java_DownloadController_getInstance(env); |
| 482 } | 524 } |
| 483 | 525 |
| 484 DCHECK(java_object_); | 526 DCHECK(java_object_); |
| 485 return java_object_; | 527 return java_object_; |
| 486 } | 528 } |
| 487 | 529 |
| 488 void DownloadControllerAndroidImpl::StartContextMenuDownload( | 530 void DownloadControllerAndroidImpl::StartContextMenuDownload( |
| 489 const ContextMenuParams& params, WebContents* web_contents, bool is_link, | 531 const ContextMenuParams& params, WebContents* web_contents, bool is_link, |
| 490 const std::string& extra_headers) { | 532 const std::string& extra_headers) { |
| 491 const GURL& url = is_link ? params.link_url : params.src_url; | 533 int process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 492 const GURL& referring_url = params.frame_url.is_empty() ? | 534 int routing_id = web_contents->GetRoutingID(); |
| 493 params.page_url : params.frame_url; | 535 AcquireFileAccessPermission( |
| 494 DownloadManagerImpl* dlm = static_cast<DownloadManagerImpl*>( | 536 process_id, routing_id, |
| 495 BrowserContext::GetDownloadManager(web_contents->GetBrowserContext())); | 537 base::Bind(&CreateContextMenuDownload, process_id, routing_id, params, |
| 496 scoped_ptr<DownloadUrlParameters> dl_params( | 538 is_link, extra_headers)); |
| 497 DownloadUrlParameters::FromWebContents(web_contents, url)); | |
| 498 content::Referrer referrer = content::Referrer::SanitizeForRequest( | |
| 499 url, | |
| 500 content::Referrer(referring_url.GetAsReferrer(), | |
| 501 params.referrer_policy)); | |
| 502 dl_params->set_referrer(referrer); | |
| 503 if (is_link) | |
| 504 dl_params->set_referrer_encoding(params.frame_charset); | |
| 505 net::HttpRequestHeaders headers; | |
| 506 headers.AddHeadersFromString(extra_headers); | |
| 507 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) | |
| 508 dl_params->add_request_header(it.name(), it.value()); | |
| 509 if (!is_link && extra_headers.empty()) | |
| 510 dl_params->set_prefer_cache(true); | |
| 511 dl_params->set_prompt(false); | |
| 512 dlm->DownloadUrl(dl_params.Pass()); | |
| 513 } | 539 } |
| 514 | 540 |
| 515 void DownloadControllerAndroidImpl::DangerousDownloadValidated( | 541 void DownloadControllerAndroidImpl::DangerousDownloadValidated( |
| 516 WebContents* web_contents, int download_id, bool accept) { | 542 WebContents* web_contents, int download_id, bool accept) { |
| 517 if (!web_contents) | 543 if (!web_contents) |
| 518 return; | 544 return; |
| 519 DownloadManagerImpl* dlm = static_cast<DownloadManagerImpl*>( | 545 DownloadManagerImpl* dlm = static_cast<DownloadManagerImpl*>( |
| 520 BrowserContext::GetDownloadManager(web_contents->GetBrowserContext())); | 546 BrowserContext::GetDownloadManager(web_contents->GetBrowserContext())); |
| 521 DownloadItem* item = dlm->GetDownload(download_id); | 547 DownloadItem* item = dlm->GetDownload(download_id); |
| 522 if (!item) | 548 if (!item) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 540 if (user_agent.empty()) | 566 if (user_agent.empty()) |
| 541 user_agent = GetContentClient()->GetUserAgent(); | 567 user_agent = GetContentClient()->GetUserAgent(); |
| 542 GURL referer_url(request->referrer()); | 568 GURL referer_url(request->referrer()); |
| 543 if (referer_url.is_valid()) | 569 if (referer_url.is_valid()) |
| 544 referer = referer_url.spec(); | 570 referer = referer_url.spec(); |
| 545 if (!request->url_chain().empty()) { | 571 if (!request->url_chain().empty()) { |
| 546 original_url = request->url_chain().front(); | 572 original_url = request->url_chain().front(); |
| 547 url = request->url_chain().back(); | 573 url = request->url_chain().back(); |
| 548 } | 574 } |
| 549 | 575 |
| 550 const content::ResourceRequestInfo* info = | 576 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 551 content::ResourceRequestInfo::ForRequest(request); | |
| 552 if (info) | 577 if (info) |
| 553 has_user_gesture = info->HasUserGesture(); | 578 has_user_gesture = info->HasUserGesture(); |
| 554 } | 579 } |
| 555 | 580 |
| 556 DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {} | 581 DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {} |
| 557 | 582 |
| 558 } // namespace content | 583 } // namespace content |
| OLD | NEW |