Bug 1255709. Simplify the JSContext/GlobalObject handling in ServiceWorkerRegistrationMainThread::GetPushManager a bit. r=bkelly

This commit is contained in:
Boris Zbarsky 2016-03-11 16:43:31 -05:00
Родитель ab1c6dbdb0
Коммит 57fc0e989c
5 изменённых файлов: 14 добавлений и 23 удалений

Просмотреть файл

@ -13776,8 +13776,7 @@ nsGlobalWindow::GetExternal(ErrorResult& aRv)
if (!mExternal) {
AutoJSContext cx;
JS::Rooted<JSObject*> jsImplObj(cx);
ConstructJSImplementation(cx, "@mozilla.org/sidebar;1",
this, &jsImplObj, aRv);
ConstructJSImplementation("@mozilla.org/sidebar;1", this, &jsImplObj, aRv);
if (aRv.Failed()) {
return nullptr;
}

Просмотреть файл

@ -2259,7 +2259,7 @@ GetContentGlobalForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
}
already_AddRefed<nsIGlobalObject>
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
ConstructJSImplementation(const char* aContractId,
const GlobalObject& aGlobal,
JS::MutableHandle<JSObject*> aObject,
ErrorResult& aRv)
@ -2271,7 +2271,7 @@ ConstructJSImplementation(JSContext* aCx, const char* aContractId,
return nullptr;
}
ConstructJSImplementation(aCx, aContractId, global, aObject, aRv);
ConstructJSImplementation(aContractId, global, aObject, aRv);
if (aRv.Failed()) {
return nullptr;
@ -2280,11 +2280,13 @@ ConstructJSImplementation(JSContext* aCx, const char* aContractId,
}
void
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
ConstructJSImplementation(const char* aContractId,
nsIGlobalObject* aGlobal,
JS::MutableHandle<JSObject*> aObject,
ErrorResult& aRv)
{
MOZ_ASSERT(NS_IsMainThread());
// Make sure to divorce ourselves from the calling JS while creating and
// initializing the object, so exceptions from that will get reported
// properly, since those are never exceptions that a spec wants to be thrown.
@ -2307,7 +2309,7 @@ ConstructJSImplementation(JSContext* aCx, const char* aContractId,
do_QueryInterface(implISupports);
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
if (gpi) {
JS::Rooted<JS::Value> initReturn(aCx);
JS::Rooted<JS::Value> initReturn(nsContentUtils::RootingCxForThread());
rv = gpi->Init(window, &initReturn);
if (NS_FAILED(rv)) {
aRv.Throw(rv);

Просмотреть файл

@ -2655,13 +2655,13 @@ GetContentGlobalForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
nsIGlobalObject** global);
void
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
ConstructJSImplementation(const char* aContractId,
nsIGlobalObject* aGlobal,
JS::MutableHandle<JSObject*> aObject,
ErrorResult& aRv);
already_AddRefed<nsIGlobalObject>
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
ConstructJSImplementation(const char* aContractId,
const GlobalObject& aGlobal,
JS::MutableHandle<JSObject*> aObject,
ErrorResult& aRv);

Просмотреть файл

@ -14371,7 +14371,7 @@ def genConstructorBody(descriptor, initCall=""):
"""
JS::Rooted<JSObject*> jsImplObj(cx);
nsCOMPtr<nsIGlobalObject> globalHolder =
ConstructJSImplementation(cx, "${contractId}", global, &jsImplObj, aRv);
ConstructJSImplementation("${contractId}", global, &jsImplObj, aRv);
if (aRv.Failed()) {
return nullptr;
}

Просмотреть файл

@ -22,6 +22,7 @@
#include "nsIServiceWorkerManager.h"
#include "nsISupportsPrimitives.h"
#include "nsPIDOMWindow.h"
#include "nsContentUtils.h"
#include "WorkerPrivate.h"
#include "Workers.h"
@ -767,21 +768,10 @@ ServiceWorkerRegistrationMainThread::GetPushManager(ErrorResult& aRv)
return nullptr;
}
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(globalObject))) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
JSContext* cx = jsapi.cx();
JS::RootedObject globalJs(cx, globalObject->GetGlobalJSObject());
GlobalObject global(cx, globalJs);
// TODO: bug 1148117. This will fail when swr is exposed on workers
JS::Rooted<JSObject*> jsImplObj(cx);
nsCOMPtr<nsIGlobalObject> unused = ConstructJSImplementation(cx, "@mozilla.org/push/PushManager;1",
global, &jsImplObj, aRv);
JS::Rooted<JSObject*> jsImplObj(nsContentUtils::RootingCxForThread());
ConstructJSImplementation("@mozilla.org/push/PushManager;1",
globalObject, &jsImplObj, aRv);
if (aRv.Failed()) {
return nullptr;
}