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" |
(...skipping 47 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 TabContents* tab_contents = | 68 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
69 TabContents::GetOwningTabContentsForWebContents(web_contents()); | 69 // See PromptUserForDownload(): if there's no TabContents, then |
70 // See PromptUserForDownload(): if there's no TCW, then DOWNLOADS_NOT_ALLOWED | 70 // DOWNLOADS_NOT_ALLOWED is functionally equivalent to PROMPT_BEFORE_DOWNLOAD. |
71 // is functionally equivalent to PROMPT_BEFORE_DOWNLOAD. | |
72 if ((tab_contents && | 71 if ((tab_contents && |
73 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS && | 72 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS && |
74 status_ != DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED) || | 73 status_ != DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED) || |
75 (!tab_contents && | 74 (!tab_contents && |
76 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS)) { | 75 status_ != DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS)) { |
77 // Revert to default status. | 76 // Revert to default status. |
78 host_->Remove(this); | 77 host_->Remove(this); |
79 // WARNING: We've been deleted. | 78 // WARNING: We've been deleted. |
80 } | 79 } |
81 } | 80 } |
82 | 81 |
83 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( | 82 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( |
84 WebContents* web_contents, | 83 WebContents* web_contents, |
85 const DownloadRequestLimiter::Callback& callback) { | 84 const DownloadRequestLimiter::Callback& callback) { |
86 callbacks_.push_back(callback); | 85 callbacks_.push_back(callback); |
87 | 86 |
88 if (is_showing_prompt()) | 87 if (is_showing_prompt()) |
89 return; // Already showing prompt. | 88 return; // Already showing prompt. |
90 | 89 |
91 if (DownloadRequestLimiter::delegate_) { | 90 if (DownloadRequestLimiter::delegate_) { |
92 NotifyCallbacks(DownloadRequestLimiter::delegate_->ShouldAllowDownload()); | 91 NotifyCallbacks(DownloadRequestLimiter::delegate_->ShouldAllowDownload()); |
93 return; | 92 return; |
94 } | 93 } |
95 TabContents* tab_contents = | 94 TabContents* tab_contents = TabContents::FromWebContents(web_contents); |
96 TabContents::GetOwningTabContentsForWebContents(web_contents); | |
97 if (!tab_contents) { | 95 if (!tab_contents) { |
98 // If |web_contents| doesn't have a TabContents, then it isn't what a user | 96 // 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 | 97 // thinks of as a tab, it's actually a "raw" WebContents like those used |
100 // for extension popups/bubbles and hosted apps etc. | 98 // for extension popups/bubbles and hosted apps etc. |
101 // TODO(benjhayden): If this is an automatic download from an extension, | 99 // 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 | 100 // 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 | 101 // the extension's DevTools console (as we do for CSP) about how |
104 // extensions should use chrome.downloads.download() (requires the | 102 // extensions should use chrome.downloads.download() (requires the |
105 // "downloads" permission) to automatically download >1 files. | 103 // "downloads" permission) to automatically download >1 files. |
106 Cancel(); | 104 Cancel(); |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 request_id, | 310 request_id, |
313 request_method)) { | 311 request_method)) { |
314 ScheduleNotification(callback, false); | 312 ScheduleNotification(callback, false); |
315 return; | 313 return; |
316 } | 314 } |
317 | 315 |
318 // If the tab requesting the download is a constrained popup that is not | 316 // 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. | 317 // shown, treat the request as if it came from the parent. |
320 WebContents* effective_contents = originating_contents; | 318 WebContents* effective_contents = originating_contents; |
321 TabContents* originating_tab_contents = | 319 TabContents* originating_tab_contents = |
322 TabContents::GetOwningTabContentsForWebContents(originating_contents); | 320 TabContents::FromWebContents(originating_contents); |
323 if (originating_tab_contents && | 321 if (originating_tab_contents && |
324 originating_tab_contents->blocked_content_tab_helper()->delegate()) { | 322 originating_tab_contents->blocked_content_tab_helper()->delegate()) { |
325 effective_contents = | 323 effective_contents = |
326 originating_tab_contents->blocked_content_tab_helper()->delegate()-> | 324 originating_tab_contents->blocked_content_tab_helper()->delegate()-> |
327 GetConstrainingTabContents(originating_tab_contents)-> | 325 GetConstrainingTabContents(originating_tab_contents)-> |
328 web_contents(); | 326 web_contents(); |
329 } | 327 } |
330 | 328 |
331 TabDownloadState* state = GetDownloadState( | 329 TabDownloadState* state = GetDownloadState( |
332 effective_contents, originating_contents, true); | 330 effective_contents, originating_contents, true); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 | 364 |
367 void DownloadRequestLimiter::Remove(TabDownloadState* state) { | 365 void DownloadRequestLimiter::Remove(TabDownloadState* state) { |
368 DCHECK(ContainsKey(state_map_, state->web_contents())); | 366 DCHECK(ContainsKey(state_map_, state->web_contents())); |
369 state_map_.erase(state->web_contents()); | 367 state_map_.erase(state->web_contents()); |
370 delete state; | 368 delete state; |
371 } | 369 } |
372 | 370 |
373 // static | 371 // static |
374 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = | 372 DownloadRequestLimiter::TestingDelegate* DownloadRequestLimiter::delegate_ = |
375 NULL; | 373 NULL; |
OLD | NEW |