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

View file

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

View file

@ -279,29 +279,6 @@ interface nsIXPConnect : nsISupports
NS_DEFINE_STATIC_CID_ACCESSOR(NS_XPCONNECT_CID) 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 INIT_JS_STANDARD_CLASSES = 1 << 0;
const uint32_t DONT_FIRE_ONNEWGLOBALHOOK = 1 << 1; const uint32_t DONT_FIRE_ONNEWGLOBALHOOK = 1 << 1;
const uint32_t OMIT_COMPONENTS_OBJECT = 1 << 2; 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 // Defer firing OnNewGlobalObject until after the __URI__ property has
// been defined so the JS debugger can tell what module the global is // been defined so the JS debugger can tell what module the global is
// for // for
nsCOMPtr<nsIXPConnectJSObjectHolder> holder; RootedObject global(aCx);
rv = nsXPConnect::XPConnect()-> rv = xpc::InitClassesWithNewWrappedGlobal(aCx,
InitClassesWithNewWrappedGlobal(aCx, static_cast<nsIGlobalObject*>(backstagePass),
static_cast<nsIGlobalObject*>(backstagePass), nsContentUtils::GetSystemPrincipal(),
nsContentUtils::GetSystemPrincipal(), nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK, options,
options, &global);
getter_AddRefs(holder));
NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_SUCCESS_VOID(rv);
RootedObject global(aCx, holder->GetJSObject());
NS_ENSURE_TRUE_VOID(global); NS_ENSURE_TRUE_VOID(global);
backstagePass->SetGlobalObject(global); backstagePass->SetGlobalObject(global);

View file

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

View file

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

View file

@ -175,6 +175,29 @@ XrayAwareCalleeGlobalForSpecializedGetters(JSContext* cx,
void void
TraceXPCGlobal(JSTracer* trc, JSObject* obj); 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 xpc */
namespace JS { namespace JS {