2016-04-29 07:52:54 +03:00
|
|
|
/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set sts=2 ts=8 sw=2 tw=99 et: */
|
|
|
|
/* 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/. */
|
2016-08-17 01:41:12 +03:00
|
|
|
|
2016-04-29 07:52:56 +03:00
|
|
|
#include "gfxFeature.h"
|
2016-08-17 01:41:12 +03:00
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Sprintf.h"
|
2016-05-07 05:01:58 +03:00
|
|
|
#include "nsString.h"
|
2016-04-29 07:52:54 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2016-04-29 07:52:55 +03:00
|
|
|
bool
|
|
|
|
FeatureState::IsEnabled() const
|
|
|
|
{
|
2016-05-12 20:30:54 +03:00
|
|
|
return IsInitialized() && IsFeatureStatusSuccess(GetValue());
|
2016-04-29 07:52:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:54 +03:00
|
|
|
FeatureStatus
|
|
|
|
FeatureState::GetValue() const
|
|
|
|
{
|
2016-04-29 07:52:55 +03:00
|
|
|
AssertInitialized();
|
|
|
|
|
2016-04-29 07:52:54 +03:00
|
|
|
if (mRuntime.mStatus != FeatureStatus::Unused) {
|
|
|
|
return mRuntime.mStatus;
|
|
|
|
}
|
2016-04-29 07:52:55 +03:00
|
|
|
if (mUser.mStatus == FeatureStatus::ForceEnabled) {
|
|
|
|
return FeatureStatus::ForceEnabled;
|
|
|
|
}
|
|
|
|
if (mEnvironment.mStatus != FeatureStatus::Unused) {
|
|
|
|
return mEnvironment.mStatus;
|
|
|
|
}
|
2016-04-29 07:52:54 +03:00
|
|
|
if (mUser.mStatus != FeatureStatus::Unused) {
|
|
|
|
return mUser.mStatus;
|
|
|
|
}
|
|
|
|
return mDefault.mStatus;
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:55 +03:00
|
|
|
bool
|
|
|
|
FeatureState::SetDefault(bool aEnable,
|
|
|
|
FeatureStatus aDisableStatus,
|
|
|
|
const char* aDisableMessage)
|
|
|
|
{
|
|
|
|
if (!aEnable) {
|
2016-06-03 21:54:56 +03:00
|
|
|
DisableByDefault(aDisableStatus, aDisableMessage,
|
|
|
|
NS_LITERAL_CSTRING("FEATURE_FAILURE_DISABLED"));
|
2016-04-29 07:52:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EnableByDefault();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:56 +03:00
|
|
|
void
|
|
|
|
FeatureState::SetDefaultFromPref(const char* aPrefName,
|
|
|
|
bool aIsEnablePref,
|
|
|
|
bool aDefaultValue)
|
|
|
|
{
|
|
|
|
bool baseValue = Preferences::GetDefaultBool(aPrefName, aDefaultValue);
|
|
|
|
SetDefault(baseValue == aIsEnablePref, FeatureStatus::Disabled, "Disabled by default");
|
|
|
|
|
|
|
|
if (Preferences::HasUserValue(aPrefName)) {
|
|
|
|
bool userValue = Preferences::GetBool(aPrefName, aDefaultValue);
|
|
|
|
if (userValue == aIsEnablePref) {
|
2016-05-07 05:01:58 +03:00
|
|
|
nsCString message("Enabled via ");
|
|
|
|
message.AppendASCII(aPrefName);
|
|
|
|
UserEnable(message.get());
|
2016-04-29 07:52:56 +03:00
|
|
|
} else {
|
2016-05-07 05:01:58 +03:00
|
|
|
nsCString message("Disabled via ");
|
|
|
|
message.AppendASCII(aPrefName);
|
2016-06-03 21:54:56 +03:00
|
|
|
UserDisable(message.get(), NS_LITERAL_CSTRING("FEATURE_FAILURE_PREF_OFF"));
|
2016-04-29 07:52:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:55 +03:00
|
|
|
bool
|
|
|
|
FeatureState::InitOrUpdate(bool aEnable,
|
|
|
|
FeatureStatus aDisableStatus,
|
|
|
|
const char* aDisableMessage)
|
|
|
|
{
|
|
|
|
if (!IsInitialized()) {
|
|
|
|
return SetDefault(aEnable, aDisableStatus, aDisableMessage);
|
|
|
|
}
|
2016-06-03 21:54:56 +03:00
|
|
|
return MaybeSetFailed(aEnable, aDisableStatus, aDisableMessage, nsCString());
|
2016-04-29 07:52:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FeatureState::UserEnable(const char* aMessage)
|
|
|
|
{
|
|
|
|
AssertInitialized();
|
|
|
|
SetUser(FeatureStatus::Available, aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FeatureState::UserForceEnable(const char* aMessage)
|
|
|
|
{
|
|
|
|
AssertInitialized();
|
|
|
|
SetUser(FeatureStatus::ForceEnabled, aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-03 21:54:56 +03:00
|
|
|
FeatureState::UserDisable(const char* aMessage, const nsACString& aFailureId)
|
2016-04-29 07:52:55 +03:00
|
|
|
{
|
|
|
|
AssertInitialized();
|
|
|
|
SetUser(FeatureStatus::Disabled, aMessage);
|
2016-06-03 21:54:56 +03:00
|
|
|
SetFailureId(aFailureId);
|
2016-04-29 07:52:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-03 21:54:56 +03:00
|
|
|
FeatureState::Disable(FeatureStatus aStatus, const char* aMessage,
|
|
|
|
const nsACString& aFailureId)
|
2016-04-29 07:52:55 +03:00
|
|
|
{
|
|
|
|
AssertInitialized();
|
|
|
|
|
|
|
|
// We should never bother setting an environment status to "enabled," since
|
|
|
|
// it could override an explicit user decision to disable it.
|
|
|
|
MOZ_ASSERT(IsFeatureStatusFailure(aStatus));
|
|
|
|
|
|
|
|
SetEnvironment(aStatus, aMessage);
|
2016-06-03 21:54:56 +03:00
|
|
|
SetFailureId(aFailureId);
|
2016-04-29 07:52:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-03 21:54:56 +03:00
|
|
|
FeatureState::SetFailed(FeatureStatus aStatus, const char* aMessage,
|
|
|
|
const nsACString& aFailureId)
|
2016-04-29 07:52:55 +03:00
|
|
|
{
|
|
|
|
AssertInitialized();
|
|
|
|
|
|
|
|
// We should never bother setting a runtime status to "enabled," since it could
|
|
|
|
// override an explicit user decision to disable it.
|
|
|
|
MOZ_ASSERT(IsFeatureStatusFailure(aStatus));
|
|
|
|
|
|
|
|
SetRuntime(aStatus, aMessage);
|
2016-06-03 21:54:56 +03:00
|
|
|
SetFailureId(aFailureId);
|
2016-04-29 07:52:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-06-03 21:54:56 +03:00
|
|
|
FeatureState::MaybeSetFailed(bool aEnable, FeatureStatus aStatus, const char* aMessage,
|
|
|
|
const nsACString& aFailureId)
|
2016-04-29 07:52:55 +03:00
|
|
|
{
|
|
|
|
if (!aEnable) {
|
2016-06-03 21:54:56 +03:00
|
|
|
SetFailed(aStatus, aMessage, aFailureId);
|
2016-04-29 07:52:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-06-03 21:54:56 +03:00
|
|
|
FeatureState::MaybeSetFailed(FeatureStatus aStatus, const char* aMessage,
|
|
|
|
const nsACString& aFailureId)
|
2016-04-29 07:52:55 +03:00
|
|
|
{
|
2016-06-03 21:54:56 +03:00
|
|
|
return MaybeSetFailed(IsFeatureStatusSuccess(aStatus), aStatus, aMessage,
|
|
|
|
aFailureId);
|
2016-04-29 07:52:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:54 +03:00
|
|
|
bool
|
|
|
|
FeatureState::DisabledByDefault() const
|
|
|
|
{
|
2016-04-29 07:52:55 +03:00
|
|
|
AssertInitialized();
|
2016-04-29 07:52:54 +03:00
|
|
|
return mDefault.mStatus != FeatureStatus::Available;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FeatureState::IsForcedOnByUser() const
|
|
|
|
{
|
2016-04-29 07:52:55 +03:00
|
|
|
AssertInitialized();
|
2016-04-29 07:52:54 +03:00
|
|
|
return mUser.mStatus == FeatureStatus::ForceEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FeatureState::EnableByDefault()
|
|
|
|
{
|
|
|
|
// User/runtime decisions should not have been made yet.
|
2016-04-29 07:52:56 +03:00
|
|
|
MOZ_ASSERT(!mUser.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mEnvironment.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mRuntime.IsInitialized());
|
2016-04-29 07:52:54 +03:00
|
|
|
|
|
|
|
mDefault.Set(FeatureStatus::Available);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-03 21:54:56 +03:00
|
|
|
FeatureState::DisableByDefault(FeatureStatus aStatus, const char* aMessage,
|
|
|
|
const nsACString& aFailureId)
|
2016-04-29 07:52:54 +03:00
|
|
|
{
|
|
|
|
// User/runtime decisions should not have been made yet.
|
2016-04-29 07:52:56 +03:00
|
|
|
MOZ_ASSERT(!mUser.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mEnvironment.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mRuntime.IsInitialized());
|
2016-04-29 07:52:54 +03:00
|
|
|
|
|
|
|
mDefault.Set(aStatus, aMessage);
|
2016-06-03 21:54:56 +03:00
|
|
|
SetFailureId(aFailureId);
|
2016-04-29 07:52:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FeatureState::SetUser(FeatureStatus aStatus, const char* aMessage)
|
|
|
|
{
|
2016-04-29 07:52:55 +03:00
|
|
|
// Default decision must have been made, but not runtime or environment.
|
2016-04-29 07:52:56 +03:00
|
|
|
MOZ_ASSERT(mDefault.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mEnvironment.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mRuntime.IsInitialized());
|
2016-04-29 07:52:54 +03:00
|
|
|
|
|
|
|
mUser.Set(aStatus, aMessage);
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:55 +03:00
|
|
|
void
|
|
|
|
FeatureState::SetEnvironment(FeatureStatus aStatus, const char* aMessage)
|
|
|
|
{
|
|
|
|
// Default decision must have been made, but not runtime.
|
2016-04-29 07:52:56 +03:00
|
|
|
MOZ_ASSERT(mDefault.IsInitialized());
|
|
|
|
MOZ_ASSERT(!mRuntime.IsInitialized());
|
2016-04-29 07:52:55 +03:00
|
|
|
|
|
|
|
mEnvironment.Set(aStatus, aMessage);
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:54 +03:00
|
|
|
void
|
|
|
|
FeatureState::SetRuntime(FeatureStatus aStatus, const char* aMessage)
|
|
|
|
{
|
2016-04-29 07:52:56 +03:00
|
|
|
AssertInitialized();
|
2016-04-29 07:52:54 +03:00
|
|
|
|
|
|
|
mRuntime.Set(aStatus, aMessage);
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:55 +03:00
|
|
|
const char*
|
|
|
|
FeatureState::GetRuntimeMessage() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsFeatureStatusFailure(mRuntime.mStatus));
|
|
|
|
return mRuntime.mMessage;
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:56 +03:00
|
|
|
void
|
|
|
|
FeatureState::ForEachStatusChange(const StatusIterCallback& aCallback) const
|
|
|
|
{
|
|
|
|
AssertInitialized();
|
|
|
|
|
|
|
|
aCallback("default", mDefault.mStatus, mDefault.MessageOrNull());
|
|
|
|
if (mUser.IsInitialized()) {
|
|
|
|
aCallback("user", mUser.mStatus, mUser.Message());
|
|
|
|
}
|
|
|
|
if (mEnvironment.IsInitialized()) {
|
|
|
|
aCallback("env", mEnvironment.mStatus, mEnvironment.Message());
|
|
|
|
}
|
|
|
|
if (mRuntime.IsInitialized()) {
|
|
|
|
aCallback("runtime", mRuntime.mStatus, mRuntime.Message());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 21:54:56 +03:00
|
|
|
void
|
|
|
|
FeatureState::SetFailureId(const nsACString& aFailureId)
|
|
|
|
{
|
|
|
|
if (mFailureId.IsEmpty()) {
|
|
|
|
mFailureId = aFailureId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsACString&
|
|
|
|
FeatureState::GetFailureId() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsEnabled());
|
|
|
|
return mFailureId;
|
|
|
|
}
|
|
|
|
|
2016-04-29 07:52:54 +03:00
|
|
|
void
|
|
|
|
FeatureState::Instance::Set(FeatureStatus aStatus, const char* aMessage /* = nullptr */)
|
|
|
|
{
|
|
|
|
mStatus = aStatus;
|
|
|
|
if (aMessage) {
|
2016-08-17 01:41:12 +03:00
|
|
|
SprintfLiteral(mMessage, "%s", aMessage);
|
2016-04-29 07:52:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|