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_context.h"
13 #include "content/public/browser/resource_controller.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/referrer.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/url_request/redirect_info.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_job_factory.h"
21 #include "net/url_request/url_request.h"
22 #include "ui/base/page_transition_types.h"
24 using content::BrowserThread
;
25 using content::ChildProcessSecurityPolicy
;
26 using ui::PageTransition
;
27 using content::Referrer
;
28 using content::RenderProcessHost
;
29 using content::ResourceRequestInfo
;
31 namespace navigation_interception
{
35 void CheckIfShouldIgnoreNavigationOnUIThread(
36 int render_process_id
,
38 const NavigationParams
& navigation_params
,
39 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback
40 should_ignore_callback
,
41 base::Callback
<void(bool)> callback
) {
42 bool should_ignore_navigation
= false;
43 RenderProcessHost
* rph
= RenderProcessHost::FromID(render_process_id
);
45 NavigationParams
validated_params(navigation_params
);
46 rph
->FilterURL(false, &validated_params
.url());
48 content::RenderFrameHost
* render_frame_host
=
49 content::RenderFrameHost::FromID(render_process_id
, render_frame_id
);
50 content::WebContents
* web_contents
=
51 content::WebContents::FromRenderFrameHost(render_frame_host
);
54 should_ignore_navigation
= should_ignore_callback
.Run(web_contents
,
59 BrowserThread::PostTask(
62 base::Bind(callback
, should_ignore_navigation
));
67 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle(
68 net::URLRequest
* request
,
69 CheckOnUIThreadCallback should_ignore_callback
)
71 should_ignore_callback_(should_ignore_callback
),
72 weak_ptr_factory_(this) {
75 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
79 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer
) {
81 CheckIfShouldIgnoreNavigation(request_
->url(), request_
->method(), false);
84 void InterceptNavigationResourceThrottle::WillRedirectRequest(
85 const net::RedirectInfo
& redirect_info
,
87 *defer
= CheckIfShouldIgnoreNavigation(redirect_info
.new_url
,
88 redirect_info
.new_method
, true);
91 const char* InterceptNavigationResourceThrottle::GetNameForLogging() const {
92 return "InterceptNavigationResourceThrottle";
95 bool InterceptNavigationResourceThrottle::CheckIfShouldIgnoreNavigation(
97 const std::string
& method
,
99 const ResourceRequestInfo
* info
= ResourceRequestInfo::ForRequest(request_
);
103 int render_process_id
, render_frame_id
;
104 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
))
107 bool is_external_protocol
=
108 !info
->GetContext()->GetRequestContext()->job_factory()->IsHandledURL(
110 NavigationParams
navigation_params(
112 Referrer::SanitizeForRequest(
113 url
, Referrer(GURL(request_
->referrer()), info
->GetReferrerPolicy())),
114 info
->HasUserGesture(), method
== "POST", info
->GetPageTransition(),
115 is_redirect
, is_external_protocol
);
117 BrowserThread::PostTask(
121 &CheckIfShouldIgnoreNavigationOnUIThread
,
125 should_ignore_callback_
,
127 &InterceptNavigationResourceThrottle::OnResultObtained
,
128 weak_ptr_factory_
.GetWeakPtr())));
130 // Defer request while we wait for the UI thread to check if the navigation
131 // should be ignored.
135 void InterceptNavigationResourceThrottle::OnResultObtained(
136 bool should_ignore_navigation
) {
137 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
139 if (should_ignore_navigation
) {
140 controller()->CancelAndIgnore();
142 controller()->Resume();
146 } // namespace navigation_interception