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 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "components/content_settings/core/common/content_settings.h" | 16 #include "components/content_settings/core/common/content_settings.h" |
17 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "content/public/browser/resource_request_info.h" |
19 #include "content/public/browser/web_contents_observer.h" | 20 #include "content/public/browser/web_contents_observer.h" |
20 | 21 |
21 class HostContentSettingsMap; | 22 class HostContentSettingsMap; |
22 class DownloadRequestInfoBarDelegateAndroid; | 23 class DownloadRequestInfoBarDelegateAndroid; |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 class NavigationController; | 26 class NavigationController; |
26 class WebContents; | 27 class WebContents; |
27 } | 28 } |
28 | 29 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 176 |
176 static void SetContentSettingsForTesting(HostContentSettingsMap* settings); | 177 static void SetContentSettingsForTesting(HostContentSettingsMap* settings); |
177 | 178 |
178 DownloadRequestLimiter(); | 179 DownloadRequestLimiter(); |
179 | 180 |
180 // Returns the download status for a page. This does not change the state in | 181 // Returns the download status for a page. This does not change the state in |
181 // anyway. | 182 // anyway. |
182 DownloadStatus GetDownloadStatus(content::WebContents* tab); | 183 DownloadStatus GetDownloadStatus(content::WebContents* tab); |
183 | 184 |
184 // Check if download can proceed and notifies the callback on UI thread. | 185 // Check if download can proceed and notifies the callback on UI thread. |
185 void CanDownload(int render_process_host_id, | 186 void CanDownload( |
186 int render_view_id, | 187 const content::ResourceRequestInfo::WebContentsGetterOnUIThread& |
187 const GURL& url, | 188 web_contents_getter, |
188 const std::string& request_method, | 189 const GURL& url, |
189 const Callback& callback); | 190 const std::string& request_method, |
| 191 const Callback& callback); |
190 | 192 |
191 private: | 193 private: |
192 FRIEND_TEST_ALL_PREFIXES(DownloadTest, DownloadResourceThrottleCancels); | 194 FRIEND_TEST_ALL_PREFIXES(DownloadTest, DownloadResourceThrottleCancels); |
193 friend class base::RefCountedThreadSafe<DownloadRequestLimiter>; | 195 friend class base::RefCountedThreadSafe<DownloadRequestLimiter>; |
194 friend class DownloadRequestLimiterTest; | 196 friend class DownloadRequestLimiterTest; |
195 friend class TabDownloadState; | 197 friend class TabDownloadState; |
196 | 198 |
197 ~DownloadRequestLimiter(); | 199 ~DownloadRequestLimiter(); |
198 | 200 |
199 // Gets the download state for the specified controller. If the | 201 // Gets the download state for the specified controller. If the |
200 // TabDownloadState does not exist and |create| is true, one is created. | 202 // TabDownloadState does not exist and |create| is true, one is created. |
201 // See TabDownloadState's constructor description for details on the two | 203 // See TabDownloadState's constructor description for details on the two |
202 // controllers. | 204 // controllers. |
203 // | 205 // |
204 // The returned TabDownloadState is owned by the DownloadRequestLimiter and | 206 // The returned TabDownloadState is owned by the DownloadRequestLimiter and |
205 // deleted when no longer needed (the Remove method is invoked). | 207 // deleted when no longer needed (the Remove method is invoked). |
206 TabDownloadState* GetDownloadState( | 208 TabDownloadState* GetDownloadState( |
207 content::WebContents* web_contents, | 209 content::WebContents* web_contents, |
208 content::WebContents* originating_web_contents, | 210 content::WebContents* originating_web_contents, |
209 bool create); | 211 bool create); |
210 | 212 |
211 // Does the work of updating the download status on the UI thread and | 213 // Does the work of updating the download status on the UI thread and |
212 // potentially prompting the user. | 214 // potentially prompting the user. |
213 void CanDownloadImpl(content::WebContents* originating_contents, | 215 void CanDownloadImpl(content::WebContents* originating_contents, |
214 const std::string& request_method, | 216 const std::string& request_method, |
215 const Callback& callback); | 217 const Callback& callback); |
216 | 218 |
217 // Invoked when decision to download has been made. | 219 // Invoked when decision to download has been made. |
218 void OnCanDownloadDecided(int render_process_host_id, | 220 void OnCanDownloadDecided( |
219 int render_view_id, | 221 const content::ResourceRequestInfo::WebContentsGetterOnUIThread& |
220 const std::string& request_method, | 222 web_contents_getter, |
221 const Callback& orig_callback, | 223 const std::string& request_method, |
222 bool allow); | 224 const Callback& orig_callback, |
| 225 bool allow); |
223 | 226 |
224 // Removes the specified TabDownloadState from the internal map and deletes | 227 // Removes the specified TabDownloadState from the internal map and deletes |
225 // it. This has the effect of resetting the status for the tab to | 228 // it. This has the effect of resetting the status for the tab to |
226 // ALLOW_ONE_DOWNLOAD. | 229 // ALLOW_ONE_DOWNLOAD. |
227 void Remove(TabDownloadState* state, content::WebContents* contents); | 230 void Remove(TabDownloadState* state, content::WebContents* contents); |
228 | 231 |
229 static HostContentSettingsMap* content_settings_; | 232 static HostContentSettingsMap* content_settings_; |
230 static HostContentSettingsMap* GetContentSettings( | 233 static HostContentSettingsMap* GetContentSettings( |
231 content::WebContents* contents); | 234 content::WebContents* contents); |
232 | 235 |
233 // Maps from tab to download state. The download state for a tab only exists | 236 // Maps from tab to download state. The download state for a tab only exists |
234 // if the state is other than ALLOW_ONE_DOWNLOAD. Similarly once the state | 237 // if the state is other than ALLOW_ONE_DOWNLOAD. Similarly once the state |
235 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD | 238 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD |
236 // the TabDownloadState is removed and deleted (by way of Remove). | 239 // the TabDownloadState is removed and deleted (by way of Remove). |
237 typedef std::map<content::WebContents*, TabDownloadState*> StateMap; | 240 typedef std::map<content::WebContents*, TabDownloadState*> StateMap; |
238 StateMap state_map_; | 241 StateMap state_map_; |
239 | 242 |
240 // Weak ptr factory used when |CanDownload| asks the delegate asynchronously | 243 // Weak ptr factory used when |CanDownload| asks the delegate asynchronously |
241 // about the download. | 244 // about the download. |
242 base::WeakPtrFactory<DownloadRequestLimiter> factory_; | 245 base::WeakPtrFactory<DownloadRequestLimiter> factory_; |
243 | 246 |
244 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); | 247 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); |
245 }; | 248 }; |
246 | 249 |
247 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ | 250 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ |
OLD | NEW |