2022-10-26 00:50:29 +02:00
|
|
|
// Copyright (c) 2022 The Chromium Embedded Framework 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_helper.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/common/values_impl.h"
|
|
|
|
|
|
|
|
#include "base/notreached.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "components/prefs/pref_service.h"
|
|
|
|
|
|
|
|
namespace pref_helper {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* GetTypeString(base::Value::Type type) {
|
|
|
|
switch (type) {
|
|
|
|
case base::Value::Type::NONE:
|
|
|
|
return "NULL";
|
|
|
|
case base::Value::Type::BOOLEAN:
|
|
|
|
return "BOOLEAN";
|
|
|
|
case base::Value::Type::INTEGER:
|
|
|
|
return "INTEGER";
|
|
|
|
case base::Value::Type::DOUBLE:
|
|
|
|
return "DOUBLE";
|
|
|
|
case base::Value::Type::STRING:
|
|
|
|
return "STRING";
|
|
|
|
case base::Value::Type::BINARY:
|
|
|
|
return "BINARY";
|
2023-02-27 19:52:38 +01:00
|
|
|
case base::Value::Type::DICT:
|
2022-10-26 00:50:29 +02:00
|
|
|
return "DICTIONARY";
|
|
|
|
case base::Value::Type::LIST:
|
|
|
|
return "LIST";
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2022-10-26 00:50:29 +02:00
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool HasPreference(PrefService* pref_service, const CefString& name) {
|
|
|
|
return (pref_service->FindPreference(name) != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefValue> GetPreference(PrefService* pref_service,
|
|
|
|
const CefString& name) {
|
|
|
|
const PrefService::Preference* pref = pref_service->FindPreference(name);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!pref) {
|
2022-10-26 00:50:29 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2023-01-30 22:42:40 +01:00
|
|
|
return new CefValueImpl(pref->GetValue()->Clone());
|
2022-10-26 00:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDictionaryValue> GetAllPreferences(PrefService* pref_service,
|
|
|
|
bool include_defaults) {
|
2023-01-30 22:42:40 +01:00
|
|
|
// Returns a DeepCopy of the value.
|
2023-02-27 19:52:38 +01:00
|
|
|
auto values = pref_service->GetPreferenceValues(
|
2022-10-26 00:50:29 +02:00
|
|
|
include_defaults ? PrefService::INCLUDE_DEFAULTS
|
|
|
|
: PrefService::EXCLUDE_DEFAULTS);
|
|
|
|
|
2023-01-30 22:42:40 +01:00
|
|
|
// CefDictionaryValueImpl takes ownership of |values| contents.
|
|
|
|
return new CefDictionaryValueImpl(std::move(values), /*read_only=*/false);
|
2022-10-26 00:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CanSetPreference(PrefService* pref_service, const CefString& name) {
|
|
|
|
const PrefService::Preference* pref = pref_service->FindPreference(name);
|
|
|
|
return (pref && pref->IsUserModifiable());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetPreference(PrefService* pref_service,
|
|
|
|
const CefString& name,
|
|
|
|
CefRefPtr<CefValue> value,
|
|
|
|
CefString& error) {
|
|
|
|
// The below validation logic should match PrefService::SetUserPrefValue.
|
|
|
|
|
|
|
|
const PrefService::Preference* pref = pref_service->FindPreference(name);
|
|
|
|
if (!pref) {
|
|
|
|
error = "Trying to modify an unregistered preference";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pref->IsUserModifiable()) {
|
|
|
|
error = "Trying to modify a preference that is not user modifiable";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value.get()) {
|
|
|
|
// Reset the preference to its default value.
|
|
|
|
pref_service->ClearPref(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value->IsValid()) {
|
|
|
|
error = "A valid value is required";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefValueImpl* impl = static_cast<CefValueImpl*>(value.get());
|
|
|
|
|
|
|
|
CefValueImpl::ScopedLockedValue scoped_locked_value(impl);
|
|
|
|
base::Value* impl_value = impl->GetValueUnsafe();
|
|
|
|
|
|
|
|
if (pref->GetType() != impl_value->type()) {
|
|
|
|
error = base::StringPrintf(
|
|
|
|
"Trying to set a preference of type %s to value of type %s",
|
|
|
|
GetTypeString(pref->GetType()), GetTypeString(impl_value->type()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrefService will make a DeepCopy of |impl_value|.
|
|
|
|
pref_service->Set(name, *impl_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pref_helper
|