Chromium Code Reviews| Index: chrome/browser/renderer_host/should_ignore_navigation_resource_throttle.cc |
| diff --git a/chrome/browser/renderer_host/should_ignore_navigation_resource_throttle.cc b/chrome/browser/renderer_host/should_ignore_navigation_resource_throttle.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d40ee2df61e283a3fe7077a1831d831ea321157e |
| --- /dev/null |
| +++ b/chrome/browser/renderer_host/should_ignore_navigation_resource_throttle.cc |
| @@ -0,0 +1,114 @@ |
| +// 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/should_ignore_navigation_resource_throttle.h" |
| + |
| +#include "base/callback.h" |
| +#include "content/browser/renderer_host/resource_request_info_impl.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( |
| + 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); |
|
joth
2012/05/11 15:34:13
Enclose in {} when it covers more than one physica
mkosiba (inactive)
2012/05/15 14:20:23
Done.
|
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, should_ignore_navigation)); |
| +} |
| + |
| +} // anonymous namespace |
|
joth
2012/05/11 15:34:13
nit: // namespace
mkosiba (inactive)
2012/05/15 14:20:23
Done.
|
| + |
| +ShouldIgnoreNavigationResourceThrottle::ShouldIgnoreNavigationResourceThrottle( |
| + net::URLRequest* request) : request_(request) { |
|
joth
2012/05/11 15:34:13
nit: initializer on its own line when the c'tor do
mkosiba (inactive)
2012/05/15 14:20:23
Done.
mkosiba (inactive)
2012/05/15 14:20:23
Done.
|
| +} |
| + |
| +ShouldIgnoreNavigationResourceThrottle:: |
| + ~ShouldIgnoreNavigationResourceThrottle() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| +} |
| + |
| +void ShouldIgnoreNavigationResourceThrottle::WillStartRequest(bool* defer) { |
| + ShouldDeferRequest(request_->url(), defer); |
| +} |
| + |
| +void ShouldIgnoreNavigationResourceThrottle::WillRedirectRequest( |
| + const GURL& new_url, |
| + bool* defer) { |
| + ShouldDeferRequest(new_url, defer); |
|
joth
2012/05/11 15:34:13
nit: ShouldDeferRequest is a slightly open name (s
mkosiba (inactive)
2012/05/15 14:20:23
Done
|
| +} |
| + |
| +void ShouldIgnoreNavigationResourceThrottle::ShouldDeferRequest( |
| + const GURL& url, |
| + bool* defer) { |
| + int render_process_id, render_view_id; |
| + if (!ResourceRequestInfo::GetRenderViewForRequest( |
| + request_, |
| + &render_process_id, |
| + &render_view_id)) { |
| + *defer = false; |
| + return; |
| + } |
| + *defer = true; |
| + |
| + const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| + // The ShouldIngoreNavigationResourceThrottle should only be created for top |
| + // level frame requests. |
| + DCHECK(info->IsMainFrame()); |
| + // TODO: Promote has_user_gesture to ResourceRequestInfo? |
|
joth
2012/05/11 15:34:13
TODO(mkosiba). Or word as a 'in future we may want
|
| + const bool is_content_initiated = |
|
joth
2012/05/11 15:34:13
nit: chromium style generally shuns const here. I
mkosiba (inactive)
2012/05/15 14:20:23
Done.
|
| + content::ResourceRequestInfoImpl::ForRequest( |
| + request_)->has_user_gesture(); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind( |
| + &CheckShouldIgnoreNavigationOnUiThread, |
| + render_process_id, |
| + render_view_id, |
| + url, |
| + Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()), |
| + is_content_initiated, |
| + base::Bind(&ShouldIgnoreNavigationResourceThrottle::OnResultObtained, |
| + AsWeakPtr()))); |
|
joth
2012/05/11 15:34:13
currytastic :)
|
| +} |
| + |
| +void ShouldIgnoreNavigationResourceThrottle::OnResultObtained( |
| + bool should_ignore_navigation) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + if (should_ignore_navigation) |
| + controller()->Cancel(); |
| + else |
|
joth
2012/05/11 15:34:13
nit: use braces when there's an else
mkosiba (inactive)
2012/05/15 14:20:23
Done.
|
| + controller()->Resume(); |
| +} |