Commit 5a3755a8 authored by Oksana Zhuravlova's avatar Oksana Zhuravlova Committed by Commit Bot

Convert preferences.mojom to use mojo/public/mojom/base/values.mojom

This change also converts the minimum necessary amount of C++ code to pass
objects of base::Value by value instead of std::unique_ptr.

Bug: 646113,799482
Change-Id: Iebebef972fc8e503c030639c8aefa0909bfcdbf9
Reviewed-on: https://chromium-review.googlesource.com/1024286
Commit-Queue: Oksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553772}
parent c3adf4f0
......@@ -22,11 +22,12 @@ mojom::PrefUpdateValuePtr CreatePrefUpdate(const std::vector<std::string>& path,
const base::Value* value) {
if (path.empty()) {
return mojom::PrefUpdateValue::NewAtomicUpdate(
value ? value->CreateDeepCopy() : nullptr);
value ? base::make_optional(value->Clone()) : base::nullopt);
}
std::vector<mojom::SubPrefUpdatePtr> pref_updates;
pref_updates.emplace_back(base::in_place, path,
value ? value->CreateDeepCopy() : nullptr);
pref_updates.emplace_back(
base::in_place, path,
value ? base::make_optional(value->Clone()) : base::nullopt);
return mojom::PrefUpdateValue::NewSplitUpdates(std::move(pref_updates));
}
......@@ -191,7 +192,7 @@ PersistentPrefStoreImpl::CreateConnection(ObservedPrefs observed_prefs) {
connections_.insert(std::make_pair(connection_ptr, std::move(connection)));
return mojom::PersistentPrefStoreConnection::New(
mojom::PrefStoreConnection::New(std::move(observer_request),
std::move(values), true),
std::move(*values), true),
std::move(pref_store_info), backing_pref_store_->GetReadError(),
backing_pref_store_->ReadOnly());
}
......@@ -204,9 +205,9 @@ void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) {
for (auto& entry : connections_) {
auto update_value = mojom::PrefUpdateValue::New();
if (GetValue(key, &value)) {
update_value->set_atomic_update(value->CreateDeepCopy());
update_value->set_atomic_update(value->Clone());
} else {
update_value->set_atomic_update(nullptr);
update_value->set_atomic_update(base::nullopt);
}
std::vector<mojom::PrefUpdatePtr> updates;
updates.emplace_back(base::in_place, key, std::move(update_value), 0);
......@@ -230,8 +231,9 @@ void PersistentPrefStoreImpl::SetValues(
if (update->value->is_atomic_update()) {
auto& value = update->value->get_atomic_update();
if (value) {
backing_pref_store_->SetValue(update->key, std::move(value),
update->flags);
backing_pref_store_->SetValue(
update->key, base::Value::ToUniquePtrValue(std::move(*value)),
update->flags);
} else {
backing_pref_store_->RemoveValue(update->key, update->flags);
}
......@@ -251,7 +253,9 @@ void PersistentPrefStoreImpl::SetValues(
SetValue(dictionary_value,
{split_update->path.begin(), split_update->path.end()},
std::move(split_update->value));
split_update->value ? base::Value::ToUniquePtrValue(
std::move(*split_update->value))
: nullptr);
updated_paths.insert(std::move(split_update->path));
}
if (pending_dictionary) {
......
......@@ -29,8 +29,7 @@ class PrefStoreImpl::Observer {
std::vector<mojom::PrefUpdatePtr> updates;
updates.push_back(mojom::PrefUpdate::New(
key, mojom::PrefUpdateValue::NewAtomicUpdate(value.CreateDeepCopy()),
0));
key, mojom::PrefUpdateValue::NewAtomicUpdate(value.Clone()), 0));
observer_->OnPrefsChanged(std::move(updates));
}
......@@ -40,7 +39,7 @@ class PrefStoreImpl::Observer {
std::vector<mojom::PrefUpdatePtr> updates;
updates.push_back(mojom::PrefUpdate::New(
key, mojom::PrefUpdateValue::NewAtomicUpdate(nullptr), 0));
key, mojom::PrefUpdateValue::NewAtomicUpdate(base::nullopt), 0));
observer_->OnPrefsChanged(std::move(updates));
}
......@@ -94,7 +93,8 @@ mojom::PrefStoreConnectionPtr PrefStoreImpl::AddObserver(
prefs_to_observe.end());
auto result = mojom::PrefStoreConnection::New(
std::move(request),
FilterPrefs(backing_pref_store_->GetValues(), observed_prefs),
base::Value::FromUniquePtrValue(
FilterPrefs(backing_pref_store_->GetValues(), observed_prefs)),
backing_pref_store_->IsInitializationComplete());
observers_.push_back(std::make_unique<Observer>(std::move(observer_ptr),
std::move(observed_prefs)));
......
......@@ -155,8 +155,9 @@ PersistentPrefStoreClient::PersistentPrefStoreClient(
error_delegate_->OnError(read_error_);
error_delegate_.reset();
if (connection->pref_store_connection) {
Init(std::move(connection->pref_store_connection->initial_prefs), true,
std::move(connection->pref_store_connection->observer));
Init(base::DictionaryValue::From(base::Value::ToUniquePtrValue(
std::move(connection->pref_store_connection->initial_prefs))),
true, std::move(connection->pref_store_connection->observer));
} else {
Init(nullptr, false, nullptr);
}
......@@ -300,21 +301,21 @@ void PersistentPrefStoreClient::FlushPendingWrites() {
const base::Value* nested_value = LookupPath(value, path);
if (nested_value) {
pref_updates.emplace_back(base::in_place, path,
nested_value->CreateDeepCopy());
nested_value->Clone());
} else {
pref_updates.emplace_back(base::in_place, path, nullptr);
pref_updates.emplace_back(base::in_place, path, base::nullopt);
}
sub_pref_writes.push_back(path);
}
if (pref_updates.empty()) {
update_value->set_atomic_update(value->CreateDeepCopy());
update_value->set_atomic_update(value->Clone());
writes.push_back({pref.first});
} else {
update_value->set_split_updates(std::move(pref_updates));
writes.push_back({pref.first, std::move(sub_pref_writes)});
}
} else {
update_value->set_atomic_update(nullptr);
update_value->set_atomic_update(base::nullopt);
writes.push_back({pref.first});
}
updates.emplace_back(base::in_place, pref.first, std::move(update_value),
......
......@@ -15,7 +15,7 @@ mojom::PrefRegistryPtr SerializePrefRegistry(
auto flags = pref_registry.GetRegistrationFlags(pref.first);
if (flags & PrefRegistry::PUBLIC) {
registry->public_registrations.emplace_back(mojom::PrefRegistration::New(
pref.first, pref.second->CreateDeepCopy(), flags));
pref.first, pref.second->Clone(), flags));
} else {
registry->private_registrations.push_back(pref.first);
}
......
......@@ -66,7 +66,8 @@ void RegisterRemoteDefaults(PrefRegistry* pref_registry,
std::vector<mojom::PrefRegistrationPtr> defaults) {
for (auto& registration : defaults) {
pref_registry->SetDefaultForeignPrefValue(
registration->key, std::move(registration->default_value),
registration->key,
base::Value::ToUniquePtrValue(std::move(registration->default_value)),
registration->flags);
}
}
......
......@@ -9,8 +9,9 @@
namespace prefs {
PrefStoreClient::PrefStoreClient(mojom::PrefStoreConnectionPtr connection) {
Init(std::move(connection->initial_prefs), connection->is_initialized,
std::move(connection->observer));
Init(base::DictionaryValue::From(
base::Value::ToUniquePtrValue(std::move(connection->initial_prefs))),
connection->is_initialized, std::move(connection->observer));
}
PrefStoreClient::~PrefStoreClient() = default;
......
......@@ -6,6 +6,7 @@
#include <utility>
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "services/preferences/public/cpp/lib/util.h"
......@@ -111,22 +112,24 @@ void PrefStoreClientMixin<BasePrefStore>::OnPrefChanged(
bool changed = false;
if (update_value->is_atomic_update()) {
if (ShouldSkipWrite(key, std::vector<std::string>(),
update_value->get_atomic_update().get())) {
OptionalOrNullptr(update_value->get_atomic_update()))) {
return;
}
auto& value = update_value->get_atomic_update();
if (!value) { // Delete
auto& optional = update_value->get_atomic_update();
if (!optional.has_value()) { // Delete
if (cached_prefs_->RemovePath(key, nullptr))
changed = true;
} else {
const base::Value* prev;
if (cached_prefs_->Get(key, &prev)) {
if (!prev->Equals(value.get())) {
cached_prefs_->Set(key, std::move(value));
if (!prev->Equals(&optional.value())) {
cached_prefs_->Set(
key, base::Value::ToUniquePtrValue(std::move(optional.value())));
changed = true;
}
} else {
cached_prefs_->Set(key, std::move(value));
cached_prefs_->Set(
key, base::Value::ToUniquePtrValue(std::move(optional.value())));
changed = true;
}
}
......@@ -137,14 +140,18 @@ void PrefStoreClientMixin<BasePrefStore>::OnPrefChanged(
for (auto& update : updates) {
// Clients shouldn't send empty paths.
if (update->path.empty() ||
ShouldSkipWrite(key, update->path, update->value.get())) {
ShouldSkipWrite(key, update->path,
OptionalOrNullptr(update->value))) {
continue;
}
std::vector<base::StringPiece> full_path = base::SplitStringPiece(
key, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
full_path.insert(full_path.end(), update->path.begin(),
update->path.end());
prefs::SetValue(cached_prefs_.get(), full_path, std::move(update->value));
prefs::SetValue(cached_prefs_.get(), full_path,
update->value ? base::Value::ToUniquePtrValue(
std::move(*update->value))
: nullptr);
}
}
if (changed && initialized_)
......
......@@ -41,7 +41,7 @@ class PersistentPrefStoreClientTest : public testing::Test,
mojom::PersistentPrefStoreConnection::New(
mojom::PrefStoreConnection::New(
mojom::PrefStoreObserverRequest(),
std::make_unique<base::DictionaryValue>(), true),
base::Value(base::Value::Type::DICTIONARY), true),
std::move(store_proxy_info),
::PersistentPrefStore::PREF_READ_ERROR_NONE, false));
auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
......
......@@ -41,8 +41,7 @@ class PrefStoreClientTest : public testing::Test {
void OnPrefChanged(const std::string& key, const base::Value& value) {
std::vector<mojom::PrefUpdatePtr> updates;
updates.push_back(mojom::PrefUpdate::New(
key, mojom::PrefUpdateValue::NewAtomicUpdate(value.CreateDeepCopy()),
0));
key, mojom::PrefUpdateValue::NewAtomicUpdate(value.Clone()), 0));
observer_ptr_->OnPrefsChanged(std::move(updates));
}
void OnInitializationCompleted() {
......@@ -53,7 +52,7 @@ class PrefStoreClientTest : public testing::Test {
void SetUp() override {
store_ = new PrefStoreClient(mojom::PrefStoreConnection::New(
mojo::MakeRequest(&observer_ptr_),
std::make_unique<base::DictionaryValue>(), false));
base::Value(base::Value::Type::DICTIONARY), false));
store_->AddObserver(&observer_);
}
void TearDown() override {
......@@ -166,9 +165,8 @@ TEST_F(PrefStoreClientTest, Initialized) {
PrefStoreObserverMock observer;
const char key[] = "hey";
const int kValue = 42;
base::Value pref(kValue);
auto prefs = std::make_unique<base::DictionaryValue>();
prefs->Set(key, pref.CreateDeepCopy());
base::Value prefs(base::Value::Type::DICTIONARY);
prefs.SetKey(key, base::Value(kValue));
auto store =
base::MakeRefCounted<PrefStoreClient>(mojom::PrefStoreConnection::New(
mojo::MakeRequest(&observer_ptr), std::move(prefs), true));
......
......@@ -4,9 +4,9 @@
module prefs.mojom;
import "mojo/common/values.mojom";
import "mojo/public/mojom/base/file_path.mojom";
import "mojo/public/mojom/base/string16.mojom";
import "mojo/public/mojom/base/values.mojom";
import "services/preferences/public/mojom/tracked_preference_validation_delegate.mojom";
const string kServiceName = "preferences";
......@@ -49,7 +49,7 @@ struct PrefStoreConnection {
// Initial values of the PrefStore. These will not be communicated through
// OnPrefChanged.
mojo.common.mojom.DictionaryValue initial_prefs;
mojo_base.mojom.DictionaryValue initial_prefs;
// Is the PrefStore initialized? If not it should not be used before
// OnInitializationCompleted has been called.
......@@ -105,7 +105,7 @@ struct SubPrefUpdate {
// The path to the changed value within the pref.
array<string> path;
// The new value; a null |value| indicates a delete.
mojo.common.mojom.Value? value;
mojo_base.mojom.Value? value;
};
union PrefUpdateValue {
......@@ -113,7 +113,7 @@ union PrefUpdateValue {
// under the pref key).
array<SubPrefUpdate> split_updates;
// An atomic update to the pref. A null |atomic_update| indicates a delete.
mojo.common.mojom.Value? atomic_update;
mojo_base.mojom.Value? atomic_update;
};
// An update to a pref.
......@@ -162,7 +162,7 @@ struct PrefRegistry {
struct PrefRegistration {
string key;
mojo.common.mojom.Value default_value;
mojo_base.mojom.Value default_value;
// A bitfield of flags. Flag values are defined in
// PrefRegistry::PrefRegistrationFlags and
......
......@@ -146,8 +146,9 @@ void SharedPrefRegistry::ProcessPublicPrefs(
<< key << "\"";
#endif
registry_->RegisterForeignPref(key);
registry_->SetDefaultForeignPrefValue(key, std::move(default_value),
registration->flags);
registry_->SetDefaultForeignPrefValue(
key, base::Value::ToUniquePtrValue(std::move(default_value)),
registration->flags);
observed_prefs->push_back(key);
new_public_prefs.push_back(key);
......@@ -167,7 +168,7 @@ void SharedPrefRegistry::ProcessPublicPrefs(
const base::Value* existing_default_value = nullptr;
DCHECK(registry_->defaults()->GetValue(registration->key,
&existing_default_value));
DCHECK_EQ(*existing_default_value, *registration->default_value);
DCHECK_EQ(*existing_default_value, registration->default_value);
#endif
}
}
......@@ -180,7 +181,7 @@ void SharedPrefRegistry::ProvideDefaultPrefs(
for (const auto& key : foreign_prefs) {
const base::Value* value = nullptr;
registry_->defaults()->GetValue(key, &value);
defaults.emplace_back(base::in_place, key, value->CreateDeepCopy(),
defaults.emplace_back(base::in_place, key, value->Clone(),
registry_->GetRegistrationFlags(key));
}
......
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