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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/scoped_observer.h" | |
20 #include "components/content_settings/core/browser/content_settings_observer.h" | |
19 #include "components/content_settings/core/common/content_settings.h" | 21 #include "components/content_settings/core/common/content_settings.h" |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
22 #include "content/public/browser/resource_request_info.h" | 22 #include "content/public/browser/resource_request_info.h" |
23 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
24 | 24 |
25 class HostContentSettingsMap; | 25 class HostContentSettingsMap; |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 class NavigationController; | |
29 class WebContents; | 28 class WebContents; |
30 } | 29 } |
31 | 30 |
32 // DownloadRequestLimiter is responsible for determining whether a download | 31 // DownloadRequestLimiter is responsible for determining whether a download |
33 // should be allowed or not. It is designed to keep pages from downloading | 32 // should be allowed or not. It is designed to keep pages from downloading |
34 // multiple files without user interaction. DownloadRequestLimiter is invoked | 33 // multiple files without user interaction. DownloadRequestLimiter is invoked |
35 // from ResourceDispatcherHost any time a download begins | 34 // from ResourceDispatcherHost any time a download begins |
36 // (CanDownloadOnIOThread). The request is processed on the UI thread, and the | 35 // (CanDownloadOnIOThread). The request is processed on the UI thread, and the |
37 // request is notified (back on the IO thread) as to whether the download should | 36 // request is notified (back on the IO thread) as to whether the download should |
38 // be allowed or denied. | 37 // be allowed or denied. |
(...skipping 28 matching lines...) Expand all Loading... | |
67 | 66 |
68 // The callback from CanDownloadOnIOThread. This is invoked on the io thread. | 67 // The callback from CanDownloadOnIOThread. This is invoked on the io thread. |
69 // The boolean parameter indicates whether or not the download is allowed. | 68 // The boolean parameter indicates whether or not the download is allowed. |
70 typedef base::Callback<void(bool /*allow*/)> Callback; | 69 typedef base::Callback<void(bool /*allow*/)> Callback; |
71 | 70 |
72 // TabDownloadState maintains the download state for a particular tab. | 71 // TabDownloadState maintains the download state for a particular tab. |
73 // TabDownloadState prompts the user with an infobar as necessary. | 72 // TabDownloadState prompts the user with an infobar as necessary. |
74 // TabDownloadState deletes itself (by invoking | 73 // TabDownloadState deletes itself (by invoking |
75 // DownloadRequestLimiter::Remove) as necessary. | 74 // DownloadRequestLimiter::Remove) as necessary. |
76 // TODO(gbillock): just make this class implement PermissionRequest. | 75 // TODO(gbillock): just make this class implement PermissionRequest. |
77 class TabDownloadState : public content::NotificationObserver, | 76 class TabDownloadState : public content_settings::Observer, |
78 public content::WebContentsObserver { | 77 public content::WebContentsObserver { |
79 public: | 78 public: |
80 // Creates a new TabDownloadState. |controller| is the controller the | 79 // Creates a new TabDownloadState. |controller| is the controller the |
81 // TabDownloadState tracks the state of and is the host for any dialogs that | 80 // TabDownloadState tracks the state of and is the host for any dialogs that |
82 // are displayed. |originating_controller| is used to determine the host of | 81 // are displayed. |originating_controller| is used to determine the host of |
83 // the initial download. If |originating_controller| is null, |controller| | 82 // the initial download. If |originating_controller| is null, |controller| |
84 // is used. |originating_controller| is typically null, but differs from | 83 // is used. |originating_controller| is typically null, but differs from |
85 // |controller| in the case of a constrained popup requesting the download. | 84 // |controller| in the case of a constrained popup requesting the download. |
86 TabDownloadState(DownloadRequestLimiter* host, | 85 TabDownloadState(DownloadRequestLimiter* host, |
87 content::WebContents* web_contents, | 86 content::WebContents* web_contents, |
88 content::WebContents* originating_web_contents); | 87 content::WebContents* originating_web_contents); |
89 ~TabDownloadState() override; | 88 ~TabDownloadState() override; |
90 | 89 |
90 // Sets the current limiter state and the underlying automatic downloads | |
91 // content setting. Sends a notification that the content setting has been | |
92 // changed (if it has changed). | |
93 void SetDownloadStatusAndNotify( | |
94 DownloadRequestLimiter::DownloadStatus status); | |
95 // Updates the current limiter state with given content setting and | |
Bernhard Bauer
2017/02/07 17:02:57
Nit: Please add empty lines before comments.
alshabalin
2017/02/08 11:16:14
Done.
| |
96 // notifies if the setting was actually changed. | |
97 void UpdateDownloadStatusAndNotify(ContentSetting setting); | |
91 // Status of the download. | 98 // Status of the download. |
92 void set_download_status(DownloadRequestLimiter::DownloadStatus status) { | |
93 status_ = status; | |
94 } | |
95 DownloadRequestLimiter::DownloadStatus download_status() const { | 99 DownloadRequestLimiter::DownloadStatus download_status() const { |
96 return status_; | 100 return status_; |
97 } | 101 } |
98 | 102 |
99 // Number of "ALLOWED" downloads. | 103 // Number of "ALLOWED" downloads. |
100 void increment_download_count() { | 104 void increment_download_count() { |
101 download_count_++; | 105 download_count_++; |
102 } | 106 } |
103 size_t download_count() const { | 107 size_t download_count() const { |
104 return download_count_; | 108 return download_count_; |
(...skipping 24 matching lines...) Expand all Loading... | |
129 protected: | 133 protected: |
130 // Used for testing. | 134 // Used for testing. |
131 TabDownloadState(); | 135 TabDownloadState(); |
132 | 136 |
133 private: | 137 private: |
134 // Are we showing a prompt to the user? Determined by whether | 138 // Are we showing a prompt to the user? Determined by whether |
135 // we have an outstanding weak pointer--weak pointers are only | 139 // we have an outstanding weak pointer--weak pointers are only |
136 // given to the info bar delegate or permission bubble request. | 140 // given to the info bar delegate or permission bubble request. |
137 bool is_showing_prompt() const; | 141 bool is_showing_prompt() const; |
138 | 142 |
139 // content::NotificationObserver method. | 143 // content_settings::Observer overrides. |
140 void Observe(int type, | 144 void OnContentSettingChanged( |
141 const content::NotificationSource& source, | 145 const ContentSettingsPattern& primary_pattern, |
142 const content::NotificationDetails& details) override; | 146 const ContentSettingsPattern& secondary_pattern, |
147 ContentSettingsType content_type, | |
148 std::string resource_identifier) override; | |
143 | 149 |
144 // Remember to either block or allow automatic downloads from this origin. | 150 // Remember to either block or allow automatic downloads from this origin. |
145 void SetContentSetting(ContentSetting setting); | 151 void SetContentSetting(ContentSetting setting); |
146 | 152 |
147 // Notifies the callbacks as to whether the download is allowed or not. | 153 // Notifies the callbacks as to whether the download is allowed or not. |
148 // Updates status_ appropriately. | 154 // Returns false if it didn't notify all callbacks. |
149 void NotifyCallbacks(bool allow); | 155 bool NotifyCallbacks(bool allow); |
150 | 156 |
151 content::WebContents* web_contents_; | 157 content::WebContents* web_contents_; |
152 | 158 |
153 DownloadRequestLimiter* host_; | 159 DownloadRequestLimiter* host_; |
154 | 160 |
155 // Host of the first page the download started on. This may be empty. | 161 // Host of the first page the download started on. This may be empty. |
156 std::string initial_page_host_; | 162 std::string initial_page_host_; |
157 | 163 |
158 DownloadRequestLimiter::DownloadStatus status_; | 164 DownloadRequestLimiter::DownloadStatus status_; |
159 | 165 |
160 size_t download_count_; | 166 size_t download_count_; |
161 | 167 |
162 // Callbacks we need to notify. This is only non-empty if we're showing a | 168 // Callbacks we need to notify. This is only non-empty if we're showing a |
163 // dialog. | 169 // dialog. |
164 // See description above CanDownloadOnIOThread for details on lifetime of | 170 // See description above CanDownloadOnIOThread for details on lifetime of |
165 // callbacks. | 171 // callbacks. |
166 std::vector<DownloadRequestLimiter::Callback> callbacks_; | 172 std::vector<DownloadRequestLimiter::Callback> callbacks_; |
167 | 173 |
168 // Used to remove observers installed on NavigationController. | 174 ScopedObserver<HostContentSettingsMap, content_settings::Observer> |
169 content::NotificationRegistrar registrar_; | 175 observer_; |
170 | 176 |
171 // Weak pointer factory for generating a weak pointer to pass to the | 177 // Weak pointer factory for generating a weak pointer to pass to the |
172 // infobar. User responses to the throttling prompt will be returned | 178 // infobar. User responses to the throttling prompt will be returned |
173 // through this channel, and it can be revoked if the user prompt result | 179 // through this channel, and it can be revoked if the user prompt result |
174 // becomes moot. | 180 // becomes moot. |
175 base::WeakPtrFactory<DownloadRequestLimiter::TabDownloadState> factory_; | 181 base::WeakPtrFactory<DownloadRequestLimiter::TabDownloadState> factory_; |
176 | 182 |
177 DISALLOW_COPY_AND_ASSIGN(TabDownloadState); | 183 DISALLOW_COPY_AND_ASSIGN(TabDownloadState); |
178 }; | 184 }; |
179 | 185 |
180 static void SetContentSettingsForTesting(HostContentSettingsMap* settings); | 186 static void SetContentSettingsForTesting(HostContentSettingsMap* settings); |
181 | 187 |
182 DownloadRequestLimiter(); | 188 DownloadRequestLimiter(); |
183 | 189 |
184 // Returns the download status for a page. This does not change the state in | 190 // Returns the download status for a page. This does not change the state in |
185 // anyway. | 191 // anyway. |
186 DownloadStatus GetDownloadStatus(content::WebContents* tab); | 192 DownloadStatus GetDownloadStatus(content::WebContents* tab); |
187 | 193 |
188 // Check if download can proceed and notifies the callback on UI thread. | 194 // Check if download can proceed and notifies the callback on UI thread. |
189 void CanDownload(const content::ResourceRequestInfo::WebContentsGetter& | 195 void CanDownload(const content::ResourceRequestInfo::WebContentsGetter& |
190 web_contents_getter, | 196 web_contents_getter, |
191 const GURL& url, | 197 const GURL& url, |
192 const std::string& request_method, | 198 const std::string& request_method, |
193 const Callback& callback); | 199 const Callback& callback); |
194 | 200 |
195 private: | 201 private: |
196 FRIEND_TEST_ALL_PREFIXES(DownloadTest, DownloadResourceThrottleCancels); | 202 FRIEND_TEST_ALL_PREFIXES(DownloadTest, DownloadResourceThrottleCancels); |
203 FRIEND_TEST_ALL_PREFIXES(ContentSettingImageModelBrowserTest, | |
204 CreateBubbleModel); | |
197 friend class base::RefCountedThreadSafe<DownloadRequestLimiter>; | 205 friend class base::RefCountedThreadSafe<DownloadRequestLimiter>; |
198 friend class DownloadRequestLimiterTest; | 206 friend class DownloadRequestLimiterTest; |
199 friend class TabDownloadState; | 207 friend class TabDownloadState; |
200 | 208 |
201 ~DownloadRequestLimiter(); | 209 ~DownloadRequestLimiter(); |
202 | 210 |
203 // Gets the download state for the specified controller. If the | 211 // Gets the download state for the specified controller. If the |
204 // TabDownloadState does not exist and |create| is true, one is created. | 212 // TabDownloadState does not exist and |create| is true, one is created. |
205 // See TabDownloadState's constructor description for details on the two | 213 // See TabDownloadState's constructor description for details on the two |
206 // controllers. | 214 // controllers. |
(...skipping 21 matching lines...) Expand all Loading... | |
228 | 236 |
229 // Removes the specified TabDownloadState from the internal map and deletes | 237 // Removes the specified TabDownloadState from the internal map and deletes |
230 // it. This has the effect of resetting the status for the tab to | 238 // it. This has the effect of resetting the status for the tab to |
231 // ALLOW_ONE_DOWNLOAD. | 239 // ALLOW_ONE_DOWNLOAD. |
232 void Remove(TabDownloadState* state, content::WebContents* contents); | 240 void Remove(TabDownloadState* state, content::WebContents* contents); |
233 | 241 |
234 static HostContentSettingsMap* content_settings_; | 242 static HostContentSettingsMap* content_settings_; |
235 static HostContentSettingsMap* GetContentSettings( | 243 static HostContentSettingsMap* GetContentSettings( |
236 content::WebContents* contents); | 244 content::WebContents* contents); |
237 | 245 |
246 // TODO(bauerb): Change this to use WebContentsUserData. | |
238 // Maps from tab to download state. The download state for a tab only exists | 247 // Maps from tab to download state. The download state for a tab only exists |
239 // if the state is other than ALLOW_ONE_DOWNLOAD. Similarly once the state | 248 // if the state is other than ALLOW_ONE_DOWNLOAD. Similarly once the state |
240 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD | 249 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD |
241 // the TabDownloadState is removed and deleted (by way of Remove). | 250 // the TabDownloadState is removed and deleted (by way of Remove). |
242 typedef std::map<content::WebContents*, TabDownloadState*> StateMap; | 251 typedef std::map<content::WebContents*, TabDownloadState*> StateMap; |
243 StateMap state_map_; | 252 StateMap state_map_; |
244 | 253 |
245 // Weak ptr factory used when |CanDownload| asks the delegate asynchronously | 254 // Weak ptr factory used when |CanDownload| asks the delegate asynchronously |
246 // about the download. | 255 // about the download. |
247 base::WeakPtrFactory<DownloadRequestLimiter> factory_; | 256 base::WeakPtrFactory<DownloadRequestLimiter> factory_; |
248 | 257 |
249 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); | 258 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); |
250 }; | 259 }; |
251 | 260 |
252 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ | 261 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ |
OLD | NEW |