| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/browser/android/download_controller_android_impl.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/android/context_utils.h" | |
| 11 #include "base/android/jni_android.h" | |
| 12 #include "base/android/jni_string.h" | |
| 13 #include "base/bind.h" | |
| 14 #include "base/lazy_instance.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "content/browser/android/content_view_core_impl.h" | |
| 19 #include "content/browser/android/deferred_download_observer.h" | |
| 20 #include "content/browser/download/download_item_impl.h" | |
| 21 #include "content/browser/download/download_manager_impl.h" | |
| 22 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 23 #include "content/browser/renderer_host/render_process_host_impl.h" | |
| 24 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
| 25 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 26 #include "content/browser/web_contents/web_contents_impl.h" | |
| 27 #include "content/public/browser/browser_context.h" | |
| 28 #include "content/public/browser/browser_thread.h" | |
| 29 #include "content/public/browser/download_url_parameters.h" | |
| 30 #include "content/public/browser/global_request_id.h" | |
| 31 #include "content/public/browser/resource_request_info.h" | |
| 32 #include "content/public/common/content_client.h" | |
| 33 #include "content/public/common/referrer.h" | |
| 34 #include "jni/DownloadController_jni.h" | |
| 35 #include "net/base/filename_util.h" | |
| 36 #include "net/cookies/cookie_options.h" | |
| 37 #include "net/cookies/cookie_store.h" | |
| 38 #include "net/http/http_content_disposition.h" | |
| 39 #include "net/http/http_request_headers.h" | |
| 40 #include "net/http/http_response_headers.h" | |
| 41 #include "net/url_request/url_request.h" | |
| 42 #include "net/url_request/url_request_context.h" | |
| 43 | |
| 44 using base::android::ConvertUTF8ToJavaString; | |
| 45 using base::android::ScopedJavaLocalRef; | |
| 46 | |
| 47 namespace { | |
| 48 // Guards download_controller_ | |
| 49 base::LazyInstance<base::Lock> g_download_controller_lock_; | |
| 50 | |
| 51 content::WebContents* GetWebContents(int render_process_id, | |
| 52 int render_view_id) { | |
| 53 content::RenderViewHost* render_view_host = | |
| 54 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
| 55 | |
| 56 if (!render_view_host) | |
| 57 return nullptr; | |
| 58 | |
| 59 return content::WebContents::FromRenderViewHost(render_view_host); | |
| 60 } | |
| 61 | |
| 62 void CreateContextMenuDownload(int render_process_id, | |
| 63 int render_view_id, | |
| 64 const content::ContextMenuParams& params, | |
| 65 bool is_link, | |
| 66 const std::string& extra_headers, | |
| 67 bool granted) { | |
| 68 if (!granted) | |
| 69 return; | |
| 70 | |
| 71 content::WebContents* web_contents = | |
| 72 GetWebContents(render_process_id, render_view_id); | |
| 73 if (!web_contents) | |
| 74 return; | |
| 75 | |
| 76 const GURL& url = is_link ? params.link_url : params.src_url; | |
| 77 const GURL& referring_url = | |
| 78 params.frame_url.is_empty() ? params.page_url : params.frame_url; | |
| 79 content::DownloadManagerImpl* dlm = | |
| 80 static_cast<content::DownloadManagerImpl*>( | |
| 81 content::BrowserContext::GetDownloadManager( | |
| 82 web_contents->GetBrowserContext())); | |
| 83 std::unique_ptr<content::DownloadUrlParameters> dl_params( | |
| 84 content::DownloadUrlParameters::CreateForWebContentsMainFrame( | |
| 85 web_contents, url)); | |
| 86 content::Referrer referrer = content::Referrer::SanitizeForRequest( | |
| 87 url, | |
| 88 content::Referrer(referring_url.GetAsReferrer(), params.referrer_policy)); | |
| 89 dl_params->set_referrer(referrer); | |
| 90 if (is_link) | |
| 91 dl_params->set_referrer_encoding(params.frame_charset); | |
| 92 net::HttpRequestHeaders headers; | |
| 93 headers.AddHeadersFromString(extra_headers); | |
| 94 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) | |
| 95 dl_params->add_request_header(it.name(), it.value()); | |
| 96 if (!is_link && extra_headers.empty()) | |
| 97 dl_params->set_prefer_cache(true); | |
| 98 dl_params->set_prompt(false); | |
| 99 dlm->DownloadUrl(std::move(dl_params)); | |
| 100 } | |
| 101 | |
| 102 // Check if an interrupted download item can be auto resumed. | |
| 103 bool IsInterruptedDownloadAutoResumable(content::DownloadItem* download_item) { | |
| 104 int interrupt_reason = download_item->GetLastReason(); | |
| 105 DCHECK_NE(interrupt_reason, content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
| 106 return | |
| 107 interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT || | |
| 108 interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED || | |
| 109 interrupt_reason == | |
| 110 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED; | |
| 111 } | |
| 112 | |
| 113 } // namespace | |
| 114 | |
| 115 namespace content { | |
| 116 | |
| 117 // JNI methods | |
| 118 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
| 119 DownloadControllerAndroidImpl::GetInstance()->Init(env, obj); | |
| 120 } | |
| 121 | |
| 122 static void OnRequestFileAccessResult(JNIEnv* env, | |
| 123 const JavaParamRef<jobject>& obj, | |
| 124 jlong callback_id, | |
| 125 jboolean granted) { | |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 127 DCHECK(callback_id); | |
| 128 | |
| 129 // Convert java long long int to c++ pointer, take ownership. | |
| 130 std::unique_ptr< | |
| 131 DownloadControllerAndroid::AcquireFileAccessPermissionCallback> | |
| 132 cb(reinterpret_cast< | |
| 133 DownloadControllerAndroid::AcquireFileAccessPermissionCallback*>( | |
| 134 callback_id)); | |
| 135 cb->Run(granted); | |
| 136 } | |
| 137 | |
| 138 struct DownloadControllerAndroidImpl::JavaObject { | |
| 139 ScopedJavaLocalRef<jobject> Controller(JNIEnv* env) { | |
| 140 return GetRealObject(env, obj); | |
| 141 } | |
| 142 jweak obj; | |
| 143 }; | |
| 144 | |
| 145 // static | |
| 146 bool DownloadControllerAndroidImpl::RegisterDownloadController(JNIEnv* env) { | |
| 147 return RegisterNativesImpl(env); | |
| 148 } | |
| 149 | |
| 150 // static | |
| 151 DownloadControllerAndroid* DownloadControllerAndroid::Get() { | |
| 152 base::AutoLock lock(g_download_controller_lock_.Get()); | |
| 153 if (!DownloadControllerAndroid::download_controller_) | |
| 154 download_controller_ = DownloadControllerAndroidImpl::GetInstance(); | |
| 155 return DownloadControllerAndroid::download_controller_; | |
| 156 } | |
| 157 | |
| 158 //static | |
| 159 void DownloadControllerAndroid::SetDownloadControllerAndroid( | |
| 160 DownloadControllerAndroid* download_controller) { | |
| 161 base::AutoLock lock(g_download_controller_lock_.Get()); | |
| 162 DownloadControllerAndroid::download_controller_ = download_controller; | |
| 163 } | |
| 164 | |
| 165 // static | |
| 166 DownloadControllerAndroidImpl* DownloadControllerAndroidImpl::GetInstance() { | |
| 167 return base::Singleton<DownloadControllerAndroidImpl>::get(); | |
| 168 } | |
| 169 | |
| 170 DownloadControllerAndroidImpl::DownloadControllerAndroidImpl() | |
| 171 : java_object_(NULL) { | |
| 172 } | |
| 173 | |
| 174 DownloadControllerAndroidImpl::~DownloadControllerAndroidImpl() { | |
| 175 if (java_object_) { | |
| 176 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 177 env->DeleteWeakGlobalRef(java_object_->obj); | |
| 178 delete java_object_; | |
| 179 base::android::CheckException(env); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 // Initialize references to Java object. | |
| 184 void DownloadControllerAndroidImpl::Init(JNIEnv* env, jobject obj) { | |
| 185 java_object_ = new JavaObject; | |
| 186 java_object_->obj = env->NewWeakGlobalRef(obj); | |
| 187 } | |
| 188 | |
| 189 void DownloadControllerAndroidImpl::CancelDeferredDownload( | |
| 190 DeferredDownloadObserver* observer) { | |
| 191 for (auto iter = deferred_downloads_.begin(); | |
| 192 iter != deferred_downloads_.end(); ++iter) { | |
| 193 if (*iter == observer) { | |
| 194 deferred_downloads_.erase(iter); | |
| 195 return; | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void DownloadControllerAndroidImpl::AcquireFileAccessPermission( | |
| 201 WebContents* web_contents, | |
| 202 const DownloadControllerAndroid::AcquireFileAccessPermissionCallback& cb) { | |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 204 DCHECK(web_contents); | |
| 205 | |
| 206 ScopedJavaLocalRef<jobject> view = | |
| 207 GetContentViewCoreFromWebContents(web_contents); | |
| 208 if (view.is_null()) { | |
| 209 BrowserThread::PostTask( | |
| 210 BrowserThread::UI, FROM_HERE, base::Bind(cb, false)); | |
| 211 return; | |
| 212 } | |
| 213 | |
| 214 if (HasFileAccessPermission(view)) { | |
| 215 BrowserThread::PostTask( | |
| 216 BrowserThread::UI, FROM_HERE, base::Bind(cb, true)); | |
| 217 return; | |
| 218 } | |
| 219 | |
| 220 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 221 // Make copy on the heap so we can pass the pointer through JNI. | |
| 222 intptr_t callback_id = reinterpret_cast<intptr_t>( | |
| 223 new DownloadControllerAndroid::AcquireFileAccessPermissionCallback(cb)); | |
| 224 Java_DownloadController_requestFileAccess( | |
| 225 env, GetJavaObject()->Controller(env).obj(), view.obj(), callback_id); | |
| 226 } | |
| 227 | |
| 228 void DownloadControllerAndroidImpl::SetDefaultDownloadFileName( | |
| 229 const std::string& file_name) { | |
| 230 default_file_name_ = file_name; | |
| 231 } | |
| 232 | |
| 233 bool DownloadControllerAndroidImpl::HasFileAccessPermission( | |
| 234 ScopedJavaLocalRef<jobject> j_content_view_core) { | |
| 235 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 236 DCHECK(!j_content_view_core.is_null()); | |
| 237 | |
| 238 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 239 return Java_DownloadController_hasFileAccess( | |
| 240 env, GetJavaObject()->Controller(env).obj(), j_content_view_core.obj()); | |
| 241 } | |
| 242 | |
| 243 void DownloadControllerAndroidImpl::CreateGETDownload( | |
| 244 int render_process_id, int render_view_id, int request_id, | |
| 245 bool must_download) { | |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 247 GlobalRequestID global_id(render_process_id, request_id); | |
| 248 | |
| 249 // We are yielding the UI thread and render_view_host may go away by | |
| 250 // the time we come back. Pass along render_process_id and render_view_id | |
| 251 // to retrieve it later (if it still exists). | |
| 252 GetDownloadInfoCB cb = base::Bind( | |
| 253 &DownloadControllerAndroidImpl::StartAndroidDownload, | |
| 254 base::Unretained(this), render_process_id, | |
| 255 render_view_id, must_download); | |
| 256 | |
| 257 PrepareDownloadInfo( | |
| 258 global_id, | |
| 259 base::Bind(&DownloadControllerAndroidImpl::StartDownloadOnUIThread, | |
| 260 base::Unretained(this), cb)); | |
| 261 } | |
| 262 | |
| 263 void DownloadControllerAndroidImpl::PrepareDownloadInfo( | |
| 264 const GlobalRequestID& global_id, | |
| 265 const GetDownloadInfoCB& callback) { | |
| 266 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 267 | |
| 268 net::URLRequest* request = | |
| 269 ResourceDispatcherHostImpl::Get()->GetURLRequest(global_id); | |
| 270 if (!request) { | |
| 271 LOG(ERROR) << "Request to download not found."; | |
| 272 return; | |
| 273 } | |
| 274 | |
| 275 DownloadInfoAndroid info_android(request); | |
| 276 | |
| 277 net::CookieStore* cookie_store = request->context()->cookie_store(); | |
| 278 if (cookie_store) { | |
| 279 cookie_store->GetAllCookiesForURLAsync( | |
| 280 request->url(), | |
| 281 base::Bind(&DownloadControllerAndroidImpl::CheckPolicyAndLoadCookies, | |
| 282 base::Unretained(this), info_android, callback, global_id)); | |
| 283 } else { | |
| 284 // Can't get any cookies, start android download. | |
| 285 callback.Run(info_android); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 void DownloadControllerAndroidImpl::CheckPolicyAndLoadCookies( | |
| 290 const DownloadInfoAndroid& info, const GetDownloadInfoCB& callback, | |
| 291 const GlobalRequestID& global_id, const net::CookieList& cookie_list) { | |
| 292 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 293 | |
| 294 net::URLRequest* request = | |
| 295 ResourceDispatcherHostImpl::Get()->GetURLRequest(global_id); | |
| 296 if (!request) { | |
| 297 LOG(ERROR) << "Request to download not found."; | |
| 298 return; | |
| 299 } | |
| 300 | |
| 301 if (request->context()->network_delegate()->CanGetCookies( | |
| 302 *request, cookie_list)) { | |
| 303 DoLoadCookies(info, callback, global_id); | |
| 304 } else { | |
| 305 callback.Run(info); | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 void DownloadControllerAndroidImpl::DoLoadCookies( | |
| 310 const DownloadInfoAndroid& info, const GetDownloadInfoCB& callback, | |
| 311 const GlobalRequestID& global_id) { | |
| 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 313 | |
| 314 net::CookieOptions options; | |
| 315 options.set_include_httponly(); | |
| 316 | |
| 317 net::URLRequest* request = | |
| 318 ResourceDispatcherHostImpl::Get()->GetURLRequest(global_id); | |
| 319 if (!request) { | |
| 320 LOG(ERROR) << "Request to download not found."; | |
| 321 return; | |
| 322 } | |
| 323 | |
| 324 request->context()->cookie_store()->GetCookiesWithOptionsAsync( | |
| 325 info.url, options, | |
| 326 base::Bind(&DownloadControllerAndroidImpl::OnCookieResponse, | |
| 327 base::Unretained(this), info, callback)); | |
| 328 } | |
| 329 | |
| 330 void DownloadControllerAndroidImpl::OnCookieResponse( | |
| 331 DownloadInfoAndroid download_info, | |
| 332 const GetDownloadInfoCB& callback, | |
| 333 const std::string& cookie) { | |
| 334 download_info.cookie = cookie; | |
| 335 | |
| 336 // We have everything we need, start Android download. | |
| 337 callback.Run(download_info); | |
| 338 } | |
| 339 | |
| 340 void DownloadControllerAndroidImpl::StartDownloadOnUIThread( | |
| 341 const GetDownloadInfoCB& callback, | |
| 342 const DownloadInfoAndroid& info) { | |
| 343 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 344 BrowserThread::PostTask( | |
| 345 BrowserThread::UI, FROM_HERE, base::Bind(callback, info)); | |
| 346 } | |
| 347 | |
| 348 void DownloadControllerAndroidImpl::StartAndroidDownload( | |
| 349 int render_process_id, int render_view_id, bool must_download, | |
| 350 const DownloadInfoAndroid& info) { | |
| 351 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 352 | |
| 353 WebContents* web_contents = GetWebContents(render_process_id, render_view_id); | |
| 354 if (!web_contents) { | |
| 355 // The view went away. Can't proceed. | |
| 356 LOG(ERROR) << "Download failed on URL:" << info.url.spec(); | |
| 357 return; | |
| 358 } | |
| 359 ScopedJavaLocalRef<jobject> view = | |
| 360 GetContentViewCoreFromWebContents(web_contents); | |
| 361 if (view.is_null()) { | |
| 362 // ContentViewCore might not have been created yet, pass a callback to | |
| 363 // DeferredDownloadTaskManager so that the download can restart when | |
| 364 // ContentViewCore is created. | |
| 365 deferred_downloads_.push_back(new DeferredDownloadObserver( | |
| 366 web_contents, | |
| 367 base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownload, | |
| 368 base::Unretained(this), render_process_id, render_view_id, | |
| 369 must_download, info))); | |
| 370 return; | |
| 371 } | |
| 372 | |
| 373 AcquireFileAccessPermission( | |
| 374 web_contents, | |
| 375 base::Bind(&DownloadControllerAndroidImpl::StartAndroidDownloadInternal, | |
| 376 base::Unretained(this), render_process_id, render_view_id, | |
| 377 must_download, info)); | |
| 378 } | |
| 379 | |
| 380 void DownloadControllerAndroidImpl::StartAndroidDownloadInternal( | |
| 381 int render_process_id, int render_view_id, bool must_download, | |
| 382 const DownloadInfoAndroid& info, bool allowed) { | |
| 383 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 384 if (!allowed) | |
| 385 return; | |
| 386 | |
| 387 // Call newHttpGetDownload | |
| 388 WebContents* web_contents = GetWebContents(render_process_id, render_view_id); | |
| 389 // The view went away. Can't proceed. | |
| 390 if (!web_contents) | |
| 391 return; | |
| 392 ScopedJavaLocalRef<jobject> view = | |
| 393 GetContentViewCoreFromWebContents(web_contents); | |
| 394 if (view.is_null()) | |
| 395 return; | |
| 396 | |
| 397 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 398 ScopedJavaLocalRef<jstring> jurl = | |
| 399 ConvertUTF8ToJavaString(env, info.url.spec()); | |
| 400 ScopedJavaLocalRef<jstring> juser_agent = | |
| 401 ConvertUTF8ToJavaString(env, info.user_agent); | |
| 402 ScopedJavaLocalRef<jstring> jcontent_disposition = | |
| 403 ConvertUTF8ToJavaString(env, info.content_disposition); | |
| 404 ScopedJavaLocalRef<jstring> jmime_type = | |
| 405 ConvertUTF8ToJavaString(env, info.original_mime_type); | |
| 406 ScopedJavaLocalRef<jstring> jcookie = | |
| 407 ConvertUTF8ToJavaString(env, info.cookie); | |
| 408 ScopedJavaLocalRef<jstring> jreferer = | |
| 409 ConvertUTF8ToJavaString(env, info.referer); | |
| 410 | |
| 411 // net::GetSuggestedFilename will fallback to "download" as filename. | |
| 412 ScopedJavaLocalRef<jstring> jfilename = | |
| 413 base::android::ConvertUTF16ToJavaString( | |
| 414 env, net::GetSuggestedFilename(info.url, info.content_disposition, | |
| 415 std::string(), // referrer_charset | |
| 416 std::string(), // suggested_name | |
| 417 info.original_mime_type, | |
| 418 default_file_name_)); | |
| 419 | |
| 420 Java_DownloadController_newHttpGetDownload( | |
| 421 env, GetJavaObject()->Controller(env).obj(), view.obj(), jurl.obj(), | |
| 422 juser_agent.obj(), jcontent_disposition.obj(), jmime_type.obj(), | |
| 423 jcookie.obj(), jreferer.obj(), info.has_user_gesture, jfilename.obj(), | |
| 424 info.total_bytes, must_download); | |
| 425 } | |
| 426 | |
| 427 void DownloadControllerAndroidImpl::OnDownloadStarted( | |
| 428 DownloadItem* download_item) { | |
| 429 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 430 if (!download_item->GetWebContents()) | |
| 431 return; | |
| 432 | |
| 433 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 434 | |
| 435 // Register for updates to the DownloadItem. | |
| 436 download_item->AddObserver(this); | |
| 437 | |
| 438 ScopedJavaLocalRef<jobject> view = | |
| 439 GetContentViewCoreFromWebContents(download_item->GetWebContents()); | |
| 440 // The view went away. Can't proceed. | |
| 441 if (view.is_null()) | |
| 442 return; | |
| 443 | |
| 444 ScopedJavaLocalRef<jstring> jmime_type = | |
| 445 ConvertUTF8ToJavaString(env, download_item->GetMimeType()); | |
| 446 ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( | |
| 447 env, download_item->GetTargetFilePath().BaseName().value()); | |
| 448 Java_DownloadController_onDownloadStarted( | |
| 449 env, GetJavaObject()->Controller(env).obj(), view.obj(), jfilename.obj(), | |
| 450 jmime_type.obj()); | |
| 451 } | |
| 452 | |
| 453 void DownloadControllerAndroidImpl::OnDownloadUpdated(DownloadItem* item) { | |
| 454 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 455 if (item->IsDangerous() && (item->GetState() != DownloadItem::CANCELLED)) | |
| 456 OnDangerousDownload(item); | |
| 457 | |
| 458 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 459 ScopedJavaLocalRef<jstring> jguid = | |
| 460 ConvertUTF8ToJavaString(env, item->GetGuid()); | |
| 461 ScopedJavaLocalRef<jstring> jurl = | |
| 462 ConvertUTF8ToJavaString(env, item->GetURL().spec()); | |
| 463 ScopedJavaLocalRef<jstring> jmime_type = | |
| 464 ConvertUTF8ToJavaString(env, item->GetMimeType()); | |
| 465 ScopedJavaLocalRef<jstring> jpath = | |
| 466 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()); | |
| 467 ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( | |
| 468 env, item->GetTargetFilePath().BaseName().value()); | |
| 469 ScopedJavaLocalRef<jstring> joriginal_url = | |
| 470 ConvertUTF8ToJavaString(env, item->GetOriginalUrl().spec()); | |
| 471 ScopedJavaLocalRef<jstring> jreferrer_url = | |
| 472 ConvertUTF8ToJavaString(env, item->GetReferrerUrl().spec()); | |
| 473 | |
| 474 switch (item->GetState()) { | |
| 475 case DownloadItem::IN_PROGRESS: { | |
| 476 base::TimeDelta time_delta; | |
| 477 item->TimeRemaining(&time_delta); | |
| 478 Java_DownloadController_onDownloadUpdated( | |
| 479 env, GetJavaObject()->Controller(env).obj(), jurl.obj(), | |
| 480 jmime_type.obj(), jfilename.obj(), jpath.obj(), | |
| 481 item->GetReceivedBytes(), jguid.obj(), | |
| 482 item->PercentComplete(), time_delta.InMilliseconds(), | |
| 483 item->HasUserGesture(), item->IsPaused(), | |
| 484 item->GetBrowserContext()->IsOffTheRecord()); | |
| 485 break; | |
| 486 } | |
| 487 case DownloadItem::COMPLETE: | |
| 488 // Multiple OnDownloadUpdated() notifications may be issued while the | |
| 489 // download is in the COMPLETE state. Only handle one. | |
| 490 item->RemoveObserver(this); | |
| 491 | |
| 492 // Call onDownloadCompleted | |
| 493 Java_DownloadController_onDownloadCompleted( | |
| 494 env, GetJavaObject()->Controller(env).obj(), jurl.obj(), | |
| 495 jmime_type.obj(), jfilename.obj(), jpath.obj(), | |
| 496 item->GetReceivedBytes(), jguid.obj(), | |
| 497 joriginal_url.obj(), jreferrer_url.obj(), item->HasUserGesture()); | |
| 498 break; | |
| 499 case DownloadItem::CANCELLED: | |
| 500 Java_DownloadController_onDownloadCancelled( | |
| 501 env, GetJavaObject()->Controller(env).obj(), jguid.obj()); | |
| 502 break; | |
| 503 case DownloadItem::INTERRUPTED: | |
| 504 // When device loses/changes network, we get a NETWORK_TIMEOUT, | |
| 505 // NETWORK_FAILED or NETWORK_DISCONNECTED error. Download should auto | |
| 506 // resume in this case. | |
| 507 Java_DownloadController_onDownloadInterrupted( | |
| 508 env, GetJavaObject()->Controller(env).obj(), jurl.obj(), | |
| 509 jmime_type.obj(), jfilename.obj(), jpath.obj(), | |
| 510 item->GetReceivedBytes(), jguid.obj(), | |
| 511 item->CanResume(), IsInterruptedDownloadAutoResumable(item), | |
| 512 item->GetBrowserContext()->IsOffTheRecord()); | |
| 513 item->RemoveObserver(this); | |
| 514 break; | |
| 515 case DownloadItem::MAX_DOWNLOAD_STATE: | |
| 516 NOTREACHED(); | |
| 517 } | |
| 518 } | |
| 519 | |
| 520 void DownloadControllerAndroidImpl::OnDangerousDownload(DownloadItem* item) { | |
| 521 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 522 ScopedJavaLocalRef<jstring> jfilename = ConvertUTF8ToJavaString( | |
| 523 env, item->GetTargetFilePath().BaseName().value()); | |
| 524 ScopedJavaLocalRef<jstring> jguid = | |
| 525 ConvertUTF8ToJavaString(env, item->GetGuid()); | |
| 526 ScopedJavaLocalRef<jobject> view_core = GetContentViewCoreFromWebContents( | |
| 527 item->GetWebContents()); | |
| 528 if (!view_core.is_null()) { | |
| 529 Java_DownloadController_onDangerousDownload( | |
| 530 env, GetJavaObject()->Controller(env).obj(), view_core.obj(), | |
| 531 jfilename.obj(), jguid.obj()); | |
| 532 } | |
| 533 } | |
| 534 | |
| 535 ScopedJavaLocalRef<jobject> | |
| 536 DownloadControllerAndroidImpl::GetContentViewCoreFromWebContents( | |
| 537 WebContents* web_contents) { | |
| 538 if (!web_contents) | |
| 539 return ScopedJavaLocalRef<jobject>(); | |
| 540 | |
| 541 ContentViewCore* view_core = ContentViewCore::FromWebContents(web_contents); | |
| 542 return view_core ? view_core->GetJavaObject() : | |
| 543 ScopedJavaLocalRef<jobject>(); | |
| 544 } | |
| 545 | |
| 546 DownloadControllerAndroidImpl::JavaObject* | |
| 547 DownloadControllerAndroidImpl::GetJavaObject() { | |
| 548 if (!java_object_) { | |
| 549 // Initialize Java DownloadController by calling | |
| 550 // DownloadController.getInstance(), which will call Init() | |
| 551 // if Java DownloadController is not instantiated already. | |
| 552 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 553 Java_DownloadController_getInstance(env); | |
| 554 } | |
| 555 | |
| 556 DCHECK(java_object_); | |
| 557 return java_object_; | |
| 558 } | |
| 559 | |
| 560 void DownloadControllerAndroidImpl::StartContextMenuDownload( | |
| 561 const ContextMenuParams& params, WebContents* web_contents, bool is_link, | |
| 562 const std::string& extra_headers) { | |
| 563 int process_id = web_contents->GetRenderProcessHost()->GetID(); | |
| 564 int routing_id = web_contents->GetRoutingID(); | |
| 565 AcquireFileAccessPermission( | |
| 566 web_contents, base::Bind(&CreateContextMenuDownload, process_id, | |
| 567 routing_id, params, is_link, extra_headers)); | |
| 568 } | |
| 569 | |
| 570 void DownloadControllerAndroidImpl::DangerousDownloadValidated( | |
| 571 WebContents* web_contents, | |
| 572 const std::string& download_guid, | |
| 573 bool accept) { | |
| 574 if (!web_contents) | |
| 575 return; | |
| 576 DownloadManagerImpl* dlm = static_cast<DownloadManagerImpl*>( | |
| 577 BrowserContext::GetDownloadManager(web_contents->GetBrowserContext())); | |
| 578 DownloadItem* item = dlm->GetDownloadByGuid(download_guid); | |
| 579 if (!item) | |
| 580 return; | |
| 581 if (accept) | |
| 582 item->ValidateDangerousDownload(); | |
| 583 else | |
| 584 item->Remove(); | |
| 585 } | |
| 586 | |
| 587 DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid( | |
| 588 net::URLRequest* request) | |
| 589 : has_user_gesture(false) { | |
| 590 request->GetResponseHeaderByName("content-disposition", &content_disposition); | |
| 591 | |
| 592 if (request->response_headers()) | |
| 593 request->response_headers()->GetMimeType(&original_mime_type); | |
| 594 | |
| 595 request->extra_request_headers().GetHeader( | |
| 596 net::HttpRequestHeaders::kUserAgent, &user_agent); | |
| 597 if (user_agent.empty()) | |
| 598 user_agent = GetContentClient()->GetUserAgent(); | |
| 599 GURL referer_url(request->referrer()); | |
| 600 if (referer_url.is_valid()) | |
| 601 referer = referer_url.spec(); | |
| 602 if (!request->url_chain().empty()) { | |
| 603 original_url = request->url_chain().front(); | |
| 604 url = request->url_chain().back(); | |
| 605 } | |
| 606 | |
| 607 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | |
| 608 if (info) | |
| 609 has_user_gesture = info->HasUserGesture(); | |
| 610 } | |
| 611 | |
| 612 DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid( | |
| 613 const DownloadInfoAndroid& other) = default; | |
| 614 | |
| 615 DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {} | |
| 616 | |
| 617 } // namespace content | |
| OLD | NEW |