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 "content/components/navigation_interception/intercept_navigation_resource_throttle.h"
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"
17 using content::BrowserThread
;
18 using content::ChildProcessSecurityPolicy
;
19 using content::Referrer
;
20 using content::RenderViewHost
;
21 using content::ResourceRequestInfo
;
27 struct ShouldIgnoreCallbackParams
{
28 int render_process_id
;
32 bool has_user_gesture
;
34 PageTransition transition_type
;
37 void CheckIfShouldIgnoreNavigationOnUIThread(
38 const ShouldIgnoreCallbackParams
& params
,
39 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback
40 should_ignore_callback
,
41 base::Callback
<void(bool)> callback
) {
43 bool should_ignore_navigation
= false;
45 RenderViewHost::FromID(params
.render_process_id
, params
.render_view_id
);
48 GURL
validated_url(params
.url
);
49 RenderViewHost::FilterURL(rvh
->GetProcess(), false, &validated_url
);
51 should_ignore_navigation
= should_ignore_callback
.Run(
56 params
.has_user_gesture
,
57 params
.transition_type
);
60 BrowserThread::PostTask(
63 base::Bind(callback
, should_ignore_navigation
));
68 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle(
69 net::URLRequest
* request
,
70 CheckOnUIThreadCallback should_ignore_callback
)
72 should_ignore_callback_(should_ignore_callback
),
73 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
76 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
80 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer
) {
81 *defer
= CheckIfShouldIgnoreNavigation(request_
->url());
84 void InterceptNavigationResourceThrottle::WillRedirectRequest(
87 *defer
= CheckIfShouldIgnoreNavigation(new_url
);
90 bool InterceptNavigationResourceThrottle::CheckIfShouldIgnoreNavigation(
92 const ResourceRequestInfo
* info
= ResourceRequestInfo::ForRequest(request_
);
96 int render_process_id
, render_view_id
;
97 if (!info
->GetAssociatedRenderView(&render_process_id
, &render_view_id
))
100 ShouldIgnoreCallbackParams params
;
101 params
.render_process_id
= render_process_id
;
102 params
.render_view_id
= render_view_id
;
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();
110 BrowserThread::PostTask(
114 &CheckIfShouldIgnoreNavigationOnUIThread
,
116 should_ignore_callback_
,
118 &InterceptNavigationResourceThrottle::OnResultObtained
,
119 weak_ptr_factory_
.GetWeakPtr())));
121 // Defer request while we wait for the UI thread to check if the navigation
122 // should be ignored.
126 void InterceptNavigationResourceThrottle::OnResultObtained(
127 bool should_ignore_navigation
) {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
130 if (should_ignore_navigation
) {
131 controller()->CancelAndIgnore();
133 controller()->Resume();
137 } // namespace content