Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/android/download/download_controller.cc

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

Powered by Google App Engine
This is Rietveld 408576698