| 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/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "content/browser/android/content_view_core_impl.h" | 12 #include "content/browser/android/content_view_core_impl.h" |
| 13 #include "content/browser/download/download_item_impl.h" | 13 #include "content/browser/download/download_item_impl.h" |
| 14 #include "content/browser/download/download_manager_impl.h" |
| 14 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 15 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 15 #include "content/browser/renderer_host/render_process_host_impl.h" | 16 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 16 #include "content/browser/renderer_host/render_view_host_delegate.h" | 17 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 17 #include "content/browser/renderer_host/render_view_host_impl.h" | 18 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 18 #include "content/browser/web_contents/web_contents_impl.h" | 19 #include "content/browser/web_contents/web_contents_impl.h" |
| 20 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/download_url_parameters.h" |
| 20 #include "content/public/browser/global_request_id.h" | 23 #include "content/public/browser/global_request_id.h" |
| 21 #include "content/public/browser/web_contents_view.h" | 24 #include "content/public/browser/web_contents_view.h" |
| 25 #include "content/public/common/referrer.h" |
| 22 #include "jni/DownloadController_jni.h" | 26 #include "jni/DownloadController_jni.h" |
| 23 #include "net/cookies/cookie_options.h" | 27 #include "net/cookies/cookie_options.h" |
| 24 #include "net/cookies/cookie_store.h" | 28 #include "net/cookies/cookie_store.h" |
| 25 #include "net/http/http_request_headers.h" | 29 #include "net/http/http_request_headers.h" |
| 26 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 27 #include "net/url_request/url_request.h" | 31 #include "net/url_request/url_request.h" |
| 28 #include "net/url_request/url_request_context.h" | 32 #include "net/url_request/url_request_context.h" |
| 29 | 33 |
| 30 using base::android::ConvertUTF8ToJavaString; | 34 using base::android::ConvertUTF8ToJavaString; |
| 31 using base::android::ScopedJavaLocalRef; | 35 using base::android::ScopedJavaLocalRef; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 ConvertUTF8ToJavaString(env, info.cookie); | 219 ConvertUTF8ToJavaString(env, info.cookie); |
| 216 ScopedJavaLocalRef<jstring> jreferer = | 220 ScopedJavaLocalRef<jstring> jreferer = |
| 217 ConvertUTF8ToJavaString(env, info.referer); | 221 ConvertUTF8ToJavaString(env, info.referer); |
| 218 | 222 |
| 219 Java_DownloadController_newHttpGetDownload( | 223 Java_DownloadController_newHttpGetDownload( |
| 220 env, GetJavaObject()->Controller(env).obj(), view.obj(), jurl.obj(), | 224 env, GetJavaObject()->Controller(env).obj(), view.obj(), jurl.obj(), |
| 221 juser_agent.obj(), jcontent_disposition.obj(), jmime_type.obj(), | 225 juser_agent.obj(), jcontent_disposition.obj(), jmime_type.obj(), |
| 222 jcookie.obj(), jreferer.obj(), info.total_bytes); | 226 jcookie.obj(), jreferer.obj(), info.total_bytes); |
| 223 } | 227 } |
| 224 | 228 |
| 225 void DownloadControllerAndroidImpl::OnPostDownloadStarted( | 229 void DownloadControllerAndroidImpl::OnDownloadStarted( |
| 226 DownloadItem* download_item) { | 230 DownloadItem* download_item) { |
| 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 228 if (!download_item->GetWebContents()) | 232 if (!download_item->GetWebContents()) |
| 229 return; | 233 return; |
| 230 | 234 |
| 231 JNIEnv* env = base::android::AttachCurrentThread(); | 235 JNIEnv* env = base::android::AttachCurrentThread(); |
| 232 | 236 |
| 233 // Register for updates to the DownloadItem. | 237 // Register for updates to the DownloadItem. |
| 234 download_item->AddObserver(this); | 238 download_item->AddObserver(this); |
| 235 | 239 |
| 236 ScopedJavaLocalRef<jobject> view = | 240 ScopedJavaLocalRef<jobject> view = |
| 237 GetContentViewCoreFromWebContents(download_item->GetWebContents()); | 241 GetContentViewCoreFromWebContents(download_item->GetWebContents()); |
| 238 // The view went away. Can't proceed. | 242 // The view went away. Can't proceed. |
| 239 if (view.is_null()) | 243 if (view.is_null()) |
| 240 return; | 244 return; |
| 241 | 245 |
| 242 Java_DownloadController_onHttpPostDownloadStarted( | 246 Java_DownloadController_onDownloadStarted( |
| 243 env, GetJavaObject()->Controller(env).obj(), view.obj()); | 247 env, GetJavaObject()->Controller(env).obj(), view.obj()); |
| 244 } | 248 } |
| 245 | 249 |
| 246 void DownloadControllerAndroidImpl::OnDownloadUpdated(DownloadItem* item) { | 250 void DownloadControllerAndroidImpl::OnDownloadUpdated(DownloadItem* item) { |
| 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 248 | 252 |
| 249 if (!item->IsComplete()) | 253 if (!item->IsComplete()) |
| 250 return; | 254 return; |
| 251 | 255 |
| 252 // Multiple OnDownloadUpdated() notifications may be issued while the download | 256 // Multiple OnDownloadUpdated() notifications may be issued while the download |
| (...skipping 11 matching lines...) Expand all Loading... |
| 264 ScopedJavaLocalRef<jstring> jpath = | 268 ScopedJavaLocalRef<jstring> jpath = |
| 265 ConvertUTF8ToJavaString(env, item->GetFullPath().value()); | 269 ConvertUTF8ToJavaString(env, item->GetFullPath().value()); |
| 266 | 270 |
| 267 ScopedJavaLocalRef<jobject> view_core = GetContentViewCoreFromWebContents( | 271 ScopedJavaLocalRef<jobject> view_core = GetContentViewCoreFromWebContents( |
| 268 item->GetWebContents()); | 272 item->GetWebContents()); |
| 269 if (view_core.is_null()) { | 273 if (view_core.is_null()) { |
| 270 // We can get NULL WebContents from the DownloadItem. | 274 // We can get NULL WebContents from the DownloadItem. |
| 271 return; | 275 return; |
| 272 } | 276 } |
| 273 | 277 |
| 274 Java_DownloadController_onHttpPostDownloadCompleted(env, | 278 Java_DownloadController_onDownloadCompleted(env, |
| 275 GetJavaObject()->Controller(env).obj(), view_core.obj(), jurl.obj(), | 279 GetJavaObject()->Controller(env).obj(), view_core.obj(), jurl.obj(), |
| 276 jcontent_disposition.obj(), jmime_type.obj(), jpath.obj(), | 280 jcontent_disposition.obj(), jmime_type.obj(), jpath.obj(), |
| 277 item->GetReceivedBytes(), true); | 281 item->GetReceivedBytes(), true); |
| 278 } | 282 } |
| 279 | 283 |
| 280 ScopedJavaLocalRef<jobject> DownloadControllerAndroidImpl::GetContentView( | 284 ScopedJavaLocalRef<jobject> DownloadControllerAndroidImpl::GetContentView( |
| 281 int render_process_id, int render_view_id) { | 285 int render_process_id, int render_view_id) { |
| 282 RenderViewHost* render_view_host = | 286 RenderViewHost* render_view_host = |
| 283 RenderViewHost::FromID(render_process_id, render_view_id); | 287 RenderViewHost::FromID(render_process_id, render_view_id); |
| 284 | 288 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 309 // DownloadController.getInstance(), which will call Init() | 313 // DownloadController.getInstance(), which will call Init() |
| 310 // if Java DownloadController is not instantiated already. | 314 // if Java DownloadController is not instantiated already. |
| 311 JNIEnv* env = base::android::AttachCurrentThread(); | 315 JNIEnv* env = base::android::AttachCurrentThread(); |
| 312 Java_DownloadController_getInstance(env); | 316 Java_DownloadController_getInstance(env); |
| 313 } | 317 } |
| 314 | 318 |
| 315 DCHECK(java_object_); | 319 DCHECK(java_object_); |
| 316 return java_object_; | 320 return java_object_; |
| 317 } | 321 } |
| 318 | 322 |
| 323 void DownloadControllerAndroidImpl::StartContextMenuDownload( |
| 324 const ContextMenuParams& params, WebContents* web_contents, bool is_link) { |
| 325 const GURL& url = is_link ? params.link_url : params.src_url; |
| 326 const GURL& referrer = params.frame_url.is_empty() ? |
| 327 params.page_url : params.frame_url; |
| 328 DownloadManagerImpl* dlm = static_cast<DownloadManagerImpl*>( |
| 329 BrowserContext::GetDownloadManager(web_contents->GetBrowserContext())); |
| 330 scoped_ptr<DownloadUrlParameters> dl_params( |
| 331 DownloadUrlParameters::FromWebContents(web_contents, url)); |
| 332 dl_params->set_referrer( |
| 333 Referrer(referrer, params.referrer_policy)); |
| 334 if (is_link) |
| 335 dl_params->set_referrer_encoding(params.frame_charset); |
| 336 else |
| 337 dl_params->set_prefer_cache(true); |
| 338 dl_params->set_prompt(false); |
| 339 dlm->DownloadUrl(dl_params.Pass()); |
| 340 } |
| 341 |
| 319 DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid( | 342 DownloadControllerAndroidImpl::DownloadInfoAndroid::DownloadInfoAndroid( |
| 320 net::URLRequest* request) { | 343 net::URLRequest* request) { |
| 321 request->GetResponseHeaderByName("content-disposition", &content_disposition); | 344 request->GetResponseHeaderByName("content-disposition", &content_disposition); |
| 322 | 345 |
| 323 if (request->response_headers()) | 346 if (request->response_headers()) |
| 324 request->response_headers()->GetMimeType(&original_mime_type); | 347 request->response_headers()->GetMimeType(&original_mime_type); |
| 325 | 348 |
| 326 request->extra_request_headers().GetHeader( | 349 request->extra_request_headers().GetHeader( |
| 327 net::HttpRequestHeaders::kUserAgent, &user_agent); | 350 net::HttpRequestHeaders::kUserAgent, &user_agent); |
| 328 GURL referer_url(request->referrer()); | 351 GURL referer_url(request->referrer()); |
| 329 if (referer_url.is_valid()) | 352 if (referer_url.is_valid()) |
| 330 referer = referer_url.spec(); | 353 referer = referer_url.spec(); |
| 331 if (!request->url_chain().empty()) { | 354 if (!request->url_chain().empty()) { |
| 332 original_url = request->url_chain().front(); | 355 original_url = request->url_chain().front(); |
| 333 url = request->url_chain().back(); | 356 url = request->url_chain().back(); |
| 334 } | 357 } |
| 335 } | 358 } |
| 336 | 359 |
| 337 DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {} | 360 DownloadControllerAndroidImpl::DownloadInfoAndroid::~DownloadInfoAndroid() {} |
| 338 | 361 |
| 339 } // namespace content | 362 } // namespace content |
| OLD | NEW |