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

Side by Side Diff: content/browser/android/download_controller_android_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698