| 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 "chrome/browser/renderer_host/intercept_navigation_resource_throttle.h" | |
| 6 | |
| 7 #include "content/public/browser/browser_thread.h" | |
| 8 #include "content/public/browser/child_process_security_policy.h" | |
| 9 #include "content/public/browser/render_view_host.h" | |
| 10 #include "content/public/browser/render_process_host.h" | |
| 11 #include "content/public/browser/resource_request_info.h" | |
| 12 #include "content/public/browser/resource_controller.h" | |
| 13 #include "content/public/common/referrer.h" | |
| 14 #include "net/url_request/url_request.h" | |
| 15 | |
| 16 using content::BrowserThread; | |
| 17 using content::ChildProcessSecurityPolicy; | |
| 18 using content::Referrer; | |
| 19 using content::RenderViewHost; | |
| 20 using content::ResourceRequestInfo; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 void CheckIfShouldIgnoreNavigationOnUIThread( | |
| 25 int render_process_id, | |
| 26 int render_view_id, | |
| 27 const GURL& url, | |
| 28 const Referrer& referrer, | |
| 29 bool is_content_initiated, | |
| 30 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback | |
| 31 should_ignore_callback, | |
| 32 base::Callback<void(bool)> callback) { | |
| 33 | |
| 34 RenderViewHost* rvh = | |
| 35 RenderViewHost::FromID(render_process_id, render_view_id); | |
| 36 | |
| 37 GURL validated_url(url); | |
| 38 RenderViewHost::FilterURL( | |
| 39 rvh->GetProcess()->GetID(), | |
| 40 false, | |
| 41 &validated_url); | |
| 42 | |
| 43 bool should_ignore_navigation = false; | |
| 44 should_ignore_navigation = should_ignore_callback.Run( | |
| 45 rvh, validated_url, referrer, is_content_initiated); | |
| 46 | |
| 47 BrowserThread::PostTask( | |
| 48 BrowserThread::IO, | |
| 49 FROM_HERE, | |
| 50 base::Bind(callback, should_ignore_navigation)); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle( | |
| 56 net::URLRequest* request, | |
| 57 CheckOnUIThreadCallback should_ignore_callback) | |
| 58 : request_(request), | |
| 59 should_ignore_callback_(should_ignore_callback), | |
| 60 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 61 } | |
| 62 | |
| 63 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() { | |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 65 } | |
| 66 | |
| 67 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer) { | |
| 68 *defer = CheckIfShouldIgnoreNavigation(request_->url()); | |
| 69 } | |
| 70 | |
| 71 void InterceptNavigationResourceThrottle::WillRedirectRequest( | |
| 72 const GURL& new_url, | |
| 73 bool* defer) { | |
| 74 *defer = CheckIfShouldIgnoreNavigation(new_url); | |
| 75 } | |
| 76 | |
| 77 bool InterceptNavigationResourceThrottle::CheckIfShouldIgnoreNavigation( | |
| 78 const GURL& url) { | |
| 79 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | |
| 80 if (!info) | |
| 81 return false; | |
| 82 | |
| 83 int render_process_id, render_view_id; | |
| 84 if (!info->GetAssociatedRenderView(&render_process_id, &render_view_id)) | |
| 85 return false; | |
| 86 | |
| 87 // This class should only be instantiated for top level frame requests. | |
| 88 DCHECK(info->IsMainFrame()); | |
| 89 | |
| 90 BrowserThread::PostTask( | |
| 91 BrowserThread::UI, | |
| 92 FROM_HERE, | |
| 93 base::Bind( | |
| 94 &CheckIfShouldIgnoreNavigationOnUIThread, | |
| 95 render_process_id, | |
| 96 render_view_id, | |
| 97 url, | |
| 98 Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()), | |
| 99 info->HasUserGesture(), | |
| 100 should_ignore_callback_, | |
| 101 base::Bind( | |
| 102 &InterceptNavigationResourceThrottle::OnResultObtained, | |
| 103 weak_ptr_factory_.GetWeakPtr()))); | |
| 104 | |
| 105 // Defer request while we wait for the UI thread to check if the navigation | |
| 106 // should be ignored. | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 void InterceptNavigationResourceThrottle::OnResultObtained( | |
| 111 bool should_ignore_navigation) { | |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 113 | |
| 114 if (should_ignore_navigation) { | |
| 115 controller()->CancelAndIgnore(); | |
| 116 } else { | |
| 117 controller()->Resume(); | |
| 118 } | |
| 119 } | |
| OLD | NEW |