2017-05-31 17:33:30 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/prefs/pref_store.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "base/memory/ptr_util.h"
|
|
|
|
#include "base/values.h"
|
|
|
|
|
|
|
|
CefPrefStore::CefPrefStore()
|
|
|
|
: read_only_(true),
|
|
|
|
read_success_(true),
|
|
|
|
read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
|
|
|
|
block_async_read_(false),
|
|
|
|
pending_async_read_(false),
|
|
|
|
init_complete_(false),
|
|
|
|
committed_(true) {}
|
|
|
|
|
2022-10-17 19:27:40 +02:00
|
|
|
bool CefPrefStore::GetValue(base::StringPiece key,
|
2017-05-31 17:33:30 +02:00
|
|
|
const base::Value** value) const {
|
|
|
|
return prefs_.GetValue(key, value);
|
|
|
|
}
|
|
|
|
|
2022-08-23 03:37:40 +02:00
|
|
|
base::Value::Dict CefPrefStore::GetValues() const {
|
|
|
|
return prefs_.AsDict();
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::GetMutableValue(const std::string& key,
|
|
|
|
base::Value** value) {
|
|
|
|
return prefs_.GetValue(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::AddObserver(PrefStore::Observer* observer) {
|
|
|
|
observers_.AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::RemoveObserver(PrefStore::Observer* observer) {
|
|
|
|
observers_.RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::HasObservers() const {
|
2021-04-21 00:52:34 +02:00
|
|
|
return !observers_.empty();
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::IsInitializationComplete() const {
|
|
|
|
return init_complete_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SetValue(const std::string& key,
|
2022-10-17 19:27:40 +02:00
|
|
|
base::Value value,
|
2017-05-31 17:33:30 +02:00
|
|
|
uint32_t flags) {
|
2022-10-17 19:27:40 +02:00
|
|
|
if (prefs_.SetValue(key, std::move(value))) {
|
2017-05-31 17:33:30 +02:00
|
|
|
committed_ = false;
|
|
|
|
NotifyPrefValueChanged(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SetValueSilently(const std::string& key,
|
2022-10-17 19:27:40 +02:00
|
|
|
base::Value value,
|
2017-05-31 17:33:30 +02:00
|
|
|
uint32_t flags) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (prefs_.SetValue(key, std::move(value))) {
|
2017-05-31 17:33:30 +02:00
|
|
|
committed_ = false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 19:48:00 +02:00
|
|
|
void CefPrefStore::RemoveValuesByPrefixSilently(const std::string& prefix) {
|
|
|
|
prefs_.ClearWithPrefix(prefix);
|
|
|
|
committed_ = false;
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
void CefPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
|
|
|
|
if (prefs_.RemoveValue(key)) {
|
|
|
|
committed_ = false;
|
|
|
|
NotifyPrefValueChanged(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::ReadOnly() const {
|
|
|
|
return read_only_;
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentPrefStore::PrefReadError CefPrefStore::GetReadError() const {
|
|
|
|
return read_error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentPrefStore::PrefReadError CefPrefStore::ReadPrefs() {
|
|
|
|
NotifyInitializationCompleted();
|
|
|
|
return read_error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
|
|
|
|
DCHECK(!pending_async_read_);
|
|
|
|
error_delegate_.reset(error_delegate);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (block_async_read_) {
|
2017-05-31 17:33:30 +02:00
|
|
|
pending_async_read_ = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2017-05-31 17:33:30 +02:00
|
|
|
NotifyInitializationCompleted();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 22:18:24 +02:00
|
|
|
void CefPrefStore::CommitPendingWrite(
|
|
|
|
base::OnceClosure done_callback,
|
|
|
|
base::OnceClosure synchronous_done_callback) {
|
2017-05-31 17:33:30 +02:00
|
|
|
committed_ = true;
|
2018-08-09 22:18:24 +02:00
|
|
|
PersistentPrefStore::CommitPendingWrite(std::move(done_callback),
|
|
|
|
std::move(synchronous_done_callback));
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SchedulePendingLossyWrites() {}
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
void CefPrefStore::OnStoreDeletionFromDisk() {}
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
void CefPrefStore::SetInitializationCompleted() {
|
|
|
|
NotifyInitializationCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::NotifyPrefValueChanged(const std::string& key) {
|
2023-01-02 23:59:03 +01:00
|
|
|
for (Observer& observer : observers_) {
|
2017-05-31 17:33:30 +02:00
|
|
|
observer.OnPrefValueChanged(key);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::NotifyInitializationCompleted() {
|
|
|
|
DCHECK(!init_complete_);
|
|
|
|
init_complete_ = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_) {
|
2017-05-31 17:33:30 +02:00
|
|
|
error_delegate_->OnError(read_error_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
for (Observer& observer : observers_) {
|
2017-05-31 17:33:30 +02:00
|
|
|
observer.OnInitializationCompleted(read_success_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::ReportValueChanged(const std::string& key, uint32_t flags) {
|
2023-01-02 23:59:03 +01:00
|
|
|
for (Observer& observer : observers_) {
|
2017-05-31 17:33:30 +02:00
|
|
|
observer.OnPrefValueChanged(key);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SetString(const std::string& key, const std::string& value) {
|
2022-10-17 19:27:40 +02:00
|
|
|
SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SetInteger(const std::string& key, int value) {
|
2022-10-17 19:27:40 +02:00
|
|
|
SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SetBoolean(const std::string& key, bool value) {
|
2022-10-17 19:27:40 +02:00
|
|
|
SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::GetString(const std::string& key, std::string* value) const {
|
|
|
|
const base::Value* stored_value;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!prefs_.GetValue(key, &stored_value) || !stored_value) {
|
2017-05-31 17:33:30 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
|
2021-07-23 18:40:13 +02:00
|
|
|
if (value && stored_value->is_string()) {
|
|
|
|
*value = stored_value->GetString();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return stored_value->is_string();
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::GetInteger(const std::string& key, int* value) const {
|
|
|
|
const base::Value* stored_value;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!prefs_.GetValue(key, &stored_value) || !stored_value) {
|
2017-05-31 17:33:30 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
|
2021-07-23 18:40:13 +02:00
|
|
|
if (value && stored_value->is_int()) {
|
|
|
|
*value = stored_value->GetInt();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return stored_value->is_int();
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrefStore::GetBoolean(const std::string& key, bool* value) const {
|
|
|
|
const base::Value* stored_value;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!prefs_.GetValue(key, &stored_value) || !stored_value) {
|
2017-05-31 17:33:30 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
|
2021-07-23 18:40:13 +02:00
|
|
|
if (value && stored_value->is_bool()) {
|
|
|
|
*value = stored_value->GetBool();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return stored_value->is_bool();
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::SetBlockAsyncRead(bool block_async_read) {
|
|
|
|
DCHECK(!init_complete_);
|
|
|
|
block_async_read_ = block_async_read;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (pending_async_read_ && !block_async_read_) {
|
2017-05-31 17:33:30 +02:00
|
|
|
NotifyInitializationCompleted();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::set_read_only(bool read_only) {
|
|
|
|
read_only_ = read_only;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::set_read_success(bool read_success) {
|
|
|
|
DCHECK(!init_complete_);
|
|
|
|
read_success_ = read_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrefStore::set_read_error(
|
|
|
|
PersistentPrefStore::PrefReadError read_error) {
|
|
|
|
DCHECK(!init_complete_);
|
|
|
|
read_error_ = read_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefPrefStore::~CefPrefStore() {}
|