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 "chrome/browser/renderer_host/intercept_navigation_resource_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/browser/web_contents.h" | |
14 #include "content/public/browser/web_contents_delegate.h" | |
15 #include "content/public/common/referrer.h" | |
16 #include "net/url_request/url_request.h" | |
17 | |
18 using content::BrowserThread; | |
19 using content::ChildProcessSecurityPolicy; | |
20 using content::Referrer; | |
21 using content::RenderViewHost; | |
22 using content::ResourceRequestInfo; | |
23 using content::WebContents; | |
24 using content::WebContentsDelegate; | |
25 | |
26 namespace { | |
27 | |
28 void CheckShouldIgnoreNavigationOnUIThread( | |
darin (slow to review)
2012/06/15 19:52:55
nit: CheckIfShouldIgnoreNavigationOnUIThread
mkosiba (inactive)
2012/06/18 10:47:06
Done.
| |
29 int render_process_id, | |
30 int render_view_id, | |
31 const GURL& url, | |
32 const Referrer& referrer, | |
33 bool is_content_initiated, | |
34 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback | |
35 should_ignore_callback, | |
36 base::Callback<void(bool)> callback) { | |
37 | |
38 RenderViewHost* rvh = | |
39 RenderViewHost::FromID(render_process_id, render_view_id); | |
40 WebContents* web_contents = | |
41 rvh ? WebContents::FromRenderViewHost(rvh) : NULL; | |
42 | |
43 GURL validated_url(url); | |
44 RenderViewHost::FilterURL( | |
45 rvh->GetProcess()->GetID(), | |
46 false, | |
47 &validated_url); | |
48 | |
49 bool should_ignore_navigation = false; | |
50 if (web_contents) { | |
51 should_ignore_navigation = should_ignore_callback.Run( | |
52 web_contents, validated_url, referrer, is_content_initiated); | |
darin (slow to review)
2012/06/15 19:52:55
nit: indentation
mkosiba (inactive)
2012/06/18 10:47:06
Done.
| |
53 } | |
54 | |
55 BrowserThread::PostTask( | |
56 BrowserThread::IO, | |
57 FROM_HERE, | |
58 base::Bind(callback, should_ignore_navigation)); | |
59 } | |
60 | |
61 } // namespace | |
62 | |
63 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle( | |
64 net::URLRequest* request, | |
65 CheckOnUIThreadCallback should_ignore_callback) | |
66 : request_(request), | |
67 should_ignore_callback_(should_ignore_callback), | |
68 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
darin (slow to review)
2012/06/15 19:52:55
nit: maybe just extend from SupportsWeakPtr so you
mkosiba (inactive)
2012/06/18 10:47:06
Extending from SupportsWeakPtr makes the WeakPtr s
| |
69 } | |
70 | |
71 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() { | |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
73 } | |
74 | |
75 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer) { | |
76 *defer = ShouldDeferRequestForURL(request_->url()); | |
77 } | |
78 | |
79 void InterceptNavigationResourceThrottle::WillRedirectRequest( | |
80 const GURL& new_url, | |
81 bool* defer) { | |
82 *defer = ShouldDeferRequestForURL(new_url); | |
83 } | |
84 | |
85 bool InterceptNavigationResourceThrottle::ShouldDeferRequestForURL( | |
darin (slow to review)
2012/06/15 19:52:55
it seems like this function is not named that well
mkosiba (inactive)
2012/06/18 10:47:06
Done.
| |
86 const GURL& url) { | |
87 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | |
88 if (!info) | |
89 return false; | |
90 | |
91 int render_process_id, render_view_id; | |
92 if (!info->GetAssociatedRenderView(&render_process_id, &render_view_id)) { | |
93 return false; | |
94 } | |
95 | |
96 // This class should only be instantiated for top level frame requests. | |
97 DCHECK(info->IsMainFrame()); | |
98 | |
99 BrowserThread::PostTask( | |
100 BrowserThread::UI, | |
101 FROM_HERE, | |
102 base::Bind( | |
103 &CheckShouldIgnoreNavigationOnUIThread, | |
104 render_process_id, | |
105 render_view_id, | |
106 url, | |
107 Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()), | |
108 info->HasUserGesture(), | |
109 should_ignore_callback_, | |
110 base::Bind( | |
111 &InterceptNavigationResourceThrottle::OnResultObtained, | |
112 weak_ptr_factory_.GetWeakPtr()))); | |
113 | |
114 // Defer request while we wait for the UI thread to check if the navigation | |
115 // should be ignored. | |
116 return true; | |
117 } | |
118 | |
119 void InterceptNavigationResourceThrottle::OnResultObtained( | |
120 bool should_ignore_navigation) { | |
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
122 | |
123 if (should_ignore_navigation) { | |
124 // TODO(mkosiba): Cancel() results in a CANCELLED status but what we really | |
darin (slow to review)
2012/06/15 19:52:55
I've been thinking of providing an alternate Cance
mkosiba (inactive)
2012/06/18 10:47:06
I don't think that'd help unless you also include
| |
125 // want is a HANDLED_EXTERNALLY status. | |
126 controller()->Cancel(); | |
127 } else { | |
128 controller()->Resume(); | |
129 } | |
130 } | |
OLD | NEW |