OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/cross_site_resource_handler.h" | 5 #include "content/browser/loader/cross_site_resource_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "content/browser/cross_site_request_manager.h" |
| 12 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
11 #include "content/browser/loader/resource_request_info_impl.h" | 13 #include "content/browser/loader/resource_request_info_impl.h" |
12 #include "content/browser/renderer_host/render_view_host_delegate.h" | 14 #include "content/browser/renderer_host/render_view_host_delegate.h" |
13 #include "content/browser/renderer_host/render_view_host_impl.h" | 15 #include "content/browser/renderer_host/render_view_host_impl.h" |
14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/content_browser_client.h" |
15 #include "content/public/browser/global_request_id.h" | 18 #include "content/public/browser/global_request_id.h" |
16 #include "content/public/browser/resource_controller.h" | 19 #include "content/public/browser/resource_controller.h" |
17 #include "content/public/common/resource_response.h" | 20 #include "content/public/common/resource_response.h" |
18 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request.h" |
19 | 23 |
20 namespace content { | 24 namespace content { |
21 | 25 |
22 namespace { | 26 namespace { |
23 | 27 |
24 void OnCrossSiteResponseHelper(int render_process_id, | 28 void OnCrossSiteResponseHelper(int render_process_id, |
25 int render_view_id, | 29 int render_view_id, |
26 int request_id) { | 30 const GURL& new_url, |
| 31 const Referrer& referrer, |
| 32 int64 frame_id, |
| 33 const GlobalRequestID& global_request_id) { |
27 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID(render_process_id, | 34 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID(render_process_id, |
28 render_view_id); | 35 render_view_id); |
29 if (rvh && rvh->GetDelegate()->GetRendererManagementDelegate()) { | 36 if (!rvh) |
30 rvh->GetDelegate()->GetRendererManagementDelegate()->OnCrossSiteResponse( | 37 return; |
31 rvh, GlobalRequestID(render_process_id, request_id)); | 38 RenderViewHostDelegate* delegate = rvh->GetDelegate(); |
32 } | 39 if (!delegate || !delegate->GetRendererManagementDelegate()) |
| 40 return; |
| 41 |
| 42 delegate->GetRendererManagementDelegate()->OnCrossSiteResponse( |
| 43 rvh, new_url, referrer, frame_id, global_request_id); |
33 } | 44 } |
34 | 45 |
35 } // namespace | 46 } // namespace |
36 | 47 |
37 CrossSiteResourceHandler::CrossSiteResourceHandler( | 48 CrossSiteResourceHandler::CrossSiteResourceHandler( |
38 scoped_ptr<ResourceHandler> next_handler, | 49 scoped_ptr<ResourceHandler> next_handler, |
39 int render_process_host_id, | 50 int render_process_host_id, |
40 int render_view_id, | 51 int render_view_id, |
41 net::URLRequest* request) | 52 net::URLRequest* request) |
42 : LayeredResourceHandler(next_handler.Pass()), | 53 : LayeredResourceHandler(next_handler.Pass()), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 bool* defer) { | 85 bool* defer) { |
75 // At this point, we know that the response is safe to send back to the | 86 // At this point, we know that the response is safe to send back to the |
76 // renderer: it is not a download, and it has passed the SSL and safe | 87 // renderer: it is not a download, and it has passed the SSL and safe |
77 // browsing checks. | 88 // browsing checks. |
78 // We should not have already started the transition before now. | 89 // We should not have already started the transition before now. |
79 DCHECK(!in_cross_site_transition_); | 90 DCHECK(!in_cross_site_transition_); |
80 has_started_response_ = true; | 91 has_started_response_ = true; |
81 | 92 |
82 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); | 93 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); |
83 | 94 |
| 95 // We will need to swap processes if either (1) a redirect that requires a |
| 96 // transfer occurred before we got here, or (2) a pending cross-site request |
| 97 // was already in progress. |
| 98 bool should_transfer = |
| 99 GetContentClient()->browser()->ShouldSwapProcessesForRedirect( |
| 100 info->GetContext(), request_->original_url(), request_->url()); |
| 101 bool swap_needed = should_transfer || |
| 102 CrossSiteRequestManager::GetInstance()-> |
| 103 HasPendingCrossSiteRequest(render_process_host_id_, render_view_id_); |
| 104 |
84 // If this is a download, just pass the response through without doing a | 105 // If this is a download, just pass the response through without doing a |
85 // cross-site check. The renderer will see it is a download and abort the | 106 // cross-site check. The renderer will see it is a download and abort the |
86 // request. | 107 // request. |
87 // | 108 // |
88 // Similarly, HTTP 204 (No Content) responses leave us showing the previous | 109 // Similarly, HTTP 204 (No Content) responses leave us showing the previous |
89 // page. We should allow the navigation to finish without running the unload | 110 // page. We should allow the navigation to finish without running the unload |
90 // handler or swapping in the pending RenderViewHost. | 111 // handler or swapping in the pending RenderViewHost. |
91 // | 112 // |
92 // In both cases, the pending RenderViewHost will stick around until the next | 113 // In both cases, any pending RenderViewHost (if one was created for this |
93 // cross-site navigation, since we are unable to tell when to destroy it. | 114 // navigation) will stick around until the next cross-site navigation, since |
| 115 // we are unable to tell when to destroy it. |
94 // See RenderViewHostManager::RendererAbortedProvisionalLoad. | 116 // See RenderViewHostManager::RendererAbortedProvisionalLoad. |
95 if (info->is_download() || (response->head.headers.get() && | 117 if (!swap_needed || info->is_download() || |
96 response->head.headers->response_code() == 204)) { | 118 (response->head.headers.get() && |
| 119 response->head.headers->response_code() == 204)) { |
97 return next_handler_->OnResponseStarted(request_id, response, defer); | 120 return next_handler_->OnResponseStarted(request_id, response, defer); |
98 } | 121 } |
99 | 122 |
100 // Tell the renderer to run the onunload event handler. | 123 // Now that we know a swap is needed and we have something to commit, we |
101 StartCrossSiteTransition(request_id, response); | 124 // pause to let the UI thread run the unload handler of the previous page |
| 125 // and set up a transfer if needed. |
| 126 StartCrossSiteTransition(request_id, response, should_transfer); |
102 | 127 |
103 // Defer loading until after the onunload event handler has run. | 128 // Defer loading until after the onunload event handler has run. |
104 did_defer_ = *defer = true; | 129 did_defer_ = *defer = true; |
105 return true; | 130 return true; |
106 } | 131 } |
107 | 132 |
108 bool CrossSiteResourceHandler::OnReadCompleted(int request_id, | 133 bool CrossSiteResourceHandler::OnReadCompleted(int request_id, |
109 int bytes_read, | 134 int bytes_read, |
110 bool* defer) { | 135 bool* defer) { |
111 CHECK(!in_cross_site_transition_); | 136 CHECK(!in_cross_site_transition_); |
112 return next_handler_->OnReadCompleted(request_id, bytes_read, defer); | 137 return next_handler_->OnReadCompleted(request_id, bytes_read, defer); |
113 } | 138 } |
114 | 139 |
115 bool CrossSiteResourceHandler::OnResponseCompleted( | 140 bool CrossSiteResourceHandler::OnResponseCompleted( |
116 int request_id, | 141 int request_id, |
117 const net::URLRequestStatus& status, | 142 const net::URLRequestStatus& status, |
118 const std::string& security_info) { | 143 const std::string& security_info) { |
119 if (!in_cross_site_transition_) { | 144 if (!in_cross_site_transition_) { |
| 145 // If we've already completed the transition, or we're canceling the |
| 146 // request, or an error occurred with no cross-process navigation in |
| 147 // progress, then we should just pass this through. |
120 if (has_started_response_ || | 148 if (has_started_response_ || |
121 status.status() != net::URLRequestStatus::FAILED) { | 149 status.status() != net::URLRequestStatus::FAILED || |
122 // We've already completed the transition or we're canceling the request, | 150 !CrossSiteRequestManager::GetInstance()->HasPendingCrossSiteRequest( |
123 // so just pass it through. | 151 render_process_host_id_, render_view_id_)) { |
124 return next_handler_->OnResponseCompleted(request_id, status, | 152 return next_handler_->OnResponseCompleted(request_id, status, |
125 security_info); | 153 security_info); |
126 } | 154 } |
127 | 155 |
128 // An error occured, we should wait now for the cross-site transition, | 156 // An error occurred. We should wait now for the cross-process transition, |
129 // so that the error message (e.g., 404) can be displayed to the user. | 157 // so that the error message (e.g., 404) can be displayed to the user. |
130 // Also continue with the logic below to remember that we completed | 158 // Also continue with the logic below to remember that we completed |
131 // during the cross-site transition. | 159 // during the cross-site transition. |
132 StartCrossSiteTransition(request_id, NULL); | 160 StartCrossSiteTransition(request_id, NULL, false); |
133 } | 161 } |
134 | 162 |
135 // We have to buffer the call until after the transition completes. | 163 // We have to buffer the call until after the transition completes. |
136 completed_during_transition_ = true; | 164 completed_during_transition_ = true; |
137 completed_status_ = status; | 165 completed_status_ = status; |
138 completed_security_info_ = security_info; | 166 completed_security_info_ = security_info; |
139 | 167 |
140 // Return false to tell RDH not to notify the world or clean up the | 168 // Return false to tell RDH not to notify the world or clean up the |
141 // pending request. We will do so in ResumeResponse. | 169 // pending request. We will do so in ResumeResponse. |
142 did_defer_ = true; | 170 did_defer_ = true; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 completed_security_info_)) { | 203 completed_security_info_)) { |
176 ResumeIfDeferred(); | 204 ResumeIfDeferred(); |
177 } | 205 } |
178 } | 206 } |
179 } | 207 } |
180 | 208 |
181 // Prepare to render the cross-site response in a new RenderViewHost, by | 209 // Prepare to render the cross-site response in a new RenderViewHost, by |
182 // telling the old RenderViewHost to run its onunload handler. | 210 // telling the old RenderViewHost to run its onunload handler. |
183 void CrossSiteResourceHandler::StartCrossSiteTransition( | 211 void CrossSiteResourceHandler::StartCrossSiteTransition( |
184 int request_id, | 212 int request_id, |
185 ResourceResponse* response) { | 213 ResourceResponse* response, |
| 214 bool should_transfer) { |
186 in_cross_site_transition_ = true; | 215 in_cross_site_transition_ = true; |
187 request_id_ = request_id; | 216 request_id_ = request_id; |
188 response_ = response; | 217 response_ = response; |
189 | 218 |
190 // Store this handler on the ExtraRequestInfo, so that RDH can call our | 219 // Store this handler on the ExtraRequestInfo, so that RDH can call our |
191 // ResumeResponse method when the close ACK is received. | 220 // ResumeResponse method when we are ready to resume. |
192 ResourceRequestInfoImpl* info = | 221 ResourceRequestInfoImpl* info = |
193 ResourceRequestInfoImpl::ForRequest(request_); | 222 ResourceRequestInfoImpl::ForRequest(request_); |
194 info->set_cross_site_handler(this); | 223 info->set_cross_site_handler(this); |
195 | 224 |
| 225 DCHECK_EQ(render_process_host_id_, info->GetChildID()); |
| 226 DCHECK_EQ(request_id, info->GetRequestID()); |
| 227 GlobalRequestID global_id(info->GetChildID(), info->GetRequestID()); |
| 228 |
196 // Tell the contents responsible for this request that a cross-site response | 229 // Tell the contents responsible for this request that a cross-site response |
197 // is starting, so that it can tell its old renderer to run its onunload | 230 // is starting, so that it can tell its old renderer to run its onunload |
198 // handler now. We will wait to hear the corresponding ClosePage_ACK. | 231 // handler now. We will wait until the unload is finished and (if a transfer |
| 232 // is needed) for the new renderer's request to arrive. |
| 233 GURL new_url; |
| 234 Referrer referrer; |
| 235 if (should_transfer) { |
| 236 // This will doom the handler, so we should only do this if we know a |
| 237 // transfer is required. |
| 238 // TODO(creis): Look into an alternative if we want to move the policy |
| 239 // check to the UI thread, in which case we might resume it without |
| 240 // transferring. |
| 241 ResourceDispatcherHostImpl::Get()->MarkAsTransferredNavigation(global_id); |
| 242 |
| 243 new_url = request_->url(); |
| 244 referrer = Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()); |
| 245 } |
199 BrowserThread::PostTask( | 246 BrowserThread::PostTask( |
200 BrowserThread::UI, | 247 BrowserThread::UI, |
201 FROM_HERE, | 248 FROM_HERE, |
202 base::Bind( | 249 base::Bind( |
203 &OnCrossSiteResponseHelper, | 250 &OnCrossSiteResponseHelper, |
204 render_process_host_id_, | 251 render_process_host_id_, |
205 render_view_id_, | 252 render_view_id_, |
206 request_id)); | 253 new_url, |
207 | 254 referrer, |
208 // TODO(creis): If the above call should fail, then we need to notify the IO | 255 info->GetFrameID(), |
209 // thread to proceed anyway, using ResourceDispatcherHost::OnClosePageACK. | 256 global_id)); |
210 } | 257 } |
211 | 258 |
212 void CrossSiteResourceHandler::ResumeIfDeferred() { | 259 void CrossSiteResourceHandler::ResumeIfDeferred() { |
213 if (did_defer_) { | 260 if (did_defer_) { |
214 did_defer_ = false; | 261 did_defer_ = false; |
215 controller()->Resume(); | 262 controller()->Resume(); |
216 } | 263 } |
217 } | 264 } |
218 | 265 |
219 } // namespace content | 266 } // namespace content |
OLD | NEW |