Chromium Code Reviews| Index: chrome/browser/android/download/download_controller_android_impl.cc |
| diff --git a/content/browser/android/download_controller_android_impl.cc b/chrome/browser/android/download/download_controller_android_impl.cc |
| similarity index 73% |
| rename from content/browser/android/download_controller_android_impl.cc |
| rename to chrome/browser/android/download/download_controller_android_impl.cc |
| index 874e47a361e1c8d3902820bf84da03321c1a2557..e9136ba112b5bdd381e2e0049ae08a3c262778fb 100644 |
| --- a/content/browser/android/download_controller_android_impl.cc |
| +++ b/chrome/browser/android/download/download_controller_android_impl.cc |
| @@ -2,7 +2,7 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "content/browser/android/download_controller_android_impl.h" |
| +#include "chrome/browser/android/download/download_controller_android_impl.h" |
| #include <memory> |
| #include <utility> |
| @@ -15,21 +15,17 @@ |
| #include "base/logging.h" |
| #include "base/synchronization/lock.h" |
| #include "base/time/time.h" |
| -#include "content/browser/android/content_view_core_impl.h" |
| -#include "content/browser/android/deferred_download_observer.h" |
| -#include "content/browser/download/download_item_impl.h" |
| -#include "content/browser/download/download_manager_impl.h" |
| -#include "content/browser/loader/resource_dispatcher_host_impl.h" |
| -#include "content/browser/renderer_host/render_process_host_impl.h" |
| -#include "content/browser/renderer_host/render_view_host_delegate.h" |
| -#include "content/browser/renderer_host/render_view_host_impl.h" |
| -#include "content/browser/web_contents/web_contents_impl.h" |
| +#include "chrome/browser/android/download/chrome_download_delegate.h" |
| +#include "chrome/common/chrome_content_client.h" |
| #include "content/public/browser/browser_context.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/download_manager.h" |
| #include "content/public/browser/download_url_parameters.h" |
| #include "content/public/browser/global_request_id.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/resource_dispatcher_host.h" |
| #include "content/public/browser/resource_request_info.h" |
| -#include "content/public/common/content_client.h" |
| #include "content/public/common/referrer.h" |
| #include "jni/DownloadController_jni.h" |
| #include "net/base/filename_util.h" |
| @@ -40,23 +36,33 @@ |
| #include "net/http/http_response_headers.h" |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_context.h" |
| +#include "ui/android/view_android.h" |
| +#include "ui/android/window_android.h" |
| using base::android::ConvertUTF8ToJavaString; |
| using base::android::ScopedJavaLocalRef; |
| +using content::BrowserContext; |
| +using content::BrowserThread; |
| +using content::ContextMenuParams; |
| +using content::DownloadItem; |
| +using content::DownloadManager; |
| +using content::GlobalRequestID; |
| +using content::ResourceDispatcherHost; |
| +using content::ResourceRequestInfo; |
| +using content::WebContents; |
| namespace { |
| // Guards download_controller_ |
| base::LazyInstance<base::Lock> g_download_controller_lock_; |
| -content::WebContents* GetWebContents(int render_process_id, |
| - int render_view_id) { |
| +WebContents* GetWebContents(int render_process_id, int render_view_id) { |
| content::RenderViewHost* render_view_host = |
| content::RenderViewHost::FromID(render_process_id, render_view_id); |
| if (!render_view_host) |
| return nullptr; |
| - return content::WebContents::FromRenderViewHost(render_view_host); |
| + return WebContents::FromRenderViewHost(render_view_host); |
| } |
| void CreateContextMenuDownload(int render_process_id, |
| @@ -76,10 +82,9 @@ void CreateContextMenuDownload(int render_process_id, |
| const GURL& url = is_link ? params.link_url : params.src_url; |
| const GURL& referring_url = |
| params.frame_url.is_empty() ? params.page_url : params.frame_url; |
| - content::DownloadManagerImpl* dlm = |
| - static_cast<content::DownloadManagerImpl*>( |
| - content::BrowserContext::GetDownloadManager( |
| - web_contents->GetBrowserContext())); |
| + content::DownloadManager* dlm = |
| + content::BrowserContext::GetDownloadManager( |
| + web_contents->GetBrowserContext()); |
| std::unique_ptr<content::DownloadUrlParameters> dl_params( |
| content::DownloadUrlParameters::CreateForWebContentsMainFrame( |
| web_contents, url)); |
| @@ -112,8 +117,6 @@ bool IsInterruptedDownloadAutoResumable(content::DownloadItem* download_item) { |
| } // namespace |
| -namespace content { |
| - |
| // JNI methods |
| static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| DownloadControllerAndroidImpl::GetInstance()->Init(env, obj); |
| @@ -155,7 +158,7 @@ DownloadControllerAndroid* DownloadControllerAndroid::Get() { |
| return DownloadControllerAndroid::download_controller_; |
| } |
| -//static |
| +// static |
| void DownloadControllerAndroid::SetDownloadControllerAndroid( |
| DownloadControllerAndroid* download_controller) { |
| base::AutoLock lock(g_download_controller_lock_.Get()); |
| @@ -186,43 +189,25 @@ void DownloadControllerAndroidImpl::Init(JNIEnv* env, jobject obj) { |
| java_object_->obj = env->NewWeakGlobalRef(obj); |
| } |
| -void DownloadControllerAndroidImpl::CancelDeferredDownload( |
| - DeferredDownloadObserver* observer) { |
| - for (auto iter = deferred_downloads_.begin(); |
| - iter != deferred_downloads_.end(); ++iter) { |
| - if (*iter == observer) { |
| - deferred_downloads_.erase(iter); |
| - return; |
| - } |
| - } |
| -} |
| - |
| void DownloadControllerAndroidImpl::AcquireFileAccessPermission( |
| WebContents* web_contents, |
| const DownloadControllerAndroid::AcquireFileAccessPermissionCallback& cb) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| DCHECK(web_contents); |
| - ScopedJavaLocalRef<jobject> view = |
| - GetContentViewCoreFromWebContents(web_contents); |
| - if (view.is_null()) { |
| - BrowserThread::PostTask( |
| - BrowserThread::UI, FROM_HERE, base::Bind(cb, false)); |
| - return; |
| - } |
| - |
| - if (HasFileAccessPermission(view)) { |
| + ui::WindowAndroid* window_android = |
| + ViewAndroidHelper::FromWebContents(web_contents)-> |
| + GetViewAndroid()->GetWindowAndroid(); |
| + if (HasFileAccessPermission(window_android)) { |
|
no sievers
2016/06/07 21:35:23
|window_android| can be null (with custom tabs), s
Jinsuk Kim
2016/06/08 06:29:08
Done.
|
| BrowserThread::PostTask( |
| BrowserThread::UI, FROM_HERE, base::Bind(cb, true)); |
| return; |
| } |
| - |
| - JNIEnv* env = base::android::AttachCurrentThread(); |
| // Make copy on the heap so we can pass the pointer through JNI. |
| intptr_t callback_id = reinterpret_cast<intptr_t>( |
| new DownloadControllerAndroid::AcquireFileAccessPermissionCallback(cb)); |
| - Java_DownloadController_requestFileAccess( |
| - env, GetJavaObject()->Controller(env).obj(), view.obj(), callback_id); |
| + ChromeDownloadDelegate::FromWebContents(web_contents)-> |
| + RequestFileAccess(callback_id); |
| } |
| void DownloadControllerAndroidImpl::SetDefaultDownloadFileName( |
| @@ -231,13 +216,15 @@ void DownloadControllerAndroidImpl::SetDefaultDownloadFileName( |
| } |
| bool DownloadControllerAndroidImpl::HasFileAccessPermission( |
| - ScopedJavaLocalRef<jobject> j_content_view_core) { |
| + ui::WindowAndroid* window_android) { |
| + ScopedJavaLocalRef<jobject> jwindow_android = window_android->GetJavaObject(); |
| + |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| - DCHECK(!j_content_view_core.is_null()); |
| + DCHECK(!jwindow_android.is_null()); |
| JNIEnv* env = base::android::AttachCurrentThread(); |
| return Java_DownloadController_hasFileAccess( |
| - env, GetJavaObject()->Controller(env).obj(), j_content_view_core.obj()); |
| + env, GetJavaObject()->Controller(env).obj(), jwindow_android.obj()); |
| } |
| void DownloadControllerAndroidImpl::CreateGETDownload( |
| @@ -266,7 +253,7 @@ void DownloadControllerAndroidImpl::PrepareDownloadInfo( |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| net::URLRequest* request = |
| - ResourceDispatcherHostImpl::Get()->GetURLRequest(global_id); |
| + ResourceDispatcherHost::Get()->GetURLRequest(global_id); |
| if (!request) { |
| LOG(ERROR) << "Request to download not found."; |
| return; |
| @@ -292,7 +279,7 @@ void DownloadControllerAndroidImpl::CheckPolicyAndLoadCookies( |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| net::URLRequest* request = |
| - ResourceDispatcherHostImpl::Get()->GetURLRequest(global_id); |
| + ResourceDispatcherHost::Get()->GetURLRequest(global_id); |
| if (!request) { |
| LOG(ERROR) << "Request to download not found."; |
| return; |
| @@ -315,7 +302,7 @@ void DownloadControllerAndroidImpl::DoLoadCookies( |
| options.set_include_httponly(); |
| net::URLRequest* request = |
| - ResourceDispatcherHostImpl::Get()->GetURLRequest(global_id); |
| + ResourceDispatcherHost::Get()->GetURLRequest(global_id); |
| if (!request) { |
| LOG(ERROR) << "Request to download not found."; |
| return; |
| @@ -356,19 +343,6 @@ void DownloadControllerAndroidImpl::StartAndroidDownload( |
| LOG(ERROR) << "Download failed on URL:" << info.url.spec(); |
| return; |
| } |
| - ScopedJavaLocalRef<jobject> view = |
| - GetContentViewCoreFromWebContents(web_contents); |
| - if (view.is_null()) { |
| - // ContentViewCore might not have been created yet, pass a callback to |
| - // DeferredDownloadTaskManager so that the download can restart when |
| - // ContentViewCore is created. |
| - deferred_downloads_.push_back(new DeferredDownloadObserver( |
| - web_contents, |
| - base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownload, |
| - base::Unretained(this), render_process_id, render_view_id, |
| - must_download, info))); |
| - return; |
| - } |
| AcquireFileAccessPermission( |
| web_contents, |
| @@ -384,70 +358,38 @@ void DownloadControllerAndroidImpl::StartAndroidDownloadInternal( |
| if (!allowed) |
| return; |
| - // Call newHttpGetDownload |
| WebContents* web_contents = GetWebContents(render_process_id, render_view_id); |
| // The view went away. Can't proceed. |
| if (!web_contents) |
| return; |
| - ScopedJavaLocalRef<jobject> view = |
| - GetContentViewCoreFromWebContents(web_contents); |
| - if (view.is_null()) |
| - return; |
| - JNIEnv* env = base::android::AttachCurrentThread(); |
| - ScopedJavaLocalRef<jstring> jurl = |
| - ConvertUTF8ToJavaString(env, info.url.spec()); |
| - ScopedJavaLocalRef<jstring> juser_agent = |
| - ConvertUTF8ToJavaString(env, info.user_agent); |
| - ScopedJavaLocalRef<jstring> jcontent_disposition = |
| - ConvertUTF8ToJavaString(env, info.content_disposition); |
| - ScopedJavaLocalRef<jstring> jmime_type = |
| - ConvertUTF8ToJavaString(env, info.original_mime_type); |
| - ScopedJavaLocalRef<jstring> jcookie = |
| - ConvertUTF8ToJavaString(env, info.cookie); |
| - ScopedJavaLocalRef<jstring> jreferer = |
| - ConvertUTF8ToJavaString(env, info.referer); |
| - |
| - // net::GetSuggestedFilename will fallback to "download" as filename. |
| - ScopedJavaLocalRef<jstring> jfilename = |
| - base::android::ConvertUTF16ToJavaString( |
| - env, net::GetSuggestedFilename(info.url, info.content_disposition, |
| - std::string(), // referrer_charset |
| - std::string(), // suggested_name |
| - info.original_mime_type, |
| - default_file_name_)); |
| - |
| - Java_DownloadController_newHttpGetDownload( |
| - env, GetJavaObject()->Controller(env).obj(), view.obj(), jurl.obj(), |
| - juser_agent.obj(), jcontent_disposition.obj(), jmime_type.obj(), |
| - jcookie.obj(), jreferer.obj(), info.has_user_gesture, jfilename.obj(), |
| - info.total_bytes, must_download); |
| + base::string16 filename = net::GetSuggestedFilename( |
| + info.url, info.content_disposition, |
| + std::string(), // referrer_charset |
| + std::string(), // suggested_name |
| + info.original_mime_type, |
| + default_file_name_); |
| + ChromeDownloadDelegate::FromWebContents(web_contents)->RequestHTTPGetDownload( |
| + info.url.spec(), info.user_agent, |
| + info.content_disposition, info.original_mime_type, |
| + info.cookie, info.referer, filename, |
| + info.total_bytes, info.has_user_gesture, |
| + must_download); |
| } |
| void DownloadControllerAndroidImpl::OnDownloadStarted( |
| DownloadItem* download_item) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| - if (!download_item->GetWebContents()) |
| + WebContents* web_contents = download_item->GetWebContents(); |
| + if (!web_contents) |
| return; |
| - JNIEnv* env = base::android::AttachCurrentThread(); |
| - |
| // Register for updates to the DownloadItem. |
| download_item->AddObserver(this); |
| - ScopedJavaLocalRef<jobject> view = |
| - GetContentViewCoreFromWebContents(download_item->GetWebContents()); |
| - // The view went away. Can't proceed. |
| - if (view.is_null()) |
| - return; |
| - |
| - ScopedJavaLocalRef<jstring> jmime_type = |
| - ConvertUTF8ToJavaString(env, download_item->GetMimeType()); |
| - ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( |
| - env, download_item->GetTargetFilePath().BaseName().value()); |
| - Java_DownloadController_onDownloadStarted( |
| - env, GetJavaObject()->Controller(env).obj(), view.obj(), jfilename.obj(), |
| - jmime_type.obj()); |
| + ChromeDownloadDelegate::FromWebContents(web_contents)->OnDownloadStarted( |
| + download_item->GetTargetFilePath().BaseName().value(), |
| + download_item->GetMimeType()); |
| } |
| void DownloadControllerAndroidImpl::OnDownloadUpdated(DownloadItem* item) { |
| @@ -518,29 +460,11 @@ void DownloadControllerAndroidImpl::OnDownloadUpdated(DownloadItem* item) { |
| } |
| void DownloadControllerAndroidImpl::OnDangerousDownload(DownloadItem* item) { |
| - JNIEnv* env = base::android::AttachCurrentThread(); |
| - ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( |
| - env, item->GetTargetFilePath().BaseName().value()); |
| - ScopedJavaLocalRef<jstring> jguid = |
| - ConvertUTF8ToJavaString(env, item->GetGuid()); |
| - ScopedJavaLocalRef<jobject> view_core = GetContentViewCoreFromWebContents( |
| - item->GetWebContents()); |
| - if (!view_core.is_null()) { |
| - Java_DownloadController_onDangerousDownload( |
| - env, GetJavaObject()->Controller(env).obj(), view_core.obj(), |
| - jfilename.obj(), jguid.obj()); |
| - } |
| -} |
| - |
| -ScopedJavaLocalRef<jobject> |
| - DownloadControllerAndroidImpl::GetContentViewCoreFromWebContents( |
| - WebContents* web_contents) { |
| + WebContents* web_contents = item->GetWebContents(); |
| if (!web_contents) |
| - return ScopedJavaLocalRef<jobject>(); |
| - |
| - ContentViewCore* view_core = ContentViewCore::FromWebContents(web_contents); |
| - return view_core ? view_core->GetJavaObject() : |
| - ScopedJavaLocalRef<jobject>(); |
| + return; |
| + ChromeDownloadDelegate::FromWebContents(web_contents)->OnDownloadStarted( |
|
no sievers
2016/06/07 21:35:23
not OnDangerousDownload()?
Jinsuk Kim
2016/06/08 06:29:08
Thanks for catching. Done.
|
| + item->GetTargetFilePath().BaseName().value(), item->GetGuid()); |
| } |
| DownloadControllerAndroidImpl::JavaObject* |
| @@ -573,8 +497,8 @@ void DownloadControllerAndroidImpl::DangerousDownloadValidated( |
| bool accept) { |
| if (!web_contents) |
| return; |
| - DownloadManagerImpl* dlm = static_cast<DownloadManagerImpl*>( |
| - BrowserContext::GetDownloadManager(web_contents->GetBrowserContext())); |
| + DownloadManager* dlm = |
| + BrowserContext::GetDownloadManager(web_contents->GetBrowserContext()); |
| DownloadItem* item = dlm->GetDownloadByGuid(download_guid); |
| if (!item) |
| return; |
| @@ -595,7 +519,7 @@ DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid( |
| request->extra_request_headers().GetHeader( |
| net::HttpRequestHeaders::kUserAgent, &user_agent); |
| if (user_agent.empty()) |
| - user_agent = GetContentClient()->GetUserAgent(); |
| + user_agent = GetUserAgent(); |
| GURL referer_url(request->referrer()); |
| if (referer_url.is_valid()) |
| referer = referer_url.spec(); |
| @@ -613,5 +537,3 @@ DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid( |
| const DownloadInfoAndroid& other) = default; |
| DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {} |
| - |
| -} // namespace content |