Commit 76de7516 authored by jkarlin's avatar jkarlin Committed by Commit bot

Have ServiceWorkerCache::Put return a Response when completed.

This gets Put() closer to the cache spec.

Downstream of: https://codereview.chromium.org/606843002/

BUG=392621

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

Cr-Commit-Position: refs/heads/master@{#297173}
parent addf5679
...@@ -166,7 +166,7 @@ struct PutContext { ...@@ -166,7 +166,7 @@ struct PutContext {
PutContext(scoped_ptr<ServiceWorkerFetchRequest> request, PutContext(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response, scoped_ptr<ServiceWorkerResponse> response,
scoped_ptr<storage::BlobDataHandle> blob_data_handle, scoped_ptr<storage::BlobDataHandle> blob_data_handle,
const ServiceWorkerCache::ErrorCallback& callback, const ServiceWorkerCache::ResponseCallback& callback,
net::URLRequestContext* request_context) net::URLRequestContext* request_context)
: request(request.Pass()), : request(request.Pass()),
response(response.Pass()), response(response.Pass()),
...@@ -183,7 +183,7 @@ struct PutContext { ...@@ -183,7 +183,7 @@ struct PutContext {
scoped_ptr<ServiceWorkerFetchRequest> request; scoped_ptr<ServiceWorkerFetchRequest> request;
scoped_ptr<ServiceWorkerResponse> response; scoped_ptr<ServiceWorkerResponse> response;
scoped_ptr<storage::BlobDataHandle> blob_data_handle; scoped_ptr<storage::BlobDataHandle> blob_data_handle;
ServiceWorkerCache::ErrorCallback callback; ServiceWorkerCache::ResponseCallback callback;
net::URLRequestContext* request_context; net::URLRequestContext* request_context;
...@@ -191,6 +191,9 @@ struct PutContext { ...@@ -191,6 +191,9 @@ struct PutContext {
// CreateEntry. // CreateEntry.
disk_cache::Entry* cache_entry; disk_cache::Entry* cache_entry;
// The BlobDataHandle for the output ServiceWorkerResponse.
scoped_ptr<storage::BlobDataHandle> out_blob_data_handle;
DISALLOW_COPY_AND_ASSIGN(PutContext); DISALLOW_COPY_AND_ASSIGN(PutContext);
}; };
...@@ -252,7 +255,9 @@ void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, ...@@ -252,7 +255,9 @@ void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback,
void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv) { void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv) {
if (rv != net::OK) { if (rv != net::OK) {
put_context->callback.Run(ServiceWorkerCache::ErrorTypeExists); put_context->callback.Run(ServiceWorkerCache::ErrorTypeExists,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
...@@ -285,7 +290,9 @@ void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv) { ...@@ -285,7 +290,9 @@ void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv) {
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())) {
put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage); put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
...@@ -314,7 +321,9 @@ void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, ...@@ -314,7 +321,9 @@ void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
int rv) { int rv) {
if (rv != expected_bytes) { if (rv != expected_bytes) {
put_context->cache_entry->Doom(); put_context->cache_entry->Doom();
put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage); put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
...@@ -322,7 +331,9 @@ void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, ...@@ -322,7 +331,9 @@ void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
// from the blob into the cache entry. // from the blob into the cache entry.
if (put_context->response->blob_uuid.empty()) { if (put_context->response->blob_uuid.empty()) {
put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK); put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK,
put_context->response.Pass(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
...@@ -351,11 +362,15 @@ void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, ...@@ -351,11 +362,15 @@ void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
bool success) { bool success) {
if (!success) { if (!success) {
put_context->cache_entry->Doom(); put_context->cache_entry->Doom();
put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage); put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK); put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK,
put_context->response.Pass(),
put_context->out_blob_data_handle.Pass());
} }
void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
...@@ -712,17 +727,22 @@ base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() { ...@@ -712,17 +727,22 @@ base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() {
void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response, scoped_ptr<ServiceWorkerResponse> response,
const ErrorCallback& callback) { const ResponseCallback& 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,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
blob_data_handle = blob_data_handle =
blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid);
if (!blob_data_handle) { if (!blob_data_handle) {
callback.Run(ErrorTypeStorage); callback.Run(ErrorTypeStorage,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
} }
...@@ -865,9 +885,11 @@ void ServiceWorkerCache::PutImpl( ...@@ -865,9 +885,11 @@ void ServiceWorkerCache::PutImpl(
scoped_ptr<ServiceWorkerFetchRequest> request, scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response, scoped_ptr<ServiceWorkerResponse> response,
scoped_ptr<storage::BlobDataHandle> blob_data_handle, scoped_ptr<storage::BlobDataHandle> blob_data_handle,
const ErrorCallback& callback) { const ResponseCallback& callback) {
if (!backend_) { if (!backend_) {
callback.Run(ErrorTypeStorage); callback.Run(ErrorTypeStorage,
scoped_ptr<ServiceWorkerResponse>(),
scoped_ptr<storage::BlobDataHandle>());
return; return;
} }
...@@ -877,6 +899,13 @@ void ServiceWorkerCache::PutImpl( ...@@ -877,6 +899,13 @@ void ServiceWorkerCache::PutImpl(
callback, callback,
request_context_)); request_context_));
if (put_context->blob_data_handle) {
// Grab another handle to the blob for the callback response.
put_context->out_blob_data_handle =
blob_storage_context_->GetBlobDataFromUUID(
put_context->response->blob_uuid);
}
disk_cache::Entry** entry_ptr = &put_context->cache_entry; disk_cache::Entry** entry_ptr = &put_context->cache_entry;
ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
......
...@@ -70,7 +70,7 @@ class CONTENT_EXPORT ServiceWorkerCache ...@@ -70,7 +70,7 @@ class CONTENT_EXPORT ServiceWorkerCache
// ErrorTypeOK on success. The callback will always be called. // ErrorTypeOK on success. The callback will always be called.
void Put(scoped_ptr<ServiceWorkerFetchRequest> request, void Put(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response, scoped_ptr<ServiceWorkerResponse> response,
const ErrorCallback& callback); const ResponseCallback& callback);
// Returns ErrorNotFound if not found. Otherwise deletes and returns // Returns ErrorNotFound if not found. Otherwise deletes and returns
// ErrorTypeOK. The callback will always be called. // ErrorTypeOK. The callback will always be called.
...@@ -108,7 +108,7 @@ class CONTENT_EXPORT ServiceWorkerCache ...@@ -108,7 +108,7 @@ class CONTENT_EXPORT ServiceWorkerCache
void PutImpl(scoped_ptr<ServiceWorkerFetchRequest> request, void PutImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response, scoped_ptr<ServiceWorkerResponse> response,
scoped_ptr<storage::BlobDataHandle> blob_data_handle, scoped_ptr<storage::BlobDataHandle> blob_data_handle,
const ErrorCallback& callback); const ResponseCallback& callback);
// Static callbacks for the Keys function. // Static callbacks for the Keys function.
static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context,
......
...@@ -84,7 +84,9 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test { ...@@ -84,7 +84,9 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test {
} }
void CachePutCallback(base::RunLoop* run_loop, void CachePutCallback(base::RunLoop* run_loop,
ServiceWorkerCache::ErrorType error) { ServiceWorkerCache::ErrorType error,
scoped_ptr<ServiceWorkerResponse> response,
scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
callback_cache_error_ = error; callback_cache_error_ = error;
run_loop->Quit(); run_loop->Quit();
} }
......
...@@ -139,7 +139,7 @@ class ServiceWorkerCacheTest : public testing::Test { ...@@ -139,7 +139,7 @@ class ServiceWorkerCacheTest : public testing::Test {
cache_->Put(CopyFetchRequest(request), cache_->Put(CopyFetchRequest(request),
CopyFetchResponse(response), CopyFetchResponse(response),
base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
base::Unretained(this), base::Unretained(this),
base::Unretained(loop.get()))); base::Unretained(loop.get())));
// TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle() // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle()
...@@ -270,17 +270,28 @@ class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, ...@@ -270,17 +270,28 @@ class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
TEST_P(ServiceWorkerCacheTestP, PutNoBody) { TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_)); EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(callback_response_);
EXPECT_STREQ(no_body_response_.url.spec().c_str(),
callback_response_->url.spec().c_str());
EXPECT_FALSE(callback_response_data_);
} }
TEST_P(ServiceWorkerCacheTestP, PutBody) { TEST_P(ServiceWorkerCacheTestP, PutBody) {
EXPECT_TRUE(Put(body_request_, body_response_)); EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(callback_response_);
EXPECT_STREQ(body_response_.url.spec().c_str(),
callback_response_->url.spec().c_str());
EXPECT_TRUE(callback_response_data_);
std::string response_body;
CopyBody(callback_response_data_.get(), &response_body);
EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
} }
TEST_F(ServiceWorkerCacheTest, PutBodyDropBlobRef) { TEST_F(ServiceWorkerCacheTest, PutBodyDropBlobRef) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop()); scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Put(CopyFetchRequest(body_request_), cache_->Put(CopyFetchRequest(body_request_),
CopyFetchResponse(body_response_), CopyFetchResponse(body_response_),
base::Bind(&ServiceWorkerCacheTestP::ErrorTypeCallback, base::Bind(&ServiceWorkerCacheTestP::ResponseAndErrorCallback,
base::Unretained(this), base::Unretained(this),
base::Unretained(loop.get()))); base::Unretained(loop.get())));
// The handle should be held by the cache now so the deref here should be // The handle should be held by the cache now so the deref here should be
......
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