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