Bug 1045945 - Initialize the destroy principals callback for workers r=bent

This commit is contained in:
Jon Coppeard 2014-09-15 16:49:11 +01:00
parent 3ef1f75536
commit ceda575b38
4 changed files with 25 additions and 4 deletions

View File

@ -6,23 +6,37 @@
#include "Principal.h"
#include "jsapi.h"
#include "mozilla/Assertions.h"
BEGIN_WORKERS_NAMESPACE
JSPrincipals*
GetWorkerPrincipal()
{
static Atomic<bool> sInitialized(false);
static JSPrincipals sPrincipal;
bool isInitialized = sInitialized.exchange(true);
if (!isInitialized) {
sPrincipal.refcount = 1;
/*
* To make sure the the principals refcount is initialized to one, atomically
* increment it on every pass though this function. If we discover this wasn't
* the first time, decrement it again. This avoids the need for
* synchronization.
*/
int32_t prevRefcount = sPrincipal.refcount++;
if (prevRefcount > 0) {
--sPrincipal.refcount;
} else {
#ifdef DEBUG
sPrincipal.debugToken = kJSPrincipalsDebugToken;
#endif
}
return &sPrincipal;
}
void
DestroyWorkerPrincipals(JSPrincipals* aPrincipals)
{
MOZ_ASSERT_UNREACHABLE("Worker principals refcount should never fall below one");
}
END_WORKERS_NAMESPACE

View File

@ -13,6 +13,9 @@ BEGIN_WORKERS_NAMESPACE
JSPrincipals*
GetWorkerPrincipal();
void
DestroyWorkerPrincipals(JSPrincipals* aPrincipals);
END_WORKERS_NAMESPACE
#endif /* mozilla_dom_workers_principal_h__ */

View File

@ -61,6 +61,7 @@
#include "nsThreadManager.h"
#endif
#include "Principal.h"
#include "ServiceWorker.h"
#include "SharedWorker.h"
#include "WorkerPrivate.h"
@ -852,6 +853,7 @@ public:
WORKER_DEFAULT_NURSERY_SIZE),
mWorkerPrivate(aWorkerPrivate)
{
JS_InitDestroyPrincipalsCallback(Runtime(), DestroyWorkerPrincipals);
}
~WorkerJSRuntime()

View File

@ -24,6 +24,8 @@ struct JSPrincipals {
uint32_t debugToken;
#endif
JSPrincipals() : refcount(0) {}
void setDebugToken(uint32_t token) {
# ifdef JS_DEBUG
debugToken = token;