| 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 { |
| 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(); |
| 251 load_timing_info_.send_start = fetch_start_time_; |
| 236 fetch_dispatcher_->Run(); | 252 fetch_dispatcher_->Run(); |
| 237 return; | 253 return; |
| 238 } | 254 } |
| 239 | 255 |
| 240 NOTREACHED(); | 256 NOTREACHED(); |
| 241 } | 257 } |
| 242 | 258 |
| 243 scoped_ptr<ServiceWorkerFetchRequest> | 259 scoped_ptr<ServiceWorkerFetchRequest> |
| 244 ServiceWorkerURLRequestJob::CreateFetchRequest() { | 260 ServiceWorkerURLRequestJob::CreateFetchRequest() { |
| 245 std::string blob_uuid; | 261 std::string blob_uuid; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } | 338 } |
| 323 | 339 |
| 324 request_body_blob_data_handle_ = | 340 request_body_blob_data_handle_ = |
| 325 blob_storage_context_->AddFinishedBlob(blob_data.get()); | 341 blob_storage_context_->AddFinishedBlob(blob_data.get()); |
| 326 *blob_uuid = uuid; | 342 *blob_uuid = uuid; |
| 327 *blob_size = total_size; | 343 *blob_size = total_size; |
| 328 return true; | 344 return true; |
| 329 } | 345 } |
| 330 | 346 |
| 331 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() { | 347 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() { |
| 332 // TODO(shimazu): Set the timestamp to measure the time to launch SW | 348 fetch_ready_time_ = base::TimeTicks::Now(); |
| 333 // This is related to this (http://crbug.com/401389) | |
| 334 } | 349 } |
| 335 | 350 |
| 336 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( | 351 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent( |
| 337 ServiceWorkerStatusCode status, | 352 ServiceWorkerStatusCode status, |
| 338 ServiceWorkerFetchEventResult fetch_result, | 353 ServiceWorkerFetchEventResult fetch_result, |
| 339 const ServiceWorkerResponse& response) { | 354 const ServiceWorkerResponse& response) { |
| 340 fetch_dispatcher_.reset(); | 355 fetch_dispatcher_.reset(); |
| 341 | 356 |
| 342 // Check if we're not orphaned. | 357 // Check if we're not orphaned. |
| 343 if (!request()) | 358 if (!request()) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 364 | 379 |
| 365 // We should have a response now. | 380 // We should have a response now. |
| 366 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result); | 381 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result); |
| 367 | 382 |
| 368 // Treat a response whose status is 0 as an error. | 383 // Treat a response whose status is 0 as an error. |
| 369 if (response.status_code == 0) { | 384 if (response.status_code == 0) { |
| 370 DeliverErrorResponse(); | 385 DeliverErrorResponse(); |
| 371 return; | 386 return; |
| 372 } | 387 } |
| 373 | 388 |
| 389 fetch_end_time_ = base::TimeTicks::Now(); |
| 390 load_timing_info_.send_end = fetch_end_time_; |
| 391 |
| 374 // Set up a request for reading the blob. | 392 // Set up a request for reading the blob. |
| 375 if (!response.blob_uuid.empty() && blob_storage_context_) { | 393 if (!response.blob_uuid.empty() && blob_storage_context_) { |
| 376 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 394 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
| 377 blob_storage_context_->GetBlobDataFromUUID(response.blob_uuid); | 395 blob_storage_context_->GetBlobDataFromUUID(response.blob_uuid); |
| 378 if (!blob_data_handle) { | 396 if (!blob_data_handle) { |
| 379 // The renderer gave us a bad blob UUID. | 397 // The renderer gave us a bad blob UUID. |
| 380 DeliverErrorResponse(); | 398 DeliverErrorResponse(); |
| 381 return; | 399 return; |
| 382 } | 400 } |
| 383 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( | 401 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( |
| 384 blob_data_handle.Pass(), request()->context(), this); | 402 blob_data_handle.Pass(), request()->context(), this); |
| 385 blob_request_->Start(); | 403 blob_request_->Start(); |
| 386 } | 404 } |
| 387 | 405 |
| 388 response_url_ = response.url; | 406 response_url_ = response.url; |
| 389 CreateResponseHeader( | 407 CreateResponseHeader( |
| 390 response.status_code, response.status_text, response.headers); | 408 response.status_code, response.status_text, response.headers); |
| 409 load_timing_info_.receive_headers_end = base::TimeTicks::Now(); |
| 391 if (!blob_request_) | 410 if (!blob_request_) |
| 392 CommitResponseHeader(); | 411 CommitResponseHeader(); |
| 393 } | 412 } |
| 394 | 413 |
| 395 void ServiceWorkerURLRequestJob::CreateResponseHeader( | 414 void ServiceWorkerURLRequestJob::CreateResponseHeader( |
| 396 int status_code, | 415 int status_code, |
| 397 const std::string& status_text, | 416 const std::string& status_text, |
| 398 const std::map<std::string, std::string>& headers) { | 417 const std::map<std::string, std::string>& headers) { |
| 399 // TODO(kinuko): If the response has an identifier to on-disk cache entry, | 418 // TODO(kinuko): If the response has an identifier to on-disk cache entry, |
| 400 // pull response header from the disk. | 419 // pull response header from the disk. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 423 void ServiceWorkerURLRequestJob::DeliverErrorResponse() { | 442 void ServiceWorkerURLRequestJob::DeliverErrorResponse() { |
| 424 // TODO(falken): Print an error to the console of the ServiceWorker and of | 443 // TODO(falken): Print an error to the console of the ServiceWorker and of |
| 425 // the requesting page. | 444 // the requesting page. |
| 426 CreateResponseHeader(500, | 445 CreateResponseHeader(500, |
| 427 "Service Worker Response Error", | 446 "Service Worker Response Error", |
| 428 std::map<std::string, std::string>()); | 447 std::map<std::string, std::string>()); |
| 429 CommitResponseHeader(); | 448 CommitResponseHeader(); |
| 430 } | 449 } |
| 431 | 450 |
| 432 } // namespace content | 451 } // namespace content |
| OLD | NEW |