Commit 8812fcda authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Convert components/prefs away from base::Bind/base::Callback

base::Bind/base::Callback are deprecated in favor of either
base::BindOnce/base::OnceCallback or base::BindRepeating/
base::RepeatingCallback (depending on whether the callback
is invoked once or multiple time).

Convert some uses of base::Bind/base::Callback (this is a
multipart change) in components/prefs and services/prefs
to the recommended methods/types.

Bug: 1007719, 1007831
Change-Id: I7d18d35c3148ccacf4228cc5d44349b96cefb56e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1844813
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Commit-Queue: Sam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703484}
parent e18b5ed3
......@@ -269,8 +269,9 @@ void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
// Weakly binds the read task so that it doesn't kick in during shutdown.
base::PostTaskAndReplyWithResult(
file_task_runner_.get(), FROM_HERE, base::Bind(&ReadPrefsFromDisk, path_),
base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr()));
file_task_runner_.get(), FROM_HERE,
base::BindOnce(&ReadPrefsFromDisk, path_),
base::BindOnce(&JsonPrefStore::OnFileRead, AsWeakPtr()));
}
void JsonPrefStore::CommitPendingWrite(
......@@ -324,37 +325,37 @@ void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback(
has_pending_write_reply_ = false;
if (!on_next_successful_write_reply_.is_null()) {
base::Closure on_successful_write =
base::OnceClosure on_successful_write =
std::move(on_next_successful_write_reply_);
if (write_success) {
on_successful_write.Run();
std::move(on_successful_write).Run();
} else {
RegisterOnNextSuccessfulWriteReply(on_successful_write);
RegisterOnNextSuccessfulWriteReply(std::move(on_successful_write));
}
}
}
// static
void JsonPrefStore::PostWriteCallback(
const base::Callback<void(bool success)>& on_next_write_callback,
const base::Callback<void(bool success)>& on_next_write_reply,
base::OnceCallback<void(bool success)> on_next_write_callback,
base::OnceCallback<void(bool success)> on_next_write_reply,
scoped_refptr<base::SequencedTaskRunner> reply_task_runner,
bool write_success) {
if (!on_next_write_callback.is_null())
on_next_write_callback.Run(write_success);
std::move(on_next_write_callback).Run(write_success);
// We can't run |on_next_write_reply| on the current thread. Bounce back to
// the |reply_task_runner| which is the correct sequenced thread.
reply_task_runner->PostTask(
FROM_HERE, base::BindOnce(on_next_write_reply, write_success));
FROM_HERE, base::BindOnce(std::move(on_next_write_reply), write_success));
}
void JsonPrefStore::RegisterOnNextSuccessfulWriteReply(
const base::Closure& on_next_successful_write_reply) {
base::OnceClosure on_next_successful_write_reply) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(on_next_successful_write_reply_.is_null());
on_next_successful_write_reply_ = on_next_successful_write_reply;
on_next_successful_write_reply_ = std::move(on_next_successful_write_reply);
// If there are pending callbacks, avoid erasing them; the reply will be used
// as we set |on_next_successful_write_reply_|. Otherwise, setup a reply with
......@@ -362,11 +363,12 @@ void JsonPrefStore::RegisterOnNextSuccessfulWriteReply(
if (!has_pending_write_reply_) {
has_pending_write_reply_ = true;
writer_.RegisterOnNextWriteCallbacks(
base::Closure(),
base::Bind(
&PostWriteCallback, base::Callback<void(bool success)>(),
base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback,
AsWeakPtr()),
base::OnceClosure(),
base::BindOnce(
&PostWriteCallback, base::OnceCallback<void(bool success)>(),
base::BindOnce(
&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback,
AsWeakPtr()),
base::SequencedTaskRunnerHandle::Get()));
}
}
......@@ -378,11 +380,12 @@ void JsonPrefStore::RegisterOnNextWriteSynchronousCallbacks(
has_pending_write_reply_ = true;
writer_.RegisterOnNextWriteCallbacks(
callbacks.first,
base::Bind(
&PostWriteCallback, callbacks.second,
base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback,
AsWeakPtr()),
std::move(callbacks.first),
base::BindOnce(
&PostWriteCallback, std::move(callbacks.second),
base::BindOnce(
&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback,
AsWeakPtr()),
base::SequencedTaskRunnerHandle::Get()));
}
......@@ -465,7 +468,7 @@ bool JsonPrefStore::SerializeData(std::string* output) {
OnWriteCallbackPair callbacks =
pref_filter_->FilterSerializeData(prefs_.get());
if (!callbacks.first.is_null() || !callbacks.second.is_null())
RegisterOnNextWriteSynchronousCallbacks(callbacks);
RegisterOnNextWriteSynchronousCallbacks(std::move(callbacks));
}
JSONStringValueSerializer serializer(output);
......
......@@ -48,7 +48,7 @@ class COMPONENTS_PREFS_EXPORT JsonPrefStore
// A pair of callbacks to call before and after the preference file is written
// to disk.
using OnWriteCallbackPair =
std::pair<base::Closure, base::Callback<void(bool success)>>;
std::pair<base::OnceClosure, base::OnceCallback<void(bool success)>>;
// |pref_filename| is the path to the file to read prefs from. It is incorrect
// to create multiple JsonPrefStore with the same |pref_filename|.
......@@ -113,7 +113,7 @@ class COMPONENTS_PREFS_EXPORT JsonPrefStore
// |on_next_successful_write_reply| will be called on the thread from which
// this method is called and does not need to be thread safe.
void RegisterOnNextSuccessfulWriteReply(
const base::Closure& on_next_successful_write_reply);
base::OnceClosure on_next_successful_write_reply);
void ClearMutableValues() override;
......@@ -134,8 +134,8 @@ class COMPONENTS_PREFS_EXPORT JsonPrefStore
// |on_next_write_callback| on the current thread and posts
// |on_next_write_reply| on |reply_task_runner|.
static void PostWriteCallback(
const base::Callback<void(bool success)>& on_next_write_callback,
const base::Callback<void(bool success)>& on_next_write_reply,
base::OnceCallback<void(bool success)> on_next_write_callback,
base::OnceCallback<void(bool success)> on_next_write_reply,
scoped_refptr<base::SequencedTaskRunner> reply_task_runner,
bool write_success);
......@@ -193,7 +193,7 @@ class COMPONENTS_PREFS_EXPORT JsonPrefStore
std::set<std::string> keys_need_empty_value_;
bool has_pending_write_reply_ = true;
base::Closure on_next_successful_write_reply_;
base::OnceClosure on_next_successful_write_reply_;
SEQUENCE_CHECKER(sequence_checker_);
......
......@@ -73,7 +73,7 @@ class InterceptingPrefFilter : public PrefFilter {
void FilterUpdate(const std::string& path) override {}
OnWriteCallbackPair FilterSerializeData(
base::DictionaryValue* pref_store_contents) override {
return on_write_callback_pair_;
return std::move(on_write_callback_pair_);
}
void OnStoreDeletionFromDisk() override {}
......@@ -95,7 +95,7 @@ InterceptingPrefFilter::InterceptingPrefFilter() {}
InterceptingPrefFilter::InterceptingPrefFilter(
OnWriteCallbackPair callback_pair) {
on_write_callback_pair_ = callback_pair;
on_write_callback_pair_ = std::move(callback_pair);
}
InterceptingPrefFilter::~InterceptingPrefFilter() {}
......
......@@ -22,7 +22,7 @@ class COMPONENTS_PREFS_EXPORT PrefFilter {
public:
// A pair of pre-write and post-write callbacks.
using OnWriteCallbackPair =
std::pair<base::Closure, base::Callback<void(bool success)>>;
std::pair<base::OnceClosure, base::OnceCallback<void(bool success)>>;
// A callback to be invoked when |prefs| have been read (and possibly
// pre-modified) and are now ready to be handed back to this callback's
......
......@@ -1230,7 +1230,7 @@ TEST_P(PrefHashFilterTest, CallFilterSerializeDataCallbacks) {
// before-write callback is run.
ASSERT_EQ(
0u, mock_external_validation_hash_store_contents_->cleared_paths_count());
callbacks.first.Run();
std::move(callbacks.first).Run();
ASSERT_EQ(
2u, mock_external_validation_hash_store_contents_->cleared_paths_count());
......@@ -1238,7 +1238,7 @@ TEST_P(PrefHashFilterTest, CallFilterSerializeDataCallbacks) {
ASSERT_EQ(
0u, mock_external_validation_hash_store_contents_->stored_hashes_count());
callbacks.second.Run(true);
std::move(callbacks.second).Run(true);
ASSERT_EQ(
2u, mock_external_validation_hash_store_contents_->stored_hashes_count());
......@@ -1267,13 +1267,13 @@ TEST_P(PrefHashFilterTest, CallFilterSerializeDataCallbacksWithFailure) {
ASSERT_FALSE(callbacks.first.is_null());
callbacks.first.Run();
std::move(callbacks.first).Run();
// The pref should have been cleared from the external validation store.
ASSERT_EQ(
1u, mock_external_validation_hash_store_contents_->cleared_paths_count());
callbacks.second.Run(false);
std::move(callbacks.second).Run(false);
// Expect no writes to the external validation hash store contents.
ASSERT_EQ(0u,
......
......@@ -32,9 +32,9 @@ class TrackedPreferencesMigrator
unprotected_store_cleaner,
const base::Callback<void(const std::string& key)>&
protected_store_cleaner,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_unprotected_store_write_callback,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_protected_store_write_callback,
std::unique_ptr<PrefHashStore> unprotected_pref_hash_store,
std::unique_ptr<PrefHashStore> protected_pref_hash_store,
......@@ -63,9 +63,9 @@ class TrackedPreferencesMigrator
const base::Callback<void(const std::string& key)> unprotected_store_cleaner_;
const base::Callback<void(const std::string& key)> protected_store_cleaner_;
const base::Callback<void(const base::Closure&)>
const base::Callback<void(base::OnceClosure)>
register_on_successful_unprotected_store_write_callback_;
const base::Callback<void(const base::Closure&)>
const base::Callback<void(base::OnceClosure)>
register_on_successful_protected_store_write_callback_;
InterceptablePrefFilter::FinalizeFilterOnLoadCallback
......@@ -98,7 +98,7 @@ void CleanupPrefStore(
// once the destination pref store they were migrated to was successfully
// written to disk. Otherwise, executes the cleanup right away.
void ScheduleSourcePrefStoreCleanup(
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_destination_store_write_callback,
const base::Callback<void(const std::string& key)>& source_store_cleaner,
const std::set<std::string>& keys_to_clean,
......@@ -197,9 +197,9 @@ TrackedPreferencesMigrator::TrackedPreferencesMigrator(
const base::Callback<void(const std::string& key)>&
unprotected_store_cleaner,
const base::Callback<void(const std::string& key)>& protected_store_cleaner,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_unprotected_store_write_callback,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_protected_store_write_callback,
std::unique_ptr<PrefHashStore> unprotected_pref_hash_store,
std::unique_ptr<PrefHashStore> protected_pref_hash_store,
......@@ -214,8 +214,7 @@ TrackedPreferencesMigrator::TrackedPreferencesMigrator(
register_on_successful_protected_store_write_callback_(
register_on_successful_protected_store_write_callback),
unprotected_pref_hash_store_(std::move(unprotected_pref_hash_store)),
protected_pref_hash_store_(std::move(protected_pref_hash_store)) {
}
protected_pref_hash_store_(std::move(protected_pref_hash_store)) {}
TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {}
......@@ -306,9 +305,9 @@ void SetupTrackedPreferencesMigration(
const base::Callback<void(const std::string& key)>&
unprotected_store_cleaner,
const base::Callback<void(const std::string& key)>& protected_store_cleaner,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_unprotected_store_write_callback,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_protected_store_write_callback,
std::unique_ptr<PrefHashStore> unprotected_pref_hash_store,
std::unique_ptr<PrefHashStore> protected_pref_hash_store,
......
......@@ -33,9 +33,9 @@ void SetupTrackedPreferencesMigration(
const base::Callback<void(const std::string& key)>&
unprotected_store_cleaner,
const base::Callback<void(const std::string& key)>& protected_store_cleaner,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_unprotected_store_write_callback,
const base::Callback<void(const base::Closure&)>&
const base::Callback<void(base::OnceClosure)>&
register_on_successful_protected_store_write_callback,
std::unique_ptr<PrefHashStore> unprotected_pref_hash_store,
std::unique_ptr<PrefHashStore> protected_pref_hash_store,
......
......@@ -272,13 +272,11 @@ class TrackedPreferencesMigrationTest : public testing::Test {
switch (store_id) {
case MOCK_UNPROTECTED_PREF_STORE:
EXPECT_FALSE(unprotected_store_successful_write_callback_.is_null());
unprotected_store_successful_write_callback_.Run();
unprotected_store_successful_write_callback_.Reset();
std::move(unprotected_store_successful_write_callback_).Run();
break;
case MOCK_PROTECTED_PREF_STORE:
EXPECT_FALSE(protected_store_successful_write_callback_.is_null());
protected_store_successful_write_callback_.Run();
protected_store_successful_write_callback_.Reset();
std::move(protected_store_successful_write_callback_).Run();
break;
}
}
......@@ -286,15 +284,17 @@ class TrackedPreferencesMigrationTest : public testing::Test {
private:
void RegisterSuccessfulWriteClosure(
MockPrefStoreID store_id,
const base::Closure& successful_write_closure) {
base::OnceClosure successful_write_closure) {
switch (store_id) {
case MOCK_UNPROTECTED_PREF_STORE:
EXPECT_TRUE(unprotected_store_successful_write_callback_.is_null());
unprotected_store_successful_write_callback_ = successful_write_closure;
unprotected_store_successful_write_callback_ =
std::move(successful_write_closure);
break;
case MOCK_PROTECTED_PREF_STORE:
EXPECT_TRUE(protected_store_successful_write_callback_.is_null());
protected_store_successful_write_callback_ = successful_write_closure;
protected_store_successful_write_callback_ =
std::move(successful_write_closure);
break;
}
}
......@@ -362,8 +362,8 @@ class TrackedPreferencesMigrationTest : public testing::Test {
SimpleInterceptablePrefFilter mock_unprotected_pref_filter_;
SimpleInterceptablePrefFilter mock_protected_pref_filter_;
base::Closure unprotected_store_successful_write_callback_;
base::Closure protected_store_successful_write_callback_;
base::OnceClosure unprotected_store_successful_write_callback_;
base::OnceClosure protected_store_successful_write_callback_;
bool migration_modified_unprotected_store_;
bool migration_modified_protected_store_;
......
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