| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 <string> | |
| 6 | |
| 7 #include "chrome/browser/renderer_host/cross_site_resource_handler.h" | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "chrome/browser/renderer_host/global_request_id.h" | |
| 11 #include "chrome/browser/renderer_host/render_view_host.h" | |
| 12 #include "chrome/browser/renderer_host/render_view_host_delegate.h" | |
| 13 #include "chrome/browser/renderer_host/render_view_host_notification_task.h" | |
| 14 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | |
| 15 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" | |
| 16 #include "net/base/io_buffer.h" | |
| 17 | |
| 18 CrossSiteResourceHandler::CrossSiteResourceHandler( | |
| 19 ResourceHandler* handler, | |
| 20 int render_process_host_id, | |
| 21 int render_view_id, | |
| 22 ResourceDispatcherHost* resource_dispatcher_host) | |
| 23 : next_handler_(handler), | |
| 24 render_process_host_id_(render_process_host_id), | |
| 25 render_view_id_(render_view_id), | |
| 26 has_started_response_(false), | |
| 27 in_cross_site_transition_(false), | |
| 28 request_id_(-1), | |
| 29 completed_during_transition_(false), | |
| 30 completed_status_(), | |
| 31 response_(NULL), | |
| 32 rdh_(resource_dispatcher_host) {} | |
| 33 | |
| 34 bool CrossSiteResourceHandler::OnUploadProgress(int request_id, | |
| 35 uint64 position, | |
| 36 uint64 size) { | |
| 37 return next_handler_->OnUploadProgress(request_id, position, size); | |
| 38 } | |
| 39 | |
| 40 bool CrossSiteResourceHandler::OnRequestRedirected(int request_id, | |
| 41 const GURL& new_url, | |
| 42 ResourceResponse* response, | |
| 43 bool* defer) { | |
| 44 // We should not have started the transition before being redirected. | |
| 45 DCHECK(!in_cross_site_transition_); | |
| 46 return next_handler_->OnRequestRedirected( | |
| 47 request_id, new_url, response, defer); | |
| 48 } | |
| 49 | |
| 50 bool CrossSiteResourceHandler::OnResponseStarted(int request_id, | |
| 51 ResourceResponse* response) { | |
| 52 // At this point, we know that the response is safe to send back to the | |
| 53 // renderer: it is not a download, and it has passed the SSL and safe | |
| 54 // browsing checks. | |
| 55 // We should not have already started the transition before now. | |
| 56 DCHECK(!in_cross_site_transition_); | |
| 57 has_started_response_ = true; | |
| 58 | |
| 59 // Look up the request and associated info. | |
| 60 GlobalRequestID global_id(render_process_host_id_, request_id); | |
| 61 net::URLRequest* request = rdh_->GetURLRequest(global_id); | |
| 62 if (!request) { | |
| 63 DLOG(WARNING) << "Request wasn't found"; | |
| 64 return false; | |
| 65 } | |
| 66 ResourceDispatcherHostRequestInfo* info = | |
| 67 ResourceDispatcherHost::InfoForRequest(request); | |
| 68 | |
| 69 // If this is a download, just pass the response through without doing a | |
| 70 // cross-site check. The renderer will see it is a download and abort the | |
| 71 // request. | |
| 72 if (info->is_download()) { | |
| 73 return next_handler_->OnResponseStarted(request_id, response); | |
| 74 } | |
| 75 | |
| 76 // Tell the renderer to run the onunload event handler, and wait for the | |
| 77 // reply. | |
| 78 StartCrossSiteTransition(request_id, response, global_id); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 bool CrossSiteResourceHandler::OnWillStart(int request_id, | |
| 83 const GURL& url, | |
| 84 bool* defer) { | |
| 85 return next_handler_->OnWillStart(request_id, url, defer); | |
| 86 } | |
| 87 | |
| 88 bool CrossSiteResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | |
| 89 int* buf_size, int min_size) { | |
| 90 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); | |
| 91 } | |
| 92 | |
| 93 bool CrossSiteResourceHandler::OnReadCompleted(int request_id, | |
| 94 int* bytes_read) { | |
| 95 if (!in_cross_site_transition_) { | |
| 96 return next_handler_->OnReadCompleted(request_id, bytes_read); | |
| 97 } | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 bool CrossSiteResourceHandler::OnResponseCompleted( | |
| 102 int request_id, | |
| 103 const net::URLRequestStatus& status, | |
| 104 const std::string& security_info) { | |
| 105 if (!in_cross_site_transition_) { | |
| 106 if (has_started_response_) { | |
| 107 // We've already completed the transition, so just pass it through. | |
| 108 return next_handler_->OnResponseCompleted(request_id, status, | |
| 109 security_info); | |
| 110 } else { | |
| 111 // An error occured, we should wait now for the cross-site transition, | |
| 112 // so that the error message (e.g., 404) can be displayed to the user. | |
| 113 // Also continue with the logic below to remember that we completed | |
| 114 // during the cross-site transition. | |
| 115 GlobalRequestID global_id(render_process_host_id_, request_id); | |
| 116 StartCrossSiteTransition(request_id, NULL, global_id); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 // We have to buffer the call until after the transition completes. | |
| 121 completed_during_transition_ = true; | |
| 122 completed_status_ = status; | |
| 123 completed_security_info_ = security_info; | |
| 124 | |
| 125 // Return false to tell RDH not to notify the world or clean up the | |
| 126 // pending request. We will do so in ResumeResponse. | |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 void CrossSiteResourceHandler::OnRequestClosed() { | |
| 131 next_handler_->OnRequestClosed(); | |
| 132 } | |
| 133 | |
| 134 // We can now send the response to the new renderer, which will cause | |
| 135 // TabContents to swap in the new renderer and destroy the old one. | |
| 136 void CrossSiteResourceHandler::ResumeResponse() { | |
| 137 DCHECK(request_id_ != -1); | |
| 138 DCHECK(in_cross_site_transition_); | |
| 139 in_cross_site_transition_ = false; | |
| 140 | |
| 141 // Find the request for this response. | |
| 142 GlobalRequestID global_id(render_process_host_id_, request_id_); | |
| 143 net::URLRequest* request = rdh_->GetURLRequest(global_id); | |
| 144 if (!request) { | |
| 145 DLOG(WARNING) << "Resuming a request that wasn't found"; | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 if (has_started_response_) { | |
| 150 // Send OnResponseStarted to the new renderer. | |
| 151 DCHECK(response_); | |
| 152 next_handler_->OnResponseStarted(request_id_, response_); | |
| 153 | |
| 154 // Unpause the request to resume reading. Any further reads will be | |
| 155 // directed toward the new renderer. | |
| 156 rdh_->PauseRequest(render_process_host_id_, request_id_, false); | |
| 157 } | |
| 158 | |
| 159 // Remove ourselves from the ExtraRequestInfo. | |
| 160 ResourceDispatcherHostRequestInfo* info = | |
| 161 ResourceDispatcherHost::InfoForRequest(request); | |
| 162 info->set_cross_site_handler(NULL); | |
| 163 | |
| 164 // If the response completed during the transition, notify the next | |
| 165 // event handler. | |
| 166 if (completed_during_transition_) { | |
| 167 next_handler_->OnResponseCompleted(request_id_, completed_status_, | |
| 168 completed_security_info_); | |
| 169 | |
| 170 // Since we didn't notify the world or clean up the pending request in | |
| 171 // RDH::OnResponseCompleted during the transition, we should do it now. | |
| 172 rdh_->NotifyResponseCompleted(request, render_process_host_id_); | |
| 173 rdh_->RemovePendingRequest(render_process_host_id_, request_id_); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 CrossSiteResourceHandler::~CrossSiteResourceHandler() {} | |
| 178 | |
| 179 // Prepare to render the cross-site response in a new RenderViewHost, by | |
| 180 // telling the old RenderViewHost to run its onunload handler. | |
| 181 void CrossSiteResourceHandler::StartCrossSiteTransition( | |
| 182 int request_id, | |
| 183 ResourceResponse* response, | |
| 184 const GlobalRequestID& global_id) { | |
| 185 in_cross_site_transition_ = true; | |
| 186 request_id_ = request_id; | |
| 187 response_ = response; | |
| 188 | |
| 189 // Store this handler on the ExtraRequestInfo, so that RDH can call our | |
| 190 // ResumeResponse method when the close ACK is received. | |
| 191 net::URLRequest* request = rdh_->GetURLRequest(global_id); | |
| 192 if (!request) { | |
| 193 DLOG(WARNING) << "Cross site response for a request that wasn't found"; | |
| 194 return; | |
| 195 } | |
| 196 ResourceDispatcherHostRequestInfo* info = | |
| 197 ResourceDispatcherHost::InfoForRequest(request); | |
| 198 info->set_cross_site_handler(this); | |
| 199 | |
| 200 if (has_started_response_) { | |
| 201 // Pause the request until the old renderer is finished and the new | |
| 202 // renderer is ready. | |
| 203 rdh_->PauseRequest(render_process_host_id_, request_id, true); | |
| 204 } | |
| 205 // If our OnResponseStarted wasn't called, then we're being called by | |
| 206 // OnResponseCompleted after a failure. We don't need to pause, because | |
| 207 // there will be no reads. | |
| 208 | |
| 209 // Tell the tab responsible for this request that a cross-site response is | |
| 210 // starting, so that it can tell its old renderer to run its onunload | |
| 211 // handler now. We will wait to hear the corresponding ClosePage_ACK. | |
| 212 CallRenderViewHostRendererManagementDelegate( | |
| 213 render_process_host_id_, render_view_id_, | |
| 214 &RenderViewHostDelegate::RendererManagement::OnCrossSiteResponse, | |
| 215 render_process_host_id_, request_id); | |
| 216 | |
| 217 // TODO(creis): If the above call should fail, then we need to notify the IO | |
| 218 // thread to proceed anyway, using ResourceDispatcherHost::OnClosePageACK. | |
| 219 } | |
| OLD | NEW |