| 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 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | 5 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading |
| 6 | 6 |
| 7 #include "content/child/resource_dispatcher.h" | 7 #include "content/child/resource_dispatcher.h" |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/child/sync_load_response.h" | 21 #include "content/child/sync_load_response.h" |
| 22 #include "content/common/inter_process_time_ticks_converter.h" | 22 #include "content/common/inter_process_time_ticks_converter.h" |
| 23 #include "content/common/resource_messages.h" | 23 #include "content/common/resource_messages.h" |
| 24 #include "content/public/child/request_peer.h" | 24 #include "content/public/child/request_peer.h" |
| 25 #include "content/public/child/resource_dispatcher_delegate.h" | 25 #include "content/public/child/resource_dispatcher_delegate.h" |
| 26 #include "content/public/common/resource_response.h" | 26 #include "content/public/common/resource_response.h" |
| 27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 28 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 29 #include "net/base/request_priority.h" | 29 #include "net/base/request_priority.h" |
| 30 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 31 #include "webkit/child/resource_loader_bridge.h" | |
| 32 #include "webkit/common/resource_type.h" | 31 #include "webkit/common/resource_type.h" |
| 33 | 32 |
| 34 using webkit_glue::ResourceLoaderBridge; | 33 using webkit_glue::ResourceLoaderBridge; |
| 35 using webkit_glue::ResourceResponseInfo; | 34 using webkit_glue::ResourceResponseInfo; |
| 36 | 35 |
| 37 namespace content { | 36 namespace content { |
| 38 | 37 |
| 39 namespace { | 38 namespace { |
| 40 | 39 |
| 41 // Converts |time| from a remote to local TimeTicks, overwriting the original | 40 // Converts |time| from a remote to local TimeTicks, overwriting the original |
| 42 // value. | 41 // value. |
| 43 void RemoteToLocalTimeTicks( | 42 void RemoteToLocalTimeTicks( |
| 44 const InterProcessTimeTicksConverter& converter, | 43 const InterProcessTimeTicksConverter& converter, |
| 45 base::TimeTicks* time) { | 44 base::TimeTicks* time) { |
| 46 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time); | 45 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time); |
| 47 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks(); | 46 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks(); |
| 48 } | 47 } |
| 49 | 48 |
| 50 | 49 void CrashOnMapFailure() { |
| 51 } // namespace | |
| 52 | |
| 53 static void CrashOnMapFailure() { | |
| 54 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
| 55 DWORD last_err = GetLastError(); | 51 DWORD last_err = GetLastError(); |
| 56 base::debug::Alias(&last_err); | 52 base::debug::Alias(&last_err); |
| 57 #endif | 53 #endif |
| 58 CHECK(false); | 54 CHECK(false); |
| 59 } | 55 } |
| 60 | 56 |
| 61 // Each resource request is assigned an ID scoped to this process. | 57 // Each resource request is assigned an ID scoped to this process. |
| 62 static int MakeRequestID() { | 58 int MakeRequestID() { |
| 63 // NOTE: The resource_dispatcher_host also needs probably unique | 59 // NOTE: The resource_dispatcher_host also needs probably unique |
| 64 // request_ids, so they count down from -2 (-1 is a special we're | 60 // request_ids, so they count down from -2 (-1 is a special we're |
| 65 // screwed value), while the renderer process counts up. | 61 // screwed value), while the renderer process counts up. |
| 66 static int next_request_id = 0; | 62 static int next_request_id = 0; |
| 67 return next_request_id++; | 63 return next_request_id++; |
| 68 } | 64 } |
| 69 | 65 |
| 70 // ResourceLoaderBridge implementation ---------------------------------------- | 66 } // namespace |
| 71 | |
| 72 class IPCResourceLoaderBridge : public ResourceLoaderBridge { | |
| 73 public: | |
| 74 IPCResourceLoaderBridge(ResourceDispatcher* dispatcher, | |
| 75 const RequestInfo& request_info); | |
| 76 virtual ~IPCResourceLoaderBridge(); | |
| 77 | |
| 78 // ResourceLoaderBridge | |
| 79 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE; | |
| 80 virtual bool Start(RequestPeer* peer) OVERRIDE; | |
| 81 virtual void Cancel() OVERRIDE; | |
| 82 virtual void SetDefersLoading(bool value) OVERRIDE; | |
| 83 virtual void DidChangePriority(net::RequestPriority new_priority, | |
| 84 int intra_priority_value) OVERRIDE; | |
| 85 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE; | |
| 86 | |
| 87 private: | |
| 88 RequestPeer* peer_; | |
| 89 | |
| 90 // The resource dispatcher for this loader. The bridge doesn't own it, but | |
| 91 // it's guaranteed to outlive the bridge. | |
| 92 ResourceDispatcher* dispatcher_; | |
| 93 | |
| 94 // The request to send, created on initialization for modification and | |
| 95 // appending data. | |
| 96 ResourceHostMsg_Request request_; | |
| 97 | |
| 98 // ID for the request, valid once Start()ed, -1 if not valid yet. | |
| 99 int request_id_; | |
| 100 | |
| 101 // The routing id used when sending IPC messages. | |
| 102 int routing_id_; | |
| 103 | |
| 104 // The security origin of the frame that initiates this request. | |
| 105 GURL frame_origin_; | |
| 106 | |
| 107 bool is_synchronous_request_; | |
| 108 }; | |
| 109 | |
| 110 IPCResourceLoaderBridge::IPCResourceLoaderBridge( | |
| 111 ResourceDispatcher* dispatcher, | |
| 112 const RequestInfo& request_info) | |
| 113 : peer_(NULL), | |
| 114 dispatcher_(dispatcher), | |
| 115 request_id_(-1), | |
| 116 routing_id_(request_info.routing_id), | |
| 117 is_synchronous_request_(false) { | |
| 118 DCHECK(dispatcher_) << "no resource dispatcher"; | |
| 119 request_.method = request_info.method; | |
| 120 request_.url = request_info.url; | |
| 121 request_.first_party_for_cookies = request_info.first_party_for_cookies; | |
| 122 request_.referrer = request_info.referrer; | |
| 123 request_.referrer_policy = request_info.referrer_policy; | |
| 124 request_.headers = request_info.headers; | |
| 125 request_.load_flags = request_info.load_flags; | |
| 126 request_.origin_pid = request_info.requestor_pid; | |
| 127 request_.resource_type = request_info.request_type; | |
| 128 request_.priority = request_info.priority; | |
| 129 request_.request_context = request_info.request_context; | |
| 130 request_.appcache_host_id = request_info.appcache_host_id; | |
| 131 request_.download_to_file = request_info.download_to_file; | |
| 132 request_.has_user_gesture = request_info.has_user_gesture; | |
| 133 | |
| 134 const RequestExtraData kEmptyData; | |
| 135 const RequestExtraData* extra_data; | |
| 136 if (request_info.extra_data) | |
| 137 extra_data = static_cast<RequestExtraData*>(request_info.extra_data); | |
| 138 else | |
| 139 extra_data = &kEmptyData; | |
| 140 request_.visiblity_state = extra_data->visibility_state(); | |
| 141 request_.render_frame_id = extra_data->render_frame_id(); | |
| 142 request_.is_main_frame = extra_data->is_main_frame(); | |
| 143 request_.parent_is_main_frame = extra_data->parent_is_main_frame(); | |
| 144 request_.parent_render_frame_id = extra_data->parent_render_frame_id(); | |
| 145 request_.allow_download = extra_data->allow_download(); | |
| 146 request_.transition_type = extra_data->transition_type(); | |
| 147 request_.should_replace_current_entry = | |
| 148 extra_data->should_replace_current_entry(); | |
| 149 request_.transferred_request_child_id = | |
| 150 extra_data->transferred_request_child_id(); | |
| 151 request_.transferred_request_request_id = | |
| 152 extra_data->transferred_request_request_id(); | |
| 153 request_.service_worker_provider_id = | |
| 154 extra_data->service_worker_provider_id(); | |
| 155 frame_origin_ = extra_data->frame_origin(); | |
| 156 } | |
| 157 | |
| 158 IPCResourceLoaderBridge::~IPCResourceLoaderBridge() { | |
| 159 // we remove our hook for the resource dispatcher only when going away, since | |
| 160 // it doesn't keep track of whether we've force terminated the request | |
| 161 if (request_id_ >= 0) { | |
| 162 // this operation may fail, as the dispatcher will have preemptively | |
| 163 // removed us when the renderer sends the ReceivedAllData message. | |
| 164 dispatcher_->RemovePendingRequest(request_id_); | |
| 165 | |
| 166 if (request_.download_to_file) { | |
| 167 dispatcher_->message_sender()->Send( | |
| 168 new ResourceHostMsg_ReleaseDownloadedFile(request_id_)); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void IPCResourceLoaderBridge::SetRequestBody( | |
| 174 ResourceRequestBody* request_body) { | |
| 175 DCHECK(request_id_ == -1) << "request already started"; | |
| 176 request_.request_body = request_body; | |
| 177 } | |
| 178 | |
| 179 // Writes a footer on the message and sends it | |
| 180 bool IPCResourceLoaderBridge::Start(RequestPeer* peer) { | |
| 181 if (request_id_ != -1) { | |
| 182 NOTREACHED() << "Starting a request twice"; | |
| 183 return false; | |
| 184 } | |
| 185 | |
| 186 peer_ = peer; | |
| 187 | |
| 188 // generate the request ID, and append it to the message | |
| 189 request_id_ = dispatcher_->AddPendingRequest(peer_, | |
| 190 request_.resource_type, | |
| 191 request_.origin_pid, | |
| 192 frame_origin_, | |
| 193 request_.url); | |
| 194 | |
| 195 return dispatcher_->message_sender()->Send( | |
| 196 new ResourceHostMsg_RequestResource(routing_id_, request_id_, request_)); | |
| 197 } | |
| 198 | |
| 199 void IPCResourceLoaderBridge::Cancel() { | |
| 200 if (request_id_ < 0) { | |
| 201 NOTREACHED() << "Trying to cancel an unstarted request"; | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 if (!is_synchronous_request_) | |
| 206 dispatcher_->CancelPendingRequest(request_id_); | |
| 207 | |
| 208 // We can't remove the request ID from the resource dispatcher because more | |
| 209 // data might be pending. Sending the cancel message may cause more data | |
| 210 // to be flushed, and will then cause a complete message to be sent. | |
| 211 } | |
| 212 | |
| 213 void IPCResourceLoaderBridge::SetDefersLoading(bool value) { | |
| 214 if (request_id_ < 0) { | |
| 215 NOTREACHED() << "Trying to (un)defer an unstarted request"; | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 dispatcher_->SetDefersLoading(request_id_, value); | |
| 220 } | |
| 221 | |
| 222 void IPCResourceLoaderBridge::DidChangePriority( | |
| 223 net::RequestPriority new_priority, int intra_priority_value) { | |
| 224 if (request_id_ < 0) { | |
| 225 NOTREACHED() << "Trying to change priority of an unstarted request"; | |
| 226 return; | |
| 227 } | |
| 228 | |
| 229 dispatcher_->DidChangePriority(routing_id_, request_id_, new_priority, | |
| 230 intra_priority_value); | |
| 231 } | |
| 232 | |
| 233 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) { | |
| 234 if (request_id_ != -1) { | |
| 235 NOTREACHED() << "Starting a request twice"; | |
| 236 response->error_code = net::ERR_FAILED; | |
| 237 return; | |
| 238 } | |
| 239 | |
| 240 request_id_ = MakeRequestID(); | |
| 241 is_synchronous_request_ = true; | |
| 242 | |
| 243 SyncLoadResult result; | |
| 244 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad(routing_id_, request_id_, | |
| 245 request_, &result); | |
| 246 // NOTE: This may pump events (see RenderThread::Send). | |
| 247 if (!dispatcher_->message_sender()->Send(msg)) { | |
| 248 response->error_code = net::ERR_FAILED; | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 response->error_code = result.error_code; | |
| 253 response->url = result.final_url; | |
| 254 response->headers = result.headers; | |
| 255 response->mime_type = result.mime_type; | |
| 256 response->charset = result.charset; | |
| 257 response->request_time = result.request_time; | |
| 258 response->response_time = result.response_time; | |
| 259 response->encoded_data_length = result.encoded_data_length; | |
| 260 response->load_timing = result.load_timing; | |
| 261 response->devtools_info = result.devtools_info; | |
| 262 response->data.swap(result.data); | |
| 263 response->download_file_path = result.download_file_path; | |
| 264 } | |
| 265 | |
| 266 // ResourceDispatcher --------------------------------------------------------- | |
| 267 | 67 |
| 268 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender) | 68 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender) |
| 269 : message_sender_(sender), | 69 : message_sender_(sender), |
| 270 weak_factory_(this), | 70 weak_factory_(this), |
| 271 delegate_(NULL), | 71 delegate_(NULL), |
| 272 io_timestamp_(base::TimeTicks()) { | 72 io_timestamp_(base::TimeTicks()) { |
| 273 } | 73 } |
| 274 | 74 |
| 275 ResourceDispatcher::~ResourceDispatcher() { | 75 ResourceDispatcher::~ResourceDispatcher() { |
| 276 } | 76 } |
| 277 | 77 |
| 278 // ResourceDispatcher implementation ------------------------------------------ | |
| 279 | |
| 280 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) { | 78 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 281 if (!IsResourceDispatcherMessage(message)) { | 79 if (!IsResourceDispatcherMessage(message)) { |
| 282 return false; | 80 return false; |
| 283 } | 81 } |
| 284 | 82 |
| 285 int request_id; | 83 int request_id; |
| 286 | 84 |
| 287 PickleIterator iter(message); | 85 PickleIterator iter(message); |
| 288 if (!message.ReadInt(&iter, &request_id)) { | 86 if (!message.ReadInt(&iter, &request_id)) { |
| 289 NOTREACHED() << "malformed resource message"; | 87 NOTREACHED() << "malformed resource message"; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 128 |
| 331 void ResourceDispatcher::OnUploadProgress(int request_id, int64 position, | 129 void ResourceDispatcher::OnUploadProgress(int request_id, int64 position, |
| 332 int64 size) { | 130 int64 size) { |
| 333 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 131 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 334 if (!request_info) | 132 if (!request_info) |
| 335 return; | 133 return; |
| 336 | 134 |
| 337 request_info->peer->OnUploadProgress(position, size); | 135 request_info->peer->OnUploadProgress(position, size); |
| 338 | 136 |
| 339 // Acknowledge receipt | 137 // Acknowledge receipt |
| 340 message_sender()->Send(new ResourceHostMsg_UploadProgress_ACK(request_id)); | 138 message_sender_->Send(new ResourceHostMsg_UploadProgress_ACK(request_id)); |
| 341 } | 139 } |
| 342 | 140 |
| 343 void ResourceDispatcher::OnReceivedResponse( | 141 void ResourceDispatcher::OnReceivedResponse( |
| 344 int request_id, const ResourceResponseHead& response_head) { | 142 int request_id, |
| 143 const ResourceResponseHead& response_head) { |
| 345 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse"); | 144 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse"); |
| 346 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 145 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 347 if (!request_info) | 146 if (!request_info) |
| 348 return; | 147 return; |
| 349 request_info->response_start = ConsumeIOTimestamp(); | 148 request_info->response_start = ConsumeIOTimestamp(); |
| 350 | 149 |
| 351 if (delegate_) { | 150 if (delegate_) { |
| 352 RequestPeer* new_peer = | 151 RequestPeer* new_peer = |
| 353 delegate_->OnReceivedResponse( | 152 delegate_->OnReceivedResponse( |
| 354 request_info->peer, response_head.mime_type, request_info->url); | 153 request_info->peer, response_head.mime_type, request_info->url); |
| 355 if (new_peer) | 154 if (new_peer) |
| 356 request_info->peer = new_peer; | 155 request_info->peer = new_peer; |
| 357 } | 156 } |
| 358 | 157 |
| 359 ResourceResponseInfo renderer_response_info; | 158 ResourceResponseInfo renderer_response_info; |
| 360 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info); | 159 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info); |
| 361 request_info->site_isolation_metadata = | 160 request_info->site_isolation_metadata = |
| 362 SiteIsolationPolicy::OnReceivedResponse(request_info->frame_origin, | 161 SiteIsolationPolicy::OnReceivedResponse(request_info->frame_origin, |
| 363 request_info->response_url, | 162 request_info->response_url, |
| 364 request_info->resource_type, | 163 request_info->resource_type, |
| 365 request_info->origin_pid, | 164 request_info->origin_pid, |
| 366 renderer_response_info); | 165 renderer_response_info); |
| 367 request_info->peer->OnReceivedResponse(renderer_response_info); | 166 request_info->peer->OnReceivedResponse(renderer_response_info); |
| 368 } | 167 } |
| 369 | 168 |
| 370 void ResourceDispatcher::OnReceivedCachedMetadata( | 169 void ResourceDispatcher::OnReceivedCachedMetadata( |
| 371 int request_id, const std::vector<char>& data) { | 170 int request_id, |
| 171 const std::vector<char>& data) { |
| 372 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 172 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 373 if (!request_info) | 173 if (!request_info) |
| 374 return; | 174 return; |
| 375 | 175 |
| 376 if (data.size()) | 176 if (data.size()) |
| 377 request_info->peer->OnReceivedCachedMetadata(&data.front(), data.size()); | 177 request_info->peer->OnReceivedCachedMetadata(&data.front(), data.size()); |
| 378 } | 178 } |
| 379 | 179 |
| 380 void ResourceDispatcher::OnSetDataBuffer(int request_id, | 180 void ResourceDispatcher::OnSetDataBuffer(int request_id, |
| 381 base::SharedMemoryHandle shm_handle, | 181 base::SharedMemoryHandle shm_handle, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 int data_length, | 213 int data_length, |
| 414 int encoded_data_length) { | 214 int encoded_data_length) { |
| 415 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); | 215 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); |
| 416 DCHECK_GT(data_length, 0); | 216 DCHECK_GT(data_length, 0); |
| 417 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 217 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 418 if (request_info && data_length > 0) { | 218 if (request_info && data_length > 0) { |
| 419 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); | 219 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); |
| 420 CHECK_GE(request_info->buffer_size, data_offset + data_length); | 220 CHECK_GE(request_info->buffer_size, data_offset + data_length); |
| 421 | 221 |
| 422 // Ensure that the SHM buffer remains valid for the duration of this scope. | 222 // Ensure that the SHM buffer remains valid for the duration of this scope. |
| 423 // It is possible for CancelPendingRequest() to be called before we exit | 223 // It is possible for Cancel() to be called before we exit this scope. |
| 424 // this scope. | |
| 425 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); | 224 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); |
| 426 | 225 |
| 427 base::TimeTicks time_start = base::TimeTicks::Now(); | 226 base::TimeTicks time_start = base::TimeTicks::Now(); |
| 428 | 227 |
| 429 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); | 228 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); |
| 430 CHECK(data_ptr); | 229 CHECK(data_ptr); |
| 431 CHECK(data_ptr + data_offset); | 230 CHECK(data_ptr + data_offset); |
| 432 | 231 |
| 433 // Check whether this response data is compliant with our cross-site | 232 // Check whether this response data is compliant with our cross-site |
| 434 // document blocking policy. We only do this for the first packet. | 233 // document blocking policy. We only do this for the first packet. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 452 request_info->peer->OnReceivedData(alternative_data.data(), | 251 request_info->peer->OnReceivedData(alternative_data.data(), |
| 453 alternative_data.size(), | 252 alternative_data.size(), |
| 454 alternative_data.size()); | 253 alternative_data.size()); |
| 455 } | 254 } |
| 456 | 255 |
| 457 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", | 256 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", |
| 458 base::TimeTicks::Now() - time_start); | 257 base::TimeTicks::Now() - time_start); |
| 459 } | 258 } |
| 460 | 259 |
| 461 // Acknowledge the reception of this data. | 260 // Acknowledge the reception of this data. |
| 462 message_sender()->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); | 261 message_sender_->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); |
| 463 } | 262 } |
| 464 | 263 |
| 465 void ResourceDispatcher::OnDownloadedData(int request_id, | 264 void ResourceDispatcher::OnDownloadedData(int request_id, |
| 466 int data_len, | 265 int data_len, |
| 467 int encoded_data_length) { | 266 int encoded_data_length) { |
| 468 // Acknowledge the reception of this message. | 267 // Acknowledge the reception of this message. |
| 469 message_sender()->Send( | 268 message_sender_->Send( |
| 470 new ResourceHostMsg_DataDownloaded_ACK(request_id)); | 269 new ResourceHostMsg_DataDownloaded_ACK(request_id)); |
| 471 | 270 |
| 472 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 271 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 473 if (!request_info) | 272 if (!request_info) |
| 474 return; | 273 return; |
| 475 | 274 |
| 476 request_info->peer->OnDownloadedData(data_len, encoded_data_length); | 275 request_info->peer->OnDownloadedData(data_len, encoded_data_length); |
| 477 } | 276 } |
| 478 | 277 |
| 479 void ResourceDispatcher::OnReceivedRedirect( | 278 void ResourceDispatcher::OnReceivedRedirect( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 502 // SiteIsolationPolicy later when OnReceivedResponse is called. | 301 // SiteIsolationPolicy later when OnReceivedResponse is called. |
| 503 request_info->response_url = new_url; | 302 request_info->response_url = new_url; |
| 504 request_info->pending_redirect_message.reset( | 303 request_info->pending_redirect_message.reset( |
| 505 new ResourceHostMsg_FollowRedirect(request_id, | 304 new ResourceHostMsg_FollowRedirect(request_id, |
| 506 has_new_first_party_for_cookies, | 305 has_new_first_party_for_cookies, |
| 507 new_first_party_for_cookies)); | 306 new_first_party_for_cookies)); |
| 508 if (!request_info->is_deferred) { | 307 if (!request_info->is_deferred) { |
| 509 FollowPendingRedirect(request_id, *request_info); | 308 FollowPendingRedirect(request_id, *request_info); |
| 510 } | 309 } |
| 511 } else { | 310 } else { |
| 512 CancelPendingRequest(request_id); | 311 Cancel(request_id); |
| 513 } | 312 } |
| 514 } | 313 } |
| 515 | 314 |
| 516 void ResourceDispatcher::FollowPendingRedirect( | 315 void ResourceDispatcher::FollowPendingRedirect( |
| 517 int request_id, | 316 int request_id, |
| 518 PendingRequestInfo& request_info) { | 317 PendingRequestInfo& request_info) { |
| 519 IPC::Message* msg = request_info.pending_redirect_message.release(); | 318 IPC::Message* msg = request_info.pending_redirect_message.release(); |
| 520 if (msg) | 319 if (msg) |
| 521 message_sender()->Send(msg); | 320 message_sender_->Send(msg); |
| 522 } | 321 } |
| 523 | 322 |
| 524 void ResourceDispatcher::OnRequestComplete( | 323 void ResourceDispatcher::OnRequestComplete( |
| 525 int request_id, | 324 int request_id, |
| 526 const ResourceMsg_RequestCompleteData& request_complete_data) { | 325 const ResourceMsg_RequestCompleteData& request_complete_data) { |
| 527 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete"); | 326 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete"); |
| 528 | 327 |
| 529 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 328 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 530 if (!request_info) | 329 if (!request_info) |
| 531 return; | 330 return; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 550 // Normally, dispatching this message causes the reference-counted request to | 349 // Normally, dispatching this message causes the reference-counted request to |
| 551 // die immediately. | 350 // die immediately. |
| 552 peer->OnCompletedRequest(request_complete_data.error_code, | 351 peer->OnCompletedRequest(request_complete_data.error_code, |
| 553 request_complete_data.was_ignored_by_handler, | 352 request_complete_data.was_ignored_by_handler, |
| 554 request_complete_data.exists_in_cache, | 353 request_complete_data.exists_in_cache, |
| 555 request_complete_data.security_info, | 354 request_complete_data.security_info, |
| 556 renderer_completion_time, | 355 renderer_completion_time, |
| 557 request_complete_data.encoded_data_length); | 356 request_complete_data.encoded_data_length); |
| 558 } | 357 } |
| 559 | 358 |
| 560 int ResourceDispatcher::AddPendingRequest(RequestPeer* callback, | |
| 561 ResourceType::Type resource_type, | |
| 562 int origin_pid, | |
| 563 const GURL& frame_origin, | |
| 564 const GURL& request_url) { | |
| 565 // Compute a unique request_id for this renderer process. | |
| 566 int id = MakeRequestID(); | |
| 567 pending_requests_[id] = PendingRequestInfo( | |
| 568 callback, resource_type, origin_pid, frame_origin, request_url); | |
| 569 return id; | |
| 570 } | |
| 571 | |
| 572 bool ResourceDispatcher::RemovePendingRequest(int request_id) { | 359 bool ResourceDispatcher::RemovePendingRequest(int request_id) { |
| 573 PendingRequestList::iterator it = pending_requests_.find(request_id); | 360 PendingRequestList::iterator it = pending_requests_.find(request_id); |
| 574 if (it == pending_requests_.end()) | 361 if (it == pending_requests_.end()) |
| 575 return false; | 362 return false; |
| 576 | 363 |
| 577 PendingRequestInfo& request_info = it->second; | 364 PendingRequestInfo& request_info = it->second; |
| 578 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue); | 365 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue); |
| 366 |
| 367 if (request_info.download_to_file) { |
| 368 message_sender_->Send( |
| 369 new ResourceHostMsg_ReleaseDownloadedFile(request_id)); |
| 370 } |
| 371 |
| 579 pending_requests_.erase(it); | 372 pending_requests_.erase(it); |
| 580 | 373 |
| 581 return true; | 374 return true; |
| 582 } | 375 } |
| 583 | 376 |
| 584 void ResourceDispatcher::CancelPendingRequest(int request_id) { | 377 void ResourceDispatcher::Cancel(int request_id) { |
| 585 PendingRequestList::iterator it = pending_requests_.find(request_id); | 378 PendingRequestList::iterator it = pending_requests_.find(request_id); |
| 586 if (it == pending_requests_.end()) { | 379 if (it == pending_requests_.end()) { |
| 587 DVLOG(1) << "unknown request"; | 380 DVLOG(1) << "unknown request"; |
| 588 return; | 381 return; |
| 589 } | 382 } |
| 590 | 383 |
| 591 // |request_id| will be removed from |pending_requests_| when | 384 // |request_id| will be removed from |pending_requests_| when |
| 592 // OnRequestComplete returns with ERR_ABORTED. | 385 // OnRequestComplete returns with ERR_ABORTED. |
| 593 message_sender()->Send(new ResourceHostMsg_CancelRequest(request_id)); | 386 message_sender_->Send(new ResourceHostMsg_CancelRequest(request_id)); |
| 594 } | 387 } |
| 595 | 388 |
| 596 void ResourceDispatcher::SetDefersLoading(int request_id, bool value) { | 389 void ResourceDispatcher::SetDefersLoading(int request_id, bool value) { |
| 597 PendingRequestList::iterator it = pending_requests_.find(request_id); | 390 PendingRequestList::iterator it = pending_requests_.find(request_id); |
| 598 if (it == pending_requests_.end()) { | 391 if (it == pending_requests_.end()) { |
| 599 DLOG(ERROR) << "unknown request"; | 392 DLOG(ERROR) << "unknown request"; |
| 600 return; | 393 return; |
| 601 } | 394 } |
| 602 PendingRequestInfo& request_info = it->second; | 395 PendingRequestInfo& request_info = it->second; |
| 603 if (value) { | 396 if (value) { |
| 604 request_info.is_deferred = value; | 397 request_info.is_deferred = value; |
| 605 } else if (request_info.is_deferred) { | 398 } else if (request_info.is_deferred) { |
| 606 request_info.is_deferred = false; | 399 request_info.is_deferred = false; |
| 607 | 400 |
| 608 FollowPendingRedirect(request_id, request_info); | 401 FollowPendingRedirect(request_id, request_info); |
| 609 | 402 |
| 610 base::MessageLoop::current()->PostTask( | 403 base::MessageLoop::current()->PostTask( |
| 611 FROM_HERE, | 404 FROM_HERE, |
| 612 base::Bind(&ResourceDispatcher::FlushDeferredMessages, | 405 base::Bind(&ResourceDispatcher::FlushDeferredMessages, |
| 613 weak_factory_.GetWeakPtr(), | 406 weak_factory_.GetWeakPtr(), |
| 614 request_id)); | 407 request_id)); |
| 615 } | 408 } |
| 616 } | 409 } |
| 617 | 410 |
| 618 void ResourceDispatcher::DidChangePriority( | 411 void ResourceDispatcher::DidChangePriority(int request_id, |
| 619 int routing_id, int request_id, net::RequestPriority new_priority, | 412 net::RequestPriority new_priority, |
| 620 int intra_priority_value) { | 413 int intra_priority_value) { |
| 621 DCHECK(ContainsKey(pending_requests_, request_id)); | 414 DCHECK(ContainsKey(pending_requests_, request_id)); |
| 622 message_sender()->Send(new ResourceHostMsg_DidChangePriority( | 415 message_sender_->Send(new ResourceHostMsg_DidChangePriority( |
| 623 request_id, new_priority, intra_priority_value)); | 416 request_id, new_priority, intra_priority_value)); |
| 624 } | 417 } |
| 625 | 418 |
| 626 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() | 419 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() |
| 627 : peer(NULL), | 420 : peer(NULL), |
| 628 resource_type(ResourceType::SUB_RESOURCE), | 421 resource_type(ResourceType::SUB_RESOURCE), |
| 629 is_deferred(false), | 422 is_deferred(false), |
| 423 download_to_file(false), |
| 630 blocked_response(false), | 424 blocked_response(false), |
| 631 buffer_size(0) { | 425 buffer_size(0) { |
| 632 } | 426 } |
| 633 | 427 |
| 634 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( | 428 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( |
| 635 RequestPeer* peer, | 429 RequestPeer* peer, |
| 636 ResourceType::Type resource_type, | 430 ResourceType::Type resource_type, |
| 637 int origin_pid, | 431 int origin_pid, |
| 638 const GURL& frame_origin, | 432 const GURL& frame_origin, |
| 639 const GURL& request_url) | 433 const GURL& request_url, |
| 434 bool download_to_file) |
| 640 : peer(peer), | 435 : peer(peer), |
| 641 resource_type(resource_type), | 436 resource_type(resource_type), |
| 642 origin_pid(origin_pid), | 437 origin_pid(origin_pid), |
| 643 is_deferred(false), | 438 is_deferred(false), |
| 644 url(request_url), | 439 url(request_url), |
| 645 frame_origin(frame_origin), | 440 frame_origin(frame_origin), |
| 646 response_url(request_url), | 441 response_url(request_url), |
| 442 download_to_file(download_to_file), |
| 647 request_start(base::TimeTicks::Now()), | 443 request_start(base::TimeTicks::Now()), |
| 648 blocked_response(false) {} | 444 blocked_response(false) {} |
| 649 | 445 |
| 650 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} | 446 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} |
| 651 | 447 |
| 652 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { | 448 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { |
| 653 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) | 449 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) |
| 654 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) | 450 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) |
| 655 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) | 451 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) |
| 656 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, | 452 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 687 if (index != pending_requests_.end()) { | 483 if (index != pending_requests_.end()) { |
| 688 PendingRequestInfo& pending_request = index->second; | 484 PendingRequestInfo& pending_request = index->second; |
| 689 if (pending_request.is_deferred) { | 485 if (pending_request.is_deferred) { |
| 690 pending_request.deferred_message_queue.swap(q); | 486 pending_request.deferred_message_queue.swap(q); |
| 691 return; | 487 return; |
| 692 } | 488 } |
| 693 } | 489 } |
| 694 } | 490 } |
| 695 } | 491 } |
| 696 | 492 |
| 697 ResourceLoaderBridge* ResourceDispatcher::CreateBridge( | 493 void ResourceDispatcher::StartSync(const RequestInfo& request_info, |
| 698 const RequestInfo& request_info) { | 494 ResourceRequestBody* request_body, |
| 699 return new IPCResourceLoaderBridge(this, request_info); | 495 SyncLoadResponse* response) { |
| 496 scoped_ptr<ResourceHostMsg_Request> request = |
| 497 CreateRequest(request_info, request_body, NULL); |
| 498 |
| 499 SyncLoadResult result; |
| 500 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad( |
| 501 request_info.routing_id, MakeRequestID(), *request, &result); |
| 502 |
| 503 // NOTE: This may pump events (see RenderThread::Send). |
| 504 if (!message_sender_->Send(msg)) { |
| 505 response->error_code = net::ERR_FAILED; |
| 506 return; |
| 507 } |
| 508 |
| 509 response->error_code = result.error_code; |
| 510 response->url = result.final_url; |
| 511 response->headers = result.headers; |
| 512 response->mime_type = result.mime_type; |
| 513 response->charset = result.charset; |
| 514 response->request_time = result.request_time; |
| 515 response->response_time = result.response_time; |
| 516 response->encoded_data_length = result.encoded_data_length; |
| 517 response->load_timing = result.load_timing; |
| 518 response->devtools_info = result.devtools_info; |
| 519 response->data.swap(result.data); |
| 520 response->download_file_path = result.download_file_path; |
| 521 } |
| 522 |
| 523 int ResourceDispatcher::StartAsync(const RequestInfo& request_info, |
| 524 ResourceRequestBody* request_body, |
| 525 RequestPeer* peer) { |
| 526 GURL frame_origin; |
| 527 scoped_ptr<ResourceHostMsg_Request> request = |
| 528 CreateRequest(request_info, request_body, &frame_origin); |
| 529 |
| 530 // Compute a unique request_id for this renderer process. |
| 531 int request_id = MakeRequestID(); |
| 532 pending_requests_[request_id] = |
| 533 PendingRequestInfo(peer, |
| 534 request->resource_type, |
| 535 request->origin_pid, |
| 536 frame_origin, |
| 537 request->url, |
| 538 request_info.download_to_file); |
| 539 |
| 540 message_sender_->Send(new ResourceHostMsg_RequestResource( |
| 541 request_info.routing_id, request_id, *request)); |
| 542 |
| 543 return request_id; |
| 700 } | 544 } |
| 701 | 545 |
| 702 void ResourceDispatcher::ToResourceResponseInfo( | 546 void ResourceDispatcher::ToResourceResponseInfo( |
| 703 const PendingRequestInfo& request_info, | 547 const PendingRequestInfo& request_info, |
| 704 const ResourceResponseHead& browser_info, | 548 const ResourceResponseHead& browser_info, |
| 705 ResourceResponseInfo* renderer_info) const { | 549 ResourceResponseInfo* renderer_info) const { |
| 706 *renderer_info = browser_info; | 550 *renderer_info = browser_info; |
| 707 if (request_info.request_start.is_null() || | 551 if (request_info.request_start.is_null() || |
| 708 request_info.response_start.is_null() || | 552 request_info.response_start.is_null() || |
| 709 browser_info.request_start.is_null() || | 553 browser_info.request_start.is_null() || |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 // static | 648 // static |
| 805 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { | 649 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { |
| 806 while (!queue->empty()) { | 650 while (!queue->empty()) { |
| 807 IPC::Message* message = queue->front(); | 651 IPC::Message* message = queue->front(); |
| 808 ReleaseResourcesInDataMessage(*message); | 652 ReleaseResourcesInDataMessage(*message); |
| 809 queue->pop_front(); | 653 queue->pop_front(); |
| 810 delete message; | 654 delete message; |
| 811 } | 655 } |
| 812 } | 656 } |
| 813 | 657 |
| 658 scoped_ptr<ResourceHostMsg_Request> ResourceDispatcher::CreateRequest( |
| 659 const RequestInfo& request_info, |
| 660 ResourceRequestBody* request_body, |
| 661 GURL* frame_origin) { |
| 662 scoped_ptr<ResourceHostMsg_Request> request(new ResourceHostMsg_Request); |
| 663 request->method = request_info.method; |
| 664 request->url = request_info.url; |
| 665 request->first_party_for_cookies = request_info.first_party_for_cookies; |
| 666 request->referrer = request_info.referrer; |
| 667 request->referrer_policy = request_info.referrer_policy; |
| 668 request->headers = request_info.headers; |
| 669 request->load_flags = request_info.load_flags; |
| 670 request->origin_pid = request_info.requestor_pid; |
| 671 request->resource_type = request_info.request_type; |
| 672 request->priority = request_info.priority; |
| 673 request->request_context = request_info.request_context; |
| 674 request->appcache_host_id = request_info.appcache_host_id; |
| 675 request->download_to_file = request_info.download_to_file; |
| 676 request->has_user_gesture = request_info.has_user_gesture; |
| 677 |
| 678 const RequestExtraData kEmptyData; |
| 679 const RequestExtraData* extra_data; |
| 680 if (request_info.extra_data) |
| 681 extra_data = static_cast<RequestExtraData*>(request_info.extra_data); |
| 682 else |
| 683 extra_data = &kEmptyData; |
| 684 request->visiblity_state = extra_data->visibility_state(); |
| 685 request->render_frame_id = extra_data->render_frame_id(); |
| 686 request->is_main_frame = extra_data->is_main_frame(); |
| 687 request->parent_is_main_frame = extra_data->parent_is_main_frame(); |
| 688 request->parent_render_frame_id = extra_data->parent_render_frame_id(); |
| 689 request->allow_download = extra_data->allow_download(); |
| 690 request->transition_type = extra_data->transition_type(); |
| 691 request->should_replace_current_entry = |
| 692 extra_data->should_replace_current_entry(); |
| 693 request->transferred_request_child_id = |
| 694 extra_data->transferred_request_child_id(); |
| 695 request->transferred_request_request_id = |
| 696 extra_data->transferred_request_request_id(); |
| 697 request->service_worker_provider_id = |
| 698 extra_data->service_worker_provider_id(); |
| 699 request->request_body = request_body; |
| 700 if (frame_origin) |
| 701 *frame_origin = extra_data->frame_origin(); |
| 702 return request.Pass(); |
| 703 } |
| 704 |
| 814 } // namespace content | 705 } // namespace content |
| OLD | NEW |