OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_url_request_job.h" | 5 #include "content/browser/service_worker/service_worker_url_request_job.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/guid.h" | 12 #include "base/guid.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/time/time.h" | |
14 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" | 15 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" |
15 #include "content/browser/service_worker/service_worker_provider_host.h" | 16 #include "content/browser/service_worker/service_worker_provider_host.h" |
16 #include "content/common/resource_request_body.h" | 17 #include "content/common/resource_request_body.h" |
17 #include "content/common/service_worker/service_worker_types.h" | 18 #include "content/common/service_worker/service_worker_types.h" |
18 #include "content/public/browser/blob_handle.h" | 19 #include "content/public/browser/blob_handle.h" |
19 #include "content/public/browser/resource_request_info.h" | 20 #include "content/public/browser/resource_request_info.h" |
20 #include "content/public/common/page_transition_types.h" | 21 #include "content/public/common/page_transition_types.h" |
21 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
22 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
23 #include "net/http/http_response_info.h" | 24 #include "net/http/http_response_info.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 bool ServiceWorkerURLRequestJob::GetMimeType(std::string* mime_type) const { | 82 bool ServiceWorkerURLRequestJob::GetMimeType(std::string* mime_type) const { |
82 if (!http_info()) | 83 if (!http_info()) |
83 return false; | 84 return false; |
84 return http_info()->headers->GetMimeType(mime_type); | 85 return http_info()->headers->GetMimeType(mime_type); |
85 } | 86 } |
86 | 87 |
87 void ServiceWorkerURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { | 88 void ServiceWorkerURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
88 if (!http_info()) | 89 if (!http_info()) |
89 return; | 90 return; |
90 *info = *http_info(); | 91 *info = *http_info(); |
92 info->response_time = response_time_; | |
93 } | |
94 | |
95 void ServiceWorkerURLRequestJob::GetLoadTimingInfo( | |
96 net::LoadTimingInfo* load_timing_info) const { | |
97 *load_timing_info = load_timing_info_; | |
91 } | 98 } |
92 | 99 |
93 int ServiceWorkerURLRequestJob::GetResponseCode() const { | 100 int ServiceWorkerURLRequestJob::GetResponseCode() const { |
94 if (!http_info()) | 101 if (!http_info()) |
95 return -1; | 102 return -1; |
96 return http_info()->headers->response_code(); | 103 return http_info()->headers->response_code(); |
97 } | 104 } |
98 | 105 |
99 void ServiceWorkerURLRequestJob::SetExtraRequestHeaders( | 106 void ServiceWorkerURLRequestJob::SetExtraRequestHeaders( |
100 const net::HttpRequestHeaders& headers) { | 107 const net::HttpRequestHeaders& headers) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 } | 159 } |
153 | 160 |
154 void ServiceWorkerURLRequestJob::OnBeforeNetworkStart(net::URLRequest* request, | 161 void ServiceWorkerURLRequestJob::OnBeforeNetworkStart(net::URLRequest* request, |
155 bool* defer) { | 162 bool* defer) { |
156 NOTREACHED(); | 163 NOTREACHED(); |
157 } | 164 } |
158 | 165 |
159 void ServiceWorkerURLRequestJob::OnResponseStarted(net::URLRequest* request) { | 166 void ServiceWorkerURLRequestJob::OnResponseStarted(net::URLRequest* request) { |
160 // TODO(falken): Add Content-Length, Content-Type if they were not provided in | 167 // TODO(falken): Add Content-Length, Content-Type if they were not provided in |
161 // the ServiceWorkerResponse. | 168 // the ServiceWorkerResponse. |
169 response_time_ = base::Time::Now(); | |
162 CommitResponseHeader(); | 170 CommitResponseHeader(); |
163 } | 171 } |
164 | 172 |
165 void ServiceWorkerURLRequestJob::OnReadCompleted(net::URLRequest* request, | 173 void ServiceWorkerURLRequestJob::OnReadCompleted(net::URLRequest* request, |
166 int bytes_read) { | 174 int bytes_read) { |
167 SetStatus(request->status()); | 175 SetStatus(request->status()); |
168 if (!request->status().is_success()) { | 176 if (!request->status().is_success()) { |
169 NotifyDone(request->status()); | 177 NotifyDone(request->status()); |
170 return; | 178 return; |
171 } | 179 } |
172 NotifyReadComplete(bytes_read); | 180 NotifyReadComplete(bytes_read); |
173 if (bytes_read == 0) | 181 if (bytes_read == 0) |
174 NotifyDone(request->status()); | 182 NotifyDone(request->status()); |
175 } | 183 } |
176 | 184 |
177 const net::HttpResponseInfo* ServiceWorkerURLRequestJob::http_info() const { | 185 const net::HttpResponseInfo* ServiceWorkerURLRequestJob::http_info() const { |
mmenke
2014/09/04 14:53:51
Note: This should either be inlined in the header
| |
178 if (!http_response_info_) | 186 if (!http_response_info_) |
179 return NULL; | 187 return NULL; |
180 if (range_response_info_) | 188 if (range_response_info_) |
181 return range_response_info_.get(); | 189 return range_response_info_.get(); |
182 return http_response_info_.get(); | 190 return http_response_info_.get(); |
183 } | 191 } |
184 | 192 |
185 void ServiceWorkerURLRequestJob::GetExtraResponseInfo( | 193 void ServiceWorkerURLRequestJob::GetExtraResponseInfo( |
186 bool* was_fetched_via_service_worker, | 194 bool* was_fetched_via_service_worker, |
187 GURL* original_url_via_service_worker) const { | 195 GURL* original_url_via_service_worker, |
196 base::TimeTicks* fetch_start_time, | |
197 base::TimeTicks* fetch_ready_time, | |
198 base::TimeTicks* fetch_end_time) const { | |
188 if (response_type_ != FORWARD_TO_SERVICE_WORKER) { | 199 if (response_type_ != FORWARD_TO_SERVICE_WORKER) { |
189 *was_fetched_via_service_worker = false; | 200 *was_fetched_via_service_worker = false; |
190 *original_url_via_service_worker = GURL(); | 201 *original_url_via_service_worker = GURL(); |
191 return; | 202 return; |
192 } | 203 } |
193 *was_fetched_via_service_worker = true; | 204 *was_fetched_via_service_worker = true; |
194 *original_url_via_service_worker = response_url_; | 205 *original_url_via_service_worker = response_url_; |
206 *fetch_start_time = fetch_start_time_; | |
207 *fetch_ready_time = fetch_ready_time_; | |
208 *fetch_end_time = fetch_end_time_; | |
195 } | 209 } |
196 | 210 |
197 | 211 |
198 ServiceWorkerURLRequestJob::~ServiceWorkerURLRequestJob() { | 212 ServiceWorkerURLRequestJob::~ServiceWorkerURLRequestJob() { |
199 } | 213 } |
200 | 214 |
201 void ServiceWorkerURLRequestJob::MaybeStartRequest() { | 215 void ServiceWorkerURLRequestJob::MaybeStartRequest() { |
202 if (is_started_ && response_type_ != NOT_DETERMINED) { | 216 if (is_started_ && response_type_ != NOT_DETERMINED) { |
203 // Start asynchronously. | 217 // Start asynchronously. |
204 base::MessageLoop::current()->PostTask( | 218 base::MessageLoop::current()->PostTask( |
(...skipping 21 matching lines...) Expand all Loading... | |
226 DCHECK(!fetch_dispatcher_); | 240 DCHECK(!fetch_dispatcher_); |
227 // Send a fetch event to the ServiceWorker associated to the | 241 // Send a fetch event to the ServiceWorker associated to the |
228 // provider_host. | 242 // provider_host. |
229 fetch_dispatcher_.reset(new ServiceWorkerFetchDispatcher( | 243 fetch_dispatcher_.reset(new ServiceWorkerFetchDispatcher( |
230 CreateFetchRequest(), | 244 CreateFetchRequest(), |
231 provider_host_->active_version(), | 245 provider_host_->active_version(), |
232 base::Bind(&ServiceWorkerURLRequestJob::DidPrepareFetchEvent, | 246 base::Bind(&ServiceWorkerURLRequestJob::DidPrepareFetchEvent, |
233 weak_factory_.GetWeakPtr()), | 247 weak_factory_.GetWeakPtr()), |
234 base::Bind(&ServiceWorkerURLRequestJob::DidDispatchFetchEvent, | 248 base::Bind(&ServiceWorkerURLRequestJob::DidDispatchFetchEvent, |
235 weak_factory_.GetWeakPtr()))); | 249 weak_factory_.GetWeakPtr()))); |
250 fetch_start_time_ = base::TimeTicks::Now(); | |
236 fetch_dispatcher_->Run(); | 251 fetch_dispatcher_->Run(); |
237 return; | 252 return; |
238 } | 253 } |
239 | 254 |
240 NOTREACHED(); | 255 NOTREACHED(); |
241 } | 256 } |
242 | 257 |
243 scoped_ptr<ServiceWorkerFetchRequest> | 258 scoped_ptr<ServiceWorkerFetchRequest> |
244 ServiceWorkerURLRequestJob::CreateFetchRequest() { | 259 ServiceWorkerURLRequestJob::CreateFetchRequest() { |
245 std::string blob_uuid; | 260 std::string blob_uuid; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 } | 337 } |
323 | 338 |
324 request_body_blob_data_handle_ = | 339 request_body_blob_data_handle_ = |
325 blob_storage_context_->AddFinishedBlob(blob_data.get()); | 340 blob_storage_context_->AddFinishedBlob(blob_data.get()); |
326 *blob_uuid = uuid; | 341 *blob_uuid = uuid; |
327 *blob_size = total_size; | 342 *blob_size = total_size; |
328 return true; | 343 return true; |
329 } | 344 } |
330 | 345 |
331 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() { | 346 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() { |
332 // TODO(shimazu): Set the timestamp to measure the time to launch SW | 347 fetch_ready_time_ = base::TimeTicks::Now(); |
333 // This is related to this (http://crbug.com/401389) | |
334 } | 348 } |
335 | 349 |
336 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( | 350 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( |
337 ServiceWorkerStatusCode status, | 351 ServiceWorkerStatusCode status, |
338 ServiceWorkerFetchEventResult fetch_result, | 352 ServiceWorkerFetchEventResult fetch_result, |
339 const ServiceWorkerResponse& response) { | 353 const ServiceWorkerResponse& response) { |
340 fetch_dispatcher_.reset(); | 354 fetch_dispatcher_.reset(); |
341 | 355 |
342 // Check if we're not orphaned. | 356 // Check if we're not orphaned. |
343 if (!request()) | 357 if (!request()) |
(...skipping 13 matching lines...) Expand all Loading... | |
357 if (fetch_result == SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK) { | 371 if (fetch_result == SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK) { |
358 // Change the response type and restart the request to fallback to | 372 // Change the response type and restart the request to fallback to |
359 // the network. | 373 // the network. |
360 response_type_ = FALLBACK_TO_NETWORK; | 374 response_type_ = FALLBACK_TO_NETWORK; |
361 NotifyRestartRequired(); | 375 NotifyRestartRequired(); |
362 return; | 376 return; |
363 } | 377 } |
364 | 378 |
365 // We should have a response now. | 379 // We should have a response now. |
366 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result); | 380 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result); |
367 | 381 fetch_end_time_ = base::TimeTicks::Now(); |
368 // Set up a request for reading the blob. | 382 // Set up a request for reading the blob. |
369 if (!response.blob_uuid.empty() && blob_storage_context_) { | 383 if (!response.blob_uuid.empty() && blob_storage_context_) { |
370 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 384 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
371 blob_storage_context_->GetBlobDataFromUUID(response.blob_uuid); | 385 blob_storage_context_->GetBlobDataFromUUID(response.blob_uuid); |
372 if (!blob_data_handle) { | 386 if (!blob_data_handle) { |
373 // The renderer gave us a bad blob UUID. | 387 // The renderer gave us a bad blob UUID. |
374 DeliverErrorResponse(); | 388 DeliverErrorResponse(); |
375 return; | 389 return; |
376 } | 390 } |
377 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( | 391 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( |
378 blob_data_handle.Pass(), request()->context(), this); | 392 blob_data_handle.Pass(), request()->context(), this); |
379 blob_request_->Start(); | 393 blob_request_->Start(); |
380 } | 394 } |
381 | 395 |
382 response_url_ = response.url; | 396 response_url_ = response.url; |
383 CreateResponseHeader( | 397 CreateResponseHeader( |
384 response.status_code, response.status_text, response.headers); | 398 response.status_code, response.status_text, response.headers); |
399 load_timing_info_.receive_headers_end = base::TimeTicks::Now(); | |
mmenke
2014/09/04 14:53:51
Currently, either all of send_start/send_end/recei
shimazu
2014/09/05 04:18:48
Thanks, I didn't know the spec.
I set the send_sta
| |
385 if (!blob_request_) | 400 if (!blob_request_) |
386 CommitResponseHeader(); | 401 CommitResponseHeader(); |
387 } | 402 } |
388 | 403 |
389 void ServiceWorkerURLRequestJob::CreateResponseHeader( | 404 void ServiceWorkerURLRequestJob::CreateResponseHeader( |
390 int status_code, | 405 int status_code, |
391 const std::string& status_text, | 406 const std::string& status_text, |
392 const std::map<std::string, std::string>& headers) { | 407 const std::map<std::string, std::string>& headers) { |
393 // TODO(kinuko): If the response has an identifier to on-disk cache entry, | 408 // TODO(kinuko): If the response has an identifier to on-disk cache entry, |
394 // pull response header from the disk. | 409 // pull response header from the disk. |
(...skipping 22 matching lines...) Expand all Loading... | |
417 void ServiceWorkerURLRequestJob::DeliverErrorResponse() { | 432 void ServiceWorkerURLRequestJob::DeliverErrorResponse() { |
418 // TODO(falken): Print an error to the console of the ServiceWorker and of | 433 // TODO(falken): Print an error to the console of the ServiceWorker and of |
419 // the requesting page. | 434 // the requesting page. |
420 CreateResponseHeader(500, | 435 CreateResponseHeader(500, |
421 "Service Worker Response Error", | 436 "Service Worker Response Error", |
422 std::map<std::string, std::string>()); | 437 std::map<std::string, std::string>()); |
423 CommitResponseHeader(); | 438 CommitResponseHeader(); |
424 } | 439 } |
425 | 440 |
426 } // namespace content | 441 } // namespace content |
OLD | NEW |