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" |
31 #include "webkit/common/resource_type.h" | 32 #include "webkit/common/resource_type.h" |
32 | 33 |
33 using webkit_glue::ResourceLoaderBridge; | 34 using webkit_glue::ResourceLoaderBridge; |
34 using webkit_glue::ResourceResponseInfo; | 35 using webkit_glue::ResourceResponseInfo; |
35 | 36 |
36 namespace content { | 37 namespace content { |
37 | 38 |
38 namespace { | 39 namespace { |
39 | 40 |
40 // Converts |time| from a remote to local TimeTicks, overwriting the original | 41 // Converts |time| from a remote to local TimeTicks, overwriting the original |
41 // value. | 42 // value. |
42 void RemoteToLocalTimeTicks( | 43 void RemoteToLocalTimeTicks( |
43 const InterProcessTimeTicksConverter& converter, | 44 const InterProcessTimeTicksConverter& converter, |
44 base::TimeTicks* time) { | 45 base::TimeTicks* time) { |
45 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time); | 46 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time); |
46 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks(); | 47 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks(); |
47 } | 48 } |
48 | 49 |
49 void CrashOnMapFailure() { | 50 |
| 51 } // namespace |
| 52 |
| 53 static void CrashOnMapFailure() { |
50 #if defined(OS_WIN) | 54 #if defined(OS_WIN) |
51 DWORD last_err = GetLastError(); | 55 DWORD last_err = GetLastError(); |
52 base::debug::Alias(&last_err); | 56 base::debug::Alias(&last_err); |
53 #endif | 57 #endif |
54 CHECK(false); | 58 CHECK(false); |
55 } | 59 } |
56 | 60 |
57 // Each resource request is assigned an ID scoped to this process. | 61 // Each resource request is assigned an ID scoped to this process. |
58 int MakeRequestID() { | 62 static int MakeRequestID() { |
59 // NOTE: The resource_dispatcher_host also needs probably unique | 63 // NOTE: The resource_dispatcher_host also needs probably unique |
60 // request_ids, so they count down from -2 (-1 is a special we're | 64 // request_ids, so they count down from -2 (-1 is a special we're |
61 // screwed value), while the renderer process counts up. | 65 // screwed value), while the renderer process counts up. |
62 static int next_request_id = 0; | 66 static int next_request_id = 0; |
63 return next_request_id++; | 67 return next_request_id++; |
64 } | 68 } |
65 | 69 |
66 } // namespace | 70 // ResourceLoaderBridge implementation ---------------------------------------- |
| 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 --------------------------------------------------------- |
67 | 267 |
68 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender) | 268 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender) |
69 : message_sender_(sender), | 269 : message_sender_(sender), |
70 weak_factory_(this), | 270 weak_factory_(this), |
71 delegate_(NULL), | 271 delegate_(NULL), |
72 io_timestamp_(base::TimeTicks()) { | 272 io_timestamp_(base::TimeTicks()) { |
73 } | 273 } |
74 | 274 |
75 ResourceDispatcher::~ResourceDispatcher() { | 275 ResourceDispatcher::~ResourceDispatcher() { |
76 } | 276 } |
77 | 277 |
| 278 // ResourceDispatcher implementation ------------------------------------------ |
| 279 |
78 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) { | 280 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) { |
79 if (!IsResourceDispatcherMessage(message)) { | 281 if (!IsResourceDispatcherMessage(message)) { |
80 return false; | 282 return false; |
81 } | 283 } |
82 | 284 |
83 int request_id; | 285 int request_id; |
84 | 286 |
85 PickleIterator iter(message); | 287 PickleIterator iter(message); |
86 if (!message.ReadInt(&iter, &request_id)) { | 288 if (!message.ReadInt(&iter, &request_id)) { |
87 NOTREACHED() << "malformed resource message"; | 289 NOTREACHED() << "malformed resource message"; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 330 |
129 void ResourceDispatcher::OnUploadProgress(int request_id, int64 position, | 331 void ResourceDispatcher::OnUploadProgress(int request_id, int64 position, |
130 int64 size) { | 332 int64 size) { |
131 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 333 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
132 if (!request_info) | 334 if (!request_info) |
133 return; | 335 return; |
134 | 336 |
135 request_info->peer->OnUploadProgress(position, size); | 337 request_info->peer->OnUploadProgress(position, size); |
136 | 338 |
137 // Acknowledge receipt | 339 // Acknowledge receipt |
138 message_sender_->Send(new ResourceHostMsg_UploadProgress_ACK(request_id)); | 340 message_sender()->Send(new ResourceHostMsg_UploadProgress_ACK(request_id)); |
139 } | 341 } |
140 | 342 |
141 void ResourceDispatcher::OnReceivedResponse( | 343 void ResourceDispatcher::OnReceivedResponse( |
142 int request_id, | 344 int request_id, const ResourceResponseHead& response_head) { |
143 const ResourceResponseHead& response_head) { | |
144 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse"); | 345 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse"); |
145 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 346 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
146 if (!request_info) | 347 if (!request_info) |
147 return; | 348 return; |
148 request_info->response_start = ConsumeIOTimestamp(); | 349 request_info->response_start = ConsumeIOTimestamp(); |
149 | 350 |
150 if (delegate_) { | 351 if (delegate_) { |
151 RequestPeer* new_peer = | 352 RequestPeer* new_peer = |
152 delegate_->OnReceivedResponse( | 353 delegate_->OnReceivedResponse( |
153 request_info->peer, response_head.mime_type, request_info->url); | 354 request_info->peer, response_head.mime_type, request_info->url); |
154 if (new_peer) | 355 if (new_peer) |
155 request_info->peer = new_peer; | 356 request_info->peer = new_peer; |
156 } | 357 } |
157 | 358 |
158 ResourceResponseInfo renderer_response_info; | 359 ResourceResponseInfo renderer_response_info; |
159 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info); | 360 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info); |
160 request_info->site_isolation_metadata = | 361 request_info->site_isolation_metadata = |
161 SiteIsolationPolicy::OnReceivedResponse(request_info->frame_origin, | 362 SiteIsolationPolicy::OnReceivedResponse(request_info->frame_origin, |
162 request_info->response_url, | 363 request_info->response_url, |
163 request_info->resource_type, | 364 request_info->resource_type, |
164 request_info->origin_pid, | 365 request_info->origin_pid, |
165 renderer_response_info); | 366 renderer_response_info); |
166 request_info->peer->OnReceivedResponse(renderer_response_info); | 367 request_info->peer->OnReceivedResponse(renderer_response_info); |
167 } | 368 } |
168 | 369 |
169 void ResourceDispatcher::OnReceivedCachedMetadata( | 370 void ResourceDispatcher::OnReceivedCachedMetadata( |
170 int request_id, | 371 int request_id, const std::vector<char>& data) { |
171 const std::vector<char>& data) { | |
172 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 372 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
173 if (!request_info) | 373 if (!request_info) |
174 return; | 374 return; |
175 | 375 |
176 if (data.size()) | 376 if (data.size()) |
177 request_info->peer->OnReceivedCachedMetadata(&data.front(), data.size()); | 377 request_info->peer->OnReceivedCachedMetadata(&data.front(), data.size()); |
178 } | 378 } |
179 | 379 |
180 void ResourceDispatcher::OnSetDataBuffer(int request_id, | 380 void ResourceDispatcher::OnSetDataBuffer(int request_id, |
181 base::SharedMemoryHandle shm_handle, | 381 base::SharedMemoryHandle shm_handle, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 int data_length, | 413 int data_length, |
214 int encoded_data_length) { | 414 int encoded_data_length) { |
215 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); | 415 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); |
216 DCHECK_GT(data_length, 0); | 416 DCHECK_GT(data_length, 0); |
217 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 417 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
218 if (request_info && data_length > 0) { | 418 if (request_info && data_length > 0) { |
219 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); | 419 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); |
220 CHECK_GE(request_info->buffer_size, data_offset + data_length); | 420 CHECK_GE(request_info->buffer_size, data_offset + data_length); |
221 | 421 |
222 // Ensure that the SHM buffer remains valid for the duration of this scope. | 422 // Ensure that the SHM buffer remains valid for the duration of this scope. |
223 // It is possible for Cancel() to be called before we exit this scope. | 423 // It is possible for CancelPendingRequest() to be called before we exit |
| 424 // this scope. |
224 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); | 425 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); |
225 | 426 |
226 base::TimeTicks time_start = base::TimeTicks::Now(); | 427 base::TimeTicks time_start = base::TimeTicks::Now(); |
227 | 428 |
228 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); | 429 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); |
229 CHECK(data_ptr); | 430 CHECK(data_ptr); |
230 CHECK(data_ptr + data_offset); | 431 CHECK(data_ptr + data_offset); |
231 | 432 |
232 // Check whether this response data is compliant with our cross-site | 433 // Check whether this response data is compliant with our cross-site |
233 // document blocking policy. We only do this for the first packet. | 434 // document blocking policy. We only do this for the first packet. |
(...skipping 17 matching lines...) Expand all Loading... |
251 request_info->peer->OnReceivedData(alternative_data.data(), | 452 request_info->peer->OnReceivedData(alternative_data.data(), |
252 alternative_data.size(), | 453 alternative_data.size(), |
253 alternative_data.size()); | 454 alternative_data.size()); |
254 } | 455 } |
255 | 456 |
256 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", | 457 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", |
257 base::TimeTicks::Now() - time_start); | 458 base::TimeTicks::Now() - time_start); |
258 } | 459 } |
259 | 460 |
260 // Acknowledge the reception of this data. | 461 // Acknowledge the reception of this data. |
261 message_sender_->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); | 462 message_sender()->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); |
262 } | 463 } |
263 | 464 |
264 void ResourceDispatcher::OnDownloadedData(int request_id, | 465 void ResourceDispatcher::OnDownloadedData(int request_id, |
265 int data_len, | 466 int data_len, |
266 int encoded_data_length) { | 467 int encoded_data_length) { |
267 // Acknowledge the reception of this message. | 468 // Acknowledge the reception of this message. |
268 message_sender_->Send( | 469 message_sender()->Send( |
269 new ResourceHostMsg_DataDownloaded_ACK(request_id)); | 470 new ResourceHostMsg_DataDownloaded_ACK(request_id)); |
270 | 471 |
271 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 472 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
272 if (!request_info) | 473 if (!request_info) |
273 return; | 474 return; |
274 | 475 |
275 request_info->peer->OnDownloadedData(data_len, encoded_data_length); | 476 request_info->peer->OnDownloadedData(data_len, encoded_data_length); |
276 } | 477 } |
277 | 478 |
278 void ResourceDispatcher::OnReceivedRedirect( | 479 void ResourceDispatcher::OnReceivedRedirect( |
(...skipping 22 matching lines...) Expand all Loading... |
301 // SiteIsolationPolicy later when OnReceivedResponse is called. | 502 // SiteIsolationPolicy later when OnReceivedResponse is called. |
302 request_info->response_url = new_url; | 503 request_info->response_url = new_url; |
303 request_info->pending_redirect_message.reset( | 504 request_info->pending_redirect_message.reset( |
304 new ResourceHostMsg_FollowRedirect(request_id, | 505 new ResourceHostMsg_FollowRedirect(request_id, |
305 has_new_first_party_for_cookies, | 506 has_new_first_party_for_cookies, |
306 new_first_party_for_cookies)); | 507 new_first_party_for_cookies)); |
307 if (!request_info->is_deferred) { | 508 if (!request_info->is_deferred) { |
308 FollowPendingRedirect(request_id, *request_info); | 509 FollowPendingRedirect(request_id, *request_info); |
309 } | 510 } |
310 } else { | 511 } else { |
311 Cancel(request_id); | 512 CancelPendingRequest(request_id); |
312 } | 513 } |
313 } | 514 } |
314 | 515 |
315 void ResourceDispatcher::FollowPendingRedirect( | 516 void ResourceDispatcher::FollowPendingRedirect( |
316 int request_id, | 517 int request_id, |
317 PendingRequestInfo& request_info) { | 518 PendingRequestInfo& request_info) { |
318 IPC::Message* msg = request_info.pending_redirect_message.release(); | 519 IPC::Message* msg = request_info.pending_redirect_message.release(); |
319 if (msg) | 520 if (msg) |
320 message_sender_->Send(msg); | 521 message_sender()->Send(msg); |
321 } | 522 } |
322 | 523 |
323 void ResourceDispatcher::OnRequestComplete( | 524 void ResourceDispatcher::OnRequestComplete( |
324 int request_id, | 525 int request_id, |
325 const ResourceMsg_RequestCompleteData& request_complete_data) { | 526 const ResourceMsg_RequestCompleteData& request_complete_data) { |
326 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete"); | 527 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete"); |
327 | 528 |
328 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 529 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
329 if (!request_info) | 530 if (!request_info) |
330 return; | 531 return; |
(...skipping 18 matching lines...) Expand all Loading... |
349 // Normally, dispatching this message causes the reference-counted request to | 550 // Normally, dispatching this message causes the reference-counted request to |
350 // die immediately. | 551 // die immediately. |
351 peer->OnCompletedRequest(request_complete_data.error_code, | 552 peer->OnCompletedRequest(request_complete_data.error_code, |
352 request_complete_data.was_ignored_by_handler, | 553 request_complete_data.was_ignored_by_handler, |
353 request_complete_data.exists_in_cache, | 554 request_complete_data.exists_in_cache, |
354 request_complete_data.security_info, | 555 request_complete_data.security_info, |
355 renderer_completion_time, | 556 renderer_completion_time, |
356 request_complete_data.encoded_data_length); | 557 request_complete_data.encoded_data_length); |
357 } | 558 } |
358 | 559 |
| 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 |
359 bool ResourceDispatcher::RemovePendingRequest(int request_id) { | 572 bool ResourceDispatcher::RemovePendingRequest(int request_id) { |
360 PendingRequestList::iterator it = pending_requests_.find(request_id); | 573 PendingRequestList::iterator it = pending_requests_.find(request_id); |
361 if (it == pending_requests_.end()) | 574 if (it == pending_requests_.end()) |
362 return false; | 575 return false; |
363 | 576 |
364 PendingRequestInfo& request_info = it->second; | 577 PendingRequestInfo& request_info = it->second; |
365 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue); | 578 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 | |
372 pending_requests_.erase(it); | 579 pending_requests_.erase(it); |
373 | 580 |
374 return true; | 581 return true; |
375 } | 582 } |
376 | 583 |
377 void ResourceDispatcher::Cancel(int request_id) { | 584 void ResourceDispatcher::CancelPendingRequest(int request_id) { |
378 PendingRequestList::iterator it = pending_requests_.find(request_id); | 585 PendingRequestList::iterator it = pending_requests_.find(request_id); |
379 if (it == pending_requests_.end()) { | 586 if (it == pending_requests_.end()) { |
380 DVLOG(1) << "unknown request"; | 587 DVLOG(1) << "unknown request"; |
381 return; | 588 return; |
382 } | 589 } |
383 | 590 |
384 // |request_id| will be removed from |pending_requests_| when | 591 // |request_id| will be removed from |pending_requests_| when |
385 // OnRequestComplete returns with ERR_ABORTED. | 592 // OnRequestComplete returns with ERR_ABORTED. |
386 message_sender_->Send(new ResourceHostMsg_CancelRequest(request_id)); | 593 message_sender()->Send(new ResourceHostMsg_CancelRequest(request_id)); |
387 } | 594 } |
388 | 595 |
389 void ResourceDispatcher::SetDefersLoading(int request_id, bool value) { | 596 void ResourceDispatcher::SetDefersLoading(int request_id, bool value) { |
390 PendingRequestList::iterator it = pending_requests_.find(request_id); | 597 PendingRequestList::iterator it = pending_requests_.find(request_id); |
391 if (it == pending_requests_.end()) { | 598 if (it == pending_requests_.end()) { |
392 DLOG(ERROR) << "unknown request"; | 599 DLOG(ERROR) << "unknown request"; |
393 return; | 600 return; |
394 } | 601 } |
395 PendingRequestInfo& request_info = it->second; | 602 PendingRequestInfo& request_info = it->second; |
396 if (value) { | 603 if (value) { |
397 request_info.is_deferred = value; | 604 request_info.is_deferred = value; |
398 } else if (request_info.is_deferred) { | 605 } else if (request_info.is_deferred) { |
399 request_info.is_deferred = false; | 606 request_info.is_deferred = false; |
400 | 607 |
401 FollowPendingRedirect(request_id, request_info); | 608 FollowPendingRedirect(request_id, request_info); |
402 | 609 |
403 base::MessageLoop::current()->PostTask( | 610 base::MessageLoop::current()->PostTask( |
404 FROM_HERE, | 611 FROM_HERE, |
405 base::Bind(&ResourceDispatcher::FlushDeferredMessages, | 612 base::Bind(&ResourceDispatcher::FlushDeferredMessages, |
406 weak_factory_.GetWeakPtr(), | 613 weak_factory_.GetWeakPtr(), |
407 request_id)); | 614 request_id)); |
408 } | 615 } |
409 } | 616 } |
410 | 617 |
411 void ResourceDispatcher::DidChangePriority(int request_id, | 618 void ResourceDispatcher::DidChangePriority( |
412 net::RequestPriority new_priority, | 619 int routing_id, int request_id, net::RequestPriority new_priority, |
413 int intra_priority_value) { | 620 int intra_priority_value) { |
414 DCHECK(ContainsKey(pending_requests_, request_id)); | 621 DCHECK(ContainsKey(pending_requests_, request_id)); |
415 message_sender_->Send(new ResourceHostMsg_DidChangePriority( | 622 message_sender()->Send(new ResourceHostMsg_DidChangePriority( |
416 request_id, new_priority, intra_priority_value)); | 623 request_id, new_priority, intra_priority_value)); |
417 } | 624 } |
418 | 625 |
419 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() | 626 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() |
420 : peer(NULL), | 627 : peer(NULL), |
421 resource_type(ResourceType::SUB_RESOURCE), | 628 resource_type(ResourceType::SUB_RESOURCE), |
422 is_deferred(false), | 629 is_deferred(false), |
423 download_to_file(false), | |
424 blocked_response(false), | 630 blocked_response(false), |
425 buffer_size(0) { | 631 buffer_size(0) { |
426 } | 632 } |
427 | 633 |
428 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( | 634 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( |
429 RequestPeer* peer, | 635 RequestPeer* peer, |
430 ResourceType::Type resource_type, | 636 ResourceType::Type resource_type, |
431 int origin_pid, | 637 int origin_pid, |
432 const GURL& frame_origin, | 638 const GURL& frame_origin, |
433 const GURL& request_url, | 639 const GURL& request_url) |
434 bool download_to_file) | |
435 : peer(peer), | 640 : peer(peer), |
436 resource_type(resource_type), | 641 resource_type(resource_type), |
437 origin_pid(origin_pid), | 642 origin_pid(origin_pid), |
438 is_deferred(false), | 643 is_deferred(false), |
439 url(request_url), | 644 url(request_url), |
440 frame_origin(frame_origin), | 645 frame_origin(frame_origin), |
441 response_url(request_url), | 646 response_url(request_url), |
442 download_to_file(download_to_file), | |
443 request_start(base::TimeTicks::Now()), | 647 request_start(base::TimeTicks::Now()), |
444 blocked_response(false) {} | 648 blocked_response(false) {} |
445 | 649 |
446 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} | 650 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} |
447 | 651 |
448 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { | 652 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { |
449 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) | 653 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) |
450 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) | 654 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) |
451 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) | 655 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) |
452 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, | 656 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, |
(...skipping 30 matching lines...) Expand all Loading... |
483 if (index != pending_requests_.end()) { | 687 if (index != pending_requests_.end()) { |
484 PendingRequestInfo& pending_request = index->second; | 688 PendingRequestInfo& pending_request = index->second; |
485 if (pending_request.is_deferred) { | 689 if (pending_request.is_deferred) { |
486 pending_request.deferred_message_queue.swap(q); | 690 pending_request.deferred_message_queue.swap(q); |
487 return; | 691 return; |
488 } | 692 } |
489 } | 693 } |
490 } | 694 } |
491 } | 695 } |
492 | 696 |
493 void ResourceDispatcher::StartSync(const RequestInfo& request_info, | 697 ResourceLoaderBridge* ResourceDispatcher::CreateBridge( |
494 ResourceRequestBody* request_body, | 698 const RequestInfo& request_info) { |
495 SyncLoadResponse* response) { | 699 return new IPCResourceLoaderBridge(this, request_info); |
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; | |
544 } | 700 } |
545 | 701 |
546 void ResourceDispatcher::ToResourceResponseInfo( | 702 void ResourceDispatcher::ToResourceResponseInfo( |
547 const PendingRequestInfo& request_info, | 703 const PendingRequestInfo& request_info, |
548 const ResourceResponseHead& browser_info, | 704 const ResourceResponseHead& browser_info, |
549 ResourceResponseInfo* renderer_info) const { | 705 ResourceResponseInfo* renderer_info) const { |
550 *renderer_info = browser_info; | 706 *renderer_info = browser_info; |
551 if (request_info.request_start.is_null() || | 707 if (request_info.request_start.is_null() || |
552 request_info.response_start.is_null() || | 708 request_info.response_start.is_null() || |
553 browser_info.request_start.is_null() || | 709 browser_info.request_start.is_null() || |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 // static | 804 // static |
649 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { | 805 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { |
650 while (!queue->empty()) { | 806 while (!queue->empty()) { |
651 IPC::Message* message = queue->front(); | 807 IPC::Message* message = queue->front(); |
652 ReleaseResourcesInDataMessage(*message); | 808 ReleaseResourcesInDataMessage(*message); |
653 queue->pop_front(); | 809 queue->pop_front(); |
654 delete message; | 810 delete message; |
655 } | 811 } |
656 } | 812 } |
657 | 813 |
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 | |
705 } // namespace content | 814 } // namespace content |
OLD | NEW |