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