OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_request_limiter.h" | 5 #include "chrome/browser/download/download_request_limiter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/download/download_request_infobar_delegate.h" | 9 #include "chrome/browser/download/download_request_infobar_delegate.h" |
10 #include "chrome/browser/infobars/infobar_tab_helper.h" | 10 #include "chrome/browser/infobars/infobar_tab_helper.h" |
11 #include "chrome/browser/tab_contents/tab_util.h" | 11 #include "chrome/browser/tab_contents/tab_util.h" |
12 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 12 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
13 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper_delegate.
h" | 13 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper_delegate.
h" |
14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
15 #include "content/browser/tab_contents/tab_contents.h" | 15 #include "content/browser/tab_contents/tab_contents.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
18 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/notification_types.h" | 20 #include "content/public/browser/notification_types.h" |
21 #include "content/public/browser/web_contents_delegate.h" | 21 #include "content/public/browser/web_contents_delegate.h" |
22 | 22 |
23 using content::BrowserThread; | 23 using content::BrowserThread; |
| 24 using content::NavigationController; |
24 using content::NavigationEntry; | 25 using content::NavigationEntry; |
25 using content::WebContents; | 26 using content::WebContents; |
26 | 27 |
27 // TabDownloadState ------------------------------------------------------------ | 28 // TabDownloadState ------------------------------------------------------------ |
28 | 29 |
29 DownloadRequestLimiter::TabDownloadState::TabDownloadState( | 30 DownloadRequestLimiter::TabDownloadState::TabDownloadState( |
30 DownloadRequestLimiter* host, | 31 DownloadRequestLimiter* host, |
31 content::NavigationController* controller, | 32 NavigationController* controller, |
32 content::NavigationController* originating_controller) | 33 NavigationController* originating_controller) |
33 : host_(host), | 34 : host_(host), |
34 controller_(controller), | 35 controller_(controller), |
35 status_(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD), | 36 status_(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD), |
36 download_count_(0), | 37 download_count_(0), |
37 infobar_(NULL) { | 38 infobar_(NULL) { |
38 content::Source<content::NavigationController> notification_source( | 39 content::Source<NavigationController> notification_source(controller); |
39 controller); | |
40 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 40 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
41 notification_source); | 41 notification_source); |
42 registrar_.Add(this, content::NOTIFICATION_TAB_CLOSED, notification_source); | 42 registrar_.Add(this, content::NOTIFICATION_TAB_CLOSED, notification_source); |
43 | 43 |
44 NavigationEntry* active_entry = originating_controller ? | 44 NavigationEntry* active_entry = originating_controller ? |
45 originating_controller->GetActiveEntry() : controller->GetActiveEntry(); | 45 originating_controller->GetActiveEntry() : controller->GetActiveEntry(); |
46 if (active_entry) | 46 if (active_entry) |
47 initial_page_host_ = active_entry->GetURL().host(); | 47 initial_page_host_ = active_entry->GetURL().host(); |
48 } | 48 } |
49 | 49 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 void DownloadRequestLimiter::TabDownloadState::Accept() { | 96 void DownloadRequestLimiter::TabDownloadState::Accept() { |
97 NotifyCallbacks(true); | 97 NotifyCallbacks(true); |
98 } | 98 } |
99 | 99 |
100 void DownloadRequestLimiter::TabDownloadState::Observe( | 100 void DownloadRequestLimiter::TabDownloadState::Observe( |
101 int type, | 101 int type, |
102 const content::NotificationSource& source, | 102 const content::NotificationSource& source, |
103 const content::NotificationDetails& details) { | 103 const content::NotificationDetails& details) { |
104 if ((type != content::NOTIFICATION_NAV_ENTRY_PENDING && | 104 if ((type != content::NOTIFICATION_NAV_ENTRY_PENDING && |
105 type != content::NOTIFICATION_TAB_CLOSED) || | 105 type != content::NOTIFICATION_TAB_CLOSED) || |
106 content::Source<content::NavigationController>(source).ptr() != | 106 content::Source<NavigationController>(source).ptr() != controller_) { |
107 controller_) { | |
108 NOTREACHED(); | 107 NOTREACHED(); |
109 return; | 108 return; |
110 } | 109 } |
111 | 110 |
112 switch (type) { | 111 switch (type) { |
113 case content::NOTIFICATION_NAV_ENTRY_PENDING: { | 112 case content::NOTIFICATION_NAV_ENTRY_PENDING: { |
114 // NOTE: resetting state on a pending navigate isn't ideal. In particular | 113 // NOTE: resetting state on a pending navigate isn't ideal. In particular |
115 // it is possible that queued up downloads for the page before the | 114 // it is possible that queued up downloads for the page before the |
116 // pending navigate will be delivered to us after we process this | 115 // pending navigate will be delivered to us after we process this |
117 // request. If this happens we may let a download through that we | 116 // request. If this happens we may let a download through that we |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 223 |
225 state->OnUserGesture(); | 224 state->OnUserGesture(); |
226 } | 225 } |
227 | 226 |
228 // static | 227 // static |
229 void DownloadRequestLimiter::SetTestingDelegate(TestingDelegate* delegate) { | 228 void DownloadRequestLimiter::SetTestingDelegate(TestingDelegate* delegate) { |
230 delegate_ = delegate; | 229 delegate_ = delegate; |
231 } | 230 } |
232 | 231 |
233 DownloadRequestLimiter::TabDownloadState* DownloadRequestLimiter:: | 232 DownloadRequestLimiter::TabDownloadState* DownloadRequestLimiter:: |
234 GetDownloadState(content::NavigationController* controller, | 233 GetDownloadState(NavigationController* controller, |
235 content::NavigationController* originating_controller, | 234 NavigationController* originating_controller, |
236 bool create) { | 235 bool create) { |
237 DCHECK(controller); | 236 DCHECK(controller); |
238 StateMap::iterator i = state_map_.find(controller); | 237 StateMap::iterator i = state_map_.find(controller); |
239 if (i != state_map_.end()) | 238 if (i != state_map_.end()) |
240 return i->second; | 239 return i->second; |
241 | 240 |
242 if (!create) | 241 if (!create) |
243 return NULL; | 242 return NULL; |
244 | 243 |
245 TabDownloadState* state = | 244 TabDownloadState* state = |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 339 |
341 void DownloadRequestLimiter::Remove(TabDownloadState* state) { | 340 void DownloadRequestLimiter::Remove(TabDownloadState* state) { |
342 DCHECK(ContainsKey(state_map_, state->controller())); | 341 DCHECK(ContainsKey(state_map_, state->controller())); |
343 state_map_.erase(state->controller()); | 342 state_map_.erase(state->controller()); |
344 delete state; | 343 delete state; |
345 } | 344 } |
346 | 345 |
347 // static | 346 // static |
348 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = | 347 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = |
349 NULL; | 348 NULL; |
OLD | NEW |