| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | |
| 6 | |
| 7 #include "content/common/resource_dispatcher.h" | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/debug/alias.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/metrics/histogram.h" | |
| 16 #include "base/shared_memory.h" | |
| 17 #include "base/string_util.h" | |
| 18 #include "content/common/inter_process_time_ticks_converter.h" | |
| 19 #include "content/common/request_extra_data.h" | |
| 20 #include "content/common/resource_messages.h" | |
| 21 #include "content/public/common/resource_dispatcher_delegate.h" | |
| 22 #include "content/public/common/resource_response.h" | |
| 23 #include "net/base/net_errors.h" | |
| 24 #include "net/base/net_util.h" | |
| 25 #include "net/base/request_priority.h" | |
| 26 #include "net/http/http_response_headers.h" | |
| 27 #include "webkit/glue/resource_request_body.h" | |
| 28 #include "webkit/glue/resource_type.h" | |
| 29 | |
| 30 using webkit_glue::ResourceLoaderBridge; | |
| 31 using webkit_glue::ResourceRequestBody; | |
| 32 using webkit_glue::ResourceResponseInfo; | |
| 33 | |
| 34 namespace content { | |
| 35 | |
| 36 namespace { | |
| 37 | |
| 38 // Converts |time| from a remote to local TimeTicks, overwriting the original | |
| 39 // value. | |
| 40 void RemoteToLocalTimeTicks( | |
| 41 const InterProcessTimeTicksConverter& converter, | |
| 42 base::TimeTicks* time) { | |
| 43 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time); | |
| 44 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks(); | |
| 45 } | |
| 46 | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 static void CrashOnMapFailure() { | |
| 51 #if defined(OS_WIN) | |
| 52 DWORD last_err = GetLastError(); | |
| 53 base::debug::Alias(&last_err); | |
| 54 #endif | |
| 55 CHECK(false); | |
| 56 } | |
| 57 | |
| 58 // Each resource request is assigned an ID scoped to this process. | |
| 59 static int MakeRequestID() { | |
| 60 // NOTE: The resource_dispatcher_host also needs probably unique | |
| 61 // request_ids, so they count down from -2 (-1 is a special we're | |
| 62 // screwed value), while the renderer process counts up. | |
| 63 static int next_request_id = 0; | |
| 64 return next_request_id++; | |
| 65 } | |
| 66 | |
| 67 // ResourceLoaderBridge implementation ---------------------------------------- | |
| 68 | |
| 69 class IPCResourceLoaderBridge : public ResourceLoaderBridge { | |
| 70 public: | |
| 71 IPCResourceLoaderBridge(ResourceDispatcher* dispatcher, | |
| 72 const ResourceLoaderBridge::RequestInfo& request_info); | |
| 73 virtual ~IPCResourceLoaderBridge(); | |
| 74 | |
| 75 // ResourceLoaderBridge | |
| 76 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE; | |
| 77 virtual bool Start(Peer* peer) OVERRIDE; | |
| 78 virtual void Cancel() OVERRIDE; | |
| 79 virtual void SetDefersLoading(bool value) OVERRIDE; | |
| 80 virtual void DidChangePriority(net::RequestPriority new_priority) OVERRIDE; | |
| 81 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE; | |
| 82 | |
| 83 private: | |
| 84 ResourceLoaderBridge::Peer* peer_; | |
| 85 | |
| 86 // The resource dispatcher for this loader. The bridge doesn't own it, but | |
| 87 // it's guaranteed to outlive the bridge. | |
| 88 ResourceDispatcher* dispatcher_; | |
| 89 | |
| 90 // The request to send, created on initialization for modification and | |
| 91 // appending data. | |
| 92 ResourceHostMsg_Request request_; | |
| 93 | |
| 94 // ID for the request, valid once Start()ed, -1 if not valid yet. | |
| 95 int request_id_; | |
| 96 | |
| 97 // The routing id used when sending IPC messages. | |
| 98 int routing_id_; | |
| 99 | |
| 100 bool is_synchronous_request_; | |
| 101 }; | |
| 102 | |
| 103 IPCResourceLoaderBridge::IPCResourceLoaderBridge( | |
| 104 ResourceDispatcher* dispatcher, | |
| 105 const ResourceLoaderBridge::RequestInfo& request_info) | |
| 106 : peer_(NULL), | |
| 107 dispatcher_(dispatcher), | |
| 108 request_id_(-1), | |
| 109 routing_id_(request_info.routing_id), | |
| 110 is_synchronous_request_(false) { | |
| 111 DCHECK(dispatcher_) << "no resource dispatcher"; | |
| 112 request_.method = request_info.method; | |
| 113 request_.url = request_info.url; | |
| 114 request_.first_party_for_cookies = request_info.first_party_for_cookies; | |
| 115 request_.referrer = request_info.referrer; | |
| 116 request_.referrer_policy = request_info.referrer_policy; | |
| 117 request_.headers = request_info.headers; | |
| 118 request_.load_flags = request_info.load_flags; | |
| 119 request_.origin_pid = request_info.requestor_pid; | |
| 120 request_.resource_type = request_info.request_type; | |
| 121 request_.priority = request_info.priority; | |
| 122 request_.request_context = request_info.request_context; | |
| 123 request_.appcache_host_id = request_info.appcache_host_id; | |
| 124 request_.download_to_file = request_info.download_to_file; | |
| 125 request_.has_user_gesture = request_info.has_user_gesture; | |
| 126 if (request_info.extra_data) { | |
| 127 RequestExtraData* extra_data = | |
| 128 static_cast<RequestExtraData*>(request_info.extra_data); | |
| 129 request_.is_main_frame = extra_data->is_main_frame(); | |
| 130 request_.frame_id = extra_data->frame_id(); | |
| 131 request_.parent_is_main_frame = extra_data->parent_is_main_frame(); | |
| 132 request_.parent_frame_id = extra_data->parent_frame_id(); | |
| 133 request_.allow_download = extra_data->allow_download(); | |
| 134 request_.transition_type = extra_data->transition_type(); | |
| 135 request_.transferred_request_child_id = | |
| 136 extra_data->transferred_request_child_id(); | |
| 137 request_.transferred_request_request_id = | |
| 138 extra_data->transferred_request_request_id(); | |
| 139 } else { | |
| 140 request_.is_main_frame = false; | |
| 141 request_.frame_id = -1; | |
| 142 request_.parent_is_main_frame = false; | |
| 143 request_.parent_frame_id = -1; | |
| 144 request_.allow_download = true; | |
| 145 request_.transition_type = PAGE_TRANSITION_LINK; | |
| 146 request_.transferred_request_child_id = -1; | |
| 147 request_.transferred_request_request_id = -1; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 IPCResourceLoaderBridge::~IPCResourceLoaderBridge() { | |
| 152 // we remove our hook for the resource dispatcher only when going away, since | |
| 153 // it doesn't keep track of whether we've force terminated the request | |
| 154 if (request_id_ >= 0) { | |
| 155 // this operation may fail, as the dispatcher will have preemptively | |
| 156 // removed us when the renderer sends the ReceivedAllData message. | |
| 157 dispatcher_->RemovePendingRequest(request_id_); | |
| 158 | |
| 159 if (request_.download_to_file) { | |
| 160 dispatcher_->message_sender()->Send( | |
| 161 new ResourceHostMsg_ReleaseDownloadedFile(request_id_)); | |
| 162 } | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 void IPCResourceLoaderBridge::SetRequestBody( | |
| 167 ResourceRequestBody* request_body) { | |
| 168 DCHECK(request_id_ == -1) << "request already started"; | |
| 169 request_.request_body = request_body; | |
| 170 } | |
| 171 | |
| 172 // Writes a footer on the message and sends it | |
| 173 bool IPCResourceLoaderBridge::Start(Peer* peer) { | |
| 174 if (request_id_ != -1) { | |
| 175 NOTREACHED() << "Starting a request twice"; | |
| 176 return false; | |
| 177 } | |
| 178 | |
| 179 peer_ = peer; | |
| 180 | |
| 181 // generate the request ID, and append it to the message | |
| 182 request_id_ = dispatcher_->AddPendingRequest( | |
| 183 peer_, request_.resource_type, request_.url); | |
| 184 | |
| 185 return dispatcher_->message_sender()->Send( | |
| 186 new ResourceHostMsg_RequestResource(routing_id_, request_id_, request_)); | |
| 187 } | |
| 188 | |
| 189 void IPCResourceLoaderBridge::Cancel() { | |
| 190 if (request_id_ < 0) { | |
| 191 NOTREACHED() << "Trying to cancel an unstarted request"; | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 if (!is_synchronous_request_) | |
| 196 dispatcher_->CancelPendingRequest(routing_id_, request_id_); | |
| 197 | |
| 198 // We can't remove the request ID from the resource dispatcher because more | |
| 199 // data might be pending. Sending the cancel message may cause more data | |
| 200 // to be flushed, and will then cause a complete message to be sent. | |
| 201 } | |
| 202 | |
| 203 void IPCResourceLoaderBridge::SetDefersLoading(bool value) { | |
| 204 if (request_id_ < 0) { | |
| 205 NOTREACHED() << "Trying to (un)defer an unstarted request"; | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 dispatcher_->SetDefersLoading(request_id_, value); | |
| 210 } | |
| 211 | |
| 212 void IPCResourceLoaderBridge::DidChangePriority( | |
| 213 net::RequestPriority new_priority) { | |
| 214 if (request_id_ < 0) { | |
| 215 NOTREACHED() << "Trying to change priority of an unstarted request"; | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 dispatcher_->DidChangePriority(routing_id_, request_id_, new_priority); | |
| 220 } | |
| 221 | |
| 222 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) { | |
| 223 if (request_id_ != -1) { | |
| 224 NOTREACHED() << "Starting a request twice"; | |
| 225 response->error_code = net::ERR_FAILED; | |
| 226 return; | |
| 227 } | |
| 228 | |
| 229 request_id_ = MakeRequestID(); | |
| 230 is_synchronous_request_ = true; | |
| 231 | |
| 232 SyncLoadResult result; | |
| 233 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad(routing_id_, request_id_, | |
| 234 request_, &result); | |
| 235 // NOTE: This may pump events (see RenderThread::Send). | |
| 236 if (!dispatcher_->message_sender()->Send(msg)) { | |
| 237 response->error_code = net::ERR_FAILED; | |
| 238 return; | |
| 239 } | |
| 240 | |
| 241 response->error_code = result.error_code; | |
| 242 response->url = result.final_url; | |
| 243 response->headers = result.headers; | |
| 244 response->mime_type = result.mime_type; | |
| 245 response->charset = result.charset; | |
| 246 response->request_time = result.request_time; | |
| 247 response->response_time = result.response_time; | |
| 248 response->encoded_data_length = result.encoded_data_length; | |
| 249 response->load_timing = result.load_timing; | |
| 250 response->devtools_info = result.devtools_info; | |
| 251 response->data.swap(result.data); | |
| 252 response->download_file_path = result.download_file_path; | |
| 253 } | |
| 254 | |
| 255 // ResourceDispatcher --------------------------------------------------------- | |
| 256 | |
| 257 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender) | |
| 258 : message_sender_(sender), | |
| 259 weak_factory_(this), | |
| 260 delegate_(NULL), | |
| 261 io_timestamp_(base::TimeTicks()) { | |
| 262 } | |
| 263 | |
| 264 ResourceDispatcher::~ResourceDispatcher() { | |
| 265 } | |
| 266 | |
| 267 // ResourceDispatcher implementation ------------------------------------------ | |
| 268 | |
| 269 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) { | |
| 270 if (!IsResourceDispatcherMessage(message)) { | |
| 271 return false; | |
| 272 } | |
| 273 | |
| 274 int request_id; | |
| 275 | |
| 276 PickleIterator iter(message); | |
| 277 if (!message.ReadInt(&iter, &request_id)) { | |
| 278 NOTREACHED() << "malformed resource message"; | |
| 279 return true; | |
| 280 } | |
| 281 | |
| 282 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 283 if (!request_info) { | |
| 284 // Release resources in the message if it is a data message. | |
| 285 ReleaseResourcesInDataMessage(message); | |
| 286 return true; | |
| 287 } | |
| 288 | |
| 289 if (request_info->is_deferred) { | |
| 290 request_info->deferred_message_queue.push_back(new IPC::Message(message)); | |
| 291 return true; | |
| 292 } | |
| 293 // Make sure any deferred messages are dispatched before we dispatch more. | |
| 294 if (!request_info->deferred_message_queue.empty()) { | |
| 295 FlushDeferredMessages(request_id); | |
| 296 // The request could have been deferred now. If yes then the current | |
| 297 // message has to be queued up. The request_info instance should remain | |
| 298 // valid here as there are pending messages for it. | |
| 299 DCHECK(pending_requests_.find(request_id) != pending_requests_.end()); | |
| 300 if (request_info->is_deferred) { | |
| 301 request_info->deferred_message_queue.push_back(new IPC::Message(message)); | |
| 302 return true; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 DispatchMessage(message); | |
| 307 return true; | |
| 308 } | |
| 309 | |
| 310 ResourceDispatcher::PendingRequestInfo* | |
| 311 ResourceDispatcher::GetPendingRequestInfo(int request_id) { | |
| 312 PendingRequestList::iterator it = pending_requests_.find(request_id); | |
| 313 if (it == pending_requests_.end()) { | |
| 314 // This might happen for kill()ed requests on the webkit end. | |
| 315 return NULL; | |
| 316 } | |
| 317 return &(it->second); | |
| 318 } | |
| 319 | |
| 320 void ResourceDispatcher::OnUploadProgress( | |
| 321 const IPC::Message& message, int request_id, int64 position, int64 size) { | |
| 322 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 323 if (!request_info) | |
| 324 return; | |
| 325 | |
| 326 request_info->peer->OnUploadProgress(position, size); | |
| 327 | |
| 328 // Acknowledge receipt | |
| 329 message_sender()->Send( | |
| 330 new ResourceHostMsg_UploadProgress_ACK(message.routing_id(), request_id)); | |
| 331 } | |
| 332 | |
| 333 void ResourceDispatcher::OnReceivedResponse( | |
| 334 int request_id, const ResourceResponseHead& response_head) { | |
| 335 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 336 if (!request_info) | |
| 337 return; | |
| 338 request_info->response_start = ConsumeIOTimestamp(); | |
| 339 | |
| 340 if (delegate_) { | |
| 341 ResourceLoaderBridge::Peer* new_peer = | |
| 342 delegate_->OnReceivedResponse( | |
| 343 request_info->peer, response_head.mime_type, request_info->url); | |
| 344 if (new_peer) | |
| 345 request_info->peer = new_peer; | |
| 346 } | |
| 347 | |
| 348 ResourceResponseInfo renderer_response_info; | |
| 349 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info); | |
| 350 request_info->peer->OnReceivedResponse(renderer_response_info); | |
| 351 } | |
| 352 | |
| 353 void ResourceDispatcher::OnReceivedCachedMetadata( | |
| 354 int request_id, const std::vector<char>& data) { | |
| 355 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 356 if (!request_info) | |
| 357 return; | |
| 358 | |
| 359 if (data.size()) | |
| 360 request_info->peer->OnReceivedCachedMetadata(&data.front(), data.size()); | |
| 361 } | |
| 362 | |
| 363 void ResourceDispatcher::OnSetDataBuffer(const IPC::Message& message, | |
| 364 int request_id, | |
| 365 base::SharedMemoryHandle shm_handle, | |
| 366 int shm_size, | |
| 367 base::ProcessId renderer_pid) { | |
| 368 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 369 if (!request_info) | |
| 370 return; | |
| 371 | |
| 372 bool shm_valid = base::SharedMemory::IsHandleValid(shm_handle); | |
| 373 CHECK((shm_valid && shm_size > 0) || (!shm_valid && !shm_size)); | |
| 374 | |
| 375 request_info->buffer.reset( | |
| 376 new base::SharedMemory(shm_handle, true)); // read only | |
| 377 | |
| 378 bool ok = request_info->buffer->Map(shm_size); | |
| 379 if (!ok) { | |
| 380 // Added to help debug crbug/160401. | |
| 381 base::ProcessId renderer_pid_copy = renderer_pid; | |
| 382 base::debug::Alias(&renderer_pid_copy); | |
| 383 | |
| 384 base::SharedMemoryHandle shm_handle_copy = shm_handle; | |
| 385 base::debug::Alias(&shm_handle_copy); | |
| 386 | |
| 387 CrashOnMapFailure(); | |
| 388 return; | |
| 389 } | |
| 390 | |
| 391 request_info->buffer_size = shm_size; | |
| 392 } | |
| 393 | |
| 394 void ResourceDispatcher::OnReceivedData(const IPC::Message& message, | |
| 395 int request_id, | |
| 396 int data_offset, | |
| 397 int data_length, | |
| 398 int encoded_data_length) { | |
| 399 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 400 if (request_info && data_length > 0) { | |
| 401 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); | |
| 402 CHECK_GE(request_info->buffer_size, data_offset + data_length); | |
| 403 | |
| 404 // Ensure that the SHM buffer remains valid for the duration of this scope. | |
| 405 // It is possible for CancelPendingRequest() to be called before we exit | |
| 406 // this scope. | |
| 407 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); | |
| 408 | |
| 409 base::TimeTicks time_start = base::TimeTicks::Now(); | |
| 410 | |
| 411 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); | |
| 412 CHECK(data_ptr); | |
| 413 CHECK(data_ptr + data_offset); | |
| 414 | |
| 415 request_info->peer->OnReceivedData( | |
| 416 data_ptr + data_offset, | |
| 417 data_length, | |
| 418 encoded_data_length); | |
| 419 | |
| 420 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", | |
| 421 base::TimeTicks::Now() - time_start); | |
| 422 } | |
| 423 | |
| 424 // Acknowledge the reception of this data. | |
| 425 message_sender()->Send( | |
| 426 new ResourceHostMsg_DataReceived_ACK(message.routing_id(), request_id)); | |
| 427 } | |
| 428 | |
| 429 void ResourceDispatcher::OnDownloadedData(const IPC::Message& message, | |
| 430 int request_id, | |
| 431 int data_len) { | |
| 432 // Acknowledge the reception of this message. | |
| 433 message_sender()->Send( | |
| 434 new ResourceHostMsg_DataDownloaded_ACK(message.routing_id(), request_id)); | |
| 435 | |
| 436 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 437 if (!request_info) | |
| 438 return; | |
| 439 | |
| 440 request_info->peer->OnDownloadedData(data_len); | |
| 441 } | |
| 442 | |
| 443 void ResourceDispatcher::OnReceivedRedirect( | |
| 444 const IPC::Message& message, | |
| 445 int request_id, | |
| 446 const GURL& new_url, | |
| 447 const ResourceResponseHead& response_head) { | |
| 448 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 449 if (!request_info) | |
| 450 return; | |
| 451 request_info->response_start = ConsumeIOTimestamp(); | |
| 452 | |
| 453 int32 routing_id = message.routing_id(); | |
| 454 bool has_new_first_party_for_cookies = false; | |
| 455 GURL new_first_party_for_cookies; | |
| 456 ResourceResponseInfo renderer_response_info; | |
| 457 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info); | |
| 458 if (request_info->peer->OnReceivedRedirect(new_url, renderer_response_info, | |
| 459 &has_new_first_party_for_cookies, | |
| 460 &new_first_party_for_cookies)) { | |
| 461 // Double-check if the request is still around. The call above could | |
| 462 // potentially remove it. | |
| 463 request_info = GetPendingRequestInfo(request_id); | |
| 464 if (!request_info) | |
| 465 return; | |
| 466 request_info->pending_redirect_message.reset( | |
| 467 new ResourceHostMsg_FollowRedirect(routing_id, request_id, | |
| 468 has_new_first_party_for_cookies, | |
| 469 new_first_party_for_cookies)); | |
| 470 if (!request_info->is_deferred) { | |
| 471 FollowPendingRedirect(request_id, *request_info); | |
| 472 } | |
| 473 } else { | |
| 474 CancelPendingRequest(routing_id, request_id); | |
| 475 } | |
| 476 } | |
| 477 | |
| 478 void ResourceDispatcher::FollowPendingRedirect( | |
| 479 int request_id, | |
| 480 PendingRequestInfo& request_info) { | |
| 481 IPC::Message* msg = request_info.pending_redirect_message.release(); | |
| 482 if (msg) | |
| 483 message_sender()->Send(msg); | |
| 484 } | |
| 485 | |
| 486 void ResourceDispatcher::OnRequestComplete( | |
| 487 int request_id, | |
| 488 int error_code, | |
| 489 bool was_ignored_by_handler, | |
| 490 const std::string& security_info, | |
| 491 const base::TimeTicks& browser_completion_time) { | |
| 492 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | |
| 493 if (!request_info) | |
| 494 return; | |
| 495 request_info->completion_time = ConsumeIOTimestamp(); | |
| 496 request_info->buffer.reset(); | |
| 497 request_info->buffer_size = 0; | |
| 498 | |
| 499 ResourceLoaderBridge::Peer* peer = request_info->peer; | |
| 500 | |
| 501 if (delegate_) { | |
| 502 ResourceLoaderBridge::Peer* new_peer = | |
| 503 delegate_->OnRequestComplete( | |
| 504 request_info->peer, request_info->resource_type, error_code); | |
| 505 if (new_peer) | |
| 506 request_info->peer = new_peer; | |
| 507 } | |
| 508 | |
| 509 base::TimeTicks renderer_completion_time = ToRendererCompletionTime( | |
| 510 *request_info, browser_completion_time); | |
| 511 // The request ID will be removed from our pending list in the destructor. | |
| 512 // Normally, dispatching this message causes the reference-counted request to | |
| 513 // die immediately. | |
| 514 peer->OnCompletedRequest(error_code, was_ignored_by_handler, security_info, | |
| 515 renderer_completion_time); | |
| 516 } | |
| 517 | |
| 518 int ResourceDispatcher::AddPendingRequest( | |
| 519 ResourceLoaderBridge::Peer* callback, | |
| 520 ResourceType::Type resource_type, | |
| 521 const GURL& request_url) { | |
| 522 // Compute a unique request_id for this renderer process. | |
| 523 int id = MakeRequestID(); | |
| 524 pending_requests_[id] = | |
| 525 PendingRequestInfo(callback, resource_type, request_url); | |
| 526 return id; | |
| 527 } | |
| 528 | |
| 529 bool ResourceDispatcher::RemovePendingRequest(int request_id) { | |
| 530 PendingRequestList::iterator it = pending_requests_.find(request_id); | |
| 531 if (it == pending_requests_.end()) | |
| 532 return false; | |
| 533 | |
| 534 PendingRequestInfo& request_info = it->second; | |
| 535 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue); | |
| 536 pending_requests_.erase(it); | |
| 537 | |
| 538 return true; | |
| 539 } | |
| 540 | |
| 541 void ResourceDispatcher::CancelPendingRequest(int routing_id, | |
| 542 int request_id) { | |
| 543 PendingRequestList::iterator it = pending_requests_.find(request_id); | |
| 544 if (it == pending_requests_.end()) { | |
| 545 DVLOG(1) << "unknown request"; | |
| 546 return; | |
| 547 } | |
| 548 | |
| 549 PendingRequestInfo& request_info = it->second; | |
| 550 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue); | |
| 551 pending_requests_.erase(it); | |
| 552 | |
| 553 message_sender()->Send( | |
| 554 new ResourceHostMsg_CancelRequest(routing_id, request_id)); | |
| 555 } | |
| 556 | |
| 557 void ResourceDispatcher::SetDefersLoading(int request_id, bool value) { | |
| 558 PendingRequestList::iterator it = pending_requests_.find(request_id); | |
| 559 if (it == pending_requests_.end()) { | |
| 560 DLOG(ERROR) << "unknown request"; | |
| 561 return; | |
| 562 } | |
| 563 PendingRequestInfo& request_info = it->second; | |
| 564 if (value) { | |
| 565 request_info.is_deferred = value; | |
| 566 } else if (request_info.is_deferred) { | |
| 567 request_info.is_deferred = false; | |
| 568 | |
| 569 FollowPendingRedirect(request_id, request_info); | |
| 570 | |
| 571 base::MessageLoop::current()->PostTask( | |
| 572 FROM_HERE, | |
| 573 base::Bind(&ResourceDispatcher::FlushDeferredMessages, | |
| 574 weak_factory_.GetWeakPtr(), | |
| 575 request_id)); | |
| 576 } | |
| 577 } | |
| 578 | |
| 579 void ResourceDispatcher::DidChangePriority( | |
| 580 int routing_id, int request_id, net::RequestPriority new_priority) { | |
| 581 DCHECK(ContainsKey(pending_requests_, request_id)); | |
| 582 message_sender()->Send(new ResourceHostMsg_DidChangePriority( | |
| 583 routing_id, request_id, new_priority)); | |
| 584 } | |
| 585 | |
| 586 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() | |
| 587 : peer(NULL), | |
| 588 resource_type(ResourceType::SUB_RESOURCE), | |
| 589 is_deferred(false), | |
| 590 buffer_size(0) { | |
| 591 } | |
| 592 | |
| 593 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( | |
| 594 webkit_glue::ResourceLoaderBridge::Peer* peer, | |
| 595 ResourceType::Type resource_type, | |
| 596 const GURL& request_url) | |
| 597 : peer(peer), | |
| 598 resource_type(resource_type), | |
| 599 is_deferred(false), | |
| 600 url(request_url), | |
| 601 request_start(base::TimeTicks::Now()) { | |
| 602 } | |
| 603 | |
| 604 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} | |
| 605 | |
| 606 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { | |
| 607 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) | |
| 608 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) | |
| 609 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) | |
| 610 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, | |
| 611 OnReceivedCachedMetadata) | |
| 612 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect, OnReceivedRedirect) | |
| 613 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer, OnSetDataBuffer) | |
| 614 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived, OnReceivedData) | |
| 615 IPC_MESSAGE_HANDLER(ResourceMsg_DataDownloaded, OnDownloadedData) | |
| 616 IPC_MESSAGE_HANDLER(ResourceMsg_RequestComplete, OnRequestComplete) | |
| 617 IPC_END_MESSAGE_MAP() | |
| 618 } | |
| 619 | |
| 620 void ResourceDispatcher::FlushDeferredMessages(int request_id) { | |
| 621 PendingRequestList::iterator it = pending_requests_.find(request_id); | |
| 622 if (it == pending_requests_.end()) // The request could have become invalid. | |
| 623 return; | |
| 624 PendingRequestInfo& request_info = it->second; | |
| 625 if (request_info.is_deferred) | |
| 626 return; | |
| 627 // Because message handlers could result in request_info being destroyed, | |
| 628 // we need to work with a stack reference to the deferred queue. | |
| 629 MessageQueue q; | |
| 630 q.swap(request_info.deferred_message_queue); | |
| 631 while (!q.empty()) { | |
| 632 IPC::Message* m = q.front(); | |
| 633 q.pop_front(); | |
| 634 DispatchMessage(*m); | |
| 635 delete m; | |
| 636 // If this request is deferred in the context of the above message, then | |
| 637 // we should honor the same and stop dispatching further messages. | |
| 638 // We need to find the request again in the list as it may have completed | |
| 639 // by now and the request_info instance above may be invalid. | |
| 640 PendingRequestList::iterator index = pending_requests_.find(request_id); | |
| 641 if (index != pending_requests_.end()) { | |
| 642 PendingRequestInfo& pending_request = index->second; | |
| 643 if (pending_request.is_deferred) { | |
| 644 pending_request.deferred_message_queue.swap(q); | |
| 645 return; | |
| 646 } | |
| 647 } | |
| 648 } | |
| 649 } | |
| 650 | |
| 651 ResourceLoaderBridge* ResourceDispatcher::CreateBridge( | |
| 652 const ResourceLoaderBridge::RequestInfo& request_info) { | |
| 653 return new IPCResourceLoaderBridge(this, request_info); | |
| 654 } | |
| 655 | |
| 656 void ResourceDispatcher::ToResourceResponseInfo( | |
| 657 const PendingRequestInfo& request_info, | |
| 658 const ResourceResponseHead& browser_info, | |
| 659 ResourceResponseInfo* renderer_info) const { | |
| 660 *renderer_info = browser_info; | |
| 661 if (request_info.request_start.is_null() || | |
| 662 request_info.response_start.is_null() || | |
| 663 browser_info.request_start.is_null() || | |
| 664 browser_info.response_start.is_null() || | |
| 665 browser_info.load_timing.request_start.is_null()) { | |
| 666 return; | |
| 667 } | |
| 668 InterProcessTimeTicksConverter converter( | |
| 669 LocalTimeTicks::FromTimeTicks(request_info.request_start), | |
| 670 LocalTimeTicks::FromTimeTicks(request_info.response_start), | |
| 671 RemoteTimeTicks::FromTimeTicks(browser_info.request_start), | |
| 672 RemoteTimeTicks::FromTimeTicks(browser_info.response_start)); | |
| 673 | |
| 674 net::LoadTimingInfo* load_timing = &renderer_info->load_timing; | |
| 675 RemoteToLocalTimeTicks(converter, &load_timing->request_start); | |
| 676 RemoteToLocalTimeTicks(converter, &load_timing->proxy_resolve_start); | |
| 677 RemoteToLocalTimeTicks(converter, &load_timing->proxy_resolve_end); | |
| 678 RemoteToLocalTimeTicks(converter, &load_timing->connect_timing.dns_start); | |
| 679 RemoteToLocalTimeTicks(converter, &load_timing->connect_timing.dns_end); | |
| 680 RemoteToLocalTimeTicks(converter, &load_timing->connect_timing.connect_start); | |
| 681 RemoteToLocalTimeTicks(converter, &load_timing->connect_timing.connect_end); | |
| 682 RemoteToLocalTimeTicks(converter, &load_timing->connect_timing.ssl_start); | |
| 683 RemoteToLocalTimeTicks(converter, &load_timing->connect_timing.ssl_end); | |
| 684 RemoteToLocalTimeTicks(converter, &load_timing->send_start); | |
| 685 RemoteToLocalTimeTicks(converter, &load_timing->send_end); | |
| 686 RemoteToLocalTimeTicks(converter, &load_timing->receive_headers_end); | |
| 687 } | |
| 688 | |
| 689 base::TimeTicks ResourceDispatcher::ToRendererCompletionTime( | |
| 690 const PendingRequestInfo& request_info, | |
| 691 const base::TimeTicks& browser_completion_time) const { | |
| 692 if (request_info.completion_time.is_null()) { | |
| 693 return browser_completion_time; | |
| 694 } | |
| 695 | |
| 696 // TODO(simonjam): The optimal lower bound should be the most recent value of | |
| 697 // TimeTicks::Now() returned to WebKit. Is it worth trying to cache that? | |
| 698 // Until then, |response_start| is used as it is the most recent value | |
| 699 // returned for this request. | |
| 700 int64 result = std::max(browser_completion_time.ToInternalValue(), | |
| 701 request_info.response_start.ToInternalValue()); | |
| 702 result = std::min(result, request_info.completion_time.ToInternalValue()); | |
| 703 return base::TimeTicks::FromInternalValue(result); | |
| 704 } | |
| 705 | |
| 706 base::TimeTicks ResourceDispatcher::ConsumeIOTimestamp() { | |
| 707 if (io_timestamp_ == base::TimeTicks()) | |
| 708 return base::TimeTicks::Now(); | |
| 709 base::TimeTicks result = io_timestamp_; | |
| 710 io_timestamp_ = base::TimeTicks(); | |
| 711 return result; | |
| 712 } | |
| 713 | |
| 714 // static | |
| 715 bool ResourceDispatcher::IsResourceDispatcherMessage( | |
| 716 const IPC::Message& message) { | |
| 717 switch (message.type()) { | |
| 718 case ResourceMsg_UploadProgress::ID: | |
| 719 case ResourceMsg_ReceivedResponse::ID: | |
| 720 case ResourceMsg_ReceivedCachedMetadata::ID: | |
| 721 case ResourceMsg_ReceivedRedirect::ID: | |
| 722 case ResourceMsg_SetDataBuffer::ID: | |
| 723 case ResourceMsg_DataReceived::ID: | |
| 724 case ResourceMsg_DataDownloaded::ID: | |
| 725 case ResourceMsg_RequestComplete::ID: | |
| 726 return true; | |
| 727 | |
| 728 default: | |
| 729 break; | |
| 730 } | |
| 731 | |
| 732 return false; | |
| 733 } | |
| 734 | |
| 735 // static | |
| 736 void ResourceDispatcher::ReleaseResourcesInDataMessage( | |
| 737 const IPC::Message& message) { | |
| 738 PickleIterator iter(message); | |
| 739 int request_id; | |
| 740 if (!message.ReadInt(&iter, &request_id)) { | |
| 741 NOTREACHED() << "malformed resource message"; | |
| 742 return; | |
| 743 } | |
| 744 | |
| 745 // If the message contains a shared memory handle, we should close the handle | |
| 746 // or there will be a memory leak. | |
| 747 if (message.type() == ResourceMsg_SetDataBuffer::ID) { | |
| 748 base::SharedMemoryHandle shm_handle; | |
| 749 if (IPC::ParamTraits<base::SharedMemoryHandle>::Read(&message, | |
| 750 &iter, | |
| 751 &shm_handle)) { | |
| 752 if (base::SharedMemory::IsHandleValid(shm_handle)) | |
| 753 base::SharedMemory::CloseHandle(shm_handle); | |
| 754 } | |
| 755 } | |
| 756 } | |
| 757 | |
| 758 // static | |
| 759 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { | |
| 760 while (!queue->empty()) { | |
| 761 IPC::Message* message = queue->front(); | |
| 762 ReleaseResourcesInDataMessage(*message); | |
| 763 queue->pop_front(); | |
| 764 delete message; | |
| 765 } | |
| 766 } | |
| 767 | |
| 768 } // namespace content | |
| OLD | NEW |