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/resource_controller.h" | 9 #include "content/public/browser/resource_controller.h" |
10 | 10 |
11 #if defined(OS_ANDROID) | |
12 #include "content/public/browser/android/download_controller_android.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/render_view_host.h" | |
15 | |
16 using content::BrowserThread; | |
asanka
2015/07/15 20:23:00
Looks like non-Android code below also depend on t
qinmin
2015/07/17 00:42:35
Done.
| |
17 using content::DownloadControllerAndroid; | |
18 #endif | |
19 | |
20 namespace { | |
21 | |
22 void OnCanDownloadDecided( | |
23 const DownloadRequestLimiter::Callback& callback, bool allow) { | |
24 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
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( |
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 17 matching lines...) Expand all Loading... | |
54 if (querying_limiter_) { | 107 if (querying_limiter_) { |
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) { |
asanka
2015/07/15 20:23:00
Add a thread assertion here.
qinmin
2015/07/17 00:42:35
Done.
| |
65 querying_limiter_ = false; | 118 querying_limiter_ = false; |
66 request_allowed_ = allow; | 119 request_allowed_ = allow; |
67 | 120 |
68 if (allow) { | 121 if (allow) { |
69 // Presumes all downloads initiated by navigation use this throttle and | 122 // Presumes all downloads initiated by navigation use this throttle and |
70 // nothing else does. | 123 // nothing else does. |
71 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); | 124 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION); |
72 } else { | 125 } else { |
73 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); | 126 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING); |
74 } | 127 } |
75 | 128 |
76 if (request_deferred_) { | 129 if (request_deferred_) { |
77 request_deferred_ = false; | 130 request_deferred_ = false; |
78 if (allow) { | 131 if (allow) { |
79 controller()->Resume(); | 132 controller()->Resume(); |
80 } else { | 133 } else { |
81 controller()->Cancel(); | 134 controller()->Cancel(); |
82 } | 135 } |
83 } | 136 } |
84 } | 137 } |
OLD | NEW |