From e4ae5f26dad774857864b1f8036245c8320b0cff Mon Sep 17 00:00:00 2001 From: Jan de Mooij Date: Tue, 5 Jul 2016 16:49:46 +0200 Subject: [PATCH] Bug 1283855 part 20 - Make more principals code take JSContext instead of JSRuntime. r=luke --HG-- extra : rebase_source : ad5f2f5b5bef9d20e4e248898a3c754adb306683 --- caps/nsScriptSecurityManager.cpp | 11 ++++++----- dom/workers/RuntimeService.cpp | 2 +- js/public/Principals.h | 16 ++++++++-------- js/src/jsapi-tests/testChromeBuffer.cpp | 2 +- js/src/jsapi-tests/testCloneScript.cpp | 2 +- js/src/jsapi-tests/testStructuredClone.cpp | 4 ++-- js/src/jsapi.cpp | 16 ++++++++-------- js/src/shell/js.cpp | 6 +++--- 8 files changed, 30 insertions(+), 29 deletions(-) diff --git a/caps/nsScriptSecurityManager.cpp b/caps/nsScriptSecurityManager.cpp index 440551f562ee..58b3bee10e06 100644 --- a/caps/nsScriptSecurityManager.cpp +++ b/caps/nsScriptSecurityManager.cpp @@ -1386,11 +1386,12 @@ nsresult nsScriptSecurityManager::Init() JSPrincipalsSubsume, }; - MOZ_ASSERT(!JS_GetSecurityCallbacks(JS_GetContext(sRuntime))); - JS_SetSecurityCallbacks(JS_GetContext(sRuntime), &securityCallbacks); - JS_InitDestroyPrincipalsCallback(sRuntime, nsJSPrincipals::Destroy); + JSContext* cx = JS_GetContext(sRuntime); + MOZ_ASSERT(!JS_GetSecurityCallbacks(cx)); + JS_SetSecurityCallbacks(cx, &securityCallbacks); + JS_InitDestroyPrincipalsCallback(cx, nsJSPrincipals::Destroy); - JS_SetTrustedPrincipals(sRuntime, system); + JS_SetTrustedPrincipals(cx, system); return NS_OK; } @@ -1415,7 +1416,7 @@ nsScriptSecurityManager::Shutdown() { if (sRuntime) { JS_SetSecurityCallbacks(JS_GetContext(sRuntime), nullptr); - JS_SetTrustedPrincipals(sRuntime, nullptr); + JS_SetTrustedPrincipals(JS_GetContext(sRuntime), nullptr); sRuntime = nullptr; } diff --git a/dom/workers/RuntimeService.cpp b/dom/workers/RuntimeService.cpp index ef787c28dd6a..886ea41b1b77 100644 --- a/dom/workers/RuntimeService.cpp +++ b/dom/workers/RuntimeService.cpp @@ -892,7 +892,7 @@ public: JSContext* cx = JS_GetContext(rt); js::SetPreserveWrapperCallback(cx, PreserveWrapper); - JS_InitDestroyPrincipalsCallback(rt, DestroyWorkerPrincipals); + JS_InitDestroyPrincipalsCallback(cx, DestroyWorkerPrincipals); JS_SetWrapObjectCallbacks(cx, &WrapObjectCallbacks); if (mWorkerPrivate->IsDedicatedWorker()) { JS_SetFutexCanWait(cx); diff --git a/js/public/Principals.h b/js/public/Principals.h index 0b19b97a9c17..cf6c813af42c 100644 --- a/js/public/Principals.h +++ b/js/public/Principals.h @@ -87,14 +87,14 @@ JS_GetSecurityCallbacks(JSContext* cx); * untrusted script has exhausted the stack. This function sets the * runtime-wide trusted principal. * - * This principals is not held (via JS_HoldPrincipals/JS_DropPrincipals) since - * there is no available JSContext. Instead, the caller must ensure that the - * given principals stays valid for as long as 'rt' may point to it. If the - * principals would be destroyed before 'rt', JS_SetTrustedPrincipals must be - * called again, passing nullptr for 'prin'. + * This principals is not held (via JS_HoldPrincipals/JS_DropPrincipals). + * Instead, the caller must ensure that the given principals stays valid for as + * long as 'cx' may point to it. If the principals would be destroyed before + * 'cx', JS_SetTrustedPrincipals must be called again, passing nullptr for + * 'prin'. */ extern JS_PUBLIC_API(void) -JS_SetTrustedPrincipals(JSRuntime* rt, JSPrincipals* prin); +JS_SetTrustedPrincipals(JSContext* cx, JSPrincipals* prin); typedef void (* JSDestroyPrincipalsOp)(JSPrincipals* principals); @@ -105,7 +105,7 @@ typedef void * only once per JS runtime. */ extern JS_PUBLIC_API(void) -JS_InitDestroyPrincipalsCallback(JSRuntime* rt, JSDestroyPrincipalsOp destroyPrincipals); +JS_InitDestroyPrincipalsCallback(JSContext* cx, JSDestroyPrincipalsOp destroyPrincipals); /* * Read a JSPrincipals instance from the given |reader| and initialize the out @@ -126,7 +126,7 @@ using JSReadPrincipalsOp = bool (*)(JSContext* cx, JSStructuredCloneReader* read * buffer. The initialization can be done only once per JS runtime. */ extern JS_PUBLIC_API(void) -JS_InitReadPrincipalsCallback(JSRuntime* rt, JSReadPrincipalsOp read); +JS_InitReadPrincipalsCallback(JSContext* cx, JSReadPrincipalsOp read); #endif /* js_Principals_h */ diff --git a/js/src/jsapi-tests/testChromeBuffer.cpp b/js/src/jsapi-tests/testChromeBuffer.cpp index 028c4bcc2b47..455c069acfda 100644 --- a/js/src/jsapi-tests/testChromeBuffer.cpp +++ b/js/src/jsapi-tests/testChromeBuffer.cpp @@ -48,7 +48,7 @@ CallTrusted(JSContext* cx, unsigned argc, JS::Value* vp) BEGIN_TEST(testChromeBuffer) { - JS_SetTrustedPrincipals(rt, &system_principals); + JS_SetTrustedPrincipals(cx, &system_principals); JS::CompartmentOptions options; trusted_glob.init(cx, JS_NewGlobalObject(cx, &global_class, &system_principals, diff --git a/js/src/jsapi-tests/testCloneScript.cpp b/js/src/jsapi-tests/testCloneScript.cpp index 29388990c315..89c6a97a49e6 100644 --- a/js/src/jsapi-tests/testCloneScript.cpp +++ b/js/src/jsapi-tests/testCloneScript.cpp @@ -92,7 +92,7 @@ DestroyPrincipals(JSPrincipals* principals) BEGIN_TEST(test_cloneScriptWithPrincipals) { - JS_InitDestroyPrincipalsCallback(rt, DestroyPrincipals); + JS_InitDestroyPrincipalsCallback(cx, DestroyPrincipals); JSPrincipals* principalsA = new Principals(); AutoDropPrincipals dropA(rt, principalsA); diff --git a/js/src/jsapi-tests/testStructuredClone.cpp b/js/src/jsapi-tests/testStructuredClone.cpp index 5491787c2662..a01afccad5a2 100644 --- a/js/src/jsapi-tests/testStructuredClone.cpp +++ b/js/src/jsapi-tests/testStructuredClone.cpp @@ -131,8 +131,8 @@ JSSecurityCallbacks StructuredCloneTestPrincipals::securityCallbacks = { BEGIN_TEST(testStructuredClone_SavedFrame) { JS_SetSecurityCallbacks(cx, &StructuredCloneTestPrincipals::securityCallbacks); - JS_InitDestroyPrincipalsCallback(rt, StructuredCloneTestPrincipals::destroy); - JS_InitReadPrincipalsCallback(rt, StructuredCloneTestPrincipals::read); + JS_InitDestroyPrincipalsCallback(cx, StructuredCloneTestPrincipals::destroy); + JS_InitReadPrincipalsCallback(cx, StructuredCloneTestPrincipals::read); auto testPrincipals = new StructuredCloneTestPrincipals(42, 0); CHECK(testPrincipals); diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 9a5c3bbd33c3..8f6967e453e8 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -3289,25 +3289,25 @@ JS_GetSecurityCallbacks(JSContext* cx) } JS_PUBLIC_API(void) -JS_SetTrustedPrincipals(JSRuntime* rt, JSPrincipals* prin) +JS_SetTrustedPrincipals(JSContext* cx, JSPrincipals* prin) { - rt->setTrustedPrincipals(prin); + cx->setTrustedPrincipals(prin); } extern JS_PUBLIC_API(void) -JS_InitDestroyPrincipalsCallback(JSRuntime* rt, JSDestroyPrincipalsOp destroyPrincipals) +JS_InitDestroyPrincipalsCallback(JSContext* cx, JSDestroyPrincipalsOp destroyPrincipals) { MOZ_ASSERT(destroyPrincipals); - MOZ_ASSERT(!rt->destroyPrincipals); - rt->destroyPrincipals = destroyPrincipals; + MOZ_ASSERT(!cx->destroyPrincipals); + cx->destroyPrincipals = destroyPrincipals; } extern JS_PUBLIC_API(void) -JS_InitReadPrincipalsCallback(JSRuntime* rt, JSReadPrincipalsOp read) +JS_InitReadPrincipalsCallback(JSContext* cx, JSReadPrincipalsOp read) { MOZ_ASSERT(read); - MOZ_ASSERT(!rt->readPrincipals); - rt->readPrincipals = read; + MOZ_ASSERT(!cx->readPrincipals); + cx->readPrincipals = read; } JS_PUBLIC_API(JSFunction*) diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 1bfcf13bb4a2..aeb1df64dcd0 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -2949,7 +2949,7 @@ WorkerMain(void* arg) JS_SetRuntimePrivate(rt, sr.get()); JS_SetFutexCanWait(cx); JS::SetWarningReporter(cx, WarningReporter); - JS_InitDestroyPrincipalsCallback(rt, ShellPrincipals::destroy); + JS_InitDestroyPrincipalsCallback(cx, ShellPrincipals::destroy); SetWorkerRuntimeOptions(rt); if (!JS::InitSelfHostedCode(cx)) { @@ -7396,9 +7396,9 @@ main(int argc, char** argv, char** envp) if (availMem > 0) JS_SetGCParametersBasedOnAvailableMemory(cx, availMem); - JS_SetTrustedPrincipals(rt, &ShellPrincipals::fullyTrusted); + JS_SetTrustedPrincipals(cx, &ShellPrincipals::fullyTrusted); JS_SetSecurityCallbacks(cx, &ShellPrincipals::securityCallbacks); - JS_InitDestroyPrincipalsCallback(rt, ShellPrincipals::destroy); + JS_InitDestroyPrincipalsCallback(cx, ShellPrincipals::destroy); JS_SetInterruptCallback(rt, ShellInterruptCallback); JS::SetBuildIdOp(cx, ShellBuildId);