Skip to content

Commit

Permalink
Bug 1407494 (part 9) - Merge PREF_RegisterCallback() and PREF_Registe…
Browse files Browse the repository at this point in the history
…rPriorityCallback. r=glandium.

This avoids some code duplication.

MozReview-Commit-ID: 2Cb4YtL5RSu

--HG--
extra : rebase_source : 76f2146e8f465608371b8650d96494626f0bee82
  • Loading branch information
nnethercote committed Oct 13, 2017
1 parent 475691a commit ab27f10
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions modules/libpref/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit ab27f10

Please sign in to comment.