Bug 1716560 - Create C++ API for NimbusFeatures r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D117339
This commit is contained in:
Andrei Oprea 2021-07-07 17:12:29 +00:00
Родитель 27fd851b8d
Коммит 3d1f81b9e9
5 изменённых файлов: 159 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,60 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/browser/NimbusFeatures.h"
namespace mozilla {
void NimbusFeatures::GetPrefName(const nsACString& aFeatureId,
const nsACString& aVariable,
nsACString& aPref) {
// This branch is used to store experiment data
constexpr auto kSyncDataPrefBranch = "nimbus.syncdatastore."_ns;
aPref.Truncate();
aPref.Append(kSyncDataPrefBranch);
aPref.Append(aFeatureId);
aPref.Append(".");
aPref.Append(aVariable);
}
void NimbusFeatures::PreferencesCallback(const char* aPref, void* aData) {
PrefChangedFunc cb = reinterpret_cast<PrefChangedFunc>(aData);
(*cb)(aPref, nullptr);
}
bool NimbusFeatures::GetBool(const nsACString& aFeatureId,
const nsACString& aVariable, bool aDefault) {
nsAutoCString pref;
GetPrefName(aFeatureId, aVariable, pref);
return Preferences::GetBool(pref.get(), aDefault);
}
int NimbusFeatures::GetInt(const nsACString& aFeatureId,
const nsACString& aVariable, int aDefault) {
nsAutoCString pref;
GetPrefName(aFeatureId, aVariable, pref);
return Preferences::GetInt(pref.get(), aDefault);
}
nsresult NimbusFeatures::OnUpdate(const nsACString& aFeatureId,
const nsACString& aVariable,
PrefChangedFunc aUserCallback) {
nsAutoCString pref;
GetPrefName(aFeatureId, aVariable, pref);
return Preferences::RegisterCallback(PreferencesCallback, pref,
(void*)aUserCallback);
}
nsresult NimbusFeatures::OffUpdate(const nsACString& aFeatureId,
const nsACString& aVariable,
PrefChangedFunc aUserCallback) {
nsAutoCString pref;
GetPrefName(aFeatureId, aVariable, pref);
return Preferences::UnregisterCallback(
PreferencesCallback, pref, reinterpret_cast<void*>(aUserCallback));
}
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,41 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 mozilla_NimbusFeatures_h
#define mozilla_NimbusFeatures_h
#include "mozilla/Preferences.h"
namespace mozilla {
class NimbusFeatures {
using PrefChangedFunc = void (*)(const char* aPref, void* aData);
private:
static void GetPrefName(const nsACString& aFeatureId,
const nsACString& aVariable, nsACString& aPref);
static void PreferencesCallback(const char* aPref, void* aData);
public:
static bool GetBool(const nsACString& aFeatureId, const nsACString& aVariable,
bool aDefault);
static int GetInt(const nsACString& aFeatureId, const nsACString& aVariable,
int aDefault);
static nsresult OnUpdate(const nsACString& aFeatureId,
const nsACString& aVariable,
PrefChangedFunc aUserCallback);
static nsresult OffUpdate(const nsACString& aFeatureId,
const nsACString& aVariable,
PrefChangedFunc aUserCallback);
};
} // namespace mozilla
#endif

Просмотреть файл

@ -7,6 +7,18 @@
with Files("**"):
BUG_COMPONENT = ("Firefox", "Nimbus Desktop Client")
EXPORTS.mozilla.browser += [
"lib/NimbusFeatures.h",
]
UNIFIED_SOURCES += [
"lib/NimbusFeatures.cpp",
]
TEST_DIRS += [
"test/gtest",
]
BROWSER_CHROME_MANIFESTS += [
"test/browser/browser.ini",
]
@ -22,4 +34,6 @@ TESTING_JS_MODULES += [
"test/NimbusTestUtils.jsm",
]
FINAL_LIBRARY = "xul"
JAR_MANIFESTS += ["jar.mn"]

Просмотреть файл

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "gtest/gtest.h"
#include "mozilla/Preferences.h"
#include "mozilla/browser/NimbusFeatures.h"
#include "mozilla/SpinEventLoopUntil.h"
using namespace mozilla;
static bool gPrefUpdate = false;
TEST(NimbusFeaturesGet, Errors)
{
Preferences::SetInt("nimbus.syncdatastore.foo.value", 42,
PrefValueKind::User);
ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 42);
Preferences::SetBool("nimbus.syncdatastore.foo.enabled", true,
PrefValueKind::User);
ASSERT_TRUE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, false));
}
TEST(NimbusFeaturesUpdate, Errors)
{
NimbusFeatures::OnUpdate("foo"_ns, "value"_ns,
[](const char*, void*) { gPrefUpdate = true; });
Preferences::SetInt("nimbus.syncdatastore.foo.value", 24,
PrefValueKind::User);
ASSERT_TRUE(gPrefUpdate);
}

Просмотреть файл

@ -0,0 +1,11 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
UNIFIED_SOURCES += [
"NimbusFeatures_GetTest.cpp",
]
FINAL_LIBRARY = "xul-gtest"