Chromium Code Reviews| 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; | 28 class NavigationController; |
|
dominickn
2017/01/24 06:46:17
Nit: I think you can remove NavigationController
alshabalin
2017/02/07 16:16:29
Done.
| |
| 29 class WebContents; | 29 class WebContents; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // DownloadRequestLimiter is responsible for determining whether a download | 32 // DownloadRequestLimiter is responsible for determining whether a download |
| 33 // should be allowed or not. It is designed to keep pages from downloading | 33 // should be allowed or not. It is designed to keep pages from downloading |
| 34 // multiple files without user interaction. DownloadRequestLimiter is invoked | 34 // multiple files without user interaction. DownloadRequestLimiter is invoked |
| 35 // from ResourceDispatcherHost any time a download begins | 35 // from ResourceDispatcherHost any time a download begins |
| 36 // (CanDownloadOnIOThread). The request is processed on the UI thread, and the | 36 // (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 | 37 // request is notified (back on the IO thread) as to whether the download should |
| 38 // be allowed or denied. | 38 // be allowed or denied. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 67 | 67 |
| 68 // The callback from CanDownloadOnIOThread. This is invoked on the io thread. | 68 // The callback from CanDownloadOnIOThread. This is invoked on the io thread. |
| 69 // The boolean parameter indicates whether or not the download is allowed. | 69 // The boolean parameter indicates whether or not the download is allowed. |
| 70 typedef base::Callback<void(bool /*allow*/)> Callback; | 70 typedef base::Callback<void(bool /*allow*/)> Callback; |
| 71 | 71 |
| 72 // TabDownloadState maintains the download state for a particular tab. | 72 // TabDownloadState maintains the download state for a particular tab. |
| 73 // TabDownloadState prompts the user with an infobar as necessary. | 73 // TabDownloadState prompts the user with an infobar as necessary. |
| 74 // TabDownloadState deletes itself (by invoking | 74 // TabDownloadState deletes itself (by invoking |
| 75 // DownloadRequestLimiter::Remove) as necessary. | 75 // DownloadRequestLimiter::Remove) as necessary. |
| 76 // TODO(gbillock): just make this class implement PermissionRequest. | 76 // TODO(gbillock): just make this class implement PermissionRequest. |
| 77 class TabDownloadState : public content::NotificationObserver, | 77 class TabDownloadState : public content_settings::Observer, |
| 78 public content::WebContentsObserver { | 78 public content::WebContentsObserver { |
| 79 public: | 79 public: |
| 80 // Creates a new TabDownloadState. |controller| is the controller the | 80 // Creates a new TabDownloadState. |controller| is the controller the |
| 81 // TabDownloadState tracks the state of and is the host for any dialogs that | 81 // 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 | 82 // are displayed. |originating_controller| is used to determine the host of |
| 83 // the initial download. If |originating_controller| is null, |controller| | 83 // the initial download. If |originating_controller| is null, |controller| |
| 84 // is used. |originating_controller| is typically null, but differs from | 84 // is used. |originating_controller| is typically null, but differs from |
| 85 // |controller| in the case of a constrained popup requesting the download. | 85 // |controller| in the case of a constrained popup requesting the download. |
| 86 TabDownloadState(DownloadRequestLimiter* host, | 86 TabDownloadState(DownloadRequestLimiter* host, |
| 87 content::WebContents* web_contents, | 87 content::WebContents* web_contents, |
| 88 content::WebContents* originating_web_contents); | 88 content::WebContents* originating_web_contents); |
| 89 ~TabDownloadState() override; | 89 ~TabDownloadState() override; |
| 90 | 90 |
| 91 // Status of the download. | 91 // Status of the download. |
|
dominickn
2017/01/24 06:46:17
Nit: "Sets the current limiter state and the under
alshabalin
2017/02/07 16:16:29
Done.
| |
| 92 void set_download_status(DownloadRequestLimiter::DownloadStatus status) { | 92 void SetDownloadStatusAndNotify( |
| 93 status_ = status; | 93 DownloadRequestLimiter::DownloadStatus status); |
| 94 } | |
| 95 DownloadRequestLimiter::DownloadStatus download_status() const { | 94 DownloadRequestLimiter::DownloadStatus download_status() const { |
| 96 return status_; | 95 return status_; |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Number of "ALLOWED" downloads. | 98 // Number of "ALLOWED" downloads. |
| 100 void increment_download_count() { | 99 void increment_download_count() { |
| 101 download_count_++; | 100 download_count_++; |
| 102 } | 101 } |
| 103 size_t download_count() const { | 102 size_t download_count() const { |
| 104 return download_count_; | 103 return download_count_; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 129 protected: | 128 protected: |
| 130 // Used for testing. | 129 // Used for testing. |
| 131 TabDownloadState(); | 130 TabDownloadState(); |
| 132 | 131 |
| 133 private: | 132 private: |
| 134 // Are we showing a prompt to the user? Determined by whether | 133 // Are we showing a prompt to the user? Determined by whether |
| 135 // we have an outstanding weak pointer--weak pointers are only | 134 // we have an outstanding weak pointer--weak pointers are only |
| 136 // given to the info bar delegate or permission bubble request. | 135 // given to the info bar delegate or permission bubble request. |
| 137 bool is_showing_prompt() const; | 136 bool is_showing_prompt() const; |
| 138 | 137 |
| 139 // content::NotificationObserver method. | 138 // content_settings::Observer overrides. |
| 140 void Observe(int type, | 139 void OnContentSettingChanged( |
| 141 const content::NotificationSource& source, | 140 const ContentSettingsPattern& primary_pattern, |
| 142 const content::NotificationDetails& details) override; | 141 const ContentSettingsPattern& secondary_pattern, |
| 142 ContentSettingsType content_type, | |
| 143 std::string resource_identifier) override; | |
| 143 | 144 |
| 144 // Remember to either block or allow automatic downloads from this origin. | 145 // Remember to either block or allow automatic downloads from this origin. |
| 145 void SetContentSetting(ContentSetting setting); | 146 void SetContentSetting(ContentSetting setting); |
| 146 | 147 |
| 147 // Notifies the callbacks as to whether the download is allowed or not. | 148 // Notifies the callbacks as to whether the download is allowed or not. |
| 148 // Updates status_ appropriately. | 149 // Returns false if it didn't notify all callbacks. |
| 149 void NotifyCallbacks(bool allow); | 150 bool NotifyCallbacks(bool allow); |
| 150 | 151 |
| 151 content::WebContents* web_contents_; | 152 content::WebContents* web_contents_; |
| 152 | 153 |
| 153 DownloadRequestLimiter* host_; | 154 DownloadRequestLimiter* host_; |
| 154 | 155 |
| 155 // Host of the first page the download started on. This may be empty. | 156 // Host of the first page the download started on. This may be empty. |
| 156 std::string initial_page_host_; | 157 std::string initial_page_host_; |
| 157 | 158 |
| 158 DownloadRequestLimiter::DownloadStatus status_; | 159 DownloadRequestLimiter::DownloadStatus status_; |
| 159 | 160 |
| 160 size_t download_count_; | 161 size_t download_count_; |
| 161 | 162 |
| 162 // Callbacks we need to notify. This is only non-empty if we're showing a | 163 // Callbacks we need to notify. This is only non-empty if we're showing a |
| 163 // dialog. | 164 // dialog. |
| 164 // See description above CanDownloadOnIOThread for details on lifetime of | 165 // See description above CanDownloadOnIOThread for details on lifetime of |
| 165 // callbacks. | 166 // callbacks. |
| 166 std::vector<DownloadRequestLimiter::Callback> callbacks_; | 167 std::vector<DownloadRequestLimiter::Callback> callbacks_; |
| 167 | 168 |
| 168 // Used to remove observers installed on NavigationController. | 169 ScopedObserver<HostContentSettingsMap, content_settings::Observer> |
| 169 content::NotificationRegistrar registrar_; | 170 observer_; |
| 170 | 171 |
| 171 // Weak pointer factory for generating a weak pointer to pass to the | 172 // Weak pointer factory for generating a weak pointer to pass to the |
| 172 // infobar. User responses to the throttling prompt will be returned | 173 // infobar. User responses to the throttling prompt will be returned |
| 173 // through this channel, and it can be revoked if the user prompt result | 174 // through this channel, and it can be revoked if the user prompt result |
| 174 // becomes moot. | 175 // becomes moot. |
| 175 base::WeakPtrFactory<DownloadRequestLimiter::TabDownloadState> factory_; | 176 base::WeakPtrFactory<DownloadRequestLimiter::TabDownloadState> factory_; |
| 176 | 177 |
| 177 DISALLOW_COPY_AND_ASSIGN(TabDownloadState); | 178 DISALLOW_COPY_AND_ASSIGN(TabDownloadState); |
| 178 }; | 179 }; |
| 179 | 180 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 | 234 |
| 234 static HostContentSettingsMap* content_settings_; | 235 static HostContentSettingsMap* content_settings_; |
| 235 static HostContentSettingsMap* GetContentSettings( | 236 static HostContentSettingsMap* GetContentSettings( |
| 236 content::WebContents* contents); | 237 content::WebContents* contents); |
| 237 | 238 |
| 238 // Maps from tab to download state. The download state for a tab only exists | 239 // 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 | 240 // 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 | 241 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD |
| 241 // the TabDownloadState is removed and deleted (by way of Remove). | 242 // the TabDownloadState is removed and deleted (by way of Remove). |
| 242 typedef std::map<content::WebContents*, TabDownloadState*> StateMap; | 243 typedef std::map<content::WebContents*, TabDownloadState*> StateMap; |
| 243 StateMap state_map_; | 244 StateMap state_map_; |
|
Bernhard Bauer
2017/01/25 19:01:14
This is existing code, but is there a reason this
dominickn
2017/01/26 07:37:05
The last time this line was changed was 2008. The
Bernhard Bauer
2017/01/26 11:10:00
Oh wow. Thanks for the investigation :)
alshabali
alshabalin
2017/02/07 16:16:29
Only added a TODO here, sorry.
| |
| 244 | 245 |
| 245 // Weak ptr factory used when |CanDownload| asks the delegate asynchronously | 246 // Weak ptr factory used when |CanDownload| asks the delegate asynchronously |
| 246 // about the download. | 247 // about the download. |
| 247 base::WeakPtrFactory<DownloadRequestLimiter> factory_; | 248 base::WeakPtrFactory<DownloadRequestLimiter> factory_; |
| 248 | 249 |
| 249 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); | 250 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); |
| 250 }; | 251 }; |
| 251 | 252 |
| 252 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ | 253 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ |
| OLD | NEW |