| 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/components/navigation_interception/intercept_navigation_resour
ce_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/page_transition_types.h" | |
| 14 #include "content/public/common/referrer.h" | |
| 15 #include "net/url_request/url_request.h" | |
| 16 | |
| 17 using content::BrowserThread; | |
| 18 using content::ChildProcessSecurityPolicy; | |
| 19 using content::Referrer; | |
| 20 using content::RenderViewHost; | |
| 21 using content::ResourceRequestInfo; | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 struct ShouldIgnoreCallbackParams { | |
| 28 int render_process_id; | |
| 29 int render_view_id; | |
| 30 GURL url; | |
| 31 Referrer referrer; | |
| 32 bool has_user_gesture; | |
| 33 bool is_post; | |
| 34 PageTransition transition_type; | |
| 35 }; | |
| 36 | |
| 37 void CheckIfShouldIgnoreNavigationOnUIThread( | |
| 38 const ShouldIgnoreCallbackParams& params, | |
| 39 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback | |
| 40 should_ignore_callback, | |
| 41 base::Callback<void(bool)> callback) { | |
| 42 | |
| 43 bool should_ignore_navigation = false; | |
| 44 RenderViewHost* rvh = | |
| 45 RenderViewHost::FromID(params.render_process_id, params.render_view_id); | |
| 46 | |
| 47 if (rvh) { | |
| 48 GURL validated_url(params.url); | |
| 49 RenderViewHost::FilterURL(rvh->GetProcess(), false, &validated_url); | |
| 50 | |
| 51 should_ignore_navigation = should_ignore_callback.Run( | |
| 52 rvh, | |
| 53 validated_url, | |
| 54 params.referrer, | |
| 55 params.is_post, | |
| 56 params.has_user_gesture, | |
| 57 params.transition_type); | |
| 58 } | |
| 59 | |
| 60 BrowserThread::PostTask( | |
| 61 BrowserThread::IO, | |
| 62 FROM_HERE, | |
| 63 base::Bind(callback, should_ignore_navigation)); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle( | |
| 69 net::URLRequest* request, | |
| 70 CheckOnUIThreadCallback should_ignore_callback) | |
| 71 : request_(request), | |
| 72 should_ignore_callback_(should_ignore_callback), | |
| 73 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 74 } | |
| 75 | |
| 76 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() { | |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 78 } | |
| 79 | |
| 80 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer) { | |
| 81 *defer = CheckIfShouldIgnoreNavigation(request_->url()); | |
| 82 } | |
| 83 | |
| 84 void InterceptNavigationResourceThrottle::WillRedirectRequest( | |
| 85 const GURL& new_url, | |
| 86 bool* defer) { | |
| 87 *defer = CheckIfShouldIgnoreNavigation(new_url); | |
| 88 } | |
| 89 | |
| 90 bool InterceptNavigationResourceThrottle::CheckIfShouldIgnoreNavigation( | |
| 91 const GURL& url) { | |
| 92 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | |
| 93 if (!info) | |
| 94 return false; | |
| 95 | |
| 96 int render_process_id, render_view_id; | |
| 97 if (!info->GetAssociatedRenderView(&render_process_id, &render_view_id)) | |
| 98 return false; | |
| 99 | |
| 100 ShouldIgnoreCallbackParams params; | |
| 101 params.render_process_id = render_process_id; | |
| 102 params.render_view_id = render_view_id; | |
| 103 params.url = url; | |
| 104 params.referrer = Referrer(GURL(request_->referrer()), | |
| 105 info->GetReferrerPolicy()); | |
| 106 params.has_user_gesture = info->HasUserGesture(); | |
| 107 params.is_post = request_->method() == "POST"; | |
| 108 params.transition_type = info->GetPageTransition(); | |
| 109 | |
| 110 BrowserThread::PostTask( | |
| 111 BrowserThread::UI, | |
| 112 FROM_HERE, | |
| 113 base::Bind( | |
| 114 &CheckIfShouldIgnoreNavigationOnUIThread, | |
| 115 params, | |
| 116 should_ignore_callback_, | |
| 117 base::Bind( | |
| 118 &InterceptNavigationResourceThrottle::OnResultObtained, | |
| 119 weak_ptr_factory_.GetWeakPtr()))); | |
| 120 | |
| 121 // Defer request while we wait for the UI thread to check if the navigation | |
| 122 // should be ignored. | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 void InterceptNavigationResourceThrottle::OnResultObtained( | |
| 127 bool should_ignore_navigation) { | |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 129 | |
| 130 if (should_ignore_navigation) { | |
| 131 controller()->CancelAndIgnore(); | |
| 132 } else { | |
| 133 controller()->Resume(); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 } // namespace content | |
| OLD | NEW |