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_service.h" | 10 #include "chrome/browser/infobars/infobar_service.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 "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
16 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
17 #include "content/public/browser/notification_source.h" | 17 #include "content/public/browser/notification_source.h" |
18 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
19 #include "content/public/browser/render_process_host.h" | |
19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
20 #include "content/public/browser/web_contents_delegate.h" | 21 #include "content/public/browser/web_contents_delegate.h" |
21 | 22 |
22 using content::BrowserThread; | 23 using content::BrowserThread; |
23 using content::NavigationController; | 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 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 | 202 |
202 for (size_t i = 0; i < callbacks.size(); ++i) | 203 for (size_t i = 0; i < callbacks.size(); ++i) |
203 host_->ScheduleNotification(callbacks[i], allow); | 204 host_->ScheduleNotification(callbacks[i], allow); |
204 | 205 |
205 if (change_status) | 206 if (change_status) |
206 set_download_status(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD); | 207 set_download_status(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD); |
207 } | 208 } |
208 | 209 |
209 // DownloadRequestLimiter ------------------------------------------------------ | 210 // DownloadRequestLimiter ------------------------------------------------------ |
210 | 211 |
211 DownloadRequestLimiter::DownloadRequestLimiter() { | 212 DownloadRequestLimiter::DownloadRequestLimiter() |
213 : factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
212 } | 214 } |
213 | 215 |
214 DownloadRequestLimiter::~DownloadRequestLimiter() { | 216 DownloadRequestLimiter::~DownloadRequestLimiter() { |
215 // All the tabs should have closed before us, which sends notification and | 217 // All the tabs should have closed before us, which sends notification and |
216 // removes from state_map_. As such, there should be no pending callbacks. | 218 // removes from state_map_. As such, there should be no pending callbacks. |
217 DCHECK(state_map_.empty()); | 219 DCHECK(state_map_.empty()); |
218 } | 220 } |
219 | 221 |
220 DownloadRequestLimiter::DownloadStatus | 222 DownloadRequestLimiter::DownloadStatus |
221 DownloadRequestLimiter::GetDownloadStatus(WebContents* web_contents) { | 223 DownloadRequestLimiter::GetDownloadStatus(WebContents* web_contents) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
267 | 269 |
268 WebContents* originating_contents = | 270 WebContents* originating_contents = |
269 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); | 271 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); |
270 if (!originating_contents) { | 272 if (!originating_contents) { |
271 // The WebContents was closed, don't allow the download. | 273 // The WebContents was closed, don't allow the download. |
272 ScheduleNotification(callback, false); | 274 ScheduleNotification(callback, false); |
273 return; | 275 return; |
274 } | 276 } |
275 | 277 |
276 CanDownloadImpl( | 278 if (!originating_contents->GetDelegate()) { |
277 originating_contents, | 279 ScheduleNotification(callback, false); |
280 return; | |
281 } | |
282 | |
283 base::Callback<void(bool)> can_download_callback = base::Bind( | |
284 &DownloadRequestLimiter::OnCanDownloadDecided, | |
285 factory_.GetWeakPtr(), | |
286 originating_contents, request_id, request_method, callback); | |
asanka
2013/03/27 15:34:53
|originating_contents| might go away before OnCanD
lazyboy
2013/03/27 17:09:22
Done.
| |
287 | |
288 // FYI: Chrome Frame overrides CanDownload in ExternalTabContainer in order | |
289 // to cancel the download operation in chrome and let the host browser | |
290 // take care of it. | |
asanka
2013/03/27 15:34:53
Nit: Android also overrides CanDownload to cancel
lazyboy
2013/03/27 17:09:22
OK, removed.
| |
291 originating_contents->GetDelegate()->CanDownload( | |
292 originating_contents->GetRenderViewHost(), | |
278 request_id, | 293 request_id, |
279 request_method, | 294 request_method, |
280 callback); | 295 can_download_callback); |
296 } | |
297 | |
298 void DownloadRequestLimiter::OnCanDownloadDecided( | |
299 WebContents* originating_contents, | |
300 int request_id, | |
301 const std::string& request_method, | |
302 const Callback& orig_callback, bool allow) { | |
asanka
2013/03/27 15:34:53
Can you add a DCHECK here to check whether this ca
lazyboy
2013/03/27 17:09:22
Done.
| |
303 if (!allow) { | |
304 ScheduleNotification(orig_callback, false); | |
305 return; | |
306 } | |
307 | |
308 CanDownloadImpl(originating_contents, | |
309 request_id, | |
310 request_method, | |
311 orig_callback); | |
281 } | 312 } |
282 | 313 |
283 void DownloadRequestLimiter::CanDownloadImpl(WebContents* originating_contents, | 314 void DownloadRequestLimiter::CanDownloadImpl(WebContents* originating_contents, |
284 int request_id, | 315 int request_id, |
285 const std::string& request_method, | 316 const std::string& request_method, |
286 const Callback& callback) { | 317 const Callback& callback) { |
287 DCHECK(originating_contents); | 318 DCHECK(originating_contents); |
288 | 319 |
289 // FYI: Chrome Frame overrides CanDownload in ExternalTabContainer in order | |
290 // to cancel the download operation in chrome and let the host browser | |
291 // take care of it. | |
292 if (originating_contents->GetDelegate() && | |
293 !originating_contents->GetDelegate()->CanDownload( | |
294 originating_contents->GetRenderViewHost(), | |
295 request_id, | |
296 request_method)) { | |
297 ScheduleNotification(callback, false); | |
298 return; | |
299 } | |
300 | |
301 // If the tab requesting the download is a constrained popup that is not | 320 // If the tab requesting the download is a constrained popup that is not |
302 // shown, treat the request as if it came from the parent. | 321 // shown, treat the request as if it came from the parent. |
303 WebContents* effective_contents = originating_contents; | 322 WebContents* effective_contents = originating_contents; |
304 BlockedContentTabHelper* blocked_content_tab_helper = | 323 BlockedContentTabHelper* blocked_content_tab_helper = |
305 BlockedContentTabHelper::FromWebContents(originating_contents); | 324 BlockedContentTabHelper::FromWebContents(originating_contents); |
306 if (blocked_content_tab_helper && | 325 if (blocked_content_tab_helper && |
307 blocked_content_tab_helper->delegate()) { | 326 blocked_content_tab_helper->delegate()) { |
308 effective_contents = blocked_content_tab_helper->delegate()-> | 327 effective_contents = blocked_content_tab_helper->delegate()-> |
309 GetConstrainingWebContents(originating_contents); | 328 GetConstrainingWebContents(originating_contents); |
310 } | 329 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 bool allow) { | 362 bool allow) { |
344 BrowserThread::PostTask( | 363 BrowserThread::PostTask( |
345 BrowserThread::IO, FROM_HERE, base::Bind(callback, allow)); | 364 BrowserThread::IO, FROM_HERE, base::Bind(callback, allow)); |
346 } | 365 } |
347 | 366 |
348 void DownloadRequestLimiter::Remove(TabDownloadState* state) { | 367 void DownloadRequestLimiter::Remove(TabDownloadState* state) { |
349 DCHECK(ContainsKey(state_map_, state->web_contents())); | 368 DCHECK(ContainsKey(state_map_, state->web_contents())); |
350 state_map_.erase(state->web_contents()); | 369 state_map_.erase(state->web_contents()); |
351 delete state; | 370 delete state; |
352 } | 371 } |
OLD | NEW |