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

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: first draft Created 5 years, 4 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..e7df07ab81afd07992c78b2b2aadcda77bc42bb8
--- /dev/null
+++ b/content/browser/service_worker/service_worker_cache_writer.cc
@@ -0,0 +1,596 @@
+// 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 <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;
+
+enum {
+ STATE_READ_HEADERS_FOR_COMPARE,
+ STATE_READ_HEADERS_FOR_COMPARE_DONE,
+ STATE_READ_DATA_FOR_COMPARE,
+ STATE_READ_DATA_FOR_COMPARE_DONE,
+ STATE_READ_HEADERS_FOR_COPY,
+ STATE_READ_HEADERS_FOR_COPY_DONE,
+ STATE_READ_DATA_FOR_COPY,
+ STATE_READ_DATA_FOR_COPY_DONE,
+ STATE_WRITE_HEADERS_FOR_PASSTHROUGH,
+ STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE,
+ STATE_WRITE_HEADERS_FOR_COPY,
+ STATE_WRITE_HEADERS_FOR_COPY_DONE,
+ STATE_WRITE_DATA_FOR_COPY,
+ STATE_WRITE_DATA_FOR_COPY_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:
+ AsyncOnlyCompletionCallbackAdaptor(const net::CompletionCallback& callback)
+ : done_(false),
+ async_(false),
+ result_(net::ERR_IO_PENDING),
+ callback_(callback) {}
+
+ void set_async(bool async) { async_ = async; }
+ bool async() { return async_; }
+ bool done() { return done_; }
+ int result() { return result_; }
+
+ void WrappedCallback(int result) {
+ result_ = result;
+ if (async_)
+ callback_.Run(result);
+ else
+ done_ = true;
+ }
+
+ private:
+ friend class base::RefCounted<AsyncOnlyCompletionCallbackAdaptor>;
+ virtual ~AsyncOnlyCompletionCallbackAdaptor() {}
+
+ bool done_;
+ bool async_;
+ int result_;
+ net::CompletionCallback callback_;
+};
+
+} // namespace
+
+namespace content {
+
+ServiceWorkerCacheWriter::StateMachine::State::State() {}
+
+ServiceWorkerCacheWriter::StateMachine::State::State(
+ int id,
+ const std::string& name,
+ const StateMachine::Handler& handler)
+ : id(id), name(name), handler(handler) {}
+
+ServiceWorkerCacheWriter::StateMachine::State::~State() {}
+
+ServiceWorkerCacheWriter::StateMachine::StateMachine() : state_(STATE_START) {}
+ServiceWorkerCacheWriter::StateMachine::~StateMachine() {}
+
+void ServiceWorkerCacheWriter::StateMachine::AddState(
+ int id,
+ const std::string& name,
+ const StateMachine::Handler& handler) {
+ State state(id, name, handler);
+ states_[id] = state;
+}
+
+void ServiceWorkerCacheWriter::StateMachine::AddTransition(int from_id,
+ int to_id) {
+ DCHECK_EQ(1U, states_.count(from_id));
+ DCHECK_EQ(1U, states_.count(to_id));
+ DCHECK_NE(to_id, STATE_START);
+ DCHECK_NE(from_id, STATE_DONE);
+ states_[from_id].allowed_to.insert(to_id);
+}
+
+void ServiceWorkerCacheWriter::StateMachine::CheckValidTransition(int from_id,
+ int to_id) {
+ DCHECK_EQ(1U, states_.count(from_id)) << " State " << from_id
+ << " does not exist.";
+ DCHECK_EQ(1U, states_.count(to_id)) << " State " << to_id << " (from "
+ << states_[from_id].name
+ << ") does not exist.";
+ if (to_id == STATE_DONE)
+ return;
+ DCHECK_EQ(1U, states_[from_id].allowed_to.count(to_id))
+ << " Transition from " << states_[from_id].name << " to "
+ << states_[to_id].name << " is not allowed.";
+}
+
+int ServiceWorkerCacheWriter::StateMachine::Run(int status) {
+ DCHECK_EQ(1U, states_.count(state_));
+ bool pause = false;
+ VLOG(1) << "Run: initial " << states_[state_].name;
+ do {
+ int next_state = -1;
+ VLOG(1) << " running " << states_[state_].name;
+ status = states_[state_].handler.Run(&next_state, &pause, status);
+ VLOG(1) << " -> " << states_[next_state].name << " " << pause << " "
+ << status;
+ CheckValidTransition(state_, next_state);
+ state_ = next_state;
+ } while (status >= net::OK && state_ != STATE_DONE && !pause);
+ return status;
+}
+
+ServiceWorkerCacheWriter::ServiceWorkerCacheWriter(
+ const ResponseReaderCreator& reader_creator,
+ const ResponseWriterCreator& writer_creator)
+ : reader_creator_(reader_creator),
+ writer_creator_(writer_creator),
+ weak_factory_(this) {
+// This macro defines the state |id| to be named "name" and be bound to this
+// object's method called name, so:
+// DEFINE_STATE(STATE_FOO, Foo)
+// Defines a state STATE_FOO, named "Foo", that is bound to this->Foo().
+#define DEFINE_STATE(id, name) \
+ state_machine_.AddState( \
+ id, #name, \
+ base::Bind(&ServiceWorkerCacheWriter::name, base::Unretained(this)))
+
+ DEFINE_STATE(StateMachine::STATE_START, Start);
+ DEFINE_STATE(StateMachine::STATE_DONE, Done);
+
+ DEFINE_STATE(STATE_WRITE_HEADERS_FOR_PASSTHROUGH, WriteHeadersForPassthrough);
+ DEFINE_STATE(STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE,
+ WriteHeadersForPassthroughDone);
+ DEFINE_STATE(STATE_WRITE_DATA_FOR_PASSTHROUGH, WriteDataForPassthrough);
+ DEFINE_STATE(STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE,
+ WriteDataForPassthroughDone);
+
+ DEFINE_STATE(STATE_READ_HEADERS_FOR_COMPARE, ReadHeadersForCompare);
+ DEFINE_STATE(STATE_READ_HEADERS_FOR_COMPARE_DONE, ReadHeadersForCompareDone);
+ DEFINE_STATE(STATE_READ_DATA_FOR_COMPARE, ReadDataForCompare);
+ DEFINE_STATE(STATE_READ_DATA_FOR_COMPARE_DONE, ReadDataForCompareDone);
+
+ DEFINE_STATE(STATE_READ_HEADERS_FOR_COPY, ReadHeadersForCopy);
+ DEFINE_STATE(STATE_READ_HEADERS_FOR_COPY_DONE, ReadHeadersForCopyDone);
+ DEFINE_STATE(STATE_WRITE_HEADERS_FOR_COPY, WriteHeadersForCopy);
+ DEFINE_STATE(STATE_WRITE_HEADERS_FOR_COPY_DONE, WriteHeadersForCopyDone);
+
+ DEFINE_STATE(STATE_READ_DATA_FOR_COPY, ReadDataForCopy);
+ DEFINE_STATE(STATE_READ_DATA_FOR_COPY_DONE, ReadDataForCopyDone);
+ DEFINE_STATE(STATE_WRITE_DATA_FOR_COPY, WriteDataForCopy);
+ DEFINE_STATE(STATE_WRITE_DATA_FOR_COPY_DONE, WriteDataForCopyDone);
+
+#undef DEFINE_STATE
+
+ state_machine_.AddTransition(StateMachine::STATE_START,
+ STATE_WRITE_HEADERS_FOR_PASSTHROUGH);
+ state_machine_.AddTransition(StateMachine::STATE_START,
+ STATE_READ_HEADERS_FOR_COMPARE);
+ // The passthrough write loop:
+ // WriteHeaders -> WriteHeadersDone -> WriteData -> WriteDataDone
+ // -> WriteData
+ state_machine_.AddTransition(STATE_WRITE_HEADERS_FOR_PASSTHROUGH,
+ STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE);
+ state_machine_.AddTransition(STATE_WRITE_HEADERS_FOR_PASSTHROUGH_DONE,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH);
+ state_machine_.AddTransition(STATE_WRITE_DATA_FOR_PASSTHROUGH,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE);
+ state_machine_.AddTransition(STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH);
+
+ // The comparator loop:
+ // ReadHeaders -> ReadHeadersDone -> ReadData -> ReadDataDone -> ReadData
+ // Exits via ReadHeadersForCopy.
+ // ReadDataForCompareDone has a self-edge, see the comments in the
+ // function for details.
+ state_machine_.AddTransition(STATE_READ_HEADERS_FOR_COMPARE,
+ STATE_READ_HEADERS_FOR_COMPARE_DONE);
+ state_machine_.AddTransition(STATE_READ_HEADERS_FOR_COMPARE_DONE,
+ STATE_READ_DATA_FOR_COMPARE);
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COMPARE,
+ STATE_READ_DATA_FOR_COMPARE_DONE);
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COMPARE_DONE,
+ STATE_READ_DATA_FOR_COMPARE);
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COMPARE_DONE,
+ STATE_READ_DATA_FOR_COMPARE_DONE);
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COMPARE_DONE,
+ STATE_READ_HEADERS_FOR_COPY);
+
+ // ReadHeaders -> ReadHeadersDone -> WriteHeaders -> WriteHeadersDone
+ // -> ReadData
+ state_machine_.AddTransition(STATE_READ_HEADERS_FOR_COPY,
+ STATE_READ_HEADERS_FOR_COPY_DONE);
+ state_machine_.AddTransition(STATE_READ_HEADERS_FOR_COPY_DONE,
+ STATE_WRITE_HEADERS_FOR_COPY);
+ state_machine_.AddTransition(STATE_WRITE_HEADERS_FOR_COPY,
+ STATE_WRITE_HEADERS_FOR_COPY_DONE);
+ state_machine_.AddTransition(STATE_WRITE_HEADERS_FOR_COPY_DONE,
+ STATE_READ_DATA_FOR_COPY);
+
+ // The ReadDataForCopy loop:
+ // ReadData -> ReadDataDone -> WriteData -> WriteDataDone -> ReadData
+ // This can bail out via:
+ // ReadData -> WriteDataForPassthrough
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COPY,
+ STATE_READ_DATA_FOR_COPY_DONE);
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COPY_DONE,
+ STATE_WRITE_DATA_FOR_COPY);
+ state_machine_.AddTransition(STATE_WRITE_DATA_FOR_COPY,
+ STATE_WRITE_DATA_FOR_COPY_DONE);
+ state_machine_.AddTransition(STATE_WRITE_DATA_FOR_COPY_DONE,
+ STATE_READ_DATA_FOR_COPY);
+ state_machine_.AddTransition(STATE_READ_DATA_FOR_COPY,
+ STATE_WRITE_DATA_FOR_PASSTHROUGH);
+}
+
+ServiceWorkerCacheWriter::~ServiceWorkerCacheWriter() {}
+
+net::Error ServiceWorkerCacheWriter::WriteHeaders(
+ HttpResponseInfoIOBuffer* headers,
+ const OnWriteCompleteCallback& callback) {
+ headers_to_write_ = headers;
+ pending_callback_ = callback;
+ int result = state_machine_.Run(net::OK);
falken 2015/08/28 05:45:24 When trying to trace through this code I found it'
Elly Fong-Jones 2015/08/31 15:03:45 We're actually in STATE_START here. I've added a D
+ return result >= 0 ? net::OK : static_cast<net::Error>(result);
+}
+
+net::Error ServiceWorkerCacheWriter::WriteData(
+ net::IOBuffer* buf,
+ size_t buf_size,
+ const OnWriteCompleteCallback& callback) {
+ data_to_write_ = buf;
+ len_to_write_ = buf_size;
+ pending_callback_ = callback;
+ int result = state_machine_.Run(net::OK);
+ return result >= 0 ? net::OK : static_cast<net::Error>(result);
+}
+
+size_t ServiceWorkerCacheWriter::BytesWritten() const {
+ return bytes_written_;
+}
+
+int ServiceWorkerCacheWriter::Start(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::ReadHeadersForCompare(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::ReadHeadersForCompareDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = StateMachine::STATE_DONE;
+ return static_cast<int>(result);
+ }
+ cached_length_ = headers_to_read_->response_data_size;
+ net_length_ = headers_to_write_->response_data_size;
+ bytes_compared_ = 0;
+ *pause = true;
+ *next_state = STATE_READ_DATA_FOR_COMPARE;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::ReadDataForCompare(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;
+ return ReadDataHelper(compare_reader_, data_to_read_.get(), len_to_read_);
+}
+
+int ServiceWorkerCacheWriter::ReadDataForCompareDone(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 = StateMachine::STATE_DONE;
+ return result;
+ }
+
+ // Premature EOF while reading the AppCache data to compare. Fail the
falken 2015/08/28 05:45:24 I wouldn't call it AppCache data. Service worker h
Elly Fong-Jones 2015/08/31 15:03:45 Done.
+ // comparison.
+ if (result == 0) {
+ *next_state = STATE_READ_HEADERS_FOR_COPY;
+ return net::OK;
+ }
+
+ // Compare the data from AppCache 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 (bytes_compared_ + compare_offset_ >= net_length_ &&
+ net_length_ < 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::WriteHeadersForPassthrough(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::WriteHeadersForPassthroughDone(int* next_state,
+ bool* pause,
+ int result) {
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
+ *pause = true;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::WriteDataForPassthrough(int* next_state,
+ bool* pause,
+ int result) {
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH_DONE;
+ return WriteDataHelper(writer_, data_to_write_.get(), len_to_write_);
+}
+
+int ServiceWorkerCacheWriter::WriteDataForPassthroughDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = StateMachine::STATE_DONE;
+ return static_cast<int>(result);
+ }
+ bytes_written_ += result;
+ *next_state = STATE_WRITE_DATA_FOR_PASSTHROUGH;
+ *pause = true;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::ReadHeadersForCopy(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::ReadHeadersForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = StateMachine::STATE_DONE;
+ return static_cast<int>(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::WriteHeadersForCopy(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::WriteHeadersForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = StateMachine::STATE_DONE;
+ return static_cast<int>(result);
+ }
+ *next_state = STATE_READ_DATA_FOR_COPY;
+ return net::OK;
+}
+
+int ServiceWorkerCacheWriter::ReadDataForCopy(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::ReadDataForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = StateMachine::STATE_DONE;
+ return result;
+ }
+ *next_state = STATE_WRITE_DATA_FOR_COPY;
+ return result;
+}
+
+int ServiceWorkerCacheWriter::WriteDataForCopy(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::WriteDataForCopyDone(int* next_state,
+ bool* pause,
+ int result) {
+ if (result < 0) {
+ *next_state = StateMachine::STATE_DONE;
+ return result;
+ }
+ bytes_written_ += result;
+ bytes_copied_ += result;
+ *next_state = STATE_READ_DATA_FOR_COPY;
+ return result;
+}
+
+int ServiceWorkerCacheWriter::Done(int* next_state, bool* pause, int result) {
+ *next_state = StateMachine::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 AsyncRunStateMachine) for
+// asynchronous completions.
+
+int ServiceWorkerCacheWriter::ReadInfoHelper(
+ const scoped_ptr<ServiceWorkerResponseReader>& reader,
+ HttpResponseInfoIOBuffer* buf) {
+ net::CompletionCallback run_callback =
+ base::Bind(&ServiceWorkerCacheWriter::AsyncRunStateMachine,
+ 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::AsyncRunStateMachine,
+ 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) {
+ net::CompletionCallback run_callback =
+ base::Bind(&ServiceWorkerCacheWriter::AsyncRunStateMachine,
+ 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::AsyncRunStateMachine,
+ 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::AsyncRunStateMachine(int result) {
+ result = state_machine_.Run(result);
+ // If the result is ERR_IO_PENDING, the pending callback will be run by a
+ // later invocation of AsyncRunStateMachine.
+ 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