Commit 56ca76ea authored by David Bokan's avatar David Bokan Committed by Chromium LUCI CQ

DataItem Callback Conversion - Test callbacks

This CL is the fist in a series to convert DataItem from the deprecated
base::Callback (and Bind) to [Once|Repeating]Callback. See the relation
chain of CLs for the complete change.

This CL starts by converting a set of injectable callbacks used for unit
tests to observe this class. These callbacks may be called repeatedly
after being registered so they are RepeatingCallbacks. However,
ItemFactoryCallback and ItemStoreDeleter both take a callback as a
parameter. The unit test implementations only invoke these parameters
once so we convert them to the "Once" variant.

The RegisteredValuesCallback is a type defined in DataItem and used
elsewhere. For now, we add a "Once" typedef to prevent changing too
much. This will be removed in a downstream patch once all uses of the
ambiguous form are removed.

Bug: 1152268
Change-Id: I694df544b97ede7b1e5aa8af388ca4ed186d8156
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2595932Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: David Bokan <bokan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838226}
parent bfa7bc5d
...@@ -37,9 +37,15 @@ class DataItem { ...@@ -37,9 +37,15 @@ class DataItem {
using ReadCallback = using ReadCallback =
base::Callback<void(OperationResult result, base::Callback<void(OperationResult result,
std::unique_ptr<std::vector<char>> data)>; std::unique_ptr<std::vector<char>> data)>;
// TODO(bokan): The multiple callback versions are temporary while we remove
// the base::Callback version in favor of base::OnceCallback.
// https://crbug.com/1152268.
using RegisteredValuesCallback = using RegisteredValuesCallback =
base::Callback<void(OperationResult result, base::Callback<void(OperationResult result,
std::unique_ptr<base::DictionaryValue> values)>; std::unique_ptr<base::DictionaryValue> values)>;
using RegisteredValuesOnceCallback =
base::OnceCallback<void(OperationResult result,
std::unique_ptr<base::DictionaryValue> values)>;
// Gets all registered data items for the extension with the provided // Gets all registered data items for the extension with the provided
// extension ID - the items are returned as a DictionaryValue with keys set // extension ID - the items are returned as a DictionaryValue with keys set
......
...@@ -140,15 +140,16 @@ class LockScreenItemStorage : public ExtensionRegistryObserver { ...@@ -140,15 +140,16 @@ class LockScreenItemStorage : public ExtensionRegistryObserver {
ValueStoreMigratorFactoryCallback* factory_callback); ValueStoreMigratorFactoryCallback* factory_callback);
// Used in tests to inject fake data items implementations. // Used in tests to inject fake data items implementations.
using ItemFactoryCallback = using ItemFactoryCallback = base::RepeatingCallback<std::unique_ptr<DataItem>(
base::Callback<std::unique_ptr<DataItem>(const std::string& id, const std::string& id,
const std::string& extension_id, const std::string& extension_id,
const std::string& crypto_key)>; const std::string& crypto_key)>;
using RegisteredItemsGetter = using RegisteredItemsGetter = base::RepeatingCallback<void(
base::Callback<void(const std::string& extension_id, const std::string& extension_id,
const DataItem::RegisteredValuesCallback& callback)>; DataItem::RegisteredValuesOnceCallback callback)>;
using ItemStoreDeleter = base::Callback<void(const std::string& extension_id, using ItemStoreDeleter =
const base::Closure& callback)>; base::RepeatingCallback<void(const std::string& extension_id,
base::OnceClosure callback)>;
static void SetItemProvidersForTesting( static void SetItemProvidersForTesting(
RegisteredItemsGetter* item_fetch_callback, RegisteredItemsGetter* item_fetch_callback,
ItemFactoryCallback* factory_callback, ItemFactoryCallback* factory_callback,
......
...@@ -157,22 +157,20 @@ class ItemRegistry { ...@@ -157,22 +157,20 @@ class ItemRegistry {
void RemoveAll() { items_.clear(); } void RemoveAll() { items_.clear(); }
// Gets the set of registered data items. // Gets the set of registered data items.
void HandleGetRequest(const DataItem::RegisteredValuesCallback& callback) { void HandleGetRequest(DataItem::RegisteredValuesOnceCallback callback) {
if (!throttle_get_) { if (!throttle_get_) {
RunCallback(callback); RunCallback(std::move(callback));
return; return;
} }
ASSERT_TRUE(pending_callback_.is_null()); ASSERT_TRUE(pending_callback_.is_null());
pending_callback_ = callback; pending_callback_ = std::move(callback);
} }
// Completes a pending |HandleGetRequest| request. // Completes a pending |HandleGetRequest| request.
void RunPendingCallback() { void RunPendingCallback() {
ASSERT_FALSE(pending_callback_.is_null()); ASSERT_FALSE(pending_callback_.is_null());
DataItem::RegisteredValuesCallback callback = pending_callback_; RunCallback(std::move(pending_callback_));
pending_callback_.Reset();
RunCallback(callback);
} }
bool HasPendingCallback() const { return !pending_callback_.is_null(); } bool HasPendingCallback() const { return !pending_callback_.is_null(); }
...@@ -182,8 +180,9 @@ class ItemRegistry { ...@@ -182,8 +180,9 @@ class ItemRegistry {
void set_throttle_get(bool throttle_get) { throttle_get_ = throttle_get; } void set_throttle_get(bool throttle_get) { throttle_get_ = throttle_get; }
private: private:
void RunCallback(const DataItem::RegisteredValuesCallback& callback) { void RunCallback(DataItem::RegisteredValuesOnceCallback callback) {
callback.Run(fail_ ? OperationResult::kFailed : OperationResult::kSuccess, std::move(callback).Run(
fail_ ? OperationResult::kFailed : OperationResult::kSuccess,
ItemsToValue()); ItemsToValue());
} }
...@@ -211,7 +210,7 @@ class ItemRegistry { ...@@ -211,7 +210,7 @@ class ItemRegistry {
// complete the request. // complete the request.
bool throttle_get_ = false; bool throttle_get_ = false;
DataItem::RegisteredValuesCallback pending_callback_; DataItem::RegisteredValuesOnceCallback pending_callback_;
// Set of registered item ids. // Set of registered item ids.
std::set<std::string> items_; std::set<std::string> items_;
...@@ -483,12 +482,12 @@ class LockScreenItemStorageTest : public ExtensionsTest { ...@@ -483,12 +482,12 @@ class LockScreenItemStorageTest : public ExtensionsTest {
LockScreenItemStorage::SetValueStoreMigratorFactoryForTesting( LockScreenItemStorage::SetValueStoreMigratorFactoryForTesting(
&migrator_factory_); &migrator_factory_);
item_factory_ = base::Bind(&LockScreenItemStorageTest::CreateItem, item_factory_ = base::BindRepeating(&LockScreenItemStorageTest::CreateItem,
base::Unretained(this)); base::Unretained(this));
registered_items_getter_ = base::Bind( registered_items_getter_ = base::BindRepeating(
&LockScreenItemStorageTest::GetRegisteredItems, base::Unretained(this)); &LockScreenItemStorageTest::GetRegisteredItems, base::Unretained(this));
item_store_deleter_ = base::Bind(&LockScreenItemStorageTest::RemoveAllItems, item_store_deleter_ = base::BindRepeating(
base::Unretained(this)); &LockScreenItemStorageTest::RemoveAllItems, base::Unretained(this));
LockScreenItemStorage::SetItemProvidersForTesting( LockScreenItemStorage::SetItemProvidersForTesting(
&registered_items_getter_, &item_factory_, &item_store_deleter_); &registered_items_getter_, &item_factory_, &item_store_deleter_);
...@@ -735,19 +734,19 @@ class LockScreenItemStorageTest : public ExtensionsTest { ...@@ -735,19 +734,19 @@ class LockScreenItemStorageTest : public ExtensionsTest {
} }
void GetRegisteredItems(const std::string& extension_id, void GetRegisteredItems(const std::string& extension_id,
const DataItem::RegisteredValuesCallback& callback) { DataItem::RegisteredValuesOnceCallback callback) {
if (extension()->id() != extension_id) { if (extension()->id() != extension_id) {
callback.Run(OperationResult::kUnknownExtension, nullptr); std::move(callback).Run(OperationResult::kUnknownExtension, nullptr);
return; return;
} }
item_registry_->HandleGetRequest(callback); item_registry_->HandleGetRequest(std::move(callback));
} }
void RemoveAllItems(const std::string& extension_id, void RemoveAllItems(const std::string& extension_id,
const base::Closure& callback) { base::OnceClosure callback) {
ASSERT_EQ(extension()->id(), extension_id); ASSERT_EQ(extension()->id(), extension_id);
item_registry_->RemoveAll(); item_registry_->RemoveAll();
callback.Run(); std::move(callback).Run();
} }
std::unique_ptr<LockScreenItemStorage> lock_screen_item_storage_; std::unique_ptr<LockScreenItemStorage> lock_screen_item_storage_;
......
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