Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: chrome/browser/download/download_request_limiter.h

Issue 10412061: Fix crashes in DownloadRequestLimiter when extension popups/bubbles initiate downloads automatically (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update download_browsertest.cc Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "content/public/browser/notification_observer.h" 16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h" 17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_observer.h"
18 19
19 class DownloadRequestInfoBarDelegate; 20 class DownloadRequestInfoBarDelegate;
20 class TabContentsWrapper; 21 class TabContentsWrapper;
21 22
22 namespace content { 23 namespace content {
23 class NavigationController; 24 class NavigationController;
24 class WebContents; 25 class WebContents;
25 } 26 }
26 27
27 // DownloadRequestLimiter is responsible for determining whether a download 28 // DownloadRequestLimiter is responsible for determining whether a download
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 static const size_t kMaxDownloadsAtOnce = 50; 62 static const size_t kMaxDownloadsAtOnce = 50;
62 63
63 // The callback from CanDownloadOnIOThread. This is invoked on the io thread. 64 // The callback from CanDownloadOnIOThread. This is invoked on the io thread.
64 // The boolean parameter indicates whether or not the download is allowed. 65 // The boolean parameter indicates whether or not the download is allowed.
65 typedef base::Callback<void(bool /*allow*/)> Callback; 66 typedef base::Callback<void(bool /*allow*/)> Callback;
66 67
67 // TabDownloadState maintains the download state for a particular tab. 68 // TabDownloadState maintains the download state for a particular tab.
68 // TabDownloadState prompts the user with an infobar as necessary. 69 // TabDownloadState prompts the user with an infobar as necessary.
69 // TabDownloadState deletes itself (by invoking 70 // TabDownloadState deletes itself (by invoking
70 // DownloadRequestLimiter::Remove) as necessary. 71 // DownloadRequestLimiter::Remove) as necessary.
71 class TabDownloadState : public content::NotificationObserver { 72 class TabDownloadState : public content::NotificationObserver,
73 public content::WebContentsObserver {
72 public: 74 public:
73 // Creates a new TabDownloadState. |controller| is the controller the 75 // Creates a new TabDownloadState. |controller| is the controller the
74 // TabDownloadState tracks the state of and is the host for any dialogs that 76 // TabDownloadState tracks the state of and is the host for any dialogs that
75 // are displayed. |originating_controller| is used to determine the host of 77 // are displayed. |originating_controller| is used to determine the host of
76 // the initial download. If |originating_controller| is null, |controller| 78 // the initial download. If |originating_controller| is null, |controller|
77 // is used. |originating_controller| is typically null, but differs from 79 // is used. |originating_controller| is typically null, but differs from
78 // |controller| in the case of a constrained popup requesting the download. 80 // |controller| in the case of a constrained popup requesting the download.
79 TabDownloadState(DownloadRequestLimiter* host, 81 TabDownloadState(DownloadRequestLimiter* host,
80 content::NavigationController* controller, 82 content::WebContents* web_contents,
81 content::NavigationController* originating_controller); 83 content::WebContents* originating_web_contents);
82 virtual ~TabDownloadState(); 84 virtual ~TabDownloadState();
83 85
84 // Status of the download. 86 // Status of the download.
85 void set_download_status(DownloadRequestLimiter::DownloadStatus status) { 87 void set_download_status(DownloadRequestLimiter::DownloadStatus status) {
86 status_ = status; 88 status_ = status;
87 } 89 }
88 DownloadRequestLimiter::DownloadStatus download_status() const { 90 DownloadRequestLimiter::DownloadStatus download_status() const {
89 return status_; 91 return status_;
90 } 92 }
91 93
92 // Number of "ALLOWED" downloads. 94 // Number of "ALLOWED" downloads.
93 void increment_download_count() { 95 void increment_download_count() {
94 download_count_++; 96 download_count_++;
95 } 97 }
96 size_t download_count() const { 98 size_t download_count() const {
97 return download_count_; 99 return download_count_;
98 } 100 }
99 101
102 content::WebContents* web_contents() {
103 return content::WebContentsObserver::web_contents();
Randy Smith (Not in Mondays) 2012/05/25 18:52:34 This made me do a double take. Maybe worth a comm
benjhayden 2012/05/25 20:41:17 Done.
104 }
105
106 // content::WebContentsObserver overrides.
100 // Invoked when a user gesture occurs (mouse click, enter or space). This 107 // Invoked when a user gesture occurs (mouse click, enter or space). This
101 // may result in invoking Remove on DownloadRequestLimiter. 108 // may result in invoking Remove on DownloadRequestLimiter.
102 void OnUserGesture(); 109 virtual void DidGetUserGesture() OVERRIDE;
103 110
104 // Asks the user if they really want to allow the download. 111 // Asks the user if they really want to allow the download.
105 // See description above CanDownloadOnIOThread for details on lifetime of 112 // See description above CanDownloadOnIOThread for details on lifetime of
106 // callback. 113 // callback.
107 void PromptUserForDownload( 114 void PromptUserForDownload(
108 content::WebContents* tab, 115 content::WebContents* tab,
109 const DownloadRequestLimiter::Callback& callback); 116 const DownloadRequestLimiter::Callback& callback);
110 117
111 // Are we showing a prompt to the user? 118 // Are we showing a prompt to the user?
112 bool is_showing_prompt() const { return (infobar_ != NULL); } 119 bool is_showing_prompt() const { return (infobar_ != NULL); }
113 120
114 // NavigationController we're tracking.
115 content::NavigationController* controller() const { return controller_; }
116
117 // Invoked from DownloadRequestDialogDelegate. Notifies the delegates and 121 // Invoked from DownloadRequestDialogDelegate. Notifies the delegates and
118 // changes the status appropriately. Virtual for testing. 122 // changes the status appropriately. Virtual for testing.
119 virtual void Cancel(); 123 virtual void Cancel();
120 virtual void Accept(); 124 virtual void Accept();
121 125
122 protected: 126 protected:
123 // Used for testing. 127 // Used for testing.
124 TabDownloadState() 128 TabDownloadState()
125 : host_(NULL), 129 : host_(NULL),
126 controller_(NULL),
127 status_(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD), 130 status_(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD),
128 download_count_(0), 131 download_count_(0),
129 infobar_(NULL) { 132 infobar_(NULL) {
130 } 133 }
131 134
132 private: 135 private:
133 // content::NotificationObserver method. 136 // content::NotificationObserver method.
134 virtual void Observe(int type, 137 virtual void Observe(int type,
135 const content::NotificationSource& source, 138 const content::NotificationSource& source,
136 const content::NotificationDetails& details) OVERRIDE; 139 const content::NotificationDetails& details) OVERRIDE;
137 140
138 // Notifies the callbacks as to whether the download is allowed or not. 141 // Notifies the callbacks as to whether the download is allowed or not.
139 // Updates status_ appropriately. 142 // Updates status_ appropriately.
140 void NotifyCallbacks(bool allow); 143 void NotifyCallbacks(bool allow);
141 144
142 DownloadRequestLimiter* host_; 145 DownloadRequestLimiter* host_;
143 146
144 content::NavigationController* controller_;
145
146 // Host of the first page the download started on. This may be empty. 147 // Host of the first page the download started on. This may be empty.
147 std::string initial_page_host_; 148 std::string initial_page_host_;
148 149
149 DownloadRequestLimiter::DownloadStatus status_; 150 DownloadRequestLimiter::DownloadStatus status_;
150 151
151 size_t download_count_; 152 size_t download_count_;
152 153
153 // Callbacks we need to notify. This is only non-empty if we're showing a 154 // Callbacks we need to notify. This is only non-empty if we're showing a
154 // dialog. 155 // dialog.
155 // See description above CanDownloadOnIOThread for details on lifetime of 156 // See description above CanDownloadOnIOThread for details on lifetime of
(...skipping 16 matching lines...) Expand all
172 DownloadStatus GetDownloadStatus(content::WebContents* tab); 173 DownloadStatus GetDownloadStatus(content::WebContents* tab);
173 174
174 // Updates the state of the page as necessary and notifies the callback. 175 // Updates the state of the page as necessary and notifies the callback.
175 // WARNING: both this call and the callback are invoked on the io thread. 176 // WARNING: both this call and the callback are invoked on the io thread.
176 void CanDownloadOnIOThread(int render_process_host_id, 177 void CanDownloadOnIOThread(int render_process_host_id,
177 int render_view_id, 178 int render_view_id,
178 int request_id, 179 int request_id,
179 const std::string& request_method, 180 const std::string& request_method,
180 const Callback& callback); 181 const Callback& callback);
181 182
182 // Invoked when the user presses the mouse, enter key or space bar. This may
183 // change the download status for the page. See the class description for
184 // details.
185 void OnUserGesture(content::WebContents* tab);
186
187 private: 183 private:
188 FRIEND_TEST_ALL_PREFIXES(DownloadTest, DownloadResourceThrottleCancels); 184 FRIEND_TEST_ALL_PREFIXES(DownloadTest, DownloadResourceThrottleCancels);
189 friend class base::RefCountedThreadSafe<DownloadRequestLimiter>; 185 friend class base::RefCountedThreadSafe<DownloadRequestLimiter>;
190 friend class DownloadRequestLimiterTest; 186 friend class DownloadRequestLimiterTest;
191 friend class TabDownloadState; 187 friend class TabDownloadState;
192 188
193 ~DownloadRequestLimiter(); 189 ~DownloadRequestLimiter();
194 190
195 // For unit tests. If non-null this is used instead of creating a dialog. 191 // For unit tests. If non-null this is used instead of creating a dialog.
196 class TestingDelegate { 192 class TestingDelegate {
197 public: 193 public:
198 virtual bool ShouldAllowDownload() = 0; 194 virtual bool ShouldAllowDownload() = 0;
199 195
200 protected: 196 protected:
201 virtual ~TestingDelegate() {} 197 virtual ~TestingDelegate() {}
202 }; 198 };
203 static void SetTestingDelegate(TestingDelegate* delegate); 199 static void SetTestingDelegate(TestingDelegate* delegate);
204 200
205 // Gets the download state for the specified controller. If the 201 // Gets the download state for the specified controller. If the
206 // TabDownloadState does not exist and |create| is true, one is created. 202 // TabDownloadState does not exist and |create| is true, one is created.
207 // See TabDownloadState's constructor description for details on the two 203 // See TabDownloadState's constructor description for details on the two
208 // controllers. 204 // controllers.
209 // 205 //
210 // The returned TabDownloadState is owned by the DownloadRequestLimiter and 206 // The returned TabDownloadState is owned by the DownloadRequestLimiter and
211 // deleted when no longer needed (the Remove method is invoked). 207 // deleted when no longer needed (the Remove method is invoked).
212 TabDownloadState* GetDownloadState( 208 TabDownloadState* GetDownloadState(
213 content::NavigationController* controller, 209 content::WebContents* web_contents,
214 content::NavigationController* originating_controller, 210 content::WebContents* originating_web_contents,
215 bool create); 211 bool create);
216 212
217 // CanDownloadOnIOThread invokes this on the UI thread. This determines the 213 // CanDownloadOnIOThread invokes this on the UI thread. This determines the
218 // tab and invokes CanDownloadImpl. 214 // tab and invokes CanDownloadImpl.
219 void CanDownload(int render_process_host_id, 215 void CanDownload(int render_process_host_id,
220 int render_view_id, 216 int render_view_id,
221 int request_id, 217 int request_id,
222 const std::string& request_method, 218 const std::string& request_method,
223 const Callback& callback); 219 const Callback& callback);
224 220
225 // Does the work of updating the download status on the UI thread and 221 // Does the work of updating the download status on the UI thread and
226 // potentially prompting the user. 222 // potentially prompting the user.
227 void CanDownloadImpl(TabContentsWrapper* originating_tab, 223 void CanDownloadImpl(content::WebContents* originating_contents,
228 int request_id, 224 int request_id,
229 const std::string& request_method, 225 const std::string& request_method,
230 const Callback& callback); 226 const Callback& callback);
231 227
232 // Invoked on the UI thread. Schedules a call to NotifyCallback on the io 228 // Invoked on the UI thread. Schedules a call to NotifyCallback on the io
233 // thread. 229 // thread.
234 void ScheduleNotification(const Callback& callback, bool allow); 230 void ScheduleNotification(const Callback& callback, bool allow);
235 231
236 // Removes the specified TabDownloadState from the internal map and deletes 232 // Removes the specified TabDownloadState from the internal map and deletes
237 // it. This has the effect of resetting the status for the tab to 233 // it. This has the effect of resetting the status for the tab to
238 // ALLOW_ONE_DOWNLOAD. 234 // ALLOW_ONE_DOWNLOAD.
239 void Remove(TabDownloadState* state); 235 void Remove(TabDownloadState* state);
240 236
241 // Maps from tab to download state. The download state for a tab only exists 237 // Maps from tab to download state. The download state for a tab only exists
242 // if the state is other than ALLOW_ONE_DOWNLOAD. Similarly once the state 238 // if the state is other than ALLOW_ONE_DOWNLOAD. Similarly once the state
243 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD 239 // transitions from anything but ALLOW_ONE_DOWNLOAD back to ALLOW_ONE_DOWNLOAD
244 // the TabDownloadState is removed and deleted (by way of Remove). 240 // the TabDownloadState is removed and deleted (by way of Remove).
245 typedef std::map<content::NavigationController*, TabDownloadState*> StateMap; 241 typedef std::map<content::WebContents*, TabDownloadState*> StateMap;
246 StateMap state_map_; 242 StateMap state_map_;
247 243
248 static TestingDelegate* delegate_; 244 static TestingDelegate* delegate_;
249 245
250 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter); 246 DISALLOW_COPY_AND_ASSIGN(DownloadRequestLimiter);
251 }; 247 };
252 248
253 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_ 249 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_LIMITER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698