Bug 1176342, part 1 - De-COM InitClassesWithNewWrappedGlobal. r=mrbkap

MozReview-Commit-ID: EfRQW3QUXCG

--HG--
extra : rebase_source : 895694d28cc228cfb095639880738c84767e08a8
This commit is contained in:
Andrew McCreight 2017-09-18 16:35:39 -07:00
parent 3fa173a724
commit 36eb42025b
7 changed files with 55 additions and 70 deletions

View file

@ -1719,7 +1719,6 @@ nsMessageManagerScriptExecutor::InitChildGlobalInternal(
AutoSafeJSContext cx;
nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(mPrincipal));
nsIXPConnect* xpc = nsContentUtils::XPConnect();
const uint32_t flags = nsIXPConnect::INIT_JS_STANDARD_CLASSES;
JS::CompartmentOptions options;
@ -1730,14 +1729,13 @@ nsMessageManagerScriptExecutor::InitChildGlobalInternal(
options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
}
nsCOMPtr<nsIXPConnectJSObjectHolder> globalHolder;
nsresult rv =
xpc->InitClassesWithNewWrappedGlobal(cx, aScope, mPrincipal,
JS::Rooted<JSObject*> global(cx);
nsresult rv = xpc::InitClassesWithNewWrappedGlobal(cx, aScope, mPrincipal,
flags, options,
getter_AddRefs(globalHolder));
&global);
NS_ENSURE_SUCCESS(rv, false);
mGlobal = globalHolder->GetJSObject();
mGlobal = global;
NS_ENSURE_TRUE(mGlobal, false);
// Set the location information for the new global, so that tools like

View file

@ -423,13 +423,6 @@ XPCShellEnvironment::Init()
mGlobalHolder.init(cx);
nsCOMPtr<nsIXPConnect> xpc =
do_GetService(nsIXPConnect::GetCID());
if (!xpc) {
NS_ERROR("failed to get nsXPConnect service!");
return false;
}
nsCOMPtr<nsIPrincipal> principal;
nsCOMPtr<nsIScriptSecurityManager> securityManager =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
@ -455,18 +448,17 @@ XPCShellEnvironment::Init()
if (xpc::SharedMemoryEnabled())
options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = xpc->InitClassesWithNewWrappedGlobal(cx,
JS::Rooted<JSObject*> globalObj(cx);
rv = xpc::InitClassesWithNewWrappedGlobal(cx,
static_cast<nsIGlobalObject *>(backstagePass),
principal, 0,
options,
getter_AddRefs(holder));
&globalObj);
if (NS_FAILED(rv)) {
NS_ERROR("InitClassesWithNewWrappedGlobal failed!");
return false;
}
JS::Rooted<JSObject*> globalObj(cx, holder->GetJSObject());
if (!globalObj) {
NS_ERROR("Failed to get global JSObject!");
return false;

View file

@ -279,29 +279,6 @@ interface nsIXPConnect : nsISupports
NS_DEFINE_STATIC_CID_ACCESSOR(NS_XPCONNECT_CID)
%}
/**
* Creates a new global object using the given aCOMObj as the global
* object. The object will be set up according to the flags (defined
* below). If you do not pass INIT_JS_STANDARD_CLASSES, then aCOMObj
* must implement nsIXPCScriptable so it can resolve the standard
* classes when asked by the JS engine.
*
* @param aJSContext the context to use while creating the global object.
* @param aCOMObj the native object that represents the global object.
* @param aPrincipal the principal of the code that will run in this
* compartment. Can be null if not on the main thread.
* @param aFlags one of the flags below specifying what options this
* global object wants.
* @param aOptions JSAPI-specific options for the new compartment.
*/
nsIXPConnectJSObjectHolder
initClassesWithNewWrappedGlobal(
in JSContextPtr aJSContext,
in nsISupports aCOMObj,
in nsIPrincipal aPrincipal,
in uint32_t aFlags,
in JSCompartmentOptions aOptions);
const uint32_t INIT_JS_STANDARD_CLASSES = 1 << 0;
const uint32_t DONT_FIRE_ONNEWGLOBALHOOK = 1 << 1;
const uint32_t OMIT_COMPONENTS_OBJECT = 1 << 2;

View file

