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

Use OnceCallback and RepeatingCallback in disk_cache_perftest.cc.

Change CompletionCallback argument type to CompletionOnceCallback in
methods that guarantee that the callback will only be called once.

Change CompletionCallback local variables to CompletionRepeatingCallback
where the callback is locally passed to a function, but then called
directly if the function returns synchronously.

Bug: 807724
Change-Id: Ia7768504ce564ef2853662e3e39d752b534fa3a9
Reviewed-on: https://chromium-review.googlesource.com/1120748Reviewed-by: default avatarEric Roman <eroman@chromium.org>
Commit-Queue: Bence Béky <bnc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#571787}
parent e89a3c91
......@@ -8,7 +8,6 @@
#include "base/barrier_closure.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/hash.h"
......@@ -22,6 +21,7 @@
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "net/base/cache_type.h"
#include "net/base/completion_repeating_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
......@@ -81,7 +81,7 @@ class DiskCachePerfTest : public DiskCacheTestWithCache {
void ResetAndEvictSystemDiskCache();
// Callbacks used within tests for intermediate operations.
void WriteCallback(const net::CompletionCallback& final_callback,
void WriteCallback(net::CompletionOnceCallback final_callback,
scoped_refptr<net::IOBuffer> headers_buffer,
scoped_refptr<net::IOBuffer> body_buffer,
disk_cache::Entry* cache_entry,
......@@ -101,8 +101,8 @@ class WriteHandler {
public:
WriteHandler(const DiskCachePerfTest* test,
disk_cache::Backend* cache,
net::CompletionCallback final_callback)
: test_(test), cache_(cache), final_callback_(final_callback) {
net::CompletionOnceCallback final_callback)
: test_(test), cache_(cache), final_callback_(std::move(final_callback)) {
CacheTestFillBuffer(headers_buffer_->data(), kHeadersSize, false);
CacheTestFillBuffer(body_buffer_->data(), kChunkSize, false);
}
......@@ -126,7 +126,7 @@ class WriteHandler {
const DiskCachePerfTest* test_;
disk_cache::Backend* cache_;
net::CompletionCallback final_callback_;
net::CompletionOnceCallback final_callback_;
size_t next_entry_index_ = 0;
size_t pending_operations_count_ = 0;
......@@ -150,9 +150,9 @@ void WriteHandler::CreateNextEntry() {
TestEntry test_entry = test_->entries()[next_entry_index_++];
disk_cache::Entry** entry_ptr = new disk_cache::Entry*();
std::unique_ptr<disk_cache::Entry*> unique_entry_ptr(entry_ptr);
net::CompletionCallback callback =
base::Bind(&WriteHandler::CreateCallback, base::Unretained(this),
base::Passed(&unique_entry_ptr), test_entry.data_len);
net::CompletionRepeatingCallback callback =
base::BindRepeating(&WriteHandler::CreateCallback, base::Unretained(this),
base::Passed(&unique_entry_ptr), test_entry.data_len);
int result =
cache_->CreateEntry(test_entry.key, net::HIGHEST, entry_ptr, callback);
if (result != net::ERR_IO_PENDING)
......@@ -167,9 +167,9 @@ void WriteHandler::CreateCallback(std::unique_ptr<disk_cache::Entry*> entry_ptr,
disk_cache::Entry* entry = *entry_ptr;
net::CompletionCallback callback =
base::Bind(&WriteHandler::WriteDataCallback, base::Unretained(this),
entry, 0, data_len, kHeadersSize);
net::CompletionRepeatingCallback callback = base::BindRepeating(
&WriteHandler::WriteDataCallback, base::Unretained(this), entry, 0,
data_len, kHeadersSize);
int new_result = entry->WriteData(0, 0, headers_buffer_.get(), kHeadersSize,
callback, false);
if (new_result != net::ERR_IO_PENDING)
......@@ -193,15 +193,15 @@ void WriteHandler::WriteDataCallback(disk_cache::Entry* entry,
} else {
--pending_operations_count_;
if (pending_operations_count_ == 0)
final_callback_.Run(net::OK);
std::move(final_callback_).Run(net::OK);
}
return;
}
int write_size = std::min(kChunkSize, data_len - next_offset);
net::CompletionCallback callback =
base::Bind(&WriteHandler::WriteDataCallback, base::Unretained(this),
entry, next_offset + write_size, data_len, write_size);
net::CompletionRepeatingCallback callback = base::BindRepeating(
&WriteHandler::WriteDataCallback, base::Unretained(this), entry,
next_offset + write_size, data_len, write_size);
int new_result = entry->WriteData(1, next_offset, body_buffer_.get(),
write_size, callback, true);
if (new_result != net::ERR_IO_PENDING)
......@@ -215,7 +215,7 @@ bool WriteHandler::CheckForErrorAndCancel(int result) {
if (pending_result_ != net::OK) {
--pending_operations_count_;
if (pending_operations_count_ == 0)
final_callback_.Run(pending_result_);
std::move(final_callback_).Run(pending_result_);
return true;
}
return false;
......@@ -226,11 +226,11 @@ class ReadHandler {
ReadHandler(const DiskCachePerfTest* test,
WhatToRead what_to_read,
disk_cache::Backend* cache,
net::CompletionCallback final_callback)
net::CompletionOnceCallback final_callback)
: test_(test),
what_to_read_(what_to_read),
cache_(cache),
final_callback_(final_callback) {
final_callback_(std::move(final_callback)) {
for (int i = 0; i < kMaxParallelOperations; ++i)
read_buffers_[i] = new net::IOBuffer(std::max(kHeadersSize, kChunkSize));
}
......@@ -258,7 +258,7 @@ class ReadHandler {
const WhatToRead what_to_read_;
disk_cache::Backend* cache_;
net::CompletionCallback final_callback_;
net::CompletionOnceCallback final_callback_;
size_t next_entry_index_ = 0;
size_t pending_operations_count_ = 0;
......@@ -280,10 +280,10 @@ void ReadHandler::OpenNextEntry(int parallel_operation_index) {
TestEntry test_entry = test_->entries()[next_entry_index_++];
disk_cache::Entry** entry_ptr = new disk_cache::Entry*();
std::unique_ptr<disk_cache::Entry*> unique_entry_ptr(entry_ptr);
net::CompletionCallback callback =
base::Bind(&ReadHandler::OpenCallback, base::Unretained(this),
parallel_operation_index, base::Passed(&unique_entry_ptr),
test_entry.data_len);
net::CompletionRepeatingCallback callback =
base::BindRepeating(&ReadHandler::OpenCallback, base::Unretained(this),
parallel_operation_index,
base::Passed(&unique_entry_ptr), test_entry.data_len);
int result =
cache_->OpenEntry(test_entry.key, net::HIGHEST, entry_ptr, callback);
if (result != net::ERR_IO_PENDING)
......@@ -301,9 +301,9 @@ void ReadHandler::OpenCallback(int parallel_operation_index,
EXPECT_EQ(data_len, entry->GetDataSize(1));
net::CompletionCallback callback =
base::Bind(&ReadHandler::ReadDataCallback, base::Unretained(this),
parallel_operation_index, entry, 0, data_len, kHeadersSize);
net::CompletionRepeatingCallback callback = base::BindRepeating(
&ReadHandler::ReadDataCallback, base::Unretained(this),
parallel_operation_index, entry, 0, data_len, kHeadersSize);
int new_result =
entry->ReadData(0, 0, read_buffers_[parallel_operation_index].get(),
kChunkSize, callback);
......@@ -329,13 +329,13 @@ void ReadHandler::ReadDataCallback(int parallel_operation_index,
} else {
--pending_operations_count_;
if (pending_operations_count_ == 0)
final_callback_.Run(net::OK);
std::move(final_callback_).Run(net::OK);
}
return;
}
int expected_read_size = std::min(kChunkSize, data_len - next_offset);
net::CompletionCallback callback = base::Bind(
net::CompletionRepeatingCallback callback = base::BindRepeating(
&ReadHandler::ReadDataCallback, base::Unretained(this),
parallel_operation_index, entry, next_offset + expected_read_size,
data_len, expected_read_size);
......@@ -353,7 +353,7 @@ bool ReadHandler::CheckForErrorAndCancel(int result) {
if (pending_result_ != net::OK) {
--pending_operations_count_;
if (pending_operations_count_ == 0)
final_callback_.Run(pending_result_);
std::move(final_callback_).Run(pending_result_);
return true;
}
return false;
......@@ -547,7 +547,7 @@ TEST_F(DiskCachePerfTest, SimpleCacheInitialReadPortion) {
base::RunLoop event_loop;
base::Closure barrier =
base::BarrierClosure(kBatchSize, event_loop.QuitWhenIdleClosure());
net::CompletionCallback cb_batch(base::Bind(
net::CompletionRepeatingCallback cb_batch(base::BindRepeating(
VerifyRvAndCallClosure, base::Unretained(&barrier), kHeadersSize));
base::ElapsedTimer timer_early;
......
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