Commit b022bc19 authored by jkarlin's avatar jkarlin Committed by Commit bot

Make a context struct for ServiceWorkerCache::Put callbacks.

The Put callback functions took an absurd number of paramters.  This
CL simplifies by putting them in a context struct.  This is needed by
a downstream CL that needs to add another member to the struct.

Upstream of: https://codereview.chromium.org/608593003

BUG=392621

Review URL: https://codereview.chromium.org/606843002

Cr-Commit-Position: refs/heads/master@{#297164}
parent c3875427
...@@ -55,8 +55,7 @@ struct ResponseReadContext { ...@@ -55,8 +55,7 @@ struct ResponseReadContext {
// Streams data from a blob and writes it to a given disk_cache::Entry. // Streams data from a blob and writes it to a given disk_cache::Entry.
class BlobReader : public net::URLRequest::Delegate { class BlobReader : public net::URLRequest::Delegate {
public: public:
typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> typedef base::Callback<void(bool)> BoolCallback;
EntryBoolCallback;
BlobReader(disk_cache::ScopedEntryPtr entry) BlobReader(disk_cache::ScopedEntryPtr entry)
: cache_entry_offset_(0), : cache_entry_offset_(0),
...@@ -68,7 +67,7 @@ class BlobReader : public net::URLRequest::Delegate { ...@@ -68,7 +67,7 @@ class BlobReader : public net::URLRequest::Delegate {
void StreamBlobToCache(net::URLRequestContext* request_context, void StreamBlobToCache(net::URLRequestContext* request_context,
scoped_ptr<storage::BlobDataHandle> blob_data_handle, scoped_ptr<storage::BlobDataHandle> blob_data_handle,
const EntryBoolCallback& callback) { const BoolCallback& callback) {
callback_ = callback; callback_ = callback;
blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest(
blob_data_handle.Pass(), request_context, this); blob_data_handle.Pass(), request_context, this);
...@@ -102,7 +101,7 @@ class BlobReader : public net::URLRequest::Delegate { ...@@ -102,7 +101,7 @@ class BlobReader : public net::URLRequest::Delegate {
virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE { virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {
if (!request->status().is_success()) { if (!request->status().is_success()) {
callback_.Run(entry_.Pass(), false); callback_.Run(false);
return; return;
} }
ReadFromBlob(); ReadFromBlob();
...@@ -119,12 +118,12 @@ class BlobReader : public net::URLRequest::Delegate { ...@@ -119,12 +118,12 @@ class BlobReader : public net::URLRequest::Delegate {
virtual void OnReadCompleted(net::URLRequest* request, virtual void OnReadCompleted(net::URLRequest* request,
int bytes_read) OVERRIDE { int bytes_read) OVERRIDE {
if (!request->status().is_success()) { if (!request->status().is_success()) {
callback_.Run(entry_.Pass(), false); callback_.Run(false);
return; return;
} }
if (bytes_read == 0) { if (bytes_read == 0) {
callback_.Run(entry_.Pass(), true); callback_.Run(true);
return; return;
} }
...@@ -145,7 +144,7 @@ class BlobReader : public net::URLRequest::Delegate { ...@@ -145,7 +144,7 @@ class BlobReader : public net::URLRequest::Delegate {
void DidWriteDataToEntry(int expected_bytes, int rv) { void DidWriteDataToEntry(int expected_bytes, int rv) {
if (rv != expected_bytes) { if (rv != expected_bytes) {
callback_.Run(entry_.Pass(), false); callback_.Run(false);
return; return;
} }
...@@ -157,29 +156,51 @@ class BlobReader : public net::URLRequest::Delegate { ...@@ -157,29 +156,51 @@ class BlobReader : public net::URLRequest::Delegate {
int cache_entry_offset_; int cache_entry_offset_;
disk_cache::ScopedEntryPtr entry_; disk_cache::ScopedEntryPtr entry_;
scoped_ptr<net::URLRequest> blob_request_; scoped_ptr<net::URLRequest> blob_request_;
EntryBoolCallback callback_; BoolCallback callback_;
scoped_refptr<net::IOBufferWithSize> buffer_; scoped_refptr<net::IOBufferWithSize> buffer_;
base::WeakPtrFactory<BlobReader> weak_ptr_factory_; base::WeakPtrFactory<BlobReader> weak_ptr_factory_;
}; };
// The state needed to pass between ServiceWorkerCache::Put callbacks.
struct PutContext {
PutContext(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response,
scoped_ptr<storage::BlobDataHandle> blob_data_handle,
const ServiceWorkerCache::ErrorCallback& callback,
net::URLRequestContext* request_context)
: request(request.Pass()),
response(response.Pass()),
blob_data_handle(blob_data_handle.Pass()),
callback(callback),
request_context(request_context),
cache_entry(NULL) {}
~PutContext() {
if (cache_entry)
cache_entry->Close();
}
// Input parameters to the Put function.
scoped_ptr<ServiceWorkerFetchRequest> request;
scoped_ptr<ServiceWorkerResponse> response;
scoped_ptr<storage::BlobDataHandle> blob_data_handle;
ServiceWorkerCache::ErrorCallback callback;
net::URLRequestContext* request_context;
// This isn't a scoped_ptr because the disk_cache needs an Entry** as input to
// CreateEntry.
disk_cache::Entry* cache_entry;
DISALLOW_COPY_AND_ASSIGN(PutContext);
};
// Put callbacks // Put callbacks
void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request, void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv);
scoped_ptr<ServiceWorkerResponse> response, void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
const ServiceWorkerCache::ErrorCallback& callback,
scoped_ptr<disk_cache::Entry*> entryptr,
scoped_ptr<storage::BlobDataHandle> blob_data_handle,
net::URLRequestContext* request_context,
int rv);
void PutDidWriteHeaders(scoped_ptr<ServiceWorkerResponse> response,
const ServiceWorkerCache::ErrorCallback& callback,
disk_cache::ScopedEntryPtr entry,
scoped_ptr<storage::BlobDataHandle> blob_data_handle,
net::URLRequestContext* request_context,
int expected_bytes, int expected_bytes,
int rv); int rv);
void PutDidWriteBlobToCache(const ServiceWorkerCache::ErrorCallback& callback, void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
scoped_ptr<BlobReader> blob_reader, scoped_ptr<BlobReader> blob_reader,
disk_cache::ScopedEntryPtr entry,
bool success); bool success);
// Match callbacks // Match callbacks
...@@ -229,28 +250,22 @@ void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, ...@@ -229,28 +250,22 @@ void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback,
base::WeakPtr<ServiceWorkerCache> cache, base::WeakPtr<ServiceWorkerCache> cache,
int rv); int rv);
void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request, void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv) {
scoped_ptr<ServiceWorkerResponse> response,
const ServiceWorkerCache::ErrorCallback& callback,
scoped_ptr<disk_cache::Entry*> entryptr,
scoped_ptr<storage::BlobDataHandle> blob_data_handle,
net::URLRequestContext* request_context,
int rv) {
if (rv != net::OK) { if (rv != net::OK) {
callback.Run(ServiceWorkerCache::ErrorTypeExists); put_context->callback.Run(ServiceWorkerCache::ErrorTypeExists);
return; return;
} }
DCHECK(entryptr); DCHECK(put_context->cache_entry);
disk_cache::ScopedEntryPtr entry(*entryptr);
ServiceWorkerRequestResponseHeaders headers; ServiceWorkerRequestResponseHeaders headers;
headers.set_method(request->method); headers.set_method(put_context->request->method);
headers.set_status_code(response->status_code); headers.set_status_code(put_context->response->status_code);
headers.set_status_text(response->status_text); headers.set_status_text(put_context->response->status_text);
for (ServiceWorkerHeaderMap::const_iterator it = request->headers.begin(); for (ServiceWorkerHeaderMap::const_iterator it =
it != request->headers.end(); put_context->request->headers.begin();
it != put_context->request->headers.end();
++it) { ++it) {
ServiceWorkerRequestResponseHeaders::HeaderMap* header_map = ServiceWorkerRequestResponseHeaders::HeaderMap* header_map =
headers.add_request_headers(); headers.add_request_headers();
...@@ -258,8 +273,9 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request, ...@@ -258,8 +273,9 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
header_map->set_value(it->second); header_map->set_value(it->second);
} }
for (ServiceWorkerHeaderMap::const_iterator it = response->headers.begin(); for (ServiceWorkerHeaderMap::const_iterator it =
it != response->headers.end(); put_context->response->headers.begin();
it != put_context->response->headers.end();
++it) { ++it) {
ServiceWorkerRequestResponseHeaders::HeaderMap* header_map = ServiceWorkerRequestResponseHeaders::HeaderMap* header_map =
headers.add_response_headers(); headers.add_response_headers();
...@@ -269,7 +285,7 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request, ...@@ -269,7 +285,7 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<std::string> serialized(new std::string()); scoped_ptr<std::string> serialized(new std::string());
if (!headers.SerializeToString(serialized.get())) { if (!headers.SerializeToString(serialized.get())) {
callback.Run(ServiceWorkerCache::ErrorTypeStorage); put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage);
return; return;
} }
...@@ -277,16 +293,10 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request, ...@@ -277,16 +293,10 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
new net::StringIOBuffer(serialized.Pass())); new net::StringIOBuffer(serialized.Pass()));
// Get a temporary copy of the entry pointer before passing it in base::Bind. // Get a temporary copy of the entry pointer before passing it in base::Bind.
disk_cache::Entry* tmp_entry_ptr = entry.get(); disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry;
net::CompletionCallback write_headers_callback = net::CompletionCallback write_headers_callback = base::Bind(
base::Bind(PutDidWriteHeaders, PutDidWriteHeaders, base::Passed(put_context.Pass()), buffer->size());
base::Passed(response.Pass()),
callback,
base::Passed(entry.Pass()),
base::Passed(blob_data_handle.Pass()),
request_context,
buffer->size());
rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, rv = tmp_entry_ptr->WriteData(INDEX_HEADERS,
0 /* offset */, 0 /* offset */,
...@@ -299,50 +309,53 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request, ...@@ -299,50 +309,53 @@ void PutDidCreateEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
write_headers_callback.Run(rv); write_headers_callback.Run(rv);
} }
void PutDidWriteHeaders(scoped_ptr<ServiceWorkerResponse> response, void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
const ServiceWorkerCache::ErrorCallback& callback,
disk_cache::ScopedEntryPtr entry,
scoped_ptr<storage::BlobDataHandle> blob_data_handle,
net::URLRequestContext* request_context,
int expected_bytes, int expected_bytes,
int rv) { int rv) {
if (rv != expected_bytes) { if (rv != expected_bytes) {
entry->Doom(); put_context->cache_entry->Doom();
callback.Run(ServiceWorkerCache::ErrorTypeStorage); put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage);
return; return;
} }
// The metadata is written, now for the response content. The data is streamed // The metadata is written, now for the response content. The data is streamed
// from the blob into the cache entry. // from the blob into the cache entry.
if (response->blob_uuid.empty()) { if (put_context->response->blob_uuid.empty()) {
callback.Run(ServiceWorkerCache::ErrorTypeOK); put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK);
return; return;
} }
DCHECK(blob_data_handle); DCHECK(put_context->blob_data_handle);
disk_cache::ScopedEntryPtr entry(put_context->cache_entry);
put_context->cache_entry = NULL;
scoped_ptr<BlobReader> reader(new BlobReader(entry.Pass())); scoped_ptr<BlobReader> reader(new BlobReader(entry.Pass()));
BlobReader* reader_ptr = reader.get(); BlobReader* reader_ptr = reader.get();
reader_ptr->StreamBlobToCache( // Grab some pointers before passing put_context in Bind.
request_context, net::URLRequestContext* request_context = put_context->request_context;
blob_data_handle.Pass(), scoped_ptr<storage::BlobDataHandle> blob_data_handle =
base::Bind( put_context->blob_data_handle.Pass();
PutDidWriteBlobToCache, callback, base::Passed(reader.Pass())));
reader_ptr->StreamBlobToCache(request_context,
blob_data_handle.Pass(),
base::Bind(PutDidWriteBlobToCache,
base::Passed(put_context.Pass()),
base::Passed(reader.Pass())));
} }
void PutDidWriteBlobToCache(const ServiceWorkerCache::ErrorCallback& callback, void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
scoped_ptr<BlobReader> blob_reader, scoped_ptr<BlobReader> blob_reader,
disk_cache::ScopedEntryPtr entry,
bool success) { bool success) {
if (!success) { if (!success) {
entry->Doom(); put_context->cache_entry->Doom();
callback.Run(ServiceWorkerCache::ErrorTypeStorage); put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage);
return; return;
} }
callback.Run(ServiceWorkerCache::ErrorTypeOK); put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK);
} }
void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
...@@ -669,6 +682,8 @@ struct ServiceWorkerCache::KeysContext { ...@@ -669,6 +682,8 @@ struct ServiceWorkerCache::KeysContext {
// Used for enumerating cache entries. // Used for enumerating cache entries.
scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; scoped_ptr<disk_cache::Backend::Iterator> backend_iterator;
disk_cache::Entry* enumerated_entry; disk_cache::Entry* enumerated_entry;
DISALLOW_COPY_AND_ASSIGN(KeysContext);
}; };
// static // static
...@@ -699,7 +714,6 @@ void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, ...@@ -699,7 +714,6 @@ void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response, scoped_ptr<ServiceWorkerResponse> response,
const ErrorCallback& callback) { const ErrorCallback& callback) {
scoped_ptr<storage::BlobDataHandle> blob_data_handle; scoped_ptr<storage::BlobDataHandle> blob_data_handle;
if (!response->blob_uuid.empty()) { if (!response->blob_uuid.empty()) {
if (!blob_storage_context_) { if (!blob_storage_context_) {
callback.Run(ErrorTypeStorage); callback.Run(ErrorTypeStorage);
...@@ -857,20 +871,17 @@ void ServiceWorkerCache::PutImpl( ...@@ -857,20 +871,17 @@ void ServiceWorkerCache::PutImpl(
return; return;
} }
scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); scoped_ptr<PutContext> put_context(new PutContext(request.Pass(),
response.Pass(),
disk_cache::Entry** entry_ptr = entry.get(); blob_data_handle.Pass(),
callback,
request_context_));
ServiceWorkerFetchRequest* request_ptr = request.get(); disk_cache::Entry** entry_ptr = &put_context->cache_entry;
ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
net::CompletionCallback create_entry_callback = net::CompletionCallback create_entry_callback =
base::Bind(PutDidCreateEntry, base::Bind(PutDidCreateEntry, base::Passed(put_context.Pass()));
base::Passed(request.Pass()),
base::Passed(response.Pass()),
callback,
base::Passed(entry.Pass()),
base::Passed(blob_data_handle.Pass()),
request_context_);
int rv = backend_->CreateEntry( int rv = backend_->CreateEntry(
request_ptr->url.spec(), entry_ptr, create_entry_callback); request_ptr->url.spec(), entry_ptr, create_entry_callback);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment