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 "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 |
11 DownloadResourceThrottle::DownloadResourceThrottle( | 12 #if defined(OS_ANDROID) |
12 DownloadRequestLimiter* limiter, | 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 void CanDownload( | |
30 scoped_refptr<DownloadRequestLimiter> limiter, | |
13 int render_process_id, | 31 int render_process_id, |
14 int render_view_id, | 32 int render_view_id, |
15 const GURL& url, | 33 const GURL& url, |
34 const std::string& request_method, | |
35 const DownloadRequestLimiter::Callback& continue_callback) { | |
36 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
37 limiter->CanDownload(render_process_id, render_view_id, url, request_method, | |
38 base::Bind(&OnCanDownloadDecided, continue_callback)); | |
39 } | |
40 | |
41 #if defined(OS_ANDROID) | |
42 void OnAcquireFileAccessPermissionDone( | |
43 scoped_refptr<DownloadRequestLimiter> limiter, | |
44 int render_process_id, | |
45 int render_view_id, | |
46 const GURL& url, | |
47 const std::string& request_method, | |
48 const DownloadRequestLimiter::Callback& continue_callback, | |
49 bool granted) { | |
50 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
51 if (granted) { | |
52 CanDownload(limiter, render_process_id, render_view_id, url, | |
53 request_method, continue_callback); | |
54 } else { | |
55 OnCanDownloadDecided(continue_callback, false); | |
56 } | |
57 } | |
58 #endif | |
59 | |
60 void CanDownloadOnUIThread( | |
61 scoped_refptr<DownloadRequestLimiter> limiter, | |
62 int render_process_id, | |
63 int render_view_id, | |
64 const GURL& url, | |
65 const std::string& request_method, | |
66 const DownloadRequestLimiter::Callback& continue_callback) { | |
67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
68 #if defined(OS_ANDROID) | |
69 content::DownloadControllerAndroid::Get()->AcquireFileAccessPermission( | |
70 render_process_id, render_view_id, base::Bind( | |
71 &OnAcquireFileAccessPermissionDone, limiter, render_process_id, | |
72 render_view_id, url, request_method, continue_callback)); | |
73 #else | |
74 CanDownload(limiter, render_process_id, render_view_id, url, | |
75 request_method, continue_callback); | |
76 #endif | |
77 } | |
78 | |
79 } // namespace | |
80 | |
81 DownloadResourceThrottle::DownloadResourceThrottle( | |
82 scoped_refptr<DownloadRequestLimiter> limiter, | |
83 int render_process_id, | |
84 int render_view_id, | |
85 const GURL& url, | |
16 const std::string& request_method) | 86 const std::string& request_method) |
17 : querying_limiter_(true), | 87 : querying_limiter_(true), |
18 request_allowed_(false), | 88 request_allowed_(false), |
19 request_deferred_(false) { | 89 request_deferred_(false) { |
20 limiter->CanDownloadOnIOThread( | 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
21 render_process_id, | 91 BrowserThread::PostTask( |
22 render_view_id, | 92 BrowserThread::UI, FROM_HERE, base::Bind( |
23 url, | 93 &CanDownloadOnUIThread, limiter, render_process_id, render_view_id, |
asanka
2015/07/21 01:14:00
Nit: When you find yourself binding / unpacking /
qinmin
2015/07/21 22:42:17
Done.
| |
24 request_method, | 94 url, request_method,base::Bind( |
asanka
2015/07/21 01:14:00
Nit: space after ,
Also run through 'git cl forma
qinmin
2015/07/21 22:42:17
Done.
| |
25 base::Bind(&DownloadResourceThrottle::ContinueDownload, | 95 &DownloadResourceThrottle::ContinueDownload, AsWeakPtr()))); |
asanka
2015/07/21 01:14:00
Nit: Rather than passing up a callback that must b
qinmin
2015/07/21 22:42:17
In that case, I need to make DownloadResourceThrot
| |
26 AsWeakPtr())); | |
27 } | 96 } |
28 | 97 |
29 DownloadResourceThrottle::~DownloadResourceThrottle() { | 98 DownloadResourceThrottle::~DownloadResourceThrottle() { |
30 } | 99 } |
31 | 100 |
32 void DownloadResourceThrottle::WillStartRequest(bool* defer) { | 101 void DownloadResourceThrottle::WillStartRequest(bool* defer) { |
33 WillDownload(defer); | 102 WillDownload(defer); |
34 } | 103 } |
35 | 104 |
36 void DownloadResourceThrottle::WillRedirectRequest( | 105 void DownloadResourceThrottle::WillRedirectRequest( |
(...skipping 18 matching lines...) Expand all Loading... | |
55 request_deferred_ = true; | 124 request_deferred_ = true; |
56 *defer = true; | 125 *defer = true; |
57 return; | 126 return; |
58 } | 127 } |
59 | 128 |
60 if (!request_allowed_) | 129 if (!request_allowed_) |
61 controller()->Cancel(); | 130 controller()->Cancel(); |
62 } | 131 } |
63 | 132 |
64 void DownloadResourceThrottle::ContinueDownload(bool allow) { | 133 void DownloadResourceThrottle::ContinueDownload(bool allow) { |
134 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
65 querying_limiter_ = false; | 135 querying_limiter_ = false; |
66 request_allowed_ = allow; | 136 request_allowed_ = allow; |
67 | 137 |
68 if (allow) { | 138 if (allow) { |
69 // Presumes all downloads initiated by navigation use this throttle and | 139 // Presumes all downloads initiated by navigation use this throttle and |
70 // nothing else does. | 140 // nothing else does. |
71 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); | 141 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); |
72 } else { | 142 } else { |
73 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); | 143 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); |
74 } | 144 } |
75 | 145 |
76 if (request_deferred_) { | 146 if (request_deferred_) { |
77 request_deferred_ = false; | 147 request_deferred_ = false; |
78 if (allow) { | 148 if (allow) { |
79 controller()->Resume(); | 149 controller()->Resume(); |
80 } else { | 150 } else { |
81 controller()->Cancel(); | 151 controller()->Cancel(); |
82 } | 152 } |
83 } | 153 } |
84 } | 154 } |
OLD | NEW |