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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/prefs/pref_registrar.h"
|
2022-10-26 00:50:29 +02:00
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_app.h"
|
|
|
|
#include "cef/include/cef_browser_process_handler.h"
|
|
|
|
#include "cef/include/cef_preference.h"
|
|
|
|
#include "cef/libcef/common/app_manager.h"
|
|
|
|
#include "cef/libcef/common/values_impl.h"
|
2022-10-26 00:50:29 +02:00
|
|
|
#include "components/prefs/pref_registry_simple.h"
|
|
|
|
#include "components/prefs/pref_store.h"
|
|
|
|
|
|
|
|
namespace pref_registrar {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefPreferenceRegistrarImpl : public CefPreferenceRegistrar {
|
|
|
|
public:
|
|
|
|
explicit CefPreferenceRegistrarImpl(PrefRegistrySimple* registry)
|
|
|
|
: registry_(registry) {}
|
|
|
|
|
|
|
|
CefPreferenceRegistrarImpl(const CefPreferenceRegistrarImpl&) = delete;
|
|
|
|
CefPreferenceRegistrarImpl& operator=(const CefPreferenceRegistrarImpl&) =
|
|
|
|
delete;
|
|
|
|
|
|
|
|
// CefPreferenceRegistrar methods.
|
|
|
|
bool AddPreference(const CefString& name,
|
|
|
|
CefRefPtr<CefValue> default_value) override {
|
|
|
|
const std::string nameStr = name;
|
|
|
|
if (registry_->defaults()->GetValue(nameStr, nullptr)) {
|
|
|
|
LOG(ERROR) << "Trying to register a previously registered preference: "
|
|
|
|
<< nameStr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (default_value->GetType()) {
|
|
|
|
case VTYPE_INVALID:
|
|
|
|
case VTYPE_NULL:
|
|
|
|
case VTYPE_BINARY:
|
|
|
|
break;
|
|
|
|
case VTYPE_BOOL:
|
|
|
|
registry_->RegisterBooleanPref(nameStr, default_value->GetBool());
|
|
|
|
return true;
|
|
|
|
case VTYPE_INT:
|
|
|
|
registry_->RegisterIntegerPref(nameStr, default_value->GetInt());
|
|
|
|
return true;
|
|
|
|
case VTYPE_DOUBLE:
|
|
|
|
registry_->RegisterDoublePref(nameStr, default_value->GetDouble());
|
|
|
|
return true;
|
|
|
|
case VTYPE_STRING:
|
|
|
|
registry_->RegisterStringPref(nameStr, default_value->GetString());
|
|
|
|
return true;
|
|
|
|
case VTYPE_DICTIONARY:
|
|
|
|
case VTYPE_LIST:
|
|
|
|
RegisterComplexPref(nameStr, default_value);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
LOG(ERROR) << "Invalid value type for preference: " << nameStr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RegisterComplexPref(const std::string& name,
|
|
|
|
CefRefPtr<CefValue> default_value) {
|
|
|
|
CefValueImpl* impl = static_cast<CefValueImpl*>(default_value.get());
|
2023-01-30 22:42:40 +01:00
|
|
|
auto impl_value = impl->CopyValue();
|
2022-10-26 00:50:29 +02:00
|
|
|
|
2023-01-30 22:42:40 +01:00
|
|
|
if (impl_value.type() == base::Value::Type::DICT) {
|
2023-05-30 10:55:32 +02:00
|
|
|
registry_->RegisterDictionaryPref(name, std::move(impl_value.GetDict()));
|
2023-01-30 22:42:40 +01:00
|
|
|
} else if (impl_value.type() == base::Value::Type::LIST) {
|
2023-05-30 10:55:32 +02:00
|
|
|
registry_->RegisterListPref(name, std::move(impl_value.GetList()));
|
2022-10-26 00:50:29 +02:00
|
|
|
} else {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2022-10-26 00:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<PrefRegistrySimple> registry_;
|
2022-10-26 00:50:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void RegisterCustomPrefs(cef_preferences_type_t type,
|
|
|
|
PrefRegistrySimple* registry) {
|
|
|
|
if (auto app = CefAppManager::Get()->GetApplication()) {
|
|
|
|
if (auto handler = app->GetBrowserProcessHandler()) {
|
|
|
|
CefPreferenceRegistrarImpl registrar(registry);
|
|
|
|
handler->OnRegisterCustomPreferences(type, ®istrar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pref_registrar
|