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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 delegate->GetRendererManagementDelegate()->OnCrossSiteResponse( | 43 delegate->GetRendererManagementDelegate()->OnCrossSiteResponse( |
44 rvh, global_request_id, is_transfer, transfer_url, referrer, frame_id); | 44 rvh, global_request_id, is_transfer, transfer_url, referrer, frame_id); |
45 } | 45 } |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 CrossSiteResourceHandler::CrossSiteResourceHandler( | 49 CrossSiteResourceHandler::CrossSiteResourceHandler( |
50 scoped_ptr<ResourceHandler> next_handler, | 50 scoped_ptr<ResourceHandler> next_handler, |
51 net::URLRequest* request) | 51 net::URLRequest* request) |
52 : LayeredResourceHandler(next_handler.Pass()), | 52 : LayeredResourceHandler(request, next_handler.Pass()), |
53 request_(request), | |
54 has_started_response_(false), | 53 has_started_response_(false), |
55 in_cross_site_transition_(false), | 54 in_cross_site_transition_(false), |
56 completed_during_transition_(false), | 55 completed_during_transition_(false), |
57 did_defer_(false), | 56 did_defer_(false), |
58 completed_status_(), | 57 completed_status_(), |
59 response_(NULL) { | 58 response_(NULL) { |
60 } | 59 } |
61 | 60 |
62 CrossSiteResourceHandler::~CrossSiteResourceHandler() { | 61 CrossSiteResourceHandler::~CrossSiteResourceHandler() { |
63 // Cleanup back-pointer stored on the request info. | 62 // Cleanup back-pointer stored on the request info. |
64 ResourceRequestInfoImpl::ForRequest(request_)->set_cross_site_handler(NULL); | 63 GetRequestInfo()->set_cross_site_handler(NULL); |
65 } | 64 } |
66 | 65 |
67 bool CrossSiteResourceHandler::OnRequestRedirected( | 66 bool CrossSiteResourceHandler::OnRequestRedirected( |
68 int request_id, | 67 int request_id, |
69 const GURL& new_url, | 68 const GURL& new_url, |
70 ResourceResponse* response, | 69 ResourceResponse* response, |
71 bool* defer) { | 70 bool* defer) { |
72 // We should not have started the transition before being redirected. | 71 // We should not have started the transition before being redirected. |
73 DCHECK(!in_cross_site_transition_); | 72 DCHECK(!in_cross_site_transition_); |
74 return next_handler_->OnRequestRedirected( | 73 return next_handler_->OnRequestRedirected( |
75 request_id, new_url, response, defer); | 74 request_id, new_url, response, defer); |
76 } | 75 } |
77 | 76 |
78 bool CrossSiteResourceHandler::OnResponseStarted( | 77 bool CrossSiteResourceHandler::OnResponseStarted( |
79 int request_id, | 78 int request_id, |
80 ResourceResponse* response, | 79 ResourceResponse* response, |
81 bool* defer) { | 80 bool* defer) { |
82 // At this point, we know that the response is safe to send back to the | 81 // At this point, we know that the response is safe to send back to the |
83 // renderer: it is not a download, and it has passed the SSL and safe | 82 // renderer: it is not a download, and it has passed the SSL and safe |
84 // browsing checks. | 83 // browsing checks. |
85 // We should not have already started the transition before now. | 84 // We should not have already started the transition before now. |
86 DCHECK(!in_cross_site_transition_); | 85 DCHECK(!in_cross_site_transition_); |
87 has_started_response_ = true; | 86 has_started_response_ = true; |
88 | 87 |
89 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); | 88 ResourceRequestInfoImpl* info = GetRequestInfo(); |
90 | 89 |
91 // We will need to swap processes if either (1) a redirect that requires a | 90 // We will need to swap processes if either (1) a redirect that requires a |
92 // transfer occurred before we got here, or (2) a pending cross-site request | 91 // transfer occurred before we got here, or (2) a pending cross-site request |
93 // was already in progress. Note that a swap may no longer be needed if we | 92 // was already in progress. Note that a swap may no longer be needed if we |
94 // transferred back into the original process due to a redirect. | 93 // transferred back into the original process due to a redirect. |
95 bool should_transfer = | 94 bool should_transfer = |
96 GetContentClient()->browser()->ShouldSwapProcessesForRedirect( | 95 GetContentClient()->browser()->ShouldSwapProcessesForRedirect( |
97 info->GetContext(), request_->original_url(), request_->url()); | 96 info->GetContext(), request()->original_url(), request()->url()); |
98 bool swap_needed = should_transfer || | 97 bool swap_needed = should_transfer || |
99 CrossSiteRequestManager::GetInstance()-> | 98 CrossSiteRequestManager::GetInstance()-> |
100 HasPendingCrossSiteRequest(info->GetChildID(), info->GetRouteID()); | 99 HasPendingCrossSiteRequest(info->GetChildID(), info->GetRouteID()); |
101 | 100 |
102 // If this is a download, just pass the response through without doing a | 101 // If this is a download, just pass the response through without doing a |
103 // cross-site check. The renderer will see it is a download and abort the | 102 // cross-site check. The renderer will see it is a download and abort the |
104 // request. | 103 // request. |
105 // | 104 // |
106 // Similarly, HTTP 204 (No Content) responses leave us showing the previous | 105 // Similarly, HTTP 204 (No Content) responses leave us showing the previous |
107 // page. We should allow the navigation to finish without running the unload | 106 // page. We should allow the navigation to finish without running the unload |
(...skipping 24 matching lines...) Expand all Loading... |
132 bool* defer) { | 131 bool* defer) { |
133 CHECK(!in_cross_site_transition_); | 132 CHECK(!in_cross_site_transition_); |
134 return next_handler_->OnReadCompleted(request_id, bytes_read, defer); | 133 return next_handler_->OnReadCompleted(request_id, bytes_read, defer); |
135 } | 134 } |
136 | 135 |
137 bool CrossSiteResourceHandler::OnResponseCompleted( | 136 bool CrossSiteResourceHandler::OnResponseCompleted( |
138 int request_id, | 137 int request_id, |
139 const net::URLRequestStatus& status, | 138 const net::URLRequestStatus& status, |
140 const std::string& security_info) { | 139 const std::string& security_info) { |
141 if (!in_cross_site_transition_) { | 140 if (!in_cross_site_transition_) { |
142 ResourceRequestInfoImpl* info = | 141 ResourceRequestInfoImpl* info = GetRequestInfo(); |
143 ResourceRequestInfoImpl::ForRequest(request_); | |
144 // If we've already completed the transition, or we're canceling the | 142 // If we've already completed the transition, or we're canceling the |
145 // request, or an error occurred with no cross-process navigation in | 143 // request, or an error occurred with no cross-process navigation in |
146 // progress, then we should just pass this through. | 144 // progress, then we should just pass this through. |
147 if (has_started_response_ || | 145 if (has_started_response_ || |
148 status.status() != net::URLRequestStatus::FAILED || | 146 status.status() != net::URLRequestStatus::FAILED || |
149 !CrossSiteRequestManager::GetInstance()->HasPendingCrossSiteRequest( | 147 !CrossSiteRequestManager::GetInstance()->HasPendingCrossSiteRequest( |
150 info->GetChildID(), info->GetRouteID())) { | 148 info->GetChildID(), info->GetRouteID())) { |
151 return next_handler_->OnResponseCompleted(request_id, status, | 149 return next_handler_->OnResponseCompleted(request_id, status, |
152 security_info); | 150 security_info); |
153 } | 151 } |
(...skipping 12 matching lines...) Expand all Loading... |
166 | 164 |
167 // Return false to tell RDH not to notify the world or clean up the | 165 // Return false to tell RDH not to notify the world or clean up the |
168 // pending request. We will do so in ResumeResponse. | 166 // pending request. We will do so in ResumeResponse. |
169 did_defer_ = true; | 167 did_defer_ = true; |
170 return false; | 168 return false; |
171 } | 169 } |
172 | 170 |
173 // We can now send the response to the new renderer, which will cause | 171 // We can now send the response to the new renderer, which will cause |
174 // WebContentsImpl to swap in the new renderer and destroy the old one. | 172 // WebContentsImpl to swap in the new renderer and destroy the old one. |
175 void CrossSiteResourceHandler::ResumeResponse() { | 173 void CrossSiteResourceHandler::ResumeResponse() { |
176 DCHECK(request_); | 174 DCHECK(request()); |
177 DCHECK(in_cross_site_transition_); | 175 DCHECK(in_cross_site_transition_); |
178 in_cross_site_transition_ = false; | 176 in_cross_site_transition_ = false; |
179 ResourceRequestInfoImpl* info = | 177 ResourceRequestInfoImpl* info = GetRequestInfo(); |
180 ResourceRequestInfoImpl::ForRequest(request_); | |
181 | 178 |
182 if (has_started_response_) { | 179 if (has_started_response_) { |
183 // Send OnResponseStarted to the new renderer. | 180 // Send OnResponseStarted to the new renderer. |
184 DCHECK(response_); | 181 DCHECK(response_); |
185 bool defer = false; | 182 bool defer = false; |
186 if (!next_handler_->OnResponseStarted(info->GetRequestID(), response_, | 183 if (!next_handler_->OnResponseStarted(info->GetRequestID(), response_, |
187 &defer)) { | 184 &defer)) { |
188 controller()->Cancel(); | 185 controller()->Cancel(); |
189 } else if (!defer) { | 186 } else if (!defer) { |
190 // Unpause the request to resume reading. Any further reads will be | 187 // Unpause the request to resume reading. Any further reads will be |
(...skipping 20 matching lines...) Expand all Loading... |
211 // telling the old RenderViewHost to run its onunload handler. | 208 // telling the old RenderViewHost to run its onunload handler. |
212 void CrossSiteResourceHandler::StartCrossSiteTransition( | 209 void CrossSiteResourceHandler::StartCrossSiteTransition( |
213 int request_id, | 210 int request_id, |
214 ResourceResponse* response, | 211 ResourceResponse* response, |
215 bool should_transfer) { | 212 bool should_transfer) { |
216 in_cross_site_transition_ = true; | 213 in_cross_site_transition_ = true; |
217 response_ = response; | 214 response_ = response; |
218 | 215 |
219 // Store this handler on the ExtraRequestInfo, so that RDH can call our | 216 // Store this handler on the ExtraRequestInfo, so that RDH can call our |
220 // ResumeResponse method when we are ready to resume. | 217 // ResumeResponse method when we are ready to resume. |
221 ResourceRequestInfoImpl* info = | 218 ResourceRequestInfoImpl* info = GetRequestInfo(); |
222 ResourceRequestInfoImpl::ForRequest(request_); | |
223 info->set_cross_site_handler(this); | 219 info->set_cross_site_handler(this); |
224 | 220 |
225 DCHECK_EQ(request_id, info->GetRequestID()); | 221 DCHECK_EQ(request_id, info->GetRequestID()); |
226 GlobalRequestID global_id(info->GetChildID(), info->GetRequestID()); | 222 GlobalRequestID global_id(info->GetChildID(), info->GetRequestID()); |
227 | 223 |
228 // Tell the contents responsible for this request that a cross-site response | 224 // Tell the contents responsible for this request that a cross-site response |
229 // is starting, so that it can tell its old renderer to run its onunload | 225 // is starting, so that it can tell its old renderer to run its onunload |
230 // handler now. We will wait until the unload is finished and (if a transfer | 226 // 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. | 227 // is needed) for the new renderer's request to arrive. |
232 GURL transfer_url; | 228 GURL transfer_url; |
233 Referrer referrer; | 229 Referrer referrer; |
234 int frame_id = -1; | 230 int frame_id = -1; |
235 if (should_transfer) { | 231 if (should_transfer) { |
236 transfer_url = request_->url(); | 232 transfer_url = request()->url(); |
237 referrer = Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()); | 233 referrer = Referrer(GURL(request()->referrer()), info->GetReferrerPolicy()); |
238 frame_id = info->GetFrameID(); | 234 frame_id = info->GetFrameID(); |
239 | 235 |
240 ResourceDispatcherHostImpl::Get()->MarkAsTransferredNavigation( | 236 ResourceDispatcherHostImpl::Get()->MarkAsTransferredNavigation( |
241 global_id, transfer_url); | 237 global_id, transfer_url); |
242 } | 238 } |
243 BrowserThread::PostTask( | 239 BrowserThread::PostTask( |
244 BrowserThread::UI, | 240 BrowserThread::UI, |
245 FROM_HERE, | 241 FROM_HERE, |
246 base::Bind( | 242 base::Bind( |
247 &OnCrossSiteResponseHelper, | 243 &OnCrossSiteResponseHelper, |
248 info->GetChildID(), | 244 info->GetChildID(), |
249 info->GetRouteID(), | 245 info->GetRouteID(), |
250 global_id, | 246 global_id, |
251 should_transfer, | 247 should_transfer, |
252 transfer_url, | 248 transfer_url, |
253 referrer, | 249 referrer, |
254 frame_id)); | 250 frame_id)); |
255 } | 251 } |
256 | 252 |
257 void CrossSiteResourceHandler::ResumeIfDeferred() { | 253 void CrossSiteResourceHandler::ResumeIfDeferred() { |
258 if (did_defer_) { | 254 if (did_defer_) { |
259 did_defer_ = false; | 255 did_defer_ = false; |
260 controller()->Resume(); | 256 controller()->Resume(); |
261 } | 257 } |
262 } | 258 } |
263 | 259 |
264 } // namespace content | 260 } // namespace content |
OLD | NEW |