2015-09-28 19:07:10 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef PrefsHelper_h
|
|
|
|
#define PrefsHelper_h
|
|
|
|
|
|
|
|
#include "GeneratedJNINatives.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "nsAppShell.h"
|
2016-02-02 01:38:13 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsVariant.h"
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2015-09-28 19:07:10 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
class PrefsHelper
|
|
|
|
: public widget::PrefsHelper::Natives<PrefsHelper>
|
|
|
|
, public UsesGeckoThreadProxy
|
2015-09-28 19:07:10 +03:00
|
|
|
{
|
|
|
|
PrefsHelper() = delete;
|
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
static bool GetVariantPref(nsIObserverService* aObsServ,
|
|
|
|
nsIWritableVariant* aVariant,
|
|
|
|
jni::Object::Param aPrefHandler,
|
|
|
|
jni::String::LocalRef& aPrefName)
|
2015-09-28 19:07:10 +03:00
|
|
|
{
|
2016-02-02 01:38:13 +03:00
|
|
|
if (NS_FAILED(aObsServ->NotifyObservers(aVariant, "android-get-pref",
|
2016-02-10 01:27:28 +03:00
|
|
|
aPrefName->ToString().get()))) {
|
2016-02-02 01:38:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-24 06:03:35 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
uint16_t varType = nsIDataType::VTYPE_EMPTY;
|
|
|
|
if (NS_FAILED(aVariant->GetDataType(&varType))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t type = widget::PrefsHelper::PREF_INVALID;
|
|
|
|
bool boolVal = false;
|
|
|
|
int32_t intVal = 0;
|
|
|
|
nsAutoString strVal;
|
|
|
|
|
|
|
|
switch (varType) {
|
|
|
|
case nsIDataType::VTYPE_BOOL:
|
|
|
|
type = widget::PrefsHelper::PREF_BOOL;
|
|
|
|
if (NS_FAILED(aVariant->GetAsBool(&boolVal))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
type = widget::PrefsHelper::PREF_INT;
|
|
|
|
if (NS_FAILED(aVariant->GetAsInt32(&intVal))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_ASTRING:
|
|
|
|
type = widget::PrefsHelper::PREF_STRING;
|
|
|
|
if (NS_FAILED(aVariant->GetAsAString(strVal))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& jstrVal = type == widget::PrefsHelper::PREF_STRING ?
|
2016-02-10 01:27:28 +03:00
|
|
|
jni::StringParam(strVal, aPrefName.Env()) :
|
|
|
|
jni::StringParam(nullptr);
|
2016-02-02 01:38:13 +03:00
|
|
|
|
|
|
|
widget::PrefsHelper::CallPrefHandler(
|
|
|
|
aPrefHandler, type, aPrefName, boolVal, intVal, jstrVal);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SetVariantPref(nsIObserverService* aObsServ,
|
|
|
|
nsIWritableVariant* aVariant,
|
|
|
|
jni::String::Param aPrefName,
|
|
|
|
bool aFlush,
|
|
|
|
int32_t aType,
|
|
|
|
bool aBoolVal,
|
|
|
|
int32_t aIntVal,
|
|
|
|
jni::String::Param aStrVal)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case widget::PrefsHelper::PREF_BOOL:
|
|
|
|
rv = aVariant->SetAsBool(aBoolVal);
|
|
|
|
break;
|
|
|
|
case widget::PrefsHelper::PREF_INT:
|
|
|
|
rv = aVariant->SetAsInt32(aIntVal);
|
|
|
|
break;
|
|
|
|
case widget::PrefsHelper::PREF_STRING:
|
2016-02-10 01:27:28 +03:00
|
|
|
rv = aVariant->SetAsAString(aStrVal->ToString());
|
2016-02-02 01:38:13 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = aObsServ->NotifyObservers(aVariant, "android-set-pref",
|
2016-02-10 01:27:28 +03:00
|
|
|
aPrefName->ToString().get());
|
2016-02-02 01:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t varType = nsIDataType::VTYPE_EMPTY;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = aVariant->GetDataType(&varType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use set-to-empty to signal the pref was handled.
|
|
|
|
const bool handled = varType == nsIDataType::VTYPE_EMPTY;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && handled && aFlush) {
|
|
|
|
rv = Preferences::GetService()->SavePrefFile(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING(nsPrintfCString("Failed to set pref %s",
|
2016-02-10 01:27:28 +03:00
|
|
|
aPrefName->ToCString().get()).get());
|
2016-02-02 01:38:13 +03:00
|
|
|
// Pretend we handled the pref.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-02-10 01:27:28 +03:00
|
|
|
static void GetPrefs(const jni::Class::LocalRef& aCls,
|
2016-02-02 01:38:13 +03:00
|
|
|
jni::ObjectArray::Param aPrefNames,
|
|
|
|
jni::Object::Param aPrefHandler)
|
|
|
|
{
|
2016-02-10 01:27:28 +03:00
|
|
|
nsTArray<jni::Object::LocalRef> nameRefArray(aPrefNames->GetElements());
|
2016-02-02 01:38:13 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obsServ;
|
|
|
|
nsCOMPtr<nsIWritableVariant> value;
|
|
|
|
nsAdoptingString strVal;
|
|
|
|
|
|
|
|
for (jni::Object::LocalRef& nameRef : nameRefArray) {
|
|
|
|
jni::String::LocalRef nameStr(mozilla::Move(nameRef));
|
2016-02-10 01:27:28 +03:00
|
|
|
const nsCString& name = nameStr->ToCString();
|
2016-02-02 01:38:13 +03:00
|
|
|
|
|
|
|
int32_t type = widget::PrefsHelper::PREF_INVALID;
|
|
|
|
bool boolVal = false;
|
|
|
|
int32_t intVal = 0;
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
switch (Preferences::GetType(name.get())) {
|
|
|
|
case nsIPrefBranch::PREF_BOOL:
|
|
|
|
type = widget::PrefsHelper::PREF_BOOL;
|
|
|
|
boolVal = Preferences::GetBool(name.get());
|
|
|
|
break;
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
case nsIPrefBranch::PREF_INT:
|
|
|
|
type = widget::PrefsHelper::PREF_INT;
|
|
|
|
intVal = Preferences::GetInt(name.get());
|
|
|
|
break;
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
case nsIPrefBranch::PREF_STRING:
|
|
|
|
type = widget::PrefsHelper::PREF_STRING;
|
|
|
|
strVal = Preferences::GetLocalizedString(name.get());
|
|
|
|
if (!strVal) {
|
|
|
|
strVal = Preferences::GetString(name.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Pref not found; try to find it.
|
|
|
|
if (!obsServ) {
|
|
|
|
obsServ = services::GetObserverService();
|
|
|
|
if (!obsServ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value) {
|
|
|
|
value->SetAsEmpty();
|
|
|
|
} else {
|
|
|
|
value = new nsVariant();
|
|
|
|
}
|
|
|
|
if (!GetVariantPref(obsServ, value,
|
|
|
|
aPrefHandler, nameStr)) {
|
|
|
|
NS_WARNING(nsPrintfCString("Failed to get pref %s",
|
|
|
|
name.get()).get());
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& jstrVal = type == widget::PrefsHelper::PREF_STRING ?
|
2016-02-10 01:27:28 +03:00
|
|
|
jni::StringParam(strVal, aCls.Env()) :
|
|
|
|
jni::StringParam(nullptr);
|
2016-02-02 01:38:13 +03:00
|
|
|
|
|
|
|
widget::PrefsHelper::CallPrefHandler(
|
|
|
|
aPrefHandler, type, nameStr, boolVal, intVal, jstrVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
widget::PrefsHelper::CallPrefHandler(
|
|
|
|
aPrefHandler, widget::PrefsHelper::PREF_FINISH,
|
|
|
|
nullptr, false, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetPref(jni::String::Param aPrefName,
|
|
|
|
bool aFlush,
|
|
|
|
int32_t aType,
|
|
|
|
bool aBoolVal,
|
|
|
|
int32_t aIntVal,
|
|
|
|
jni::String::Param aStrVal)
|
|
|
|
{
|
2016-02-10 01:27:28 +03:00
|
|
|
const nsCString& name = aPrefName->ToCString();
|
2016-02-02 01:38:13 +03:00
|
|
|
|
|
|
|
if (Preferences::GetType(name.get()) == nsIPrefBranch::PREF_INVALID) {
|
|
|
|
// No pref; try asking first.
|
|
|
|
nsCOMPtr<nsIObserverService> obsServ =
|
|
|
|
services::GetObserverService();
|
|
|
|
nsCOMPtr<nsIWritableVariant> value = new nsVariant();
|
|
|
|
if (obsServ && SetVariantPref(obsServ, value, aPrefName, aFlush,
|
|
|
|
aType, aBoolVal, aIntVal, aStrVal)) {
|
|
|
|
return;
|
2015-09-28 19:07:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
switch (aType) {
|
|
|
|
case widget::PrefsHelper::PREF_BOOL:
|
|
|
|
Preferences::SetBool(name.get(), aBoolVal);
|
|
|
|
break;
|
|
|
|
case widget::PrefsHelper::PREF_INT:
|
|
|
|
Preferences::SetInt(name.get(), aIntVal);
|
|
|
|
break;
|
|
|
|
case widget::PrefsHelper::PREF_STRING:
|
2016-02-10 01:27:28 +03:00
|
|
|
Preferences::SetString(name.get(), aStrVal->ToString());
|
2016-02-02 01:38:13 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid pref type");
|
|
|
|
}
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
if (aFlush) {
|
|
|
|
Preferences::GetService()->SavePrefFile(nullptr);
|
2015-09-28 19:07:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:27:28 +03:00
|
|
|
static void AddObserver(const jni::Class::LocalRef& aCls,
|
2016-02-02 01:38:13 +03:00
|
|
|
jni::ObjectArray::Param aPrefNames,
|
|
|
|
jni::Object::Param aPrefHandler,
|
|
|
|
jni::ObjectArray::Param aPrefsToObserve)
|
2015-09-28 19:07:10 +03:00
|
|
|
{
|
2016-02-02 01:38:13 +03:00
|
|
|
// Call observer immediately with existing pref values.
|
|
|
|
GetPrefs(aCls, aPrefNames, aPrefHandler);
|
|
|
|
|
|
|
|
if (!aPrefsToObserve) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<jni::Object::LocalRef> nameRefArray(
|
2016-02-10 01:27:28 +03:00
|
|
|
aPrefsToObserve->GetElements());
|
2016-02-02 01:38:13 +03:00
|
|
|
nsAppShell* const appShell = nsAppShell::Get();
|
|
|
|
MOZ_ASSERT(appShell);
|
2015-12-24 06:03:35 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
for (jni::Object::LocalRef& nameRef : nameRefArray) {
|
|
|
|
jni::String::LocalRef nameStr(mozilla::Move(nameRef));
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(Preferences::AddStrongObserver(
|
2016-02-10 01:27:28 +03:00
|
|
|
appShell, nameStr->ToCString().get())));
|
2016-02-02 01:38:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:27:28 +03:00
|
|
|
static void RemoveObserver(const jni::Class::LocalRef& aCls,
|
2016-02-02 01:38:13 +03:00
|
|
|
jni::ObjectArray::Param aPrefsToUnobserve)
|
|
|
|
{
|
|
|
|
nsTArray<jni::Object::LocalRef> nameRefArray(
|
2016-02-10 01:27:28 +03:00
|
|
|
aPrefsToUnobserve->GetElements());
|
2015-12-24 06:03:35 +03:00
|
|
|
nsAppShell* const appShell = nsAppShell::Get();
|
|
|
|
MOZ_ASSERT(appShell);
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
for (jni::Object::LocalRef& nameRef : nameRefArray) {
|
|
|
|
jni::String::LocalRef nameStr(mozilla::Move(nameRef));
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(Preferences::RemoveObserver(
|
2016-02-10 01:27:28 +03:00
|
|
|
appShell, nameStr->ToCString().get())));
|
2016-02-02 01:38:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnPrefChange(const char16_t* aData)
|
|
|
|
{
|
|
|
|
const nsCString& name = NS_LossyConvertUTF16toASCII(aData);
|
|
|
|
|
|
|
|
int32_t type = -1;
|
|
|
|
bool boolVal = false;
|
|
|
|
int32_t intVal = false;
|
|
|
|
nsAdoptingString strVal;
|
|
|
|
|
|
|
|
switch (Preferences::GetType(name.get())) {
|
|
|
|
case nsIPrefBranch::PREF_BOOL:
|
|
|
|
type = widget::PrefsHelper::PREF_BOOL;
|
|
|
|
boolVal = Preferences::GetBool(name.get());
|
|
|
|
break;
|
|
|
|
case nsIPrefBranch::PREF_INT:
|
|
|
|
type = widget::PrefsHelper::PREF_INT;
|
|
|
|
intVal = Preferences::GetInt(name.get());
|
|
|
|
break;
|
|
|
|
case nsIPrefBranch::PREF_STRING:
|
|
|
|
type = widget::PrefsHelper::PREF_STRING;
|
|
|
|
strVal = Preferences::GetLocalizedString(name.get());
|
|
|
|
if (!strVal) {
|
|
|
|
strVal = Preferences::GetString(name.get());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING(nsPrintfCString("Invalid pref %s",
|
|
|
|
name.get()).get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& jstrVal = type == widget::PrefsHelper::PREF_STRING ?
|
2016-02-10 01:27:28 +03:00
|
|
|
jni::StringParam(strVal) :
|
|
|
|
jni::StringParam(nullptr);
|
2015-09-28 19:07:10 +03:00
|
|
|
|
2016-02-02 01:38:13 +03:00
|
|
|
widget::PrefsHelper::OnPrefChange(name, type, boolVal, intVal, jstrVal);
|
2015-09-28 19:07:10 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif // PrefsHelper_h
|