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/content_settings/host_content_settings_map_factory.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 299 |
300 if (!create) | 300 if (!create) |
301 return NULL; | 301 return NULL; |
302 | 302 |
303 TabDownloadState* state = | 303 TabDownloadState* state = |
304 new TabDownloadState(this, web_contents, originating_web_contents); | 304 new TabDownloadState(this, web_contents, originating_web_contents); |
305 state_map_[web_contents] = state; | 305 state_map_[web_contents] = state; |
306 return state; | 306 return state; |
307 } | 307 } |
308 | 308 |
309 void DownloadRequestLimiter::CanDownload(int render_process_host_id, | 309 void DownloadRequestLimiter::CanDownload( |
310 int render_view_id, | 310 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
311 const GURL& url, | 311 const GURL& url, |
312 const std::string& request_method, | 312 const std::string& request_method, |
313 const Callback& callback) { | 313 const Callback& callback) { |
314 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 314 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
315 | 315 |
316 content::WebContents* originating_contents = | 316 content::WebContents* originating_contents = web_contents_getter.Run(); |
317 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); | |
318 if (!originating_contents) { | 317 if (!originating_contents) { |
319 // The WebContents was closed, don't allow the download. | 318 // The WebContents was closed, don't allow the download. |
320 callback.Run(false); | 319 callback.Run(false); |
321 return; | 320 return; |
322 } | 321 } |
323 | 322 |
324 if (!originating_contents->GetDelegate()) { | 323 if (!originating_contents->GetDelegate()) { |
325 callback.Run(false); | 324 callback.Run(false); |
326 return; | 325 return; |
327 } | 326 } |
328 | 327 |
329 // Note that because |originating_contents| might go away before | 328 // Note that because |originating_contents| might go away before |
330 // OnCanDownloadDecided is invoked, we look it up by |render_process_host_id| | 329 // OnCanDownloadDecided is invoked, we look it up by |render_process_host_id| |
331 // and |render_view_id|. | 330 // and |render_view_id|. |
332 base::Callback<void(bool)> can_download_callback = base::Bind( | 331 base::Callback<void(bool)> can_download_callback = base::Bind( |
333 &DownloadRequestLimiter::OnCanDownloadDecided, | 332 &DownloadRequestLimiter::OnCanDownloadDecided, factory_.GetWeakPtr(), |
334 factory_.GetWeakPtr(), | 333 web_contents_getter, request_method, callback); |
335 render_process_host_id, | |
336 render_view_id, | |
337 request_method, | |
338 callback); | |
339 | 334 |
340 originating_contents->GetDelegate()->CanDownload( | 335 originating_contents->GetDelegate()->CanDownload( |
341 url, | 336 url, |
342 request_method, | 337 request_method, |
343 can_download_callback); | 338 can_download_callback); |
344 } | 339 } |
345 | 340 |
346 void DownloadRequestLimiter::OnCanDownloadDecided( | 341 void DownloadRequestLimiter::OnCanDownloadDecided( |
347 int render_process_host_id, | 342 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
348 int render_view_id, | |
349 const std::string& request_method, | 343 const std::string& request_method, |
350 const Callback& orig_callback, bool allow) { | 344 const Callback& orig_callback, |
| 345 bool allow) { |
351 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 346 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
352 content::WebContents* originating_contents = | 347 content::WebContents* originating_contents = web_contents_getter.Run(); |
353 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); | |
354 if (!originating_contents || !allow) { | 348 if (!originating_contents || !allow) { |
355 orig_callback.Run(false); | 349 orig_callback.Run(false); |
356 return; | 350 return; |
357 } | 351 } |
358 | 352 |
359 CanDownloadImpl(originating_contents, | 353 CanDownloadImpl(originating_contents, |
360 request_method, | 354 request_method, |
361 orig_callback); | 355 orig_callback); |
362 } | 356 } |
363 | 357 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 NOTREACHED(); | 437 NOTREACHED(); |
444 } | 438 } |
445 } | 439 } |
446 | 440 |
447 void DownloadRequestLimiter::Remove(TabDownloadState* state, | 441 void DownloadRequestLimiter::Remove(TabDownloadState* state, |
448 content::WebContents* contents) { | 442 content::WebContents* contents) { |
449 DCHECK(ContainsKey(state_map_, contents)); | 443 DCHECK(ContainsKey(state_map_, contents)); |
450 state_map_.erase(contents); | 444 state_map_.erase(contents); |
451 delete state; | 445 delete state; |
452 } | 446 } |
OLD | NEW |