Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Unified Diff: content/browser/service_worker/service_worker_cache_writer.cc

Issue 1315443003: ServiceWorkerWriteToCacheJob: refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Annotate virtual methods Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_cache_writer.cc
diff --git a/content/browser/service_worker/service_worker_cache_writer.cc b/content/browser/service_worker/service_worker_cache_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0e517a76dcc4c1797978b46d934357d0c516a629
--- /dev/null
+++ b/content/browser/service_worker/service_worker_cache_writer.cc
@@ -0,0 +1,587 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/service_worker/service_worker_cache_writer.h"
+
+#include <algorithm>
+#include <string>
+
+#include "content/browser/appcache/appcache_response.h"
+#include "content/browser/service_worker/service_worker_disk_cache.h"
+#include "content/browser/service_worker/service_worker_storage.h"
+
+namespace {
+
+const size_t kCopyBufferSize = 16 * 1024;
+
+// States for the state machine.
+//
+// The state machine flows roughly like this: if there is no existing cache
+// entry, incoming headers and data are written directly back to the cache
+// ("passthrough mode", the PASSTHROUGH states). If there is an existing cache
+// entry, incoming headers and data are compared to the existing cache entry
+// ("compare mode", the COMPARE states); if at any point the incoming
+// headers/data are not equal to the cached headers/data, this class copies the
+// cached data up to the point where the incoming data and the cached data
+// diverged ("copy mode", the COPY states), then switches to "passthrough mode"
+// to write the remainder of the incoming data. The overall effect is to avoid
+// rewriting the cache entry if the incoming data is identical to the cached
+// data.
Randy Smith (Not in Mondays) 2015/09/13 20:56:36 This is an awesome explanatory comment, with a gol
Elly Fong-Jones 2015/09/15 15:08:24 Acknowledged.
+enum {
+ STATE_START,
+ // Control flows linearly through these four states, then loops from
+ // READ_DATA_FOR_COMPARE_DONE to READ_DATA_FOR_COMPARE, or exits to
+ // READ_HEADERS_FOR_COPY.
+ STATE_READ_HEADERS_FOR_COMPARE,
+ STATE_READ_HEADERS_FOR_COMPARE_DONE,
+ STATE_READ_DATA_FOR_COMPARE,
+ STATE_READ_DATA_FOR_COMPARE_DONE,
+
+ // Control flows linearly through these states, with each pass from
+ // READ_DATA_FOR_COPY to WRITE_DATA_FOR_COPY_DONE copying one block of data
+ // at a time. Control loops from WRITE_DATA_FOR_COPY_DONE back to
+ // READ_DATA_FOR_COPY if there is more data to copy, or exits to
+ // WRITE_DATA_FOR_PASSTHROUGH.
+ STATE_READ_HEADERS_FOR_COPY,
+ STATE_READ_HEADERS_FOR_COPY_DONE,
+ STATE_WRITE_HEADERS_FOR_COPY,
+ STATE_WRITE_HEADERS_FOR_COPY_DONE,
+ STATE_READ_DATA_FOR_COPY,
+ STATE_READ_DATA_FOR_COPY_DONE,
+ STATE_WRITE_DATA_FOR_COPY,
+ STATE_WRITE_DATA_FOR_COPY_DONE,
+
+ // Control flows linearly through these states, with a loop between
+ // WRITE_DATA_FOR_PASSTHROUGH and WRITE_DATA_FOR_PASSTHROUGH_DONE.
+ STATE_WRITE_HEADERS_FOR_PASSTHROUGH,
+ STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE,
+ STATE_DONE,
+};
+
+// Shim class used to turn always-async functions into async-or-result
+// functions. See the comments below near ReadInfoHelper.
+class AsyncOnlyCompletionCallbackAdaptor
+ : public base::RefCounted<AsyncOnlyCompletionCallbackAdaptor> {
+ public:
+ explicit AsyncOnlyCompletionCallbackAdaptor(
+ const net::CompletionCallback& callback)
+ : async_(false), result_(net::ERR_IO_PENDING), callback_(callback) {}
+
+ void set_async(bool async) { async_ = async; }
+ bool async() { return async_; }
+ int result() { return result_; }
+
+ void WrappedCallback(int result) {
+ result_ = result;
+ if (async_)
+ callback_.Run(result);
+ }
+
+ private:
+ friend class base::RefCounted<AsyncOnlyCompletionCallbackAdaptor>;
+ virtual ~AsyncOnlyCompletionCallbackAdaptor() {}
+
+ bool async_;
+ int result_;
+ net::CompletionCallback callback_;
+};
+
+} // namespace
+
+namespace content {
+
+int ServiceWorkerCacheWriter::DoLoop(int status) {
+ bool pause = false;
+ do {
+ int next_state = -1;
+ switch (state_) {
+ case STATE_START:
+ status = DoStart(&next_state, &pause, status);
+ break;
+ case STATE_READ_HEADERS_FOR_COMPARE:
+ status = DoReadHeadersForCompare(&next_state, &pause, status);
+ break;
+ case STATE_READ_HEADERS_FOR_COMPARE_DONE:
+ status = DoReadHeadersForCompareDone(&next_state, &pause, status);
+ break;
+ case STATE_READ_DATA_FOR_COMPARE:
+ status = DoReadDataForCompare(&next_state, &pause, status);
+ break;
+ case STATE_READ_DATA_FOR_COMPARE_DONE:
+ status = DoReadDataForCompareDone(&next_state, &pause, status);
+ break;
+ case STATE_READ_HEADERS_FOR_COPY:
+ status = DoReadHeadersForCopy(&next_state, &pause, status);
+ break;
+ case STATE_READ_HEADERS_FOR_COPY_DONE:
+ status = DoReadHeadersForCopyDone(&next_state, &pause, status);
+ break;
+ case STATE_READ_DATA_FOR_COPY:
+ status = DoReadDataForCopy(&next_state, &pause, status);
+ break;
+ case STATE_READ_DATA_FOR_COPY_DONE:
+ status = DoReadDataForCopyDone(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_HEADERS_FOR_PASSTHROUGH:
+ status = DoWriteHeadersForPassthrough(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE:
+ status = DoWriteHeadersForPassthroughDone(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_DATA_FOR_PASSTHROUGH:
+ status = DoWriteDataForPassthrough(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE:
+ status = DoWriteDataForPassthroughDone(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_HEADERS_FOR_COPY:
+ status = DoWriteHeadersForCopy(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_HEADERS_FOR_COPY_DONE:
+ status = DoWriteHeadersForCopyDone(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_DATA_FOR_COPY:
+ status = DoWriteDataForCopy(&next_state, &pause, status);
+ break;
+ case STATE_WRITE_DATA_FOR_COPY_DONE:
+ status = DoWriteDataForCopyDone(&next_state, &pause, status);
+ break;
+ case STATE_DONE:
+ status = DoDone(&next_state, &pause, status);
+ break;
+ default:
+ next_state = STATE_DONE;
+ break;
+ }
+ state_ = next_state;
+ } while (status >= net::OK && state_ != STATE_DONE && !pause);
+ return status;
+}
+
+ServiceWorkerCacheWriter::ServiceWorkerCacheWriter(
+ const ResponseReaderCreator& reader_creator,
+ const ResponseWriterCreator& writer_creator)
+ : state_(STATE_START),
+ did_replace_(false),
+ reader_creator_(reader_creator),
+ writer_creator_(writer_creator),
+ weak_factory_(this) {}
+
+ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {}
+
+net::Error ServiceWorkerCacheWriter::MaybeWriteHeaders(
+ HttpResponseInfoIOBuffer* headers,
+ const OnWriteCompleteCallback& callback) {
+ headers_to_write_ = headers;
+ pending_callback_ = callback;
+ DCHECK_EQ(STATE_START, state_);
+ int result = DoLoop(net::OK);
+
+ // Ensure that the state machine is in one of the expected pause states.
+ // Synchronous errors always go to ERR_IO_PENDING.
+ if (result < 0 && result != net::ERR_IO_PENDING)
+ DCHECK_EQ(STATE_DONE, state_);
+
+ // Synchronous successes either go into passthrough mode or compare/copy mode.
+ if (result >= 0) {
+ DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE ||
+ state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH)
+ << "Unexpected state: " << state_;
+ }
+
+ // ERR_IO_PENDING has to have one of the "done" states as the next state.
+ if (result == net::ERR_IO_PENDING) {
+ DCHECK(state_ == STATE_READ_HEADERS_FOR_COMPARE_DONE ||
+ state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE ||
+ state_ == STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE)
+ << "Unexpected state: " << state_;
+ }
+
+ return result >= 0 ? net::OK : static_cast<net::Error>(result);
+}
+
+net::Error ServiceWorkerCacheWriter::MaybeWriteData(
+ net::IOBuffer* buf,
+ size_t buf_size,
+ const OnWriteCompleteCallback& callback) {
+ data_to_write_ = buf;
+ len_to_write_ = buf_size;
+ pending_callback_ = callback;
+ int result = DoLoop(net::OK);
+
+ // Check that the state machine is in one of the expected pause states.
+
+ // Synchronous errors are always STATE_DONE.
+ if (result < 0 && result != net::ERR_IO_PENDING)
+ DCHECK_EQ(STATE_DONE, state_);
+
+ // Synchronous successes mean that this object is immediately ready for a new
+ // call to MaybeWriteData, so it must be waiting in one of the ReadData
+ // states, or writing data directly in passthrough mode:
+ if (result >= 0) {
+ DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE ||
+ state_ == STATE_READ_DATA_FOR_COPY ||
+ state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH)
+ << "Unexpected state: " << state_;
+ }
+
+ // Asynchronous completion means the state machine must be waiting in one of
+ // the Done states for an IO operation to complete:
+ if (result == net::ERR_IO_PENDING) {
+ // Note that STATE_READ_HEADERS_FOR_COMPARE_DONE is excluded because the
+ // headers are compared in MaybeWriteHeaders, not here, and
+ // STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE is excluded because that write
+ // is done by MaybeWriteHeaders.
+ DCHECK(state_ == STATE_READ_DATA_FOR_COMPARE_DONE ||
+ state_ == STATE_READ_HEADERS_FOR_COPY_DONE ||
+ state_ == STATE_READ_DATA_FOR_COPY_DONE ||
+ state_ == STATE_WRITE_HEADERS_FOR_COPY_DONE ||
+ state_ == STATE_WRITE_DATA_FOR_COPY_DONE ||
+ state_ == STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE)
+ << "Unexpected state: " << state_;
+ }
+
+ return result >= 0 ? net::OK : static_cast<net::Error>(result);
+}
+
+int ServiceWorkerCacheWriter::DoStart(int* next_state,
+ bool* pause,
+ int result) {
+ bytes_written_ = 0;
+ compare_reader_ = reader_creator_.Run();
+ if (compare_reader_.get()) {
+ *next_state = STATE_READ_HEADERS_FOR_COMPARE;
+ } else {
+ // No existing reader, just write the headers back directly.
+ *next_state = STATE_WRITE_HEADERS_FOR_PASSTHROUGH;
+ }
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::DoReadHeadersForCompare(int* next_state,
+ bool* pause,
+ int result) {
+ DCHECK(headers_to_write_);
+
+ headers_to_read_ = new HttpResponseInfoIOBuffer;
+ *next_state = STATE_READ_HEADERS_FOR_COMPARE_DONE;
+ return ReadInfoHelper(compare_reader_, headers_to_read_.get());
+}
+
+int ServiceWorkerCacheWriter::DoReadHeadersForCompareDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+ cached_length_ = headers_to_read_->response_data_size;
+ bytes_compared_ = 0;
+ *pause = true;
+ *next_state = STATE_READ_DATA_FOR_COMPARE;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::DoReadDataForCompare(int* next_state,
+ bool* pause,
+ int result) {
+ DCHECK(data_to_write_);
+
+ data_to_read_ = new net::IOBuffer(len_to_write_);
+ len_to_read_ = len_to_write_;
+ *next_state = STATE_READ_DATA_FOR_COMPARE_DONE;
+ compare_offset_ = 0;
+ // If this was an EOF, don't issue a read.
+ if (len_to_write_ > 0)
+ result = ReadDataHelper(compare_reader_, data_to_read_.get(), len_to_read_);
+ return result;
+}
+
+int ServiceWorkerCacheWriter::DoReadDataForCompareDone(int* next_state,
+ bool* pause,
+ int result) {
+ DCHECK(data_to_read_);
+ DCHECK(data_to_write_);
+ DCHECK_EQ(len_to_read_, len_to_write_);
+ DCHECK_LE(result + compare_offset_, static_cast<size_t>(len_to_write_));
+
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+
+ // Premature EOF while reading the service worker script cache data to
+ // compare. Fail the comparison.
+ if (result == 0 && len_to_write_ != 0) {
+ *next_state = STATE_READ_HEADERS_FOR_COPY;
+ return net::OK;
+ }
+
+ // Compare the data from the ServiceWorker script cache to the data from the
+ // network.
+ if (memcmp(data_to_read_->data(), data_to_write_->data() + compare_offset_,
+ result)) {
+ // Data mismatched. This method already validated that all the bytes through
+ // |bytes_compared_| were identical, so copy the first |bytes_compared_|
+ // over, then start writing network data back after the changed point.
+ //
+ // Note that the state machine does NOT get paused here, since there is
+ // still further work to do before this object is ready to handle another
+ // WriteData call.
+ *next_state = STATE_READ_HEADERS_FOR_COPY;
+ return net::OK;
+ }
+
+ compare_offset_ += result;
+
+ // This is a little bit tricky. It is possible that not enough data was read
+ // to finish comparing the entire block of data from the network (which is
+ // kept in len_to_write_), so this method may need to issue another read and
+ // return to this state.
+ //
+ // Compare isn't complete yet. Issue another read for the remaining data. Note
+ // that this reuses the same IOBuffer.
+ if (compare_offset_ < static_cast<size_t>(len_to_read_)) {
+ *next_state = STATE_READ_DATA_FOR_COMPARE_DONE;
+ return ReadDataHelper(compare_reader_, data_to_read_.get(),
+ len_to_read_ - compare_offset_);
+ }
+
+ // Cached entry is longer than the network entry but the prefix matches. Copy
+ // just the prefix.
+ if (len_to_read_ == 0 && bytes_compared_ + compare_offset_ < cached_length_) {
+ *next_state = STATE_READ_HEADERS_FOR_COPY;
+ return net::OK;
+ }
+
+ // bytes_compared_ only gets incremented when a full block is compared, to
+ // avoid having to use only parts of the buffered network data.
+ bytes_compared_ += result;
+ *next_state = STATE_READ_DATA_FOR_COMPARE;
+ *pause = true;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::DoReadHeadersForCopy(int* next_state,
+ bool* pause,
+ int result) {
+ bytes_copied_ = 0;
+ copy_reader_ = reader_creator_.Run();
+ headers_to_read_ = new HttpResponseInfoIOBuffer;
+ data_to_copy_ = new net::IOBuffer(kCopyBufferSize);
+ *next_state = STATE_READ_HEADERS_FOR_COPY_DONE;
+ return ReadInfoHelper(copy_reader_, headers_to_read_.get());
+}
+
+int ServiceWorkerCacheWriter::DoReadHeadersForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+ *next_state = STATE_WRITE_HEADERS_FOR_COPY;
+ return net::OK;
+}
+
+// Write the just-read headers back to the cache.
+// Note that this method must create |writer_|, since the only paths to this
+// state never create a writer.
+// Also note that this *discards* the read headers and replaces them with the
+// net headers.
+int ServiceWorkerCacheWriter::DoWriteHeadersForCopy(int* next_state,
+ bool* pause,
+ int result) {
+ DCHECK(!writer_);
+ writer_ = writer_creator_.Run();
+ *next_state = STATE_WRITE_HEADERS_FOR_COPY_DONE;
+ return WriteInfoHelper(writer_, headers_to_write_.get());
+}
+
+int ServiceWorkerCacheWriter::DoWriteHeadersForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+ *next_state = STATE_READ_DATA_FOR_COPY;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::DoReadDataForCopy(int* next_state,
+ bool* pause,
+ int result) {
+ size_t to_read = std::min(kCopyBufferSize, bytes_compared_ - bytes_copied_);
+ // At this point, all compared bytes have been read. Currently
+ // |data_to_write_| and |len_to_write_| hold the chunk of network input that
+ // caused the comparison failure, so those need to be written back and this
+ // object needs to go into passthrough mode.
+ if (to_read == 0) {
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
+ return net::OK;
+ }
+ *next_state = STATE_READ_DATA_FOR_COPY_DONE;
+ return ReadDataHelper(copy_reader_, data_to_copy_.get(), to_read);
+}
+
+int ServiceWorkerCacheWriter::DoReadDataForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+ *next_state = STATE_WRITE_DATA_FOR_COPY;
+ return result;
+}
+
+int ServiceWorkerCacheWriter::DoWriteDataForCopy(int* next_state,
+ bool* pause,
+ int result) {
+ *next_state = STATE_WRITE_DATA_FOR_COPY_DONE;
+ DCHECK_GT(result, 0);
+ return WriteDataHelper(writer_, data_to_copy_.get(), result);
+}
+
+int ServiceWorkerCacheWriter::DoWriteDataForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+ bytes_written_ += result;
+ bytes_copied_ += result;
+ *next_state = STATE_READ_DATA_FOR_COPY;
+ return result;
+}
+
+int ServiceWorkerCacheWriter::DoWriteHeadersForPassthrough(int* next_state,
+ bool* pause,
+ int result) {
+ writer_ = writer_creator_.Run();
+ *next_state = STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE;
+ return WriteInfoHelper(writer_, headers_to_write_.get());
+}
+
+int ServiceWorkerCacheWriter::DoWriteHeadersForPassthroughDone(int* next_state,
+ bool* pause,
+ int result) {
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
+ *pause = true;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::DoWriteDataForPassthrough(int* next_state,
+ bool* pause,
+ int result) {
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE;
+ if (len_to_write_ > 0)
+ result = WriteDataHelper(writer_, data_to_write_.get(), len_to_write_);
+ return result;
+}
+
+int ServiceWorkerCacheWriter::DoWriteDataForPassthroughDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = STATE_DONE;
+ return result;
+ }
+ bytes_written_ += result;
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
+ *pause = true;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::DoDone(int* next_state, bool* pause, int result) {
+ *next_state = STATE_DONE;
+ return net::OK;
+}
+
+// These helpers adapt the AppCache "always use the callback" pattern to the
+// //net "only use the callback for async" pattern using
+// AsyncCompletionCallbackAdaptor.
+//
+// Specifically, these methods return result codes directly for synchronous
+// completions, and only run their callback (which is AsyncDoLoop) for
+// asynchronous completions.
+
+int ServiceWorkerCacheWriter::ReadInfoHelper(
+ const scoped_ptr<ServiceWorkerResponseReader>& reader,
+ HttpResponseInfoIOBuffer* buf) {
+ net::CompletionCallback run_callback = base::Bind(
+ &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
+ scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
+ new AsyncOnlyCompletionCallbackAdaptor(run_callback));
+ reader->ReadInfo(
+ buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
+ adaptor));
+ adaptor->set_async(true);
+ return adaptor->result();
+}
+
+int ServiceWorkerCacheWriter::ReadDataHelper(
+ const scoped_ptr<ServiceWorkerResponseReader>& reader,
+ net::IOBuffer* buf,
+ int buf_len) {
+ net::CompletionCallback run_callback = base::Bind(
+ &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
+ scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
+ new AsyncOnlyCompletionCallbackAdaptor(run_callback));
+ reader->ReadData(
+ buf, buf_len,
+ base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
+ adaptor));
+ adaptor->set_async(true);
+ return adaptor->result();
+}
+
+int ServiceWorkerCacheWriter::WriteInfoHelper(
+ const scoped_ptr<ServiceWorkerResponseWriter>& writer,
+ HttpResponseInfoIOBuffer* buf) {
+ did_replace_ = true;
+ net::CompletionCallback run_callback = base::Bind(
+ &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
+ scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
+ new AsyncOnlyCompletionCallbackAdaptor(run_callback));
+ writer->WriteInfo(
+ buf, base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
+ adaptor));
+ adaptor->set_async(true);
+ return adaptor->result();
+}
+
+int ServiceWorkerCacheWriter::WriteDataHelper(
+ const scoped_ptr<ServiceWorkerResponseWriter>& writer,
+ net::IOBuffer* buf,
+ int buf_len) {
+ net::CompletionCallback run_callback = base::Bind(
+ &ServiceWorkerCacheWriter::AsyncDoLoop, weak_factory_.GetWeakPtr());
+ scoped_refptr<AsyncOnlyCompletionCallbackAdaptor> adaptor(
+ new AsyncOnlyCompletionCallbackAdaptor(run_callback));
+ writer->WriteData(
+ buf, buf_len,
+ base::Bind(&AsyncOnlyCompletionCallbackAdaptor::WrappedCallback,
+ adaptor));
+ adaptor->set_async(true);
+ return adaptor->result();
+}
+
+void ServiceWorkerCacheWriter::AsyncDoLoop(int result) {
+ result = DoLoop(result);
+ // If the result is ERR_IO_PENDING, the pending callback will be run by a
+ // later invocation of AsyncDoLoop.
+ if (result != net::ERR_IO_PENDING) {
+ OnWriteCompleteCallback callback = pending_callback_;
+ pending_callback_.Reset();
+ net::Error error = result >= 0 ? net::OK : static_cast<net::Error>(result);
+ callback.Run(error);
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698