Bug 1407494 (part 9) - Merge PREF_RegisterCallback() and PREF_RegisterPriorityCallback. r=glandium.

This avoids some code duplication.

MozReview-Commit-ID: 2Cb4YtL5RSu
This commit is contained in:
Nicholas Nethercote
2017-10-13 15:24:59 +11:00
parent 06b412d376
commit a0479879cf

View File

@@ -373,13 +373,10 @@ PREF_ClearAllUserPrefs();
// returns PREF_NOERROR if a callback was found that matched all the
// parameters; otherwise it returns PREF_ERROR.
void
PREF_RegisterPriorityCallback(const char* aPrefNode,
PrefChangedFunc aCallback,
void* aData);
void
PREF_RegisterCallback(const char* aPrefNode,
PrefChangedFunc aCallback,
void* aData);
void* aData,
bool aIsPriority);
nsresult
PREF_UnregisterCallback(const char* aPrefNode,
PrefChangedFunc aCallback,
@@ -450,6 +447,8 @@ PLDHashTable* gHashTable;
static ArenaAllocator<8192, 4> gPrefNameArena;
// The callback list contains all the priority callbacks followed by the
// non-priority callbacks. gLastPriorityNode records where the first part ends.
static CallbackNode* gFirstCallback = nullptr;
static CallbackNode* gLastPriorityNode = nullptr;
@@ -1302,31 +1301,12 @@ PREF_PrefIsLocked(const char* aPrefName)
return result;
}
// Adds a node to the beginning of the callback list.
void
PREF_RegisterPriorityCallback(const char* aPrefNode,
PrefChangedFunc aCallback,
void* aData)
{
NS_PRECONDITION(aPrefNode, "aPrefNode must not be nullptr");
NS_PRECONDITION(aCallback, "aCallback must not be nullptr");
auto node = new CallbackNode();
node->mDomain = moz_xstrdup(aPrefNode);
node->mFunc = aCallback;
node->mData = aData;
node->mNext = gFirstCallback;
gFirstCallback = node;
if (!gLastPriorityNode) {
gLastPriorityNode = node;
}
}
// Adds a node to the end of the callback list.
// Adds a node to the callback list; the position depends on aIsPriority.
void
PREF_RegisterCallback(const char* aPrefNode,
PrefChangedFunc aCallback,
void* aData)
void* aData,
bool aIsPriority)
{
NS_PRECONDITION(aPrefNode, "aPrefNode must not be nullptr");
NS_PRECONDITION(aCallback, "aCallback must not be nullptr");
@@ -1335,12 +1315,23 @@ PREF_RegisterCallback(const char* aPrefNode,
node->mDomain = moz_xstrdup(aPrefNode);
node->mFunc = aCallback;
node->mData = aData;
if (gLastPriorityNode) {
node->mNext = gLastPriorityNode->mNext;
gLastPriorityNode->mNext = node;
} else {
if (aIsPriority) {
// Add to the start of the list.
node->mNext = gFirstCallback;
gFirstCallback = node;
if (!gLastPriorityNode) {
gLastPriorityNode = node;
}
} else {
// Add to the start of the non-priority part of the list.
if (gLastPriorityNode) {
node->mNext = gLastPriorityNode->mNext;
gLastPriorityNode->mNext = node;
} else {
node->mNext = gFirstCallback;
gFirstCallback = node;
}
}
}
@@ -3176,7 +3167,8 @@ nsPrefBranch::AddObserver(const char* aDomain,
// aDomain == nullptr is the only possible failure, and we trapped it with
// NS_ENSURE_ARG above.
const PrefName& pref = GetPrefName(aDomain);
PREF_RegisterCallback(pref.get(), NotifyObserver, pCallback);
PREF_RegisterCallback(
pref.get(), NotifyObserver, pCallback, /* isPriority */ false);
return NS_OK;
}
@@ -5338,8 +5330,10 @@ RegisterPriorityCallback(PrefChangedFunc aCallback,
observer = new ValueObserver(aPref, aCallback, Preferences::ExactMatch);
observer->AppendClosure(aClosure);
PREF_RegisterPriorityCallback(
aPref, NotifyObserver, static_cast<nsIObserver*>(observer));
PREF_RegisterCallback(aPref,
NotifyObserver,
static_cast<nsIObserver*>(observer),
/* isPriority */ true);
gObserverTable->Put(observer, observer);
}