Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/intercept_navigation_resource_throttle_i mpl.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 #include "content/public/browser/child_process_security_policy.h" | |
| 10 #include "content/public/browser/render_view_host.h" | |
| 11 #include "content/public/browser/render_process_host.h" | |
| 12 #include "content/public/browser/resource_request_info.h" | |
| 13 #include "content/public/browser/resource_throttle_controller.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/browser/web_contents_delegate.h" | |
| 16 #include "content/public/common/referrer.h" | |
| 17 #include "net/url_request/url_request.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 using content::ChildProcessSecurityPolicy; | |
| 21 using content::InterceptNavigationResourceThrottle; | |
| 22 using content::Referrer; | |
| 23 using content::RenderViewHost; | |
| 24 using content::ResourceRequestInfo; | |
| 25 using content::WebContents; | |
| 26 using content::WebContentsDelegate; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 void CheckShouldIgnoreNavigationOnUIThread( | |
| 31 int render_process_id, | |
| 32 int render_view_id, | |
| 33 const GURL& url, | |
| 34 const Referrer& referrer, | |
| 35 bool is_content_initiated, | |
| 36 base::Callback<void(bool)> callback) { | |
| 37 | |
| 38 RenderViewHost* rvh = | |
| 39 RenderViewHost::FromID(render_process_id, render_view_id); | |
| 40 WebContents* web_contents = | |
| 41 rvh ? WebContents::FromRenderViewHost(rvh) : NULL; | |
| 42 WebContentsDelegate* web_contents_delegate = | |
| 43 web_contents ? web_contents->GetDelegate() : NULL; | |
| 44 | |
| 45 GURL validated_url(url); | |
| 46 RenderViewHost::FilterURL( | |
| 47 rvh->GetProcess()->GetID(), | |
| 48 false, | |
| 49 &validated_url); | |
| 50 | |
| 51 bool should_ignore_navigation = false; | |
| 52 if (web_contents_delegate) { | |
| 53 should_ignore_navigation = web_contents_delegate->ShouldIgnoreNavigation( | |
| 54 web_contents, validated_url, referrer, is_content_initiated); | |
| 55 } | |
| 56 | |
| 57 BrowserThread::PostTask( | |
| 58 BrowserThread::IO, | |
| 59 FROM_HERE, | |
| 60 base::Bind(callback, should_ignore_navigation)); | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 /////////////////////////////////////////////////////////////////////////////// | |
| 66 // InterceptNavigationResourceThrottle, public: | |
| 67 | |
| 68 // static | |
| 69 InterceptNavigationResourceThrottle* | |
| 70 InterceptNavigationResourceThrottle::Create(net::URLRequest* request) { | |
| 71 return new InterceptNavigationResourceThrottleImpl(request); | |
| 72 } | |
| 73 | |
| 74 /////////////////////////////////////////////////////////////////////////////// | |
| 75 | |
| 76 InterceptNavigationResourceThrottleImpl:: | |
| 77 InterceptNavigationResourceThrottleImpl(net::URLRequest* request) | |
| 78 : request_(request), | |
| 79 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 80 } | |
| 81 | |
| 82 InterceptNavigationResourceThrottleImpl:: | |
| 83 ~InterceptNavigationResourceThrottleImpl() { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 85 } | |
| 86 | |
| 87 void InterceptNavigationResourceThrottleImpl::WillStartRequest(bool* defer) { | |
| 88 *defer = ShouldDeferRequestForURL(request_->url()); | |
| 89 } | |
| 90 | |
| 91 void InterceptNavigationResourceThrottleImpl::WillRedirectRequest( | |
| 92 const GURL& new_url, | |
| 93 bool* defer) { | |
| 94 *defer = ShouldDeferRequestForURL(new_url); | |
| 95 } | |
| 96 | |
| 97 bool InterceptNavigationResourceThrottleImpl::ShouldDeferRequestForURL( | |
| 98 const GURL& url) { | |
| 99 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | |
| 100 if (!info) | |
| 101 return false; | |
| 102 | |
| 103 int render_process_id, render_view_id; | |
| 104 if (!info->GetAssociatedRenderView(&render_process_id, &render_view_id)) { | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 // This class should only be instantiated for top level frame requests. | |
| 109 DCHECK(info->IsMainFrame()); | |
| 110 | |
| 111 BrowserThread::PostTask( | |
| 112 BrowserThread::UI, | |
| 113 FROM_HERE, | |
| 114 base::Bind( | |
| 115 &CheckShouldIgnoreNavigationOnUIThread, | |
| 116 render_process_id, | |
| 117 render_view_id, | |
| 118 url, | |
| 119 Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()), | |
| 120 info->HasUserGesture(), | |
| 121 base::Bind( | |
| 122 &InterceptNavigationResourceThrottleImpl::OnResultObtained, | |
| 123 weak_ptr_factory_.GetWeakPtr()))); | |
| 124 return true; | |
|
joth
2012/06/13 22:32:49
maybe:
// Defer request, while we wait for the UI
mkosiba (inactive)
2012/06/15 11:05:36
Done.
| |
| 125 } | |
| 126 | |
| 127 void InterceptNavigationResourceThrottleImpl::OnResultObtained( | |
| 128 bool should_ignore_navigation) { | |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 130 | |
| 131 if (should_ignore_navigation) { | |
| 132 // TODO: Cancel() results in a CANCELLED status but what we really want is | |
|
joth
2012/06/13 22:32:49
TODO(mkosiba)
mkosiba (inactive)
2012/06/15 11:05:36
Done.
| |
| 133 // a HANDLED_EXTERNALLY status. | |
| 134 controller()->Cancel(); | |
| 135 } else { | |
| 136 controller()->Resume(); | |
| 137 } | |
| 138 } | |
| OLD | NEW |