Chromium Code Reviews| Index: chrome/browser/renderer_host/ignore_navigation_resource_throttle.cc |
| diff --git a/chrome/browser/renderer_host/ignore_navigation_resource_throttle.cc b/chrome/browser/renderer_host/ignore_navigation_resource_throttle.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..31bc4d1fd32f3012d45dd327bda8a0e25e11ac52 |
| --- /dev/null |
| +++ b/chrome/browser/renderer_host/ignore_navigation_resource_throttle.cc |
| @@ -0,0 +1,111 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/renderer_host/ignore_navigation_resource_throttle.h" |
| + |
| +#include "base/callback.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/render_view_host_delegate.h" |
| +#include "content/public/browser/resource_request_info.h" |
| +#include "content/public/browser/resource_throttle_controller.h" |
| +#include "content/public/common/referrer.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +using content::BrowserThread; |
| +using content::Referrer; |
| +using content::RenderViewHost; |
| +using content::RenderViewHostDelegate; |
| +using content::ResourceRequestInfo; |
| + |
| +namespace { |
| + |
| +void CheckShouldIgnoreNavigationOnUiThread( |
|
darin (slow to review)
2012/05/23 18:18:29
nit: UiThread -> UIThread
mkosiba (inactive)
2012/05/28 16:06:52
Done.
|
| + int render_process_id, |
| + int render_view_id, |
| + const GURL& url, |
| + const Referrer& referrer, |
| + bool is_content_initiated, |
| + base::Callback<void(bool)> callback) { |
| + |
| + RenderViewHost* render_view_host = |
| + RenderViewHost::FromID(render_process_id, render_view_id); |
| + RenderViewHostDelegate* render_view_host_delegate = |
| + render_view_host ? render_view_host->GetDelegate() : NULL; |
| + bool should_ignore_navigation = false; |
| + if (render_view_host_delegate) { |
| + should_ignore_navigation = |
| + render_view_host_delegate->ShouldIgnoreNavigation( |
| + render_view_host, url, referrer, is_content_initiated); |
| + } |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, should_ignore_navigation)); |
| +} |
| + |
| +} // namespace |
| + |
| +IgnoreNavigationResourceThrottle::IgnoreNavigationResourceThrottle( |
| + net::URLRequest* request) |
| + : request_(request), |
| + weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| +} |
| + |
| +IgnoreNavigationResourceThrottle:: |
| + ~IgnoreNavigationResourceThrottle() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| +} |
| + |
| +void IgnoreNavigationResourceThrottle::WillStartRequest(bool* defer) { |
| + *defer = ShouldDeferRequestForUrl(request_->url()); |
|
darin (slow to review)
2012/05/23 18:18:29
nit: ForUrl -> ForURL
mkosiba (inactive)
2012/05/28 16:06:52
Done.
|
| +} |
| + |
| +void IgnoreNavigationResourceThrottle::WillRedirectRequest( |
| + const GURL& new_url, |
| + bool* defer) { |
| + *defer = ShouldDeferRequestForUrl(new_url); |
| +} |
| + |
| +bool IgnoreNavigationResourceThrottle::ShouldDeferRequestForUrl( |
| + const GURL& url) { |
| + int render_process_id, render_view_id; |
| + if (!ResourceRequestInfo::GetRenderViewForRequest( |
| + request_, |
| + &render_process_id, |
| + &render_view_id)) { |
| + return false; |
| + } |
| + |
| + const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
|
darin (slow to review)
2012/05/23 18:18:29
you should just call ResourceRequestInfo::GetAssoc
mkosiba (inactive)
2012/05/28 16:06:52
Done.
|
| + // The ShouldIngoreNavigationResourceThrottle should only be created for top |
|
darin (slow to review)
2012/05/23 18:18:29
nit: what is a "ShouldIngoreNavigationResourceThro
mkosiba (inactive)
2012/05/28 16:06:52
Done.
|
| + // level frame requests. |
| + DCHECK(info->IsMainFrame()); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind( |
| + &CheckShouldIgnoreNavigationOnUiThread, |
| + render_process_id, |
| + render_view_id, |
| + url, |
| + Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()), |
| + info->HasUserGesture(), |
| + base::Bind(&IgnoreNavigationResourceThrottle::OnResultObtained, |
| + weak_ptr_factory_.GetWeakPtr()))); |
| + return true; |
| +} |
| + |
| +void IgnoreNavigationResourceThrottle::OnResultObtained( |
| + bool should_ignore_navigation) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + if (should_ignore_navigation) { |
| + controller()->Cancel(); |
|
darin (slow to review)
2012/05/23 18:18:29
this may not be the ideal way to cancel the reques
mkosiba (inactive)
2012/05/28 16:06:52
Done.
|
| + } else { |
| + controller()->Resume(); |
| + } |
| +} |