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
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_request_limiter.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
10 #include "chrome/browser/download/download_permission_request.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/tab_contents/tab_util.h"
14 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/resource_dispatcher_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_contents_delegate.h"
28 #if defined(OS_ANDROID)
29 #include "chrome/browser/download/download_request_infobar_delegate.h"
32 using content::BrowserThread
;
33 using content::NavigationController
;
34 using content::NavigationEntry
;
36 // TabDownloadState ------------------------------------------------------------
38 DownloadRequestLimiter::TabDownloadState::TabDownloadState(
39 DownloadRequestLimiter
* host
,
40 content::WebContents
* contents
,
41 content::WebContents
* originating_web_contents
)
42 : content::WebContentsObserver(contents
),
43 web_contents_(contents
),
45 status_(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD
),
48 content::Source
<NavigationController
> notification_source(
49 &contents
->GetController());
50 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING
,
52 NavigationEntry
* active_entry
= originating_web_contents
?
53 originating_web_contents
->GetController().GetActiveEntry() :
54 contents
->GetController().GetActiveEntry();
56 initial_page_host_
= active_entry
->GetURL().host();
59 DownloadRequestLimiter::TabDownloadState::~TabDownloadState() {
60 // We should only be destroyed after the callbacks have been notified.
61 DCHECK(callbacks_
.empty());
63 // And we should have invalidated the back pointer.
64 DCHECK(!factory_
.HasWeakPtrs());
67 void DownloadRequestLimiter::TabDownloadState::DidNavigateMainFrame(
68 const content::LoadCommittedDetails
& details
,
69 const content::FrameNavigateParams
& params
) {
71 case ALLOW_ONE_DOWNLOAD
:
72 case PROMPT_BEFORE_DOWNLOAD
:
73 // When the user reloads the page without responding to the infobar, they
74 // are expecting DownloadRequestLimiter to behave as if they had just
75 // initially navigated to this page. See http://crbug.com/171372
76 NotifyCallbacks(false);
77 host_
->Remove(this, web_contents());
78 // WARNING: We've been deleted.
80 case DOWNLOADS_NOT_ALLOWED
:
81 case ALLOW_ALL_DOWNLOADS
:
82 // Don't drop this information. The user has explicitly said that they
83 // do/don't want downloads from this host. If they accidentally Accepted
84 // or Canceled, tough luck, they don't get another chance. They can copy
85 // the URL into a new tab, which will make a new DownloadRequestLimiter.
86 // See also the initial_page_host_ logic in Observe() for
87 // NOTIFICATION_NAV_ENTRY_PENDING.
94 void DownloadRequestLimiter::TabDownloadState::DidGetUserGesture() {
95 if (is_showing_prompt()) {
96 // Don't change the state if the user clicks on the page somewhere.
100 bool promptable
= (InfoBarService::FromWebContents(web_contents()) != NULL
);
101 if (PermissionBubbleManager::Enabled()) {
103 (PermissionBubbleManager::FromWebContents(web_contents()) != NULL
);
106 // See PromptUserForDownload(): if there's no InfoBarService, then
107 // DOWNLOADS_NOT_ALLOWED is functionally equivalent to PROMPT_BEFORE_DOWNLOAD.
108 if ((status_
!= DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS
) &&
110 (status_
!= DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED
))) {
111 // Revert to default status.
112 host_
->Remove(this, web_contents());
113 // WARNING: We've been deleted.
117 void DownloadRequestLimiter::TabDownloadState::WebContentsDestroyed() {
118 // Tab closed, no need to handle closing the dialog as it's owned by the
121 NotifyCallbacks(false);
122 host_
->Remove(this, web_contents());
123 // WARNING: We've been deleted.
126 void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload(
127 const DownloadRequestLimiter::Callback
& callback
) {
128 callbacks_
.push_back(callback
);
129 DCHECK(web_contents_
);
130 if (is_showing_prompt())
133 #if defined(OS_ANDROID)
134 DownloadRequestInfoBarDelegate::Create(
135 InfoBarService::FromWebContents(web_contents_
), factory_
.GetWeakPtr());
137 PermissionBubbleManager
* bubble_manager
=
138 PermissionBubbleManager::FromWebContents(web_contents_
);
139 if (bubble_manager
) {
140 bubble_manager
->AddRequest(new DownloadPermissionRequest(
141 factory_
.GetWeakPtr()));
148 void DownloadRequestLimiter::TabDownloadState::SetContentSetting(
149 ContentSetting setting
) {
152 HostContentSettingsMap
* settings
=
153 DownloadRequestLimiter::GetContentSettings(web_contents_
);
154 ContentSettingsPattern
pattern(
155 ContentSettingsPattern::FromURL(web_contents_
->GetURL()));
156 if (!settings
|| !pattern
.IsValid())
158 settings
->SetContentSetting(
160 ContentSettingsPattern::Wildcard(),
161 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
166 void DownloadRequestLimiter::TabDownloadState::Cancel() {
167 SetContentSetting(CONTENT_SETTING_BLOCK
);
168 NotifyCallbacks(false);
171 void DownloadRequestLimiter::TabDownloadState::CancelOnce() {
172 NotifyCallbacks(false);
175 void DownloadRequestLimiter::TabDownloadState::Accept() {
176 SetContentSetting(CONTENT_SETTING_ALLOW
);
177 NotifyCallbacks(true);
180 DownloadRequestLimiter::TabDownloadState::TabDownloadState()
181 : web_contents_(NULL
),
183 status_(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD
),
188 bool DownloadRequestLimiter::TabDownloadState::is_showing_prompt() const {
189 return factory_
.HasWeakPtrs();
192 void DownloadRequestLimiter::TabDownloadState::Observe(
194 const content::NotificationSource
& source
,
195 const content::NotificationDetails
& details
) {
196 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING
, type
);
197 content::NavigationController
* controller
= &web_contents()->GetController();
198 DCHECK_EQ(controller
, content::Source
<NavigationController
>(source
).ptr());
200 // NOTE: Resetting state on a pending navigate isn't ideal. In particular it
201 // is possible that queued up downloads for the page before the pending
202 // navigation will be delivered to us after we process this request. If this
203 // happens we may let a download through that we shouldn't have. But this is
204 // rather rare, and it is difficult to get 100% right, so we don't deal with
206 NavigationEntry
* entry
= controller
->GetPendingEntry();
210 // Redirects don't count.
211 if (ui::PageTransitionIsRedirect(entry
->GetTransitionType()))
214 if (status_
== DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS
||
215 status_
== DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED
) {
216 // User has either allowed all downloads or canceled all downloads. Only
217 // reset the download state if the user is navigating to a different host
218 // (or host is empty).
219 if (!initial_page_host_
.empty() && !entry
->GetURL().host().empty() &&
220 entry
->GetURL().host() == initial_page_host_
)
224 NotifyCallbacks(false);
225 host_
->Remove(this, web_contents());
228 void DownloadRequestLimiter::TabDownloadState::NotifyCallbacks(bool allow
) {
229 set_download_status(allow
?
230 DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS
:
231 DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED
);
232 std::vector
<DownloadRequestLimiter::Callback
> callbacks
;
233 bool change_status
= false;
235 // Selectively send first few notifications only if number of downloads exceed
236 // kMaxDownloadsAtOnce. In that case, we also retain the infobar instance and
237 // don't close it. If allow is false, we send all the notifications to cancel
238 // all remaining downloads and close the infobar.
239 if (!allow
|| (callbacks_
.size() < kMaxDownloadsAtOnce
)) {
240 // Null the generated weak pointer so we don't get notified again.
241 factory_
.InvalidateWeakPtrs();
242 callbacks
.swap(callbacks_
);
244 std::vector
<DownloadRequestLimiter::Callback
>::iterator start
, end
;
245 start
= callbacks_
.begin();
246 end
= callbacks_
.begin() + kMaxDownloadsAtOnce
;
247 callbacks
.assign(start
, end
);
248 callbacks_
.erase(start
, end
);
249 change_status
= true;
252 for (const auto& callback
: callbacks
) {
253 // When callback runs, it can cause the WebContents to be destroyed.
254 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
255 base::Bind(callback
, allow
));
259 set_download_status(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD
);
262 // DownloadRequestLimiter ------------------------------------------------------
264 HostContentSettingsMap
* DownloadRequestLimiter::content_settings_
= NULL
;
266 void DownloadRequestLimiter::SetContentSettingsForTesting(
267 HostContentSettingsMap
* content_settings
) {
268 content_settings_
= content_settings
;
271 DownloadRequestLimiter::DownloadRequestLimiter()
275 DownloadRequestLimiter::~DownloadRequestLimiter() {
276 // All the tabs should have closed before us, which sends notification and
277 // removes from state_map_. As such, there should be no pending callbacks.
278 DCHECK(state_map_
.empty());
281 DownloadRequestLimiter::DownloadStatus
282 DownloadRequestLimiter::GetDownloadStatus(content::WebContents
* web_contents
) {
283 TabDownloadState
* state
= GetDownloadState(web_contents
, NULL
, false);
284 return state
? state
->download_status() : ALLOW_ONE_DOWNLOAD
;
287 DownloadRequestLimiter::TabDownloadState
*
288 DownloadRequestLimiter::GetDownloadState(
289 content::WebContents
* web_contents
,
290 content::WebContents
* originating_web_contents
,
292 DCHECK(web_contents
);
293 StateMap::iterator i
= state_map_
.find(web_contents
);
294 if (i
!= state_map_
.end())
300 TabDownloadState
* state
=
301 new TabDownloadState(this, web_contents
, originating_web_contents
);
302 state_map_
[web_contents
] = state
;
306 void DownloadRequestLimiter::CanDownload(int render_process_host_id
,
309 const std::string
& request_method
,
310 const Callback
& callback
) {
311 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
313 content::WebContents
* originating_contents
=
314 tab_util::GetWebContentsByID(render_process_host_id
, render_view_id
);
315 if (!originating_contents
) {
316 // The WebContents was closed, don't allow the download.
321 if (!originating_contents
->GetDelegate()) {
326 // Note that because |originating_contents| might go away before
327 // OnCanDownloadDecided is invoked, we look it up by |render_process_host_id|
328 // and |render_view_id|.
329 base::Callback
<void(bool)> can_download_callback
= base::Bind(
330 &DownloadRequestLimiter::OnCanDownloadDecided
,
331 factory_
.GetWeakPtr(),
332 render_process_host_id
,
337 originating_contents
->GetDelegate()->CanDownload(
340 can_download_callback
);
343 void DownloadRequestLimiter::OnCanDownloadDecided(
344 int render_process_host_id
,
346 const std::string
& request_method
,
347 const Callback
& orig_callback
, bool allow
) {
348 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
349 content::WebContents
* originating_contents
=
350 tab_util::GetWebContentsByID(render_process_host_id
, render_view_id
);
351 if (!originating_contents
|| !allow
) {
352 orig_callback
.Run(false);
356 CanDownloadImpl(originating_contents
,
361 HostContentSettingsMap
* DownloadRequestLimiter::GetContentSettings(
362 content::WebContents
* contents
) {
363 return content_settings_
? content_settings_
: Profile::FromBrowserContext(
364 contents
->GetBrowserContext())->GetHostContentSettingsMap();
367 void DownloadRequestLimiter::CanDownloadImpl(
368 content::WebContents
* originating_contents
,
369 const std::string
& request_method
,
370 const Callback
& callback
) {
371 DCHECK(originating_contents
);
373 TabDownloadState
* state
= GetDownloadState(
374 originating_contents
, originating_contents
, true);
375 switch (state
->download_status()) {
376 case ALLOW_ALL_DOWNLOADS
:
377 if (state
->download_count() && !(state
->download_count() %
378 DownloadRequestLimiter::kMaxDownloadsAtOnce
))
379 state
->set_download_status(PROMPT_BEFORE_DOWNLOAD
);
381 state
->increment_download_count();
384 case ALLOW_ONE_DOWNLOAD
:
385 state
->set_download_status(PROMPT_BEFORE_DOWNLOAD
);
387 state
->increment_download_count();
390 case DOWNLOADS_NOT_ALLOWED
:
394 case PROMPT_BEFORE_DOWNLOAD
: {
395 HostContentSettingsMap
* content_settings
= GetContentSettings(
396 originating_contents
);
397 ContentSetting setting
= CONTENT_SETTING_ASK
;
398 if (content_settings
)
399 setting
= content_settings
->GetContentSetting(
400 originating_contents
->GetURL(),
401 originating_contents
->GetURL(),
402 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
405 case CONTENT_SETTING_ALLOW
: {
406 TabSpecificContentSettings
* settings
=
407 TabSpecificContentSettings::FromWebContents(
408 originating_contents
);
410 settings
->SetDownloadsBlocked(false);
412 state
->increment_download_count();
415 case CONTENT_SETTING_BLOCK
: {
416 TabSpecificContentSettings
* settings
=
417 TabSpecificContentSettings::FromWebContents(
418 originating_contents
);
420 settings
->SetDownloadsBlocked(true);
424 case CONTENT_SETTING_DEFAULT
:
425 case CONTENT_SETTING_ASK
:
426 case CONTENT_SETTING_SESSION_ONLY
:
427 state
->PromptUserForDownload(callback
);
428 state
->increment_download_count();
430 case CONTENT_SETTING_NUM_SETTINGS
:
443 void DownloadRequestLimiter::Remove(TabDownloadState
* state
,
444 content::WebContents
* contents
) {
445 DCHECK(ContainsKey(state_map_
, contents
));
446 state_map_
.erase(contents
);