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(base::WeakPtr<DownloadResourceThrottle> throttle, |
| 24 bool allow) { |
| 25 BrowserThread::PostTask( |
| 26 BrowserThread::IO, FROM_HERE, |
| 27 base::Bind(&DownloadResourceThrottle::ContinueDownload, throttle, allow)); |
| 28 } |
| 29 |
| 30 void CanDownload( |
| 31 scoped_ptr<DownloadResourceThrottle::DownloadRequestInfo> info) { |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 33 info->limiter->CanDownload(info->render_process_id, info->render_view_id, |
| 34 info->url, info->request_method, |
| 35 info->continue_callback); |
| 36 } |
| 37 |
| 38 #if defined(OS_ANDROID) |
| 39 void OnAcquireFileAccessPermissionDone( |
| 40 scoped_ptr<DownloadResourceThrottle::DownloadRequestInfo> info, |
| 41 bool granted) { |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 43 if (granted) |
| 44 CanDownload(info.Pass()); |
| 45 else |
| 46 info->continue_callback.Run(false); |
| 47 } |
| 48 #endif |
| 49 |
| 50 void CanDownloadOnUIThread( |
| 51 scoped_ptr<DownloadResourceThrottle::DownloadRequestInfo> info) { |
| 52 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 53 #if defined(OS_ANDROID) |
| 54 content::DownloadControllerAndroid::Get()->AcquireFileAccessPermission( |
| 55 info->render_process_id, info->render_view_id, |
| 56 base::Bind(&OnAcquireFileAccessPermissionDone, |
| 57 base::Passed(info.Pass()))); |
| 58 #else |
| 59 CanDownload(info.Pass()); |
| 60 #endif |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 DownloadResourceThrottle::DownloadRequestInfo::DownloadRequestInfo( |
| 66 scoped_refptr<DownloadRequestLimiter> limiter, |
13 int render_process_id, | 67 int render_process_id, |
14 int render_view_id, | 68 int render_view_id, |
15 const GURL& url, | 69 const GURL& url, |
| 70 const std::string& request_method, |
| 71 const DownloadRequestLimiter::Callback& continue_callback) |
| 72 : limiter(limiter), |
| 73 render_process_id(render_process_id), |
| 74 render_view_id(render_view_id), |
| 75 url(url), |
| 76 request_method(request_method), |
| 77 continue_callback(continue_callback) {} |
| 78 |
| 79 DownloadResourceThrottle::DownloadRequestInfo::~DownloadRequestInfo() {} |
| 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, |
23 url, | 93 base::Bind( |
24 request_method, | 94 &CanDownloadOnUIThread, |
25 base::Bind(&DownloadResourceThrottle::ContinueDownload, | 95 base::Passed(scoped_ptr<DownloadRequestInfo>(new DownloadRequestInfo( |
26 AsWeakPtr())); | 96 limiter, render_process_id, render_view_id, url, request_method, |
| 97 base::Bind(&OnCanDownloadDecided, AsWeakPtr())))))); |
27 } | 98 } |
28 | 99 |
29 DownloadResourceThrottle::~DownloadResourceThrottle() { | 100 DownloadResourceThrottle::~DownloadResourceThrottle() { |
30 } | 101 } |
31 | 102 |
32 void DownloadResourceThrottle::WillStartRequest(bool* defer) { | 103 void DownloadResourceThrottle::WillStartRequest(bool* defer) { |
33 WillDownload(defer); | 104 WillDownload(defer); |
34 } | 105 } |
35 | 106 |
36 void DownloadResourceThrottle::WillRedirectRequest( | 107 void DownloadResourceThrottle::WillRedirectRequest( |
(...skipping 18 matching lines...) Expand all Loading... |
55 request_deferred_ = true; | 126 request_deferred_ = true; |
56 *defer = true; | 127 *defer = true; |
57 return; | 128 return; |
58 } | 129 } |
59 | 130 |
60 if (!request_allowed_) | 131 if (!request_allowed_) |
61 controller()->Cancel(); | 132 controller()->Cancel(); |
62 } | 133 } |
63 | 134 |
64 void DownloadResourceThrottle::ContinueDownload(bool allow) { | 135 void DownloadResourceThrottle::ContinueDownload(bool allow) { |
| 136 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
65 querying_limiter_ = false; | 137 querying_limiter_ = false; |
66 request_allowed_ = allow; | 138 request_allowed_ = allow; |
67 | 139 |
68 if (allow) { | 140 if (allow) { |
69 // Presumes all downloads initiated by navigation use this throttle and | 141 // Presumes all downloads initiated by navigation use this throttle and |
70 // nothing else does. | 142 // nothing else does. |
71 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); | 143 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); |
72 } else { | 144 } else { |
73 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); | 145 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); |
74 } | 146 } |
75 | 147 |
76 if (request_deferred_) { | 148 if (request_deferred_) { |
77 request_deferred_ = false; | 149 request_deferred_ = false; |
78 if (allow) { | 150 if (allow) { |
79 controller()->Resume(); | 151 controller()->Resume(); |
80 } else { | 152 } else { |
81 controller()->Cancel(); | 153 controller()->Cancel(); |
82 } | 154 } |
83 } | 155 } |
84 } | 156 } |
OLD | NEW |