Commit a55b3520 authored by Bence Béky's avatar Bence Béky Committed by Commit Bot

Use CompletionOnceCallback in HttpCache.

Bug: 807724
Change-Id: I27e95e5a8d2b5a538cc376d6221827b1782e0bd1
Reviewed-on: https://chromium-review.googlesource.com/1092933Reviewed-by: default avatarZhongyi Shi <zhongyi@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Commit-Queue: Bence Béky <bnc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566019}
parent c7908cca
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/callback_helpers.h" #include "base/callback_helpers.h"
#include "base/logging.h" #include "base/logging.h"
#include "headless/lib/browser/headless_browser_context_impl.h" #include "headless/lib/browser/headless_browser_context_impl.h"
#include "net/base/completion_once_callback.h"
#include "net/http/http_cache_writers.h" #include "net/http/http_cache_writers.h"
#include "net/http/http_transaction.h" #include "net/http/http_transaction.h"
...@@ -19,7 +20,7 @@ class HeadlessCacheBackendFactory : public net::HttpCache::BackendFactory { ...@@ -19,7 +20,7 @@ class HeadlessCacheBackendFactory : public net::HttpCache::BackendFactory {
int CreateBackend(net::NetLog* net_log, int CreateBackend(net::NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const net::CompletionCallback& callback) override { net::CompletionOnceCallback callback) override {
return net::OK; return net::OK;
} }
}; };
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "base/trace_event/memory_usage_estimator.h" #include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/process_memory_dump.h" #include "base/trace_event/process_memory_dump.h"
#include "net/base/cache_type.h" #include "net/base/cache_type.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
...@@ -75,16 +76,11 @@ std::unique_ptr<HttpCache::BackendFactory> HttpCache::DefaultBackend::InMemory( ...@@ -75,16 +76,11 @@ std::unique_ptr<HttpCache::BackendFactory> HttpCache::DefaultBackend::InMemory(
int HttpCache::DefaultBackend::CreateBackend( int HttpCache::DefaultBackend::CreateBackend(
NetLog* net_log, NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
DCHECK_GE(max_bytes_, 0); DCHECK_GE(max_bytes_, 0);
return disk_cache::CreateCacheBackend(type_, return disk_cache::CreateCacheBackend(type_, backend_type_, path_, max_bytes_,
backend_type_, true, net_log, backend,
path_, std::move(callback));
max_bytes_,
true,
net_log,
backend,
callback);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -165,12 +161,12 @@ class HttpCache::WorkItem { ...@@ -165,12 +161,12 @@ class HttpCache::WorkItem {
backend_(NULL) {} backend_(NULL) {}
WorkItem(WorkItemOperation operation, WorkItem(WorkItemOperation operation,
Transaction* trans, Transaction* trans,
const CompletionCallback& cb, CompletionOnceCallback callback,
disk_cache::Backend** backend) disk_cache::Backend** backend)
: operation_(operation), : operation_(operation),
trans_(trans), trans_(trans),
entry_(NULL), entry_(NULL),
callback_(cb), callback_(std::move(callback)),
backend_(backend) {} backend_(backend) {}
~WorkItem() = default; ~WorkItem() = default;
...@@ -189,7 +185,7 @@ class HttpCache::WorkItem { ...@@ -189,7 +185,7 @@ class HttpCache::WorkItem {
if (backend_) if (backend_)
*backend_ = backend; *backend_ = backend;
if (!callback_.is_null()) { if (!callback_.is_null()) {
callback_.Run(result); std::move(callback_).Run(result);
return true; return true;
} }
return false; return false;
...@@ -209,7 +205,7 @@ class HttpCache::WorkItem { ...@@ -209,7 +205,7 @@ class HttpCache::WorkItem {
WorkItemOperation operation_; WorkItemOperation operation_;
Transaction* trans_; Transaction* trans_;
ActiveEntry** entry_; ActiveEntry** entry_;
CompletionCallback callback_; // User callback. CompletionOnceCallback callback_; // User callback.
disk_cache::Backend** backend_; disk_cache::Backend** backend_;
}; };
...@@ -390,7 +386,7 @@ HttpCache::~HttpCache() { ...@@ -390,7 +386,7 @@ HttpCache::~HttpCache() {
} }
int HttpCache::GetBackend(disk_cache::Backend** backend, int HttpCache::GetBackend(disk_cache::Backend** backend,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
if (disk_cache_.get()) { if (disk_cache_.get()) {
...@@ -398,7 +394,7 @@ int HttpCache::GetBackend(disk_cache::Backend** backend, ...@@ -398,7 +394,7 @@ int HttpCache::GetBackend(disk_cache::Backend** backend,
return OK; return OK;
} }
return CreateBackend(backend, callback); return CreateBackend(backend, std::move(callback));
} }
disk_cache::Backend* HttpCache::GetCurrentBackend() const { disk_cache::Backend* HttpCache::GetCurrentBackend() const {
...@@ -424,7 +420,7 @@ void HttpCache::WriteMetadata(const GURL& url, ...@@ -424,7 +420,7 @@ void HttpCache::WriteMetadata(const GURL& url,
// Do lazy initialization of disk cache if needed. // Do lazy initialization of disk cache if needed.
if (!disk_cache_.get()) { if (!disk_cache_.get()) {
// We don't care about the result. // We don't care about the result.
CreateBackend(NULL, CompletionCallback()); CreateBackend(NULL, CompletionOnceCallback());
} }
HttpCache::Transaction* trans = HttpCache::Transaction* trans =
...@@ -464,7 +460,7 @@ int HttpCache::CreateTransaction(RequestPriority priority, ...@@ -464,7 +460,7 @@ int HttpCache::CreateTransaction(RequestPriority priority,
// Do lazy initialization of disk cache if needed. // Do lazy initialization of disk cache if needed.
if (!disk_cache_.get()) { if (!disk_cache_.get()) {
// We don't care about the result. // We don't care about the result.
CreateBackend(NULL, CompletionCallback()); CreateBackend(NULL, CompletionOnceCallback());
} }
HttpCache::Transaction* transaction = HttpCache::Transaction* transaction =
...@@ -517,20 +513,21 @@ void HttpCache::DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd, ...@@ -517,20 +513,21 @@ void HttpCache::DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int HttpCache::CreateBackend(disk_cache::Backend** backend, int HttpCache::CreateBackend(disk_cache::Backend** backend,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
if (!backend_factory_.get()) if (!backend_factory_.get())
return ERR_FAILED; return ERR_FAILED;
building_backend_ = true; building_backend_ = true;
std::unique_ptr<WorkItem> item = const bool callback_is_null = callback.is_null();
std::make_unique<WorkItem>(WI_CREATE_BACKEND, nullptr, callback, backend); std::unique_ptr<WorkItem> item = std::make_unique<WorkItem>(
WI_CREATE_BACKEND, nullptr, std::move(callback), backend);
// This is the only operation that we can do that is not related to any given // This is the only operation that we can do that is not related to any given
// entry, so we use an empty key for it. // entry, so we use an empty key for it.
PendingOp* pending_op = GetPendingOp(std::string()); PendingOp* pending_op = GetPendingOp(std::string());
if (pending_op->writer) { if (pending_op->writer) {
if (!callback.is_null()) if (!callback_is_null)
pending_op->pending_queue.push_back(std::move(item)); pending_op->pending_queue.push_back(std::move(item));
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
...@@ -559,7 +556,7 @@ int HttpCache::GetBackendForTransaction(Transaction* trans) { ...@@ -559,7 +556,7 @@ int HttpCache::GetBackendForTransaction(Transaction* trans) {
return ERR_FAILED; return ERR_FAILED;
std::unique_ptr<WorkItem> item = std::make_unique<WorkItem>( std::unique_ptr<WorkItem> item = std::make_unique<WorkItem>(
WI_CREATE_BACKEND, trans, CompletionCallback(), nullptr); WI_CREATE_BACKEND, trans, CompletionOnceCallback(), nullptr);
PendingOp* pending_op = GetPendingOp(std::string()); PendingOp* pending_op = GetPendingOp(std::string());
DCHECK(pending_op->writer); DCHECK(pending_op->writer);
pending_op->pending_queue.push_back(std::move(item)); pending_op->pending_queue.push_back(std::move(item));
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#include "base/time/clock.h" #include "base/time/clock.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "net/base/cache_type.h" #include "net/base/cache_type.h"
#include "net/base/completion_callback.h" #include "net/base/completion_once_callback.h"
#include "net/base/load_states.h" #include "net/base/load_states.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
#include "net/base/request_priority.h" #include "net/base/request_priority.h"
...@@ -80,7 +80,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory { ...@@ -80,7 +80,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory {
// |callback| because the object can be deleted from within the callback. // |callback| because the object can be deleted from within the callback.
virtual int CreateBackend(NetLog* net_log, virtual int CreateBackend(NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) = 0; CompletionOnceCallback callback) = 0;
}; };
// A default backend factory for the common use cases. // A default backend factory for the common use cases.
...@@ -100,7 +100,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory { ...@@ -100,7 +100,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory {
// BackendFactory implementation. // BackendFactory implementation.
int CreateBackend(NetLog* net_log, int CreateBackend(NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) override; CompletionOnceCallback callback) override;
private: private:
CacheType type_; CacheType type_;
...@@ -174,7 +174,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory { ...@@ -174,7 +174,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory {
// |callback| will be notified when the operation completes. The pointer that // |callback| will be notified when the operation completes. The pointer that
// receives the |backend| must remain valid until the operation completes. // receives the |backend| must remain valid until the operation completes.
int GetBackend(disk_cache::Backend** backend, int GetBackend(disk_cache::Backend** backend,
const CompletionCallback& callback); CompletionOnceCallback callback);
// Returns the current backend (can be NULL). // Returns the current backend (can be NULL).
disk_cache::Backend* GetCurrentBackend() const; disk_cache::Backend* GetCurrentBackend() const;
...@@ -361,7 +361,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory { ...@@ -361,7 +361,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory {
// Creates the |backend| object and notifies the |callback| when the operation // Creates the |backend| object and notifies the |callback| when the operation
// completes. Returns an error code. // completes. Returns an error code.
int CreateBackend(disk_cache::Backend** backend, int CreateBackend(disk_cache::Backend** backend,
const CompletionCallback& callback); CompletionOnceCallback callback);
// Makes sure that the backend creation is complete before allowing the // Makes sure that the backend creation is complete before allowing the
// provided transaction to use the object. Returns an error code. |trans| // provided transaction to use the object. Returns an error code. |trans|
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "base/trace_event/process_memory_dump.h" #include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event_argument.h" #include "base/trace_event/trace_event_argument.h"
#include "net/base/cache_type.h" #include "net/base/cache_type.h"
#include "net/base/completion_callback.h"
#include "net/base/elements_upload_data_stream.h" #include "net/base/elements_upload_data_stream.h"
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
...@@ -4291,14 +4292,14 @@ TEST_F(HttpCacheTest, DeleteCacheWaitingForBackend) { ...@@ -4291,14 +4292,14 @@ TEST_F(HttpCacheTest, DeleteCacheWaitingForBackend) {
// We cannot call FinishCreation because the factory itself will go away with // We cannot call FinishCreation because the factory itself will go away with
// the cache, so grab the callback and attempt to use it. // the cache, so grab the callback and attempt to use it.
CompletionCallback callback = factory->callback(); CompletionOnceCallback callback = factory->ReleaseCallback();
std::unique_ptr<disk_cache::Backend>* backend = factory->backend(); std::unique_ptr<disk_cache::Backend>* backend = factory->backend();
cache.reset(); cache.reset();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
backend->reset(); backend->reset();
callback.Run(ERR_ABORTED); std::move(callback).Run(ERR_ABORTED);
} }
// Tests that we can delete the cache while creating the backend, from within // Tests that we can delete the cache while creating the backend, from within
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "base/location.h" #include "base/location.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "net/base/completion_callback.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/http/http_cache_writers.h" #include "net/http/http_cache_writers.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -333,7 +332,7 @@ void MockDiskEntry::IgnoreCallbacks(bool value) { ...@@ -333,7 +332,7 @@ void MockDiskEntry::IgnoreCallbacks(bool value) {
return; return;
ignore_callbacks_ = value; ignore_callbacks_ = value;
if (!value) if (!value)
StoreAndDeliverCallbacks(false, NULL, CompletionCallback(), 0); StoreAndDeliverCallbacks(false, NULL, CompletionOnceCallback(), 0);
} }
MockDiskEntry::~MockDiskEntry() = default; MockDiskEntry::~MockDiskEntry() = default;
...@@ -616,7 +615,7 @@ scoped_refptr<MockDiskEntry> MockDiskCache::GetDiskEntryRef( ...@@ -616,7 +615,7 @@ scoped_refptr<MockDiskEntry> MockDiskCache::GetDiskEntryRef(
int MockBackendFactory::CreateBackend( int MockBackendFactory::CreateBackend(
NetLog* net_log, NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
backend->reset(new MockDiskCache()); backend->reset(new MockDiskCache());
return OK; return OK;
} }
...@@ -772,7 +771,7 @@ int MockDiskCacheNoCB::CreateEntry(const std::string& key, ...@@ -772,7 +771,7 @@ int MockDiskCacheNoCB::CreateEntry(const std::string& key,
int MockBackendNoCbFactory::CreateBackend( int MockBackendNoCbFactory::CreateBackend(
NetLog* net_log, NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
backend->reset(new MockDiskCacheNoCB()); backend->reset(new MockDiskCacheNoCB());
return OK; return OK;
} }
...@@ -790,7 +789,7 @@ MockBlockingBackendFactory::~MockBlockingBackendFactory() = default; ...@@ -790,7 +789,7 @@ MockBlockingBackendFactory::~MockBlockingBackendFactory() = default;
int MockBlockingBackendFactory::CreateBackend( int MockBlockingBackendFactory::CreateBackend(
NetLog* net_log, NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
if (!block_) { if (!block_) {
if (!fail_) if (!fail_)
backend->reset(new MockDiskCache()); backend->reset(new MockDiskCache());
...@@ -798,7 +797,7 @@ int MockBlockingBackendFactory::CreateBackend( ...@@ -798,7 +797,7 @@ int MockBlockingBackendFactory::CreateBackend(
} }
backend_ = backend; backend_ = backend;
callback_ = callback; callback_ = std::move(callback);
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
......
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "net/base/completion_once_callback.h"
#include "net/base/request_priority.h" #include "net/base/request_priority.h"
#include "net/disk_cache/disk_cache.h" #include "net/disk_cache/disk_cache.h"
#include "net/http/http_cache.h" #include "net/http/http_cache.h"
...@@ -242,7 +244,7 @@ class MockBackendFactory : public HttpCache::BackendFactory { ...@@ -242,7 +244,7 @@ class MockBackendFactory : public HttpCache::BackendFactory {
public: public:
int CreateBackend(NetLog* net_log, int CreateBackend(NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) override; CompletionOnceCallback callback) override;
}; };
class MockHttpCache { class MockHttpCache {
...@@ -326,7 +328,7 @@ class MockBackendNoCbFactory : public HttpCache::BackendFactory { ...@@ -326,7 +328,7 @@ class MockBackendNoCbFactory : public HttpCache::BackendFactory {
public: public:
int CreateBackend(NetLog* net_log, int CreateBackend(NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) override; CompletionOnceCallback callback) override;
}; };
// This backend factory allows us to control the backend instantiation. // This backend factory allows us to control the backend instantiation.
...@@ -337,7 +339,7 @@ class MockBlockingBackendFactory : public HttpCache::BackendFactory { ...@@ -337,7 +339,7 @@ class MockBlockingBackendFactory : public HttpCache::BackendFactory {
int CreateBackend(NetLog* net_log, int CreateBackend(NetLog* net_log,
std::unique_ptr<disk_cache::Backend>* backend, std::unique_ptr<disk_cache::Backend>* backend,
const CompletionCallback& callback) override; CompletionOnceCallback callback) override;
// Completes the backend creation. Any blocked call will be notified via the // Completes the backend creation. Any blocked call will be notified via the
// provided callback. // provided callback.
...@@ -346,13 +348,13 @@ class MockBlockingBackendFactory : public HttpCache::BackendFactory { ...@@ -346,13 +348,13 @@ class MockBlockingBackendFactory : public HttpCache::BackendFactory {
std::unique_ptr<disk_cache::Backend>* backend() { return backend_; } std::unique_ptr<disk_cache::Backend>* backend() { return backend_; }
void set_fail(bool fail) { fail_ = fail; } void set_fail(bool fail) { fail_ = fail; }
const CompletionCallback& callback() { return callback_; } CompletionOnceCallback ReleaseCallback() { return std::move(callback_); }
private: private:
int Result() { return fail_ ? ERR_FAILED : OK; } int Result() { return fail_ ? ERR_FAILED : OK; }
std::unique_ptr<disk_cache::Backend>* backend_; std::unique_ptr<disk_cache::Backend>* backend_;
CompletionCallback callback_; CompletionOnceCallback callback_;
bool block_; bool block_;
bool fail_; bool fail_;
}; };
......
...@@ -218,8 +218,8 @@ int ViewCacheHelper::DoGetBackend() { ...@@ -218,8 +218,8 @@ int ViewCacheHelper::DoGetBackend() {
return ERR_FAILED; return ERR_FAILED;
return http_cache->GetBackend( return http_cache->GetBackend(
&disk_cache_, base::Bind(&ViewCacheHelper::OnIOComplete, &disk_cache_,
base::Unretained(this))); base::BindOnce(&ViewCacheHelper::OnIOComplete, base::Unretained(this)));
} }
int ViewCacheHelper::DoGetBackendComplete(int result) { int ViewCacheHelper::DoGetBackendComplete(int result) {
......
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