Bug 1489147 - Remove the XPCOM component registration for nsXPConnect; r=mccr8

We move the XPConnect() singleton accessor to nsIXConnect to make it available for consumers outside of XPConnect.  Most of the consumers of the singleton accessor just need the nsIXPConnect public interface, except for the IsShuttingDown() member which this patch adds to nsIXPConnect as well.

Differential Revision: https://phabricator.services.mozilla.com/D5151
This commit is contained in:
Ehsan Akhgari 2018-09-06 10:01:58 -04:00
parent 31829579c9
commit 6bbaf2cf8c
20 changed files with 72 additions and 95 deletions

View File

@ -115,7 +115,7 @@ nsStructuredCloneContainer::DeserializeToVariant(JSContext* aCx,
// Now wrap the JS::Value as an nsIVariant.
nsCOMPtr<nsIVariant> varStateObj;
nsCOMPtr<nsIXPConnect> xpconnect = do_GetService(nsIXPConnect::GetCID());
nsCOMPtr<nsIXPConnect> xpconnect = nsIXPConnect::XPConnect();
NS_ENSURE_STATE(xpconnect);
xpconnect->JSValToVariant(aCx, jsStateObj, getter_AddRefs(varStateObj));
NS_ENSURE_STATE(varStateObj);

View File

@ -734,7 +734,7 @@ _getpluginelement(NPP npp)
}
JSContext* cx = jsapi.cx();
nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID()));
nsCOMPtr<nsIXPConnect> xpc(nsIXPConnect::XPConnect());
NS_ENSURE_TRUE(xpc, nullptr);
JS::RootedValue val(cx);

View File

