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.
5 #include "components/navigation_interception/intercept_navigation_resource_throttle.h"
7 #include "components/navigation_interception/navigation_params.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_frame_host.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/resource_controller.h"
13 #include "content/public/browser/resource_request_info.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/page_transition_types.h"
16 #include "content/public/common/referrer.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/url_request/url_request.h"
20 using content::BrowserThread
;
21 using content::ChildProcessSecurityPolicy
;
22 using content::PageTransition
;
23 using content::Referrer
;
24 using content::RenderProcessHost
;
25 using content::ResourceRequestInfo
;
27 namespace navigation_interception
{
31 void CheckIfShouldIgnoreNavigationOnUIThread(
32 int render_process_id
,
34 const NavigationParams
& navigation_params
,
35 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback
36 should_ignore_callback
,
37 base::Callback
<void(bool)> callback
) {
38 bool should_ignore_navigation
= false;
39 RenderProcessHost
* rph
= RenderProcessHost::FromID(render_process_id
);
41 NavigationParams
validated_params(navigation_params
);
42 rph
->FilterURL(false, &validated_params
.url());
44 content::RenderFrameHost
* render_frame_host
=
45 content::RenderFrameHost::FromID(render_process_id
, render_frame_id
);
46 content::WebContents
* web_contents
=
47 content::WebContents::FromRenderFrameHost(render_frame_host
);
50 should_ignore_navigation
= should_ignore_callback
.Run(web_contents
,
55 BrowserThread::PostTask(
58 base::Bind(callback
, should_ignore_navigation
));
63 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle(
64 net::URLRequest
* request
,
65 CheckOnUIThreadCallback should_ignore_callback
)
67 should_ignore_callback_(should_ignore_callback
),
68 weak_ptr_factory_(this) {
71 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
75 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer
) {
77 CheckIfShouldIgnoreNavigation(request_
->url(), request_
->method(), false);
80 void InterceptNavigationResourceThrottle::WillRedirectRequest(
84 CheckIfShouldIgnoreNavigation(new_url
, GetMethodAfterRedirect(), true);
87 const char* InterceptNavigationResourceThrottle::GetNameForLogging() const {
88 return "InterceptNavigationResourceThrottle";
91 std::string
InterceptNavigationResourceThrottle::GetMethodAfterRedirect() {
92 net::HttpResponseHeaders
* headers
= request_
->response_headers();
94 return request_
->method();
95 return net::URLRequest::ComputeMethodForRedirect(
96 request_
->method(), headers
->response_code());
99 bool InterceptNavigationResourceThrottle::CheckIfShouldIgnoreNavigation(
101 const std::string
& method
,
103 const ResourceRequestInfo
* info
= ResourceRequestInfo::ForRequest(request_
);
107 int render_process_id
, render_frame_id
;
108 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
))
111 NavigationParams
navigation_params(url
,
112 Referrer(GURL(request_
->referrer()),
113 info
->GetReferrerPolicy()),
114 info
->HasUserGesture(),
116 info
->GetPageTransition(),
119 BrowserThread::PostTask(
123 &CheckIfShouldIgnoreNavigationOnUIThread
,
127 should_ignore_callback_
,
129 &InterceptNavigationResourceThrottle::OnResultObtained
,
130 weak_ptr_factory_
.GetWeakPtr())));
132 // Defer request while we wait for the UI thread to check if the navigation
133 // should be ignored.
137 void InterceptNavigationResourceThrottle::OnResultObtained(
138 bool should_ignore_navigation
) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
141 if (should_ignore_navigation
) {
142 controller()->CancelAndIgnore();
144 controller()->Resume();
148 } // namespace navigation_interception