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.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 274 |
275 DownloadRequestLimiter::DownloadStatus | 275 DownloadRequestLimiter::DownloadStatus |
276 DownloadRequestLimiter::GetDownloadStatus(content::WebContents* web_contents) { | 276 DownloadRequestLimiter::GetDownloadStatus(content::WebContents* web_contents) { |
277 TabDownloadState* state = GetDownloadState(web_contents, NULL, false); | 277 TabDownloadState* state = GetDownloadState(web_contents, NULL, false); |
278 return state ? state->download_status() : ALLOW_ONE_DOWNLOAD; | 278 return state ? state->download_status() : ALLOW_ONE_DOWNLOAD; |
279 } | 279 } |
280 | 280 |
281 void DownloadRequestLimiter::CanDownloadOnIOThread( | 281 void DownloadRequestLimiter::CanDownloadOnIOThread( |
282 int render_process_host_id, | 282 int render_process_host_id, |
283 int render_view_id, | 283 int render_view_id, |
284 int request_id, | 284 const GURL& url, |
285 const std::string& request_method, | 285 const std::string& request_method, |
286 const Callback& callback) { | 286 const Callback& callback) { |
287 // This is invoked on the IO thread. Schedule the task to run on the UI | 287 // This is invoked on the IO thread. Schedule the task to run on the UI |
288 // thread so that we can query UI state. | 288 // thread so that we can query UI state. |
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
290 BrowserThread::PostTask( | 290 BrowserThread::PostTask( |
291 BrowserThread::UI, FROM_HERE, | 291 BrowserThread::UI, FROM_HERE, |
292 base::Bind(&DownloadRequestLimiter::CanDownload, this, | 292 base::Bind(&DownloadRequestLimiter::CanDownload, this, |
293 render_process_host_id, render_view_id, request_id, | 293 render_process_host_id, render_view_id, url, |
294 request_method, callback)); | 294 request_method, callback)); |
295 } | 295 } |
296 | 296 |
297 DownloadRequestLimiter::TabDownloadState* | 297 DownloadRequestLimiter::TabDownloadState* |
298 DownloadRequestLimiter::GetDownloadState( | 298 DownloadRequestLimiter::GetDownloadState( |
299 content::WebContents* web_contents, | 299 content::WebContents* web_contents, |
300 content::WebContents* originating_web_contents, | 300 content::WebContents* originating_web_contents, |
301 bool create) { | 301 bool create) { |
302 DCHECK(web_contents); | 302 DCHECK(web_contents); |
303 StateMap::iterator i = state_map_.find(web_contents); | 303 StateMap::iterator i = state_map_.find(web_contents); |
304 if (i != state_map_.end()) | 304 if (i != state_map_.end()) |
305 return i->second; | 305 return i->second; |
306 | 306 |
307 if (!create) | 307 if (!create) |
308 return NULL; | 308 return NULL; |
309 | 309 |
310 TabDownloadState* state = | 310 TabDownloadState* state = |
311 new TabDownloadState(this, web_contents, originating_web_contents); | 311 new TabDownloadState(this, web_contents, originating_web_contents); |
312 state_map_[web_contents] = state; | 312 state_map_[web_contents] = state; |
313 return state; | 313 return state; |
314 } | 314 } |
315 | 315 |
316 void DownloadRequestLimiter::CanDownload(int render_process_host_id, | 316 void DownloadRequestLimiter::CanDownload(int render_process_host_id, |
317 int render_view_id, | 317 int render_view_id, |
318 int request_id, | 318 const GURL& url, |
319 const std::string& request_method, | 319 const std::string& request_method, |
320 const Callback& callback) { | 320 const Callback& callback) { |
321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
322 | 322 |
323 content::WebContents* originating_contents = | 323 content::WebContents* originating_contents = |
324 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); | 324 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); |
325 if (!originating_contents) { | 325 if (!originating_contents) { |
326 // The WebContents was closed, don't allow the download. | 326 // The WebContents was closed, don't allow the download. |
327 ScheduleNotification(callback, false); | 327 ScheduleNotification(callback, false); |
328 return; | 328 return; |
329 } | 329 } |
330 | 330 |
331 if (!originating_contents->GetDelegate()) { | 331 if (!originating_contents->GetDelegate()) { |
332 ScheduleNotification(callback, false); | 332 ScheduleNotification(callback, false); |
333 return; | 333 return; |
334 } | 334 } |
335 | 335 |
336 // Note that because |originating_contents| might go away before | 336 // Note that because |originating_contents| might go away before |
337 // OnCanDownloadDecided is invoked, we look it up by |render_process_host_id| | 337 // OnCanDownloadDecided is invoked, we look it up by |render_process_host_id| |
338 // and |render_view_id|. | 338 // and |render_view_id|. |
339 base::Callback<void(bool)> can_download_callback = base::Bind( | 339 base::Callback<void(bool)> can_download_callback = base::Bind( |
340 &DownloadRequestLimiter::OnCanDownloadDecided, | 340 &DownloadRequestLimiter::OnCanDownloadDecided, |
341 factory_.GetWeakPtr(), | 341 factory_.GetWeakPtr(), |
342 render_process_host_id, | 342 render_process_host_id, |
343 render_view_id, | 343 render_view_id, |
344 request_id, | |
345 request_method, | 344 request_method, |
346 callback); | 345 callback); |
347 | 346 |
348 originating_contents->GetDelegate()->CanDownload( | 347 originating_contents->GetDelegate()->CanDownload( |
349 originating_contents->GetRenderViewHost(), | 348 originating_contents->GetRenderViewHost(), |
350 request_id, | 349 url, |
351 request_method, | 350 request_method, |
352 can_download_callback); | 351 can_download_callback); |
353 } | 352 } |
354 | 353 |
355 void DownloadRequestLimiter::OnCanDownloadDecided( | 354 void DownloadRequestLimiter::OnCanDownloadDecided( |
356 int render_process_host_id, | 355 int render_process_host_id, |
357 int render_view_id, | 356 int render_view_id, |
358 int request_id, | |
359 const std::string& request_method, | 357 const std::string& request_method, |
360 const Callback& orig_callback, bool allow) { | 358 const Callback& orig_callback, bool allow) { |
361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 359 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
362 content::WebContents* originating_contents = | 360 content::WebContents* originating_contents = |
363 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); | 361 tab_util::GetWebContentsByID(render_process_host_id, render_view_id); |
364 if (!originating_contents || !allow) { | 362 if (!originating_contents || !allow) { |
365 ScheduleNotification(orig_callback, false); | 363 ScheduleNotification(orig_callback, false); |
366 return; | 364 return; |
367 } | 365 } |
368 | 366 |
369 CanDownloadImpl(originating_contents, | 367 CanDownloadImpl(originating_contents, |
370 request_id, | |
371 request_method, | 368 request_method, |
372 orig_callback); | 369 orig_callback); |
373 } | 370 } |
374 | 371 |
375 HostContentSettingsMap* DownloadRequestLimiter::GetContentSettings( | 372 HostContentSettingsMap* DownloadRequestLimiter::GetContentSettings( |
376 content::WebContents* contents) { | 373 content::WebContents* contents) { |
377 return content_settings_ ? content_settings_ : Profile::FromBrowserContext( | 374 return content_settings_ ? content_settings_ : Profile::FromBrowserContext( |
378 contents->GetBrowserContext())->GetHostContentSettingsMap(); | 375 contents->GetBrowserContext())->GetHostContentSettingsMap(); |
379 } | 376 } |
380 | 377 |
381 void DownloadRequestLimiter::CanDownloadImpl( | 378 void DownloadRequestLimiter::CanDownloadImpl( |
382 content::WebContents* originating_contents, | 379 content::WebContents* originating_contents, |
383 int request_id, | |
384 const std::string& request_method, | 380 const std::string& request_method, |
385 const Callback& callback) { | 381 const Callback& callback) { |
386 DCHECK(originating_contents); | 382 DCHECK(originating_contents); |
387 | 383 |
388 TabDownloadState* state = GetDownloadState( | 384 TabDownloadState* state = GetDownloadState( |
389 originating_contents, originating_contents, true); | 385 originating_contents, originating_contents, true); |
390 switch (state->download_status()) { | 386 switch (state->download_status()) { |
391 case ALLOW_ALL_DOWNLOADS: | 387 case ALLOW_ALL_DOWNLOADS: |
392 if (state->download_count() && !(state->download_count() % | 388 if (state->download_count() && !(state->download_count() % |
393 DownloadRequestLimiter::kMaxDownloadsAtOnce)) | 389 DownloadRequestLimiter::kMaxDownloadsAtOnce)) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 BrowserThread::PostTask( | 456 BrowserThread::PostTask( |
461 BrowserThread::IO, FROM_HERE, base::Bind(callback, allow)); | 457 BrowserThread::IO, FROM_HERE, base::Bind(callback, allow)); |
462 } | 458 } |
463 | 459 |
464 void DownloadRequestLimiter::Remove(TabDownloadState* state, | 460 void DownloadRequestLimiter::Remove(TabDownloadState* state, |
465 content::WebContents* contents) { | 461 content::WebContents* contents) { |
466 DCHECK(ContainsKey(state_map_, contents)); | 462 DCHECK(ContainsKey(state_map_, contents)); |
467 state_map_.erase(contents); | 463 state_map_.erase(contents); |
468 delete state; | 464 delete state; |
469 } | 465 } |
OLD | NEW |