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 #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.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
18 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
19 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
20 #include "content/public/browser/web_contents.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; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // And we should have closed the infobar. | 58 // And we should have closed the infobar. |
59 DCHECK(!infobar_); | 59 DCHECK(!infobar_); |
60 } | 60 } |
61 | 61 |
62 void DownloadRequestLimiter::TabDownloadState::DidGetUserGesture() { | 62 void DownloadRequestLimiter::TabDownloadState::DidGetUserGesture() { |
63 if (is_showing_prompt()) { | 63 if (is_showing_prompt()) { |
64 // Don't change the state if the user clicks on the page some where. | 64 // Don't change the state if the user clicks on the page some where. |
65 return; | 65 return; |
66 } | 66 } |
67 | 67 |
68 TabContentsWrapper* tab_wrapper = | 68 TabContents* tab_contents = |
69 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); | 69 TabContents::GetOwningTabContentsForWebContents(web_contents()); |
70 // See PromptUserForDownload(): if there's no TCW, then DOWNLOADS_NOT_ALLOWED | 70 // See PromptUserForDownload(): if there's no TCW, then DOWNLOADS_NOT_ALLOWED |
71 // is functionally equivalent to PROMPT_BEFORE_DOWNLOAD. | 71 // is functionally equivalent to PROMPT_BEFORE_DOWNLOAD. |
72 if ((tab_wrapper && | 72 if ((tab_contents && |
73 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS && | 73 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS && |
74 status_ != DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED) || | 74 status_ != DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED) || |
75 (!tab_wrapper && | 75 (!tab_contents && |
76 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS)) { | 76 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS)) { |
77 // Revert to default status. | 77 // Revert to default status. |
78 host_->Remove(this); | 78 host_->Remove(this); |
79 // WARNING: We've been deleted. | 79 // WARNING: We've been deleted. |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( | 83 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( |
84 WebContents* web_contents, | 84 WebContents* web_contents, |
85 const DownloadRequestLimiter::Callback& callback) { | 85 const DownloadRequestLimiter::Callback& callback) { |
86 callbacks_.push_back(callback); | 86 callbacks_.push_back(callback); |
87 | 87 |
88 if (is_showing_prompt()) | 88 if (is_showing_prompt()) |
89 return; // Already showing prompt. | 89 return; // Already showing prompt. |
90 | 90 |
91 if (DownloadRequestLimiter::delegate_) { | 91 if (DownloadRequestLimiter::delegate_) { |
92 NotifyCallbacks(DownloadRequestLimiter::delegate_->ShouldAllowDownload()); | 92 NotifyCallbacks(DownloadRequestLimiter::delegate_->ShouldAllowDownload()); |
93 return; | 93 return; |
94 } | 94 } |
95 TabContentsWrapper* tab_wrapper = | 95 TabContents* tab_contents = |
96 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); | 96 TabContents::GetOwningTabContentsForWebContents(web_contents); |
97 if (!tab_wrapper) { | 97 if (!tab_contents) { |
98 // If |web_contents| doesn't have a tab_wrapper, then it isn't what a user | 98 // If |web_contents| doesn't have a TabContents, then it isn't what a user |
99 // thinks of as a tab, it's actually a "raw" WebContents like those used | 99 // thinks of as a tab, it's actually a "raw" WebContents like those used |
100 // for extension popups/bubbles and hosted apps etc. | 100 // for extension popups/bubbles and hosted apps etc. |
101 // TODO(benjhayden): If this is an automatic download from an extension, | 101 // TODO(benjhayden): If this is an automatic download from an extension, |
102 // it would be convenient for the extension author if we send a message to | 102 // it would be convenient for the extension author if we send a message to |
103 // the extension's DevTools console (as we do for CSP) about how | 103 // the extension's DevTools console (as we do for CSP) about how |
104 // extensions should use chrome.downloads.download() (requires the | 104 // extensions should use chrome.downloads.download() (requires the |
105 // "downloads" permission) to automatically download >1 files. | 105 // "downloads" permission) to automatically download >1 files. |
106 Cancel(); | 106 Cancel(); |
107 return; | 107 return; |
108 } | 108 } |
109 InfoBarTabHelper* infobar_helper = tab_wrapper->infobar_tab_helper(); | 109 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper(); |
110 infobar_ = new DownloadRequestInfoBarDelegate(infobar_helper, this); | 110 infobar_ = new DownloadRequestInfoBarDelegate(infobar_helper, this); |
111 infobar_helper->AddInfoBar(infobar_); | 111 infobar_helper->AddInfoBar(infobar_); |
112 } | 112 } |
113 | 113 |
114 void DownloadRequestLimiter::TabDownloadState::Cancel() { | 114 void DownloadRequestLimiter::TabDownloadState::Cancel() { |
115 NotifyCallbacks(false); | 115 NotifyCallbacks(false); |
116 } | 116 } |
117 | 117 |
118 void DownloadRequestLimiter::TabDownloadState::Accept() { | 118 void DownloadRequestLimiter::TabDownloadState::Accept() { |
119 NotifyCallbacks(true); | 119 NotifyCallbacks(true); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 originating_contents->GetRenderViewHost(), | 311 originating_contents->GetRenderViewHost(), |
312 request_id, | 312 request_id, |
313 request_method)) { | 313 request_method)) { |
314 ScheduleNotification(callback, false); | 314 ScheduleNotification(callback, false); |
315 return; | 315 return; |
316 } | 316 } |
317 | 317 |
318 // If the tab requesting the download is a constrained popup that is not | 318 // If the tab requesting the download is a constrained popup that is not |
319 // shown, treat the request as if it came from the parent. | 319 // shown, treat the request as if it came from the parent. |
320 WebContents* effective_contents = originating_contents; | 320 WebContents* effective_contents = originating_contents; |
321 TabContentsWrapper* originating_wrapper = | 321 TabContents* originating_tab_contents = |
322 TabContentsWrapper::GetCurrentWrapperForContents(originating_contents); | 322 TabContents::GetOwningTabContentsForWebContents(originating_contents); |
323 if (originating_wrapper && | 323 if (originating_tab_contents && |
324 originating_wrapper->blocked_content_tab_helper()->delegate()) { | 324 originating_tab_contents->blocked_content_tab_helper()->delegate()) { |
325 effective_contents = originating_wrapper->blocked_content_tab_helper()-> | 325 effective_contents = |
326 delegate()->GetConstrainingContentsWrapper(originating_wrapper)-> | 326 originating_tab_contents->blocked_content_tab_helper()->delegate()-> |
327 web_contents(); | 327 GetConstrainingContentsWrapper(originating_tab_contents)-> |
| 328 web_contents(); |
328 } | 329 } |
329 | 330 |
330 TabDownloadState* state = GetDownloadState( | 331 TabDownloadState* state = GetDownloadState( |
331 effective_contents, originating_contents, true); | 332 effective_contents, originating_contents, true); |
332 switch (state->download_status()) { | 333 switch (state->download_status()) { |
333 case ALLOW_ALL_DOWNLOADS: | 334 case ALLOW_ALL_DOWNLOADS: |
334 if (state->download_count() && !(state->download_count() % | 335 if (state->download_count() && !(state->download_count() % |
335 DownloadRequestLimiter::kMaxDownloadsAtOnce)) | 336 DownloadRequestLimiter::kMaxDownloadsAtOnce)) |
336 state->set_download_status(PROMPT_BEFORE_DOWNLOAD); | 337 state->set_download_status(PROMPT_BEFORE_DOWNLOAD); |
337 ScheduleNotification(callback, true); | 338 ScheduleNotification(callback, true); |
(...skipping 27 matching lines...) Expand all Loading... |
365 | 366 |
366 void DownloadRequestLimiter::Remove(TabDownloadState* state) { | 367 void DownloadRequestLimiter::Remove(TabDownloadState* state) { |
367 DCHECK(ContainsKey(state_map_, state->web_contents())); | 368 DCHECK(ContainsKey(state_map_, state->web_contents())); |
368 state_map_.erase(state->web_contents()); | 369 state_map_.erase(state->web_contents()); |
369 delete state; | 370 delete state; |
370 } | 371 } |
371 | 372 |
372 // static | 373 // static |
373 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = | 374 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = |
374 NULL; | 375 NULL; |
OLD | NEW |