OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/loader/navigation_resource_throttle.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "content/browser/frame_host/navigation_handle_impl.h" |
| 9 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 10 #include "content/public/browser/resource_context.h" |
| 11 #include "content/public/browser/resource_controller.h" |
| 12 #include "content/public/browser/resource_request_info.h" |
| 13 #include "content/public/common/referrer.h" |
| 14 #include "net/url_request/redirect_info.h" |
| 15 #include "net/url_request/url_request.h" |
| 16 #include "net/url_request/url_request_context.h" |
| 17 #include "net/url_request/url_request_job_factory.h" |
| 18 #include "ui/base/page_transition_types.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 namespace { |
| 23 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)> |
| 24 UIChecksPerformedCallback; |
| 25 |
| 26 void CheckWillStartRequestOnUIThread(UIChecksPerformedCallback callback, |
| 27 int render_process_id, |
| 28 int render_frame_host_id, |
| 29 bool is_post, |
| 30 const Referrer& sanitized_referrer, |
| 31 bool has_user_gesture, |
| 32 ui::PageTransition transition, |
| 33 bool is_external_protocol) { |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 35 NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::PROCEED; |
| 36 RenderFrameHostImpl* render_frame_host = |
| 37 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); |
| 38 if (render_frame_host) { |
| 39 NavigationHandleImpl* navigation_handle = |
| 40 render_frame_host->navigation_handle(); |
| 41 if (navigation_handle) { |
| 42 result = navigation_handle->WillStartRequest(is_post, sanitized_referrer, |
| 43 has_user_gesture, transition, |
| 44 is_external_protocol); |
| 45 } |
| 46 } |
| 47 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 48 base::Bind(callback, result)); |
| 49 } |
| 50 |
| 51 void CheckWillRedirectRequestOnUIThread(UIChecksPerformedCallback callback, |
| 52 int render_process_id, |
| 53 int render_frame_host_id, |
| 54 const GURL& new_url, |
| 55 bool new_method_is_post, |
| 56 const GURL& new_referrer_url, |
| 57 bool new_is_external_protocol) { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 59 NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::PROCEED; |
| 60 RenderFrameHostImpl* render_frame_host = |
| 61 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); |
| 62 if (render_frame_host) { |
| 63 NavigationHandleImpl* navigation_handle = |
| 64 render_frame_host->navigation_handle(); |
| 65 if (navigation_handle) { |
| 66 RenderProcessHost* rph = RenderProcessHost::FromID(render_process_id); |
| 67 GURL new_validated_url = new_url; |
| 68 rph->FilterURL(false, &new_validated_url); |
| 69 result = navigation_handle->WillRedirectRequest( |
| 70 new_validated_url, new_method_is_post, new_referrer_url, |
| 71 new_is_external_protocol); |
| 72 } |
| 73 } |
| 74 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 75 base::Bind(callback, result)); |
| 76 } |
| 77 } // namespace |
| 78 |
| 79 NavigationResourceThrottle::NavigationResourceThrottle(net::URLRequest* request) |
| 80 : request_(request), weak_ptr_factory_(this) {} |
| 81 |
| 82 NavigationResourceThrottle::~NavigationResourceThrottle() {} |
| 83 |
| 84 void NavigationResourceThrottle::WillStartRequest(bool* defer) { |
| 85 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| 86 if (!info) |
| 87 return; |
| 88 |
| 89 int render_process_id, render_frame_id; |
| 90 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) |
| 91 return; |
| 92 |
| 93 bool is_external_protocol = |
| 94 !info->GetContext()->GetRequestContext()->job_factory()->IsHandledURL( |
| 95 request_->url()); |
| 96 UIChecksPerformedCallback callback = |
| 97 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed, |
| 98 weak_ptr_factory_.GetWeakPtr()); |
| 99 DCHECK(request_->method() == "POST" || request_->method() == "GET"); |
| 100 BrowserThread::PostTask( |
| 101 BrowserThread::UI, FROM_HERE, |
| 102 base::Bind(&CheckWillStartRequestOnUIThread, callback, render_process_id, |
| 103 render_frame_id, request_->method() == "POST", |
| 104 Referrer::SanitizeForRequest( |
| 105 request_->url(), Referrer(GURL(request_->referrer()), |
| 106 info->GetReferrerPolicy())), |
| 107 info->HasUserGesture(), info->GetPageTransition(), |
| 108 is_external_protocol)); |
| 109 *defer = true; |
| 110 } |
| 111 |
| 112 void NavigationResourceThrottle::WillRedirectRequest( |
| 113 const net::RedirectInfo& redirect_info, |
| 114 bool* defer) { |
| 115 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| 116 if (!info) |
| 117 return; |
| 118 |
| 119 int render_process_id, render_frame_id; |
| 120 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) |
| 121 return; |
| 122 |
| 123 bool new_is_external_protocol = |
| 124 !info->GetContext()->GetRequestContext()->job_factory()->IsHandledURL( |
| 125 request_->url()); |
| 126 DCHECK(redirect_info.new_method == "POST" || |
| 127 redirect_info.new_method == "GET"); |
| 128 UIChecksPerformedCallback callback = |
| 129 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed, |
| 130 weak_ptr_factory_.GetWeakPtr()); |
| 131 BrowserThread::PostTask( |
| 132 BrowserThread::UI, FROM_HERE, |
| 133 base::Bind(&CheckWillRedirectRequestOnUIThread, callback, |
| 134 render_process_id, render_frame_id, redirect_info.new_url, |
| 135 redirect_info.new_method == "POST", |
| 136 GURL(redirect_info.new_referrer), new_is_external_protocol)); |
| 137 *defer = true; |
| 138 } |
| 139 |
| 140 const char* NavigationResourceThrottle::GetNameForLogging() const { |
| 141 return "NavigationResourceThrottle"; |
| 142 } |
| 143 |
| 144 void NavigationResourceThrottle::OnUIChecksPerformed( |
| 145 NavigationThrottle::ThrottleCheckResult result) { |
| 146 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 147 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { |
| 148 controller()->CancelAndIgnore(); |
| 149 } else { |
| 150 controller()->Resume(); |
| 151 } |
| 152 } |
| 153 |
| 154 } // namespace content |
OLD | NEW |