| 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" | |
| 16 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
| 19 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/web_contents.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::NavigationController; |
| 25 using content::NavigationEntry; | 25 using content::NavigationEntry; |
| 26 using content::WebContents; | 26 using content::WebContents; |
| 27 | 27 |
| 28 // TabDownloadState ------------------------------------------------------------ | 28 // TabDownloadState ------------------------------------------------------------ |
| 29 | 29 |
| 30 DownloadRequestLimiter::TabDownloadState::TabDownloadState( | 30 DownloadRequestLimiter::TabDownloadState::TabDownloadState( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 if (status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS && | 64 if (status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS && |
| 65 status_ != DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED) { | 65 status_ != DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED) { |
| 66 // Revert to default status. | 66 // Revert to default status. |
| 67 host_->Remove(this); | 67 host_->Remove(this); |
| 68 // WARNING: We've been deleted. | 68 // WARNING: We've been deleted. |
| 69 return; | 69 return; |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( | 73 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( |
| 74 TabContents* tab, | 74 WebContents* tab, |
| 75 DownloadRequestLimiter::Callback* callback) { | 75 DownloadRequestLimiter::Callback* callback) { |
| 76 callbacks_.push_back(callback); | 76 callbacks_.push_back(callback); |
| 77 | 77 |
| 78 if (is_showing_prompt()) | 78 if (is_showing_prompt()) |
| 79 return; // Already showing prompt. | 79 return; // Already showing prompt. |
| 80 | 80 |
| 81 if (DownloadRequestLimiter::delegate_) { | 81 if (DownloadRequestLimiter::delegate_) { |
| 82 NotifyCallbacks(DownloadRequestLimiter::delegate_->ShouldAllowDownload()); | 82 NotifyCallbacks(DownloadRequestLimiter::delegate_->ShouldAllowDownload()); |
| 83 } else { | 83 } else { |
| 84 InfoBarTabHelper* infobar_helper = | 84 InfoBarTabHelper* infobar_helper = |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 DownloadRequestLimiter::DownloadRequestLimiter() { | 190 DownloadRequestLimiter::DownloadRequestLimiter() { |
| 191 } | 191 } |
| 192 | 192 |
| 193 DownloadRequestLimiter::~DownloadRequestLimiter() { | 193 DownloadRequestLimiter::~DownloadRequestLimiter() { |
| 194 // All the tabs should have closed before us, which sends notification and | 194 // All the tabs should have closed before us, which sends notification and |
| 195 // removes from state_map_. As such, there should be no pending callbacks. | 195 // removes from state_map_. As such, there should be no pending callbacks. |
| 196 DCHECK(state_map_.empty()); | 196 DCHECK(state_map_.empty()); |
| 197 } | 197 } |
| 198 | 198 |
| 199 DownloadRequestLimiter::DownloadStatus | 199 DownloadRequestLimiter::DownloadStatus |
| 200 DownloadRequestLimiter::GetDownloadStatus(TabContents* tab) { | 200 DownloadRequestLimiter::GetDownloadStatus(WebContents* tab) { |
| 201 TabDownloadState* state = GetDownloadState(&tab->GetController(), NULL, false)
; | 201 TabDownloadState* state = GetDownloadState(&tab->GetController(), NULL, false)
; |
| 202 return state ? state->download_status() : ALLOW_ONE_DOWNLOAD; | 202 return state ? state->download_status() : ALLOW_ONE_DOWNLOAD; |
| 203 } | 203 } |
| 204 | 204 |
| 205 void DownloadRequestLimiter::CanDownloadOnIOThread(int render_process_host_id, | 205 void DownloadRequestLimiter::CanDownloadOnIOThread(int render_process_host_id, |
| 206 int render_view_id, | 206 int render_view_id, |
| 207 int request_id, | 207 int request_id, |
| 208 Callback* callback) { | 208 Callback* callback) { |
| 209 // This is invoked on the IO thread. Schedule the task to run on the UI | 209 // This is invoked on the IO thread. Schedule the task to run on the UI |
| 210 // thread so that we can query UI state. | 210 // thread so that we can query UI state. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 state_map_[controller] = state; | 246 state_map_[controller] = state; |
| 247 return state; | 247 return state; |
| 248 } | 248 } |
| 249 | 249 |
| 250 void DownloadRequestLimiter::CanDownload(int render_process_host_id, | 250 void DownloadRequestLimiter::CanDownload(int render_process_host_id, |
| 251 int render_view_id, | 251 int render_view_id, |
| 252 int request_id, | 252 int request_id, |
| 253 Callback* callback) { | 253 Callback* callback) { |
| 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 255 | 255 |
| 256 TabContents* originating_tab = | 256 WebContents* originating_tab = |
| 257 tab_util::GetTabContentsByID(render_process_host_id, render_view_id); | 257 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); |
| 258 if (!originating_tab) { | 258 if (!originating_tab) { |
| 259 // The tab was closed, don't allow the download. | 259 // The tab was closed, don't allow the download. |
| 260 ScheduleNotification(callback, false); | 260 ScheduleNotification(callback, false); |
| 261 return; | 261 return; |
| 262 } | 262 } |
| 263 | 263 |
| 264 CanDownloadImpl( | 264 CanDownloadImpl( |
| 265 TabContentsWrapper::GetCurrentWrapperForContents(originating_tab), | 265 TabContentsWrapper::GetCurrentWrapperForContents(originating_tab), |
| 266 request_id, | 266 request_id, |
| 267 callback); | 267 callback); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 284 | 284 |
| 285 // If the tab requesting the download is a constrained popup that is not | 285 // If the tab requesting the download is a constrained popup that is not |
| 286 // shown, treat the request as if it came from the parent. | 286 // shown, treat the request as if it came from the parent. |
| 287 TabContentsWrapper* effective_wrapper = originating_tab; | 287 TabContentsWrapper* effective_wrapper = originating_tab; |
| 288 if (effective_wrapper->blocked_content_tab_helper()->delegate()) { | 288 if (effective_wrapper->blocked_content_tab_helper()->delegate()) { |
| 289 effective_wrapper = effective_wrapper->blocked_content_tab_helper()-> | 289 effective_wrapper = effective_wrapper->blocked_content_tab_helper()-> |
| 290 delegate()->GetConstrainingContentsWrapper(effective_wrapper); | 290 delegate()->GetConstrainingContentsWrapper(effective_wrapper); |
| 291 } | 291 } |
| 292 | 292 |
| 293 TabDownloadState* state = GetDownloadState( | 293 TabDownloadState* state = GetDownloadState( |
| 294 &effective_wrapper->tab_contents()->GetController(), | 294 &effective_wrapper->web_contents()->GetController(), |
| 295 &tab->GetController(), true); | 295 &tab->GetController(), true); |
| 296 switch (state->download_status()) { | 296 switch (state->download_status()) { |
| 297 case ALLOW_ALL_DOWNLOADS: | 297 case ALLOW_ALL_DOWNLOADS: |
| 298 if (state->download_count() && !(state->download_count() % | 298 if (state->download_count() && !(state->download_count() % |
| 299 DownloadRequestLimiter::kMaxDownloadsAtOnce)) | 299 DownloadRequestLimiter::kMaxDownloadsAtOnce)) |
| 300 state->set_download_status(PROMPT_BEFORE_DOWNLOAD); | 300 state->set_download_status(PROMPT_BEFORE_DOWNLOAD); |
| 301 ScheduleNotification(callback, true); | 301 ScheduleNotification(callback, true); |
| 302 state->increment_download_count(); | 302 state->increment_download_count(); |
| 303 break; | 303 break; |
| 304 | 304 |
| 305 case ALLOW_ONE_DOWNLOAD: | 305 case ALLOW_ONE_DOWNLOAD: |
| 306 state->set_download_status(PROMPT_BEFORE_DOWNLOAD); | 306 state->set_download_status(PROMPT_BEFORE_DOWNLOAD); |
| 307 ScheduleNotification(callback, true); | 307 ScheduleNotification(callback, true); |
| 308 break; | 308 break; |
| 309 | 309 |
| 310 case DOWNLOADS_NOT_ALLOWED: | 310 case DOWNLOADS_NOT_ALLOWED: |
| 311 ScheduleNotification(callback, false); | 311 ScheduleNotification(callback, false); |
| 312 break; | 312 break; |
| 313 | 313 |
| 314 case PROMPT_BEFORE_DOWNLOAD: | 314 case PROMPT_BEFORE_DOWNLOAD: |
| 315 state->PromptUserForDownload(effective_wrapper->tab_contents(), callback); | 315 state->PromptUserForDownload(effective_wrapper->web_contents(), callback); |
| 316 state->increment_download_count(); | 316 state->increment_download_count(); |
| 317 break; | 317 break; |
| 318 | 318 |
| 319 default: | 319 default: |
| 320 NOTREACHED(); | 320 NOTREACHED(); |
| 321 } | 321 } |
| 322 } | 322 } |
| 323 | 323 |
| 324 void DownloadRequestLimiter::ScheduleNotification(Callback* callback, | 324 void DownloadRequestLimiter::ScheduleNotification(Callback* callback, |
| 325 bool allow) { | 325 bool allow) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 339 | 339 |
| 340 void DownloadRequestLimiter::Remove(TabDownloadState* state) { | 340 void DownloadRequestLimiter::Remove(TabDownloadState* state) { |
| 341 DCHECK(ContainsKey(state_map_, state->controller())); | 341 DCHECK(ContainsKey(state_map_, state->controller())); |
| 342 state_map_.erase(state->controller()); | 342 state_map_.erase(state->controller()); |
| 343 delete state; | 343 delete state; |
| 344 } | 344 } |
| 345 | 345 |
| 346 // static | 346 // static |
| 347 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = | 347 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = |
| 348 NULL; | 348 NULL; |
| OLD | NEW |