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

Side by Side Diff: chrome/browser/download/download_resource_throttle.cc

Issue 1229933010: move file access permission logic to DownloadResourceThrottle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressing comments 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 "chrome/browser/download/download_resource_throttle.h" 5 #include "chrome/browser/download/download_resource_throttle.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/download/download_stats.h" 8 #include "chrome/browser/download/download_stats.h"
9 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/resource_controller.h" 10 #include "content/public/browser/resource_controller.h"
10 11
12 #if defined(OS_ANDROID)
13 #include "content/public/browser/android/download_controller_android.h"
14 #include "content/public/browser/render_view_host.h"
15
16 using content::DownloadControllerAndroid;
17 #endif
18
19 using content::BrowserThread;
20
21 namespace {
22
23 void OnCanDownloadDecided(
24 const DownloadRequestLimiter::Callback& callback, bool allow) {
25 BrowserThread::PostTask(
26 BrowserThread::IO, FROM_HERE, base::Bind(callback, allow));
27 }
28
29 #if defined(OS_ANDROID)
30 void OnAcquireFileAccessPermissionDone(
31 const base::Closure& can_download_callback,
32 const DownloadRequestLimiter::Callback& cancel_callback,
33 bool granted) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 if (granted)
36 can_download_callback.Run();
37 else
38 OnCanDownloadDecided(cancel_callback, false);
39 }
40
41 void AcquireFileAccessPermission(
42 int render_process_id,
43 int render_view_id,
44 const base::Closure& can_download_callback,
45 const DownloadRequestLimiter::Callback& cancel_callback) {
46 DCHECK_CURRENTLY_ON(BrowserThread::UI);
47 content::DownloadControllerAndroid::Get()->AcquireFileAccessPermission(
48 render_process_id, render_view_id, base::Bind(
49 &OnAcquireFileAccessPermissionDone,
50 can_download_callback, cancel_callback));
51 }
52 #endif
53
54 } // namespace
55
11 DownloadResourceThrottle::DownloadResourceThrottle( 56 DownloadResourceThrottle::DownloadResourceThrottle(
12 DownloadRequestLimiter* limiter, 57 scoped_refptr<DownloadRequestLimiter> limiter,
13 int render_process_id, 58 int render_process_id,
14 int render_view_id, 59 int render_view_id,
15 const GURL& url, 60 const GURL& url,
16 const std::string& request_method) 61 const std::string& request_method)
17 : querying_limiter_(true), 62 : querying_limiter_(true),
18 request_allowed_(false), 63 request_allowed_(false),
19 request_deferred_(false) { 64 request_deferred_(false) {
20 limiter->CanDownloadOnIOThread( 65 DCHECK_CURRENTLY_ON(BrowserThread::IO);
21 render_process_id, 66 DownloadRequestLimiter::Callback continue_callback = base::Bind(
asanka 2015/07/20 20:15:24 Given that none of this logic needs to happen on t
qinmin 2015/07/20 22:11:02 Done. Still leaves DownloadResourceThrottle on the
22 render_view_id, 67 &DownloadResourceThrottle::ContinueDownload, AsWeakPtr());
23 url, 68 base::Closure can_download_callback = base::Bind(
24 request_method, 69 &DownloadRequestLimiter::CanDownload, limiter, render_process_id,
25 base::Bind(&DownloadResourceThrottle::ContinueDownload, 70 render_view_id, url, request_method, base::Bind(
26 AsWeakPtr())); 71 &OnCanDownloadDecided, continue_callback));
72 #if defined(OS_ANDROID)
73 BrowserThread::PostTask(
74 BrowserThread::UI, FROM_HERE, base::Bind(
75 &AcquireFileAccessPermission, render_process_id, render_view_id,
76 can_download_callback, continue_callback));
77 #else
78 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, can_download_callback);
79 #endif
27 } 80 }
28 81
29 DownloadResourceThrottle::~DownloadResourceThrottle() { 82 DownloadResourceThrottle::~DownloadResourceThrottle() {
30 } 83 }
31 84
32 void DownloadResourceThrottle::WillStartRequest(bool* defer) { 85 void DownloadResourceThrottle::WillStartRequest(bool* defer) {
33 WillDownload(defer); 86 WillDownload(defer);
34 } 87 }
35 88
36 void DownloadResourceThrottle::WillRedirectRequest( 89 void DownloadResourceThrottle::WillRedirectRequest(
(...skipping 18 matching lines...) Expand all
55 request_deferred_ = true; 108 request_deferred_ = true;
56 *defer = true; 109 *defer = true;
57 return; 110 return;
58 } 111 }
59 112
60 if (!request_allowed_) 113 if (!request_allowed_)
61 controller()->Cancel(); 114 controller()->Cancel();
62 } 115 }
63 116
64 void DownloadResourceThrottle::ContinueDownload(bool allow) { 117 void DownloadResourceThrottle::ContinueDownload(bool allow) {
118 DCHECK_CURRENTLY_ON(BrowserThread::IO);
65 querying_limiter_ = false; 119 querying_limiter_ = false;
66 request_allowed_ = allow; 120 request_allowed_ = allow;
67 121
68 if (allow) { 122 if (allow) {
69 // Presumes all downloads initiated by navigation use this throttle and 123 // Presumes all downloads initiated by navigation use this throttle and
70 // nothing else does. 124 // nothing else does.
71 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); 125 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION);
72 } else { 126 } else {
73 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); 127 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING);
74 } 128 }
75 129
76 if (request_deferred_) { 130 if (request_deferred_) {
77 request_deferred_ = false; 131 request_deferred_ = false;
78 if (allow) { 132 if (allow) {
79 controller()->Resume(); 133 controller()->Resume();
80 } else { 134 } else {
81 controller()->Cancel(); 135 controller()->Cancel();
82 } 136 }
83 } 137 }
84 } 138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698