| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_cache_writer.h" | 5 #include "content/browser/service_worker/service_worker_cache_writer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "content/browser/appcache/appcache_response.h" | 10 #include "content/browser/appcache/appcache_response.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 NOTREACHED() << "Unknown state in DoLoop"; | 108 NOTREACHED() << "Unknown state in DoLoop"; |
| 109 state_ = STATE_DONE; | 109 state_ = STATE_DONE; |
| 110 break; | 110 break; |
| 111 } | 111 } |
| 112 } while (status != net::ERR_IO_PENDING && state_ != STATE_DONE); | 112 } while (status != net::ERR_IO_PENDING && state_ != STATE_DONE); |
| 113 io_pending_ = (status == net::ERR_IO_PENDING); | 113 io_pending_ = (status == net::ERR_IO_PENDING); |
| 114 return status; | 114 return status; |
| 115 } | 115 } |
| 116 | 116 |
| 117 ServiceWorkerCacheWriter::ServiceWorkerCacheWriter( | 117 ServiceWorkerCacheWriter::ServiceWorkerCacheWriter( |
| 118 const ResponseReaderCreator& reader_creator, | 118 scoped_ptr<ServiceWorkerResponseReader> compare_reader, |
| 119 const ResponseWriterCreator& writer_creator) | 119 scoped_ptr<ServiceWorkerResponseReader> copy_reader, |
| 120 scoped_ptr<ServiceWorkerResponseWriter> writer) |
| 120 : state_(STATE_START), | 121 : state_(STATE_START), |
| 121 io_pending_(false), | 122 io_pending_(false), |
| 122 comparing_(false), | 123 comparing_(false), |
| 123 did_replace_(false), | 124 did_replace_(false), |
| 124 reader_creator_(reader_creator), | 125 compare_reader_(std::move(compare_reader)), |
| 125 writer_creator_(writer_creator), | 126 copy_reader_(std::move(copy_reader)), |
| 127 writer_(std::move(writer)), |
| 126 weak_factory_(this) {} | 128 weak_factory_(this) {} |
| 127 | 129 |
| 128 ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {} | 130 ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {} |
| 129 | 131 |
| 130 net::Error ServiceWorkerCacheWriter::MaybeWriteHeaders( | 132 net::Error ServiceWorkerCacheWriter::MaybeWriteHeaders( |
| 131 HttpResponseInfoIOBuffer* headers, | 133 HttpResponseInfoIOBuffer* headers, |
| 132 const OnWriteCompleteCallback& callback) { | 134 const OnWriteCompleteCallback& callback) { |
| 133 DCHECK(!io_pending_); | 135 DCHECK(!io_pending_); |
| 134 | 136 |
| 135 headers_to_write_ = headers; | 137 headers_to_write_ = headers; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 state_ == STATE_WRITE_DATA_FOR_COPY_DONE || | 191 state_ == STATE_WRITE_DATA_FOR_COPY_DONE || |
| 190 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE) | 192 state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE) |
| 191 << "Unexpected state: " << state_; | 193 << "Unexpected state: " << state_; |
| 192 } | 194 } |
| 193 | 195 |
| 194 return result >= 0 ? net::OK : static_cast<net::Error>(result); | 196 return result >= 0 ? net::OK : static_cast<net::Error>(result); |
| 195 } | 197 } |
| 196 | 198 |
| 197 int ServiceWorkerCacheWriter::DoStart(int result) { | 199 int ServiceWorkerCacheWriter::DoStart(int result) { |
| 198 bytes_written_ = 0; | 200 bytes_written_ = 0; |
| 199 compare_reader_ = reader_creator_.Run(); | 201 if (compare_reader_) { |
| 200 if (compare_reader_.get()) { | |
| 201 state_ = STATE_READ_HEADERS_FOR_COMPARE; | 202 state_ = STATE_READ_HEADERS_FOR_COMPARE; |
| 202 comparing_ = true; | 203 comparing_ = true; |
| 203 } else { | 204 } else { |
| 204 // No existing reader, just write the headers back directly. | 205 // No existing reader, just write the headers back directly. |
| 205 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH; | 206 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH; |
| 206 comparing_ = false; | 207 comparing_ = false; |
| 207 } | 208 } |
| 208 return net::OK; | 209 return net::OK; |
| 209 } | 210 } |
| 210 | 211 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 | 300 |
| 300 // bytes_compared_ only gets incremented when a full block is compared, to | 301 // bytes_compared_ only gets incremented when a full block is compared, to |
| 301 // avoid having to use only parts of the buffered network data. | 302 // avoid having to use only parts of the buffered network data. |
| 302 bytes_compared_ += result; | 303 bytes_compared_ += result; |
| 303 state_ = STATE_DONE; | 304 state_ = STATE_DONE; |
| 304 return net::OK; | 305 return net::OK; |
| 305 } | 306 } |
| 306 | 307 |
| 307 int ServiceWorkerCacheWriter::DoReadHeadersForCopy(int result) { | 308 int ServiceWorkerCacheWriter::DoReadHeadersForCopy(int result) { |
| 308 DCHECK_GE(result, 0); | 309 DCHECK_GE(result, 0); |
| 310 DCHECK(copy_reader_); |
| 309 bytes_copied_ = 0; | 311 bytes_copied_ = 0; |
| 310 copy_reader_ = reader_creator_.Run(); | |
| 311 headers_to_read_ = new HttpResponseInfoIOBuffer; | 312 headers_to_read_ = new HttpResponseInfoIOBuffer; |
| 312 data_to_copy_ = new net::IOBuffer(kCopyBufferSize); | 313 data_to_copy_ = new net::IOBuffer(kCopyBufferSize); |
| 313 state_ = STATE_READ_HEADERS_FOR_COPY_DONE; | 314 state_ = STATE_READ_HEADERS_FOR_COPY_DONE; |
| 314 return ReadInfoHelper(copy_reader_, headers_to_read_.get()); | 315 return ReadInfoHelper(copy_reader_, headers_to_read_.get()); |
| 315 } | 316 } |
| 316 | 317 |
| 317 int ServiceWorkerCacheWriter::DoReadHeadersForCopyDone(int result) { | 318 int ServiceWorkerCacheWriter::DoReadHeadersForCopyDone(int result) { |
| 318 if (result < 0) { | 319 if (result < 0) { |
| 319 state_ = STATE_DONE; | 320 state_ = STATE_DONE; |
| 320 return result; | 321 return result; |
| 321 } | 322 } |
| 322 state_ = STATE_WRITE_HEADERS_FOR_COPY; | 323 state_ = STATE_WRITE_HEADERS_FOR_COPY; |
| 323 return net::OK; | 324 return net::OK; |
| 324 } | 325 } |
| 325 | 326 |
| 326 // Write the just-read headers back to the cache. | 327 // Write the just-read headers back to the cache. |
| 327 // Note that this method must create |writer_|, since the only paths to this | 328 // Note that this *discards* the read headers and replaces them with the net |
| 328 // state never create a writer. | 329 // headers. |
| 329 // Also note that this *discards* the read headers and replaces them with the | |
| 330 // net headers. | |
| 331 int ServiceWorkerCacheWriter::DoWriteHeadersForCopy(int result) { | 330 int ServiceWorkerCacheWriter::DoWriteHeadersForCopy(int result) { |
| 332 DCHECK_GE(result, 0); | 331 DCHECK_GE(result, 0); |
| 333 DCHECK(!writer_); | 332 DCHECK(writer_); |
| 334 writer_ = writer_creator_.Run(); | |
| 335 state_ = STATE_WRITE_HEADERS_FOR_COPY_DONE; | 333 state_ = STATE_WRITE_HEADERS_FOR_COPY_DONE; |
| 336 return WriteInfoHelper(writer_, headers_to_write_.get()); | 334 return WriteInfoHelper(writer_, headers_to_write_.get()); |
| 337 } | 335 } |
| 338 | 336 |
| 339 int ServiceWorkerCacheWriter::DoWriteHeadersForCopyDone(int result) { | 337 int ServiceWorkerCacheWriter::DoWriteHeadersForCopyDone(int result) { |
| 340 if (result < 0) { | 338 if (result < 0) { |
| 341 state_ = STATE_DONE; | 339 state_ = STATE_DONE; |
| 342 return result; | 340 return result; |
| 343 } | 341 } |
| 344 state_ = STATE_READ_DATA_FOR_COPY; | 342 state_ = STATE_READ_DATA_FOR_COPY; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return result; | 379 return result; |
| 382 } | 380 } |
| 383 bytes_written_ += result; | 381 bytes_written_ += result; |
| 384 bytes_copied_ += result; | 382 bytes_copied_ += result; |
| 385 state_ = STATE_READ_DATA_FOR_COPY; | 383 state_ = STATE_READ_DATA_FOR_COPY; |
| 386 return result; | 384 return result; |
| 387 } | 385 } |
| 388 | 386 |
| 389 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthrough(int result) { | 387 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthrough(int result) { |
| 390 DCHECK_GE(result, 0); | 388 DCHECK_GE(result, 0); |
| 391 writer_ = writer_creator_.Run(); | 389 DCHECK(writer_); |
| 392 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE; | 390 state_ = STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE; |
| 393 return WriteInfoHelper(writer_, headers_to_write_.get()); | 391 return WriteInfoHelper(writer_, headers_to_write_.get()); |
| 394 } | 392 } |
| 395 | 393 |
| 396 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthroughDone(int result) { | 394 int ServiceWorkerCacheWriter::DoWriteHeadersForPassthroughDone(int result) { |
| 397 state_ = STATE_DONE; | 395 state_ = STATE_DONE; |
| 398 return result; | 396 return result; |
| 399 } | 397 } |
| 400 | 398 |
| 401 int ServiceWorkerCacheWriter::DoWriteDataForPassthrough(int result) { | 399 int ServiceWorkerCacheWriter::DoWriteDataForPassthrough(int result) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 if (result != net::ERR_IO_PENDING) { | 495 if (result != net::ERR_IO_PENDING) { |
| 498 OnWriteCompleteCallback callback = pending_callback_; | 496 OnWriteCompleteCallback callback = pending_callback_; |
| 499 pending_callback_.Reset(); | 497 pending_callback_.Reset(); |
| 500 net::Error error = result >= 0 ? net::OK : static_cast<net::Error>(result); | 498 net::Error error = result >= 0 ? net::OK : static_cast<net::Error>(result); |
| 501 io_pending_ = false; | 499 io_pending_ = false; |
| 502 callback.Run(error); | 500 callback.Run(error); |
| 503 } | 501 } |
| 504 } | 502 } |
| 505 | 503 |
| 506 } // namespace content | 504 } // namespace content |
| OLD | NEW |