@ -513,17 +513,15 @@ mozJSComponentLoader::CreateLoaderGlobal(JSContext* aCx,
// Defer firing OnNewGlobalObject until after the __URI__ property has
// been defined so the JS debugger can tell what module the global is
// for
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = nsXPConnect::XPConnect()->
InitClassesWithNewWrappedGlobal(aCx,
RootedObject global(aCx);
rv = xpc::InitClassesWithNewWrappedGlobal(aCx,
static_cast<nsIGlobalObject*>(backstagePass),
nsContentUtils::GetSystemPrincipal(),
nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
options,
getter_AddRefs(holder));
&global);
NS_ENSURE_SUCCESS_VOID(rv);
RootedObject global(aCx, holder->GetJSObject());
NS_ENSURE_TRUE_VOID(global);
backstagePass->SetGlobalObject(global);

View file

@ -1306,14 +1306,13 @@ XRE_XPCShellMain(int argc, char** argv, char** envp,
if (xpc::SharedMemoryEnabled())
options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
options.behaviors().setVersion(JSVERSION_DEFAULT);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = nsXPConnect::XPConnect()->
InitClassesWithNewWrappedGlobal(cx,
JS::Rooted<JSObject*> glob(cx);
rv = xpc::InitClassesWithNewWrappedGlobal(cx,
static_cast<nsIGlobalObject*>(backstagePass),
systemprincipal,
0,
options,
getter_AddRefs(holder));
&glob);
if (NS_FAILED(rv))
return 1;
@ -1343,7 +1342,6 @@ XRE_XPCShellMain(int argc, char** argv, char** envp,
#endif
{
JS::Rooted<JSObject*> glob(cx, holder->GetJSObject());
if (!glob) {
return 1;
}

View file

@ -560,19 +560,16 @@ InitGlobalObject(JSContext* aJSContext, JS::Handle<JSObject*> aGlobal, uint32_t
return true;
}
} // namespace xpc
NS_IMETHODIMP
nsXPConnect::InitClassesWithNewWrappedGlobal(JSContext * aJSContext,
nsresult
InitClassesWithNewWrappedGlobal(JSContext* aJSContext,
nsISupports* aCOMObj,
nsIPrincipal * aPrincipal,
nsIPrincipal* aPrincipal,
uint32_t aFlags,
JS::CompartmentOptions& aOptions,
nsIXPConnectJSObjectHolder** _retval)
MutableHandleObject aNewGlobal)
{
MOZ_ASSERT(aJSContext, "bad param");
MOZ_ASSERT(aCOMObj, "bad param");
MOZ_ASSERT(_retval, "bad param");
// We pass null for the 'extra' pointer during global object creation, so
// we need to have a principal.
@ -598,10 +595,12 @@ nsXPConnect::InitClassesWithNewWrappedGlobal(JSContext * aJSContext,
if (!InitGlobalObject(aJSContext, global, aFlags))
return UnexpectedFailure(NS_ERROR_FAILURE);
wrappedGlobal.forget(_retval);
aNewGlobal.set(global);
return NS_OK;
}
} // namespace xpc
static nsresult
NativeInterface2JSObject(HandleObject aScope,
nsISupports* aCOMObj,

View file

@ -175,6 +175,29 @@ XrayAwareCalleeGlobalForSpecializedGetters(JSContext* cx,
void
TraceXPCGlobal(JSTracer* trc, JSObject* obj);
/**
* Creates a new global object using the given aCOMObj as the global
* object. The object will be set up according to the flags (defined
* below). If you do not pass INIT_JS_STANDARD_CLASSES, then aCOMObj
* must implement nsIXPCScriptable so it can resolve the standard
* classes when asked by the JS engine.
*
* @param aJSContext the context to use while creating the global object.
* @param aCOMObj the native object that represents the global object.
* @param aPrincipal the principal of the code that will run in this
* compartment. Can be null if not on the main thread.
* @param aFlags one of the flags below specifying what options this
* global object wants.
* @param aOptions JSAPI-specific options for the new compartment.
*/
nsresult
InitClassesWithNewWrappedGlobal(JSContext* aJSContext,
nsISupports* aCOMObj,
nsIPrincipal* aPrincipal,
uint32_t aFlags,
JS::CompartmentOptions& aOptions,
JS::MutableHandleObject aNewGlobal);
} /* namespace xpc */
namespace JS {