@ -36,8 +36,6 @@ static bool sandboxEnabled;
nsresult CentralizedAdminPrefManagerInit(bool aSandboxEnabled)
{
nsresult rv;
// If the sandbox is already created, no need to create it again.
if (autoconfigSb.initialized())
return NS_OK;
@ -45,10 +43,7 @@ nsresult CentralizedAdminPrefManagerInit(bool aSandboxEnabled)
sandboxEnabled = aSandboxEnabled;
// Grab XPConnect.
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
// Grab the system principal.
nsCOMPtr<nsIPrincipal> principal;
@ -58,7 +53,7 @@ nsresult CentralizedAdminPrefManagerInit(bool aSandboxEnabled)
// Create a sandbox.
AutoSafeJSContext cx;
JS::Rooted<JSObject*> sandbox(cx);
rv = xpc->CreateSandbox(cx, principal, sandbox.address());
nsresult rv = xpc->CreateSandbox(cx, principal, sandbox.address());
NS_ENSURE_SUCCESS(rv, rv);
// Unwrap, store and root the sandbox.
@ -117,8 +112,6 @@ nsresult EvaluateAdminConfigScript(JS::HandleObject sandbox,
const char *filename, bool globalContext,
bool callbacks, bool skipFirstLine)
{
nsresult rv = NS_OK;
if (skipFirstLine) {
/* In order to protect the privacy of the JavaScript preferences file
* from loading by the browser, we make the first line unparseable
@ -142,10 +135,7 @@ nsresult EvaluateAdminConfigScript(JS::HandleObject sandbox,
}
// Grab XPConnect.
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
AutoJSAPI jsapi;
if (!jsapi.Init(sandbox)) {
@ -176,8 +166,8 @@ nsresult EvaluateAdminConfigScript(JS::HandleObject sandbox,
return NS_ERROR_UNEXPECTED;
}
}
rv = xpc->EvalInSandboxObject(convertedScript, filename, cx,
sandbox, &v);
nsresult rv = xpc->EvalInSandboxObject(convertedScript, filename, cx,
sandbox, &v);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;

View File

@ -200,7 +200,7 @@ DumpXPC(JSContext *cx,
return false;
}
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
if (xpc)
xpc->DebugDump(int16_t(depth));
args.rval().setUndefined();

View File

@ -37,9 +37,6 @@ JSDebugger::~JSDebugger()
NS_IMETHODIMP
JSDebugger::AddClass(JS::Handle<JS::Value> global, JSContext* cx)
{
nsresult rv;
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
if (!global.isObject()) {
return NS_ERROR_INVALID_ARG;
}

View File

@ -191,19 +191,12 @@ interface nsIXPCWrappedJSObjectGetter : nsISupports
/***************************************************************************/
%{ C++
// For use with the service manager
// {CB6593E0-F9B2-11d2-BDD6-000064657374}
#define NS_XPCONNECT_CID \
{ 0xcb6593e0, 0xf9b2, 0x11d2, \
{ 0xbd, 0xd6, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
%}
[noscript, uuid(768507b5-b981-40c7-8276-f6a1da502a24)]
[noscript, builtinclass, uuid(768507b5-b981-40c7-8276-f6a1da502a24)]
interface nsIXPConnect : nsISupports
{
%{ C++
NS_DEFINE_STATIC_CID_ACCESSOR(NS_XPCONNECT_CID)
// This gets a non-addref'd pointer.
static nsIXPConnect* XPConnect();
%}
/**
@ -376,4 +369,6 @@ interface nsIXPConnect : nsISupports
[noscript] JSObjectPtr readFunction(in nsIObjectInputStream aStream,
in JSContextPtr aJSContext);
[infallible] readonly attribute boolean isShuttingDown;
};

View File

@ -69,7 +69,7 @@ JSValIsInterfaceOfType(JSContext* cx, HandleValue v, REFNSIID iid)
if (v.isPrimitive())
return false;
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
RootedObject obj(cx, &v.toObject());
return NS_SUCCEEDED(xpc->GetWrappedNativeOfJSObject(cx, obj, getter_AddRefs(wn))) && wn &&
NS_SUCCEEDED(wn->Native()->QueryInterface(iid, getter_AddRefs(iface))) && iface;
@ -258,7 +258,7 @@ nsXPCComponents_Interfaces::Resolve(nsIXPConnectWrappedNative* wrapper,
nsCOMPtr<nsIJSIID> nsid = nsJSIID::NewID(info);
if (nsid) {
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
RootedObject idobj(cx);
if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
static_cast<nsIJSIID*>(nsid),
@ -446,7 +446,7 @@ nsXPCComponents_InterfacesByID::Resolve(nsIXPConnectWrappedNative* wrapper,
if (!nsid)
return NS_ERROR_OUT_OF_MEMORY;
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
RootedObject idobj(cx);
if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
static_cast<nsIJSIID*>(nsid),
@ -631,7 +631,7 @@ nsXPCComponents_Classes::Resolve(nsIXPConnectWrappedNative* wrapper,
name[0] != '{') { // we only allow contractids here
nsCOMPtr<nsIJSCID> nsid = nsJSCID::NewID(name.get());
if (nsid) {
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
RootedObject idobj(cx);
if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
static_cast<nsIJSCID*>(nsid),
@ -841,7 +841,7 @@ nsXPCComponents_ClassesByID::Resolve(nsIXPConnectWrappedNative* wrapper,
{
nsCOMPtr<nsIJSCID> nsid = nsJSCID::NewID(name.get());
if (nsid) {
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
RootedObject idobj(cx);
if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
static_cast<nsIJSCID*>(nsid),
@ -1320,7 +1320,7 @@ nsXPCComponents_Exception::Construct(nsIXPConnectWrappedNative* wrapper, JSConte
struct MOZ_STACK_CLASS ExceptionArgParser
{
ExceptionArgParser(JSContext* context,
nsXPConnect* xpconnect)
nsIXPConnect* xpconnect)
: eMsg("exception")
, eResult(NS_ERROR_FAILURE)
, cx(context)
@ -1464,7 +1464,7 @@ struct MOZ_STACK_CLASS ExceptionArgParser
// Various bits and pieces that are helpful to have around.
JSContext* cx;
nsXPConnect* xpc;
nsIXPConnect* xpc;
};
// static
@ -1473,7 +1473,7 @@ nsXPCComponents_Exception::CallOrConstruct(nsIXPConnectWrappedNative* wrapper,
JSContext* cx, HandleObject obj,
const CallArgs& args, bool* _retval)
{
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
MOZ_DIAGNOSTIC_ASSERT(nsContentUtils::IsCallerChrome());
@ -1682,7 +1682,7 @@ nsresult
nsXPCConstructor::CallOrConstruct(nsIXPConnectWrappedNative* wrapper,JSContext* cx,
HandleObject obj, const CallArgs& args, bool* _retval)
{
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
// security check not required because we are going to call through the
// code which is reflected into JS which will do that for us later.
@ -1859,7 +1859,7 @@ nsXPCComponents_Constructor::CallOrConstruct(nsIXPConnectWrappedNative* wrapper,
// get the various other object pointers we need
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
XPCWrappedNativeScope* scope = ObjectScope(obj);
nsCOMPtr<nsIXPCComponents> comp;

View File

@ -758,7 +758,7 @@ xpc_NewIDObject(JSContext* cx, HandleObject scope, const nsID& aID)
nsCOMPtr<nsIJSID> iid = nsJSID::NewID(aID);
if (iid) {
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
if (xpc) {
xpc->WrapNative(cx, scope, static_cast<nsISupports*>(iid),
NS_GET_IID(nsIJSID), obj.address());

View File

@ -677,7 +677,7 @@ void XPCJSRuntime::TraceNativeBlackRoots(JSTracer* trc)
roots->TraceJSAll(trc);
}
dom::TraceBlackJS(trc, nsXPConnect::XPConnect()->IsShuttingDown());
dom::TraceBlackJS(trc, nsIXPConnect::XPConnect()->GetIsShuttingDown());
}
void XPCJSRuntime::TraceAdditionalNativeGrayRoots(JSTracer* trc)

View File

@ -21,26 +21,21 @@
#define MOZJSSUBSCRIPTLOADER_CONTRACTID "@mozilla.org/moz/jssubscript-loader;1"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect,
nsXPConnect::GetSingleton)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(mozJSComponentLoader,
mozJSComponentLoader::GetOrCreate);
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
NS_DEFINE_NAMED_CID(NS_JS_ID_CID);
NS_DEFINE_NAMED_CID(NS_XPCONNECT_CID);
NS_DEFINE_NAMED_CID(MOZJSCOMPONENTLOADER_CID);
NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADER_CID);
#define XPCONNECT_CIDENTRIES \
{ &kNS_JS_ID_CID, false, nullptr, nsJSIDConstructor }, \
{ &kNS_XPCONNECT_CID, false, nullptr, nsIXPConnectConstructor }, \
{ &kMOZJSCOMPONENTLOADER_CID, false, nullptr, mozJSComponentLoaderConstructor },\
{ &kMOZ_JSSUBSCRIPTLOADER_CID, false, nullptr, mozJSSubScriptLoaderConstructor },
#define XPCONNECT_CONTRACTS \
{ XPC_XPCONNECT_CONTRACTID, &kNS_XPCONNECT_CID }, \
{ MOZJSCOMPONENTLOADER_CONTRACTID, &kMOZJSCOMPONENTLOADER_CID }, \
{ MOZJSSUBSCRIPTLOADER_CONTRACTID, &kMOZ_JSSUBSCRIPTLOADER_CID },

View File

@ -355,7 +355,7 @@ bool XPCVariant::InitializeData(JSContext* cx)
// XXX This could be smarter and pick some more interesting iface.
nsXPConnect* xpc = nsXPConnect::XPConnect();
nsIXPConnect* xpc = nsIXPConnect::XPConnect();
nsCOMPtr<nsISupports> wrapper;
const nsIID& iid = NS_GET_IID(nsISupports);

View File

@ -11,7 +11,6 @@
#include "mozilla/Likely.h"
#include "mozilla/Unused.h"
#include "xpcprivate.h"
#include "XPCWrapper.h"
#include "jsfriendapi.h"
#include "js/ProfilingStack.h"
@ -59,7 +58,6 @@ nsIPrincipal* nsXPConnect::gSystemPrincipal = nullptr;
const char XPC_EXCEPTION_CONTRACTID[] = "@mozilla.org/js/xpc/Exception;1";
const char XPC_CONSOLE_CONTRACTID[] = "@mozilla.org/consoleservice;1";
const char XPC_SCRIPT_ERROR_CONTRACTID[] = "@mozilla.org/scripterror;1";
const char XPC_XPCONNECT_CONTRACTID[] = "@mozilla.org/js/xpc/XPConnect;1";
/***************************************************************************/
@ -154,12 +152,6 @@ nsXPConnect::InitStatics()
gSelf->mRuntime->InitSingletonScopes();
}
already_AddRefed<nsXPConnect>
nsXPConnect::GetSingleton()
{
return do_AddRef(nsXPConnect::XPConnect());
}
// static
void
nsXPConnect::ReleaseXPConnectSingleton()
@ -1098,6 +1090,32 @@ nsXPConnect::ReadFunction(nsIObjectInputStream* stream, JSContext* cx, JSObject*
return ReadScriptOrFunction(stream, cx, nullptr, functionObjp);
}
NS_IMETHODIMP
nsXPConnect::GetIsShuttingDown(bool* aIsShuttingDown)
{
if (!aIsShuttingDown) {
return NS_ERROR_INVALID_ARG;
}
*aIsShuttingDown = mShuttingDown;
return NS_OK;
}
// static
nsIXPConnect*
nsIXPConnect::XPConnect()
{
// Do a release-mode assert that we're not doing anything significant in
// XPConnect off the main thread. If you're an extension developer hitting
// this, you need to change your code. See bug 716167.
if (!MOZ_LIKELY(NS_IsMainThread())) {
MOZ_CRASH();
}
return nsXPConnect::gSelf;
}
/* These are here to be callable from a debugger */
extern "C" {

View File

@ -229,18 +229,6 @@ public:
// non-interface implementation
public:
// These get non-addref'd pointers
static nsXPConnect* XPConnect()
{
// Do a release-mode assert that we're not doing anything significant in
// XPConnect off the main thread. If you're an extension developer hitting
// this, you need to change your code. See bug 716167.
if (!MOZ_LIKELY(NS_IsMainThread()))
MOZ_CRASH();
return gSelf;
}
static XPCJSRuntime* GetRuntimeInstance();
static bool IsISupportsDescendant(const nsXPTInterfaceInfo* info);
@ -259,15 +247,11 @@ public:
return gSystemPrincipal;
}
static already_AddRefed<nsXPConnect> GetSingleton();
// Called by module code in dll startup
static void InitStatics();
// Called by module code on dll shutdown.
static void ReleaseXPConnectSingleton();
bool IsShuttingDown() const {return mShuttingDown;}
void RecordTraversal(void* p, nsISupports* s);
protected:
@ -283,11 +267,14 @@ private:
XPCJSRuntime* mRuntime;
bool mShuttingDown;
friend class nsIXPConnect;
public:
static nsIScriptSecurityManager* gScriptSecurityManager;
static nsIPrincipal* gSystemPrincipal;
};
/***************************************************************************/
class XPCRootSetElem
@ -774,7 +761,7 @@ inline void CHECK_STATE(int s) const {MOZ_ASSERT(mState >= s, "bad state");}
private:
State mState;
RefPtr<nsXPConnect> mXPC;
nsCOMPtr<nsIXPConnect> mXPC;
XPCJSContext* mXPCJSContext;
JSContext* mJSContext;

View File

@ -22,6 +22,7 @@
#include "nsIInterfaceRequestorUtils.h"
#include "nsISupportsImpl.h"
#include "nsISupportsUtils.h"
#include "nsIXPConnect.h"
#include "nsContentUtils.h"
#include "nsDocShell.h"
#include "nsGlobalWindow.h"
@ -1172,7 +1173,7 @@ LoadInfo::GetRedirects(JSContext* aCx, JS::MutableHandle<JS::Value> aRedirects,
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
for (size_t idx = 0; idx < aArray.Length(); idx++) {
JS::RootedObject jsobj(aCx);

View File

@ -1341,7 +1341,7 @@ Connection::Close()
if (isAsyncExecutionThreadAvailable()) {
#ifdef DEBUG
if (NS_IsMainThread()) {
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
Unused << xpc->DebugDumpJSStack(false, false, false);
}
#endif

View File

@ -35,7 +35,7 @@ stepFunc(JSContext *aCtx, uint32_t argc, JS::Value *_vp)
{
JS::CallArgs args = CallArgsFromVp(argc, _vp);
nsCOMPtr<nsIXPConnect> xpc(mozilla::services::GetXPConnect());
nsCOMPtr<nsIXPConnect> xpc(nsIXPConnect::XPConnect());
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
if (!args.thisv().isObject()) {

View File

@ -228,7 +228,7 @@ already_AddRefed<nsIURI>
GetJSValueAsURI(JSContext* aCtx,
const JS::Value& aValue) {
if (!aValue.isPrimitive()) {
nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
nsCOMPtr<nsIXPConnectWrappedNative> wrappedObj;
nsresult rv = xpc->GetWrappedNativeOfJSObject(aCtx, aValue.toObjectOrNull(),
@ -2521,7 +2521,7 @@ History::UpdatePlaces(JS::Handle<JS::Value> aPlaceInfos,
// a mistake.
if (data.visitTime < (PR_Now() / 1000)) {
#ifdef DEBUG
nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
Unused << xpc->DebugDumpJSStack(false, false, false);
MOZ_CRASH("invalid time format passed to updatePlaces");
#endif

View File

@ -107,7 +107,7 @@ PlaceInfo::GetVisits(JSContext* aContext,
JS::Rooted<JSObject*> global(aContext, JS::CurrentGlobalOrNull(aContext));
NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
JS::RootedObject jsobj(aContext);

View File

@ -419,19 +419,15 @@ void nsMenuX::MenuConstruct()
// bug 365405: Manually wrap the menupopup node to make sure it's bounded
if (!mXBLAttached) {
nsresult rv;
nsCOMPtr<nsIXPConnect> xpconnect =
do_GetService(nsIXPConnect::GetCID(), &rv);
if (NS_SUCCEEDED(rv)) {
nsIDocument* ownerDoc = menuPopup->OwnerDoc();
dom::AutoJSAPI jsapi;
if (ownerDoc && jsapi.Init(ownerDoc->GetInnerWindow())) {
JSContext* cx = jsapi.cx();
JS::RootedObject ignoredObj(cx);
xpconnect->WrapNative(cx, JS::CurrentGlobalOrNull(cx), menuPopup,
NS_GET_IID(nsISupports), ignoredObj.address());
mXBLAttached = true;
}
nsCOMPtr<nsIXPConnect> xpconnect = nsIXPConnect::XPConnect();
nsIDocument* ownerDoc = menuPopup->OwnerDoc();
dom::AutoJSAPI jsapi;
if (ownerDoc && jsapi.Init(ownerDoc->GetInnerWindow())) {
JSContext* cx = jsapi.cx();
JS::RootedObject ignoredObj(cx);
xpconnect->WrapNative(cx, JS::CurrentGlobalOrNull(cx), menuPopup,
NS_GET_IID(nsISupports), ignoredObj.address());
mXBLAttached = true;
}
}

View File

@ -20,8 +20,6 @@ service('ObserverService', 'nsIObserverService',
"@mozilla.org/observer-service;1")
service('StringBundleService', 'nsIStringBundleService',
"@mozilla.org/intl/stringbundle;1")
service('XPConnect', 'nsIXPConnect',
"@mozilla.org/js/xpc/XPConnect;1")
service('PermissionManager', 'nsIPermissionManager',
"@mozilla.org/permissionmanager;1")
service('ServiceWorkerManager', 'nsIServiceWorkerManager',