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 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header) || | 196 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header) || |
197 !net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { | 197 !net::HttpUtil::ParseRangeHeader(range_header, &ranges)) { |
198 return; | 198 return; |
199 } | 199 } |
200 | 200 |
201 // We don't support multiple range requests in one single URL request. | 201 // We don't support multiple range requests in one single URL request. |
202 if (ranges.size() == 1U) | 202 if (ranges.size() == 1U) |
203 byte_range_ = ranges[0]; | 203 byte_range_ = ranges[0]; |
204 } | 204 } |
205 | 205 |
206 int ServiceWorkerURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | 206 bool ServiceWorkerURLRequestJob::ReadRawData(net::IOBuffer* buf, |
| 207 int buf_size, |
| 208 int* bytes_read) { |
207 DCHECK(buf); | 209 DCHECK(buf); |
208 DCHECK_GE(buf_size, 0); | 210 DCHECK_GE(buf_size, 0); |
| 211 DCHECK(bytes_read); |
209 DCHECK(waiting_stream_url_.is_empty()); | 212 DCHECK(waiting_stream_url_.is_empty()); |
210 | |
211 int bytes_read = 0; | |
212 | |
213 if (stream_.get()) { | 213 if (stream_.get()) { |
214 switch (stream_->ReadRawData(buf, buf_size, &bytes_read)) { | 214 switch (stream_->ReadRawData(buf, buf_size, bytes_read)) { |
215 case Stream::STREAM_HAS_DATA: | 215 case Stream::STREAM_HAS_DATA: |
216 DCHECK_GT(bytes_read, 0); | 216 DCHECK_GT(*bytes_read, 0); |
217 return bytes_read; | 217 return true; |
218 case Stream::STREAM_COMPLETE: | 218 case Stream::STREAM_COMPLETE: |
219 DCHECK_EQ(0, bytes_read); | 219 DCHECK(!*bytes_read); |
220 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_STREAM_RESPONSE); | 220 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_STREAM_RESPONSE); |
221 return 0; | 221 return true; |
222 case Stream::STREAM_EMPTY: | 222 case Stream::STREAM_EMPTY: |
223 stream_pending_buffer_ = buf; | 223 stream_pending_buffer_ = buf; |
224 stream_pending_buffer_size_ = buf_size; | 224 stream_pending_buffer_size_ = buf_size; |
225 return net::ERR_IO_PENDING; | 225 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 226 return false; |
226 case Stream::STREAM_ABORTED: | 227 case Stream::STREAM_ABORTED: |
227 // Handle this as connection reset. | 228 // Handle this as connection reset. |
228 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_STREAM_ABORTED); | 229 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_STREAM_ABORTED); |
229 return net::ERR_CONNECTION_RESET; | 230 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 231 net::ERR_CONNECTION_RESET)); |
| 232 return false; |
230 } | 233 } |
231 NOTREACHED(); | 234 NOTREACHED(); |
232 return net::ERR_FAILED; | 235 return false; |
233 } | 236 } |
234 | 237 |
235 if (!blob_request_) | 238 if (!blob_request_) { |
236 return 0; | 239 *bytes_read = 0; |
237 blob_request_->Read(buf, buf_size, &bytes_read); | 240 return true; |
| 241 } |
| 242 blob_request_->Read(buf, buf_size, bytes_read); |
238 net::URLRequestStatus status = blob_request_->status(); | 243 net::URLRequestStatus status = blob_request_->status(); |
239 if (status.status() != net::URLRequestStatus::SUCCESS) | 244 SetStatus(status); |
240 return status.error(); | 245 if (status.is_io_pending()) |
241 if (bytes_read == 0) | 246 return false; |
| 247 if (status.is_success() && *bytes_read == 0) |
242 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_BLOB_RESPONSE); | 248 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_BLOB_RESPONSE); |
243 return bytes_read; | 249 return status.is_success(); |
244 } | 250 } |
245 | 251 |
246 // TODO(falken): Refactor Blob and Stream specific handling to separate classes. | 252 // TODO(falken): Refactor Blob and Stream specific handling to separate classes. |
247 // Overrides for Blob reading ------------------------------------------------- | 253 // Overrides for Blob reading ------------------------------------------------- |
248 | 254 |
249 void ServiceWorkerURLRequestJob::OnReceivedRedirect( | 255 void ServiceWorkerURLRequestJob::OnReceivedRedirect( |
250 net::URLRequest* request, | 256 net::URLRequest* request, |
251 const net::RedirectInfo& redirect_info, | 257 const net::RedirectInfo& redirect_info, |
252 bool* defer_redirect) { | 258 bool* defer_redirect) { |
253 NOTREACHED(); | 259 NOTREACHED(); |
(...skipping 25 matching lines...) Expand all Loading... |
279 | 285 |
280 void ServiceWorkerURLRequestJob::OnResponseStarted(net::URLRequest* request) { | 286 void ServiceWorkerURLRequestJob::OnResponseStarted(net::URLRequest* request) { |
281 // TODO(falken): Add Content-Length, Content-Type if they were not provided in | 287 // TODO(falken): Add Content-Length, Content-Type if they were not provided in |
282 // the ServiceWorkerResponse. | 288 // the ServiceWorkerResponse. |
283 response_time_ = base::Time::Now(); | 289 response_time_ = base::Time::Now(); |
284 CommitResponseHeader(); | 290 CommitResponseHeader(); |
285 } | 291 } |
286 | 292 |
287 void ServiceWorkerURLRequestJob::OnReadCompleted(net::URLRequest* request, | 293 void ServiceWorkerURLRequestJob::OnReadCompleted(net::URLRequest* request, |
288 int bytes_read) { | 294 int bytes_read) { |
| 295 SetStatus(request->status()); |
289 if (!request->status().is_success()) { | 296 if (!request->status().is_success()) { |
290 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_BLOB_READ); | 297 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_BLOB_READ); |
291 } else if (bytes_read == 0) { | 298 NotifyDone(request->status()); |
| 299 return; |
| 300 } |
| 301 |
| 302 if (bytes_read == 0) { |
| 303 // Protect because NotifyReadComplete() can destroy |this|. |
| 304 scoped_refptr<ServiceWorkerURLRequestJob> protect(this); |
292 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_BLOB_RESPONSE); | 305 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_BLOB_RESPONSE); |
| 306 NotifyReadComplete(bytes_read); |
| 307 NotifyDone(request->status()); |
| 308 return; |
293 } | 309 } |
294 net::URLRequestStatus status = request->status(); | 310 NotifyReadComplete(bytes_read); |
295 ReadRawDataComplete(status.is_success() ? bytes_read : status.error()); | |
296 } | 311 } |
297 | 312 |
298 // Overrides for Stream reading ----------------------------------------------- | 313 // Overrides for Stream reading ----------------------------------------------- |
299 | 314 |
300 void ServiceWorkerURLRequestJob::OnDataAvailable(Stream* stream) { | 315 void ServiceWorkerURLRequestJob::OnDataAvailable(Stream* stream) { |
| 316 // Clear the IO_PENDING status. |
| 317 SetStatus(net::URLRequestStatus()); |
301 // Do nothing if stream_pending_buffer_ is empty, i.e. there's no ReadRawData | 318 // Do nothing if stream_pending_buffer_ is empty, i.e. there's no ReadRawData |
302 // operation waiting for IO completion. | 319 // operation waiting for IO completion. |
303 if (!stream_pending_buffer_.get()) | 320 if (!stream_pending_buffer_.get()) |
304 return; | 321 return; |
305 | 322 |
306 // stream_pending_buffer_ is set to the IOBuffer instance provided to | 323 // stream_pending_buffer_ is set to the IOBuffer instance provided to |
307 // ReadRawData() by URLRequestJob. | 324 // ReadRawData() by URLRequestJob. |
308 | 325 |
309 int result = 0; | 326 int bytes_read = 0; |
310 switch (stream_->ReadRawData(stream_pending_buffer_.get(), | 327 switch (stream_->ReadRawData(stream_pending_buffer_.get(), |
311 stream_pending_buffer_size_, &result)) { | 328 stream_pending_buffer_size_, &bytes_read)) { |
312 case Stream::STREAM_HAS_DATA: | 329 case Stream::STREAM_HAS_DATA: |
313 DCHECK_GT(result, 0); | 330 DCHECK_GT(bytes_read, 0); |
314 break; | 331 break; |
315 case Stream::STREAM_COMPLETE: | 332 case Stream::STREAM_COMPLETE: |
316 // Calling NotifyReadComplete with 0 signals completion. | 333 // Calling NotifyReadComplete with 0 signals completion. |
317 DCHECK(!result); | 334 DCHECK(!bytes_read); |
318 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_STREAM_RESPONSE); | 335 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_STREAM_RESPONSE); |
319 break; | 336 break; |
320 case Stream::STREAM_EMPTY: | 337 case Stream::STREAM_EMPTY: |
321 NOTREACHED(); | 338 NOTREACHED(); |
322 break; | 339 break; |
323 case Stream::STREAM_ABORTED: | 340 case Stream::STREAM_ABORTED: |
324 // Handle this as connection reset. | 341 // Handle this as connection reset. |
325 result = net::ERR_CONNECTION_RESET; | |
326 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_STREAM_ABORTED); | 342 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_STREAM_ABORTED); |
| 343 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 344 net::ERR_CONNECTION_RESET)); |
327 break; | 345 break; |
328 } | 346 } |
329 | 347 |
330 // Clear the buffers before notifying the read is complete, so that it is | 348 // Clear the buffers before notifying the read is complete, so that it is |
331 // safe for the observer to read. | 349 // safe for the observer to read. |
332 stream_pending_buffer_ = nullptr; | 350 stream_pending_buffer_ = nullptr; |
333 stream_pending_buffer_size_ = 0; | 351 stream_pending_buffer_size_ = 0; |
334 ReadRawDataComplete(result); | 352 NotifyReadComplete(bytes_read); |
335 } | 353 } |
336 | 354 |
337 void ServiceWorkerURLRequestJob::OnStreamRegistered(Stream* stream) { | 355 void ServiceWorkerURLRequestJob::OnStreamRegistered(Stream* stream) { |
338 StreamContext* stream_context = | 356 StreamContext* stream_context = |
339 GetStreamContextForResourceContext(resource_context_); | 357 GetStreamContextForResourceContext(resource_context_); |
340 stream_context->registry()->RemoveRegisterObserver(waiting_stream_url_); | 358 stream_context->registry()->RemoveRegisterObserver(waiting_stream_url_); |
341 waiting_stream_url_ = GURL(); | 359 waiting_stream_url_ = GURL(); |
342 stream_ = stream; | 360 stream_ = stream; |
343 stream_->SetReadObserver(this); | 361 stream_->SetReadObserver(this); |
344 CommitResponseHeader(); | 362 CommitResponseHeader(); |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 return; | 638 return; |
621 } | 639 } |
622 | 640 |
623 // We should have a response now. | 641 // We should have a response now. |
624 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result); | 642 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result); |
625 | 643 |
626 // A response with status code 0 is Blink telling us to respond with network | 644 // A response with status code 0 is Blink telling us to respond with network |
627 // error. | 645 // error. |
628 if (response.status_code == 0) { | 646 if (response.status_code == 0) { |
629 RecordStatusZeroResponseError(response.error); | 647 RecordStatusZeroResponseError(response.error); |
630 NotifyStartError( | 648 NotifyDone( |
631 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); | 649 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); |
632 return; | 650 return; |
633 } | 651 } |
634 | 652 |
635 load_timing_info_.send_end = base::TimeTicks::Now(); | 653 load_timing_info_.send_end = base::TimeTicks::Now(); |
636 | 654 |
637 // Creates a new HttpResponseInfo using the the ServiceWorker script's | 655 // Creates a new HttpResponseInfo using the the ServiceWorker script's |
638 // HttpResponseInfo to show HTTPS padlock. | 656 // HttpResponseInfo to show HTTPS padlock. |
639 // TODO(horo): When we support mixed-content (HTTP) no-cors requests from a | 657 // TODO(horo): When we support mixed-content (HTTP) no-cors requests from a |
640 // ServiceWorker, we have to check the security level of the responses. | 658 // ServiceWorker, we have to check the security level of the responses. |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 } | 813 } |
796 if (!waiting_stream_url_.is_empty()) { | 814 if (!waiting_stream_url_.is_empty()) { |
797 StreamRegistry* stream_registry = | 815 StreamRegistry* stream_registry = |
798 GetStreamContextForResourceContext(resource_context_)->registry(); | 816 GetStreamContextForResourceContext(resource_context_)->registry(); |
799 stream_registry->RemoveRegisterObserver(waiting_stream_url_); | 817 stream_registry->RemoveRegisterObserver(waiting_stream_url_); |
800 stream_registry->AbortPendingStream(waiting_stream_url_); | 818 stream_registry->AbortPendingStream(waiting_stream_url_); |
801 } | 819 } |
802 } | 820 } |
803 | 821 |
804 } // namespace content | 822 } // namespace content |
OLD | NEW |