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