Bug 754771 - Add identifying information to all system compartments. r=bz

This commit is contained in:
Nils Maier 2012-05-16 19:42:10 +02:00
parent 62fad221a1
commit eb4ff8a701
12 changed files with 107 additions and 16 deletions

View File

@ -44,7 +44,7 @@ class nsXBLDocGlobalObject : public nsIScriptGlobalObject,
public nsIScriptObjectPrincipal public nsIScriptObjectPrincipal
{ {
public: public:
nsXBLDocGlobalObject(nsIScriptGlobalObjectOwner *aGlobalObjectOwner); nsXBLDocGlobalObject(nsXBLDocumentInfo *aGlobalObjectOwner);
// nsISupports interface // nsISupports interface
NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@ -82,7 +82,7 @@ protected:
nsCOMPtr<nsIScriptContext> mScriptContext; nsCOMPtr<nsIScriptContext> mScriptContext;
JSObject *mJSObject; JSObject *mJSObject;
nsIScriptGlobalObjectOwner* mGlobalObjectOwner; // weak reference nsXBLDocumentInfo* mGlobalObjectOwner; // weak reference
static JSClass gSharedGlobalClass; static JSClass gSharedGlobalClass;
}; };
@ -181,7 +181,7 @@ JSClass nsXBLDocGlobalObject::gSharedGlobalClass = {
// nsXBLDocGlobalObject // nsXBLDocGlobalObject
// //
nsXBLDocGlobalObject::nsXBLDocGlobalObject(nsIScriptGlobalObjectOwner *aGlobalObjectOwner) nsXBLDocGlobalObject::nsXBLDocGlobalObject(nsXBLDocumentInfo *aGlobalObjectOwner)
: mJSObject(nsnull), : mJSObject(nsnull),
mGlobalObjectOwner(aGlobalObjectOwner) // weak reference mGlobalObjectOwner(aGlobalObjectOwner) // weak reference
{ {
@ -281,6 +281,11 @@ nsXBLDocGlobalObject::EnsureScriptEnvironment()
false, &mJSObject, &compartment); false, &mJSObject, &compartment);
NS_ENSURE_SUCCESS(rv, NS_OK); NS_ENSURE_SUCCESS(rv, NS_OK);
// Set the location information for the new global, so that tools like
// about:memory may use that information
nsIURI *ownerURI = mGlobalObjectOwner->DocumentURI();
xpc::SetLocationForGlobal(mJSObject, ownerURI);
::JS_SetGlobalObject(cx, mJSObject); ::JS_SetGlobalObject(cx, mJSObject);
// Add an owning reference from JS back to us. This'll be // Add an owning reference from JS back to us. This'll be

View File

@ -701,6 +701,11 @@ nsXULPDGlobalObject::EnsureScriptEnvironment()
mContext = ctxNew; mContext = ctxNew;
mJSObject = global; mJSObject = global;
// Set the location information for the new global, so that tools like
// about:memory may use that information
nsIURI *ownerURI = mGlobalObjectOwner->GetURI();
xpc::SetLocationForGlobal(mJSObject, ownerURI);
return NS_OK; return NS_OK;
} }

View File

@ -1822,7 +1822,9 @@ nsGlobalWindow::SetNewDocument(nsIDocument* aDocument,
// Every script context we are initialized with must create a // Every script context we are initialized with must create a
// new global. // new global.
nsCOMPtr<nsIXPConnectJSObjectHolder> &holder = mInnerWindowHolder; nsCOMPtr<nsIXPConnectJSObjectHolder> &holder = mInnerWindowHolder;
rv = mContext->CreateNativeGlobalForInner(sgo, isChrome, rv = mContext->CreateNativeGlobalForInner(sgo,
aDocument->GetDocumentURI(),
isChrome,
aDocument->NodePrincipal(), aDocument->NodePrincipal(),
&newInnerWindow->mJSObject, &newInnerWindow->mJSObject,
getter_AddRefs(holder)); getter_AddRefs(holder));

View File

@ -25,6 +25,7 @@ class nsIObjectOutputStream;
template<class> class nsScriptObjectHolder; template<class> class nsScriptObjectHolder;
class nsIScriptObjectPrincipal; class nsIScriptObjectPrincipal;
class nsIDOMWindow; class nsIDOMWindow;
class nsIURI;
typedef void (*nsScriptTerminationFunc)(nsISupports* aRef); typedef void (*nsScriptTerminationFunc)(nsISupports* aRef);
@ -44,8 +45,8 @@ NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptContextPrincipal,
NS_ISCRIPTCONTEXTPRINCIPAL_IID) NS_ISCRIPTCONTEXTPRINCIPAL_IID)
#define NS_ISCRIPTCONTEXT_IID \ #define NS_ISCRIPTCONTEXT_IID \
{ 0xf1c8c13e, 0xc23b, 0x434e, \ { 0xec47ccd4, 0x5f6a, 0x40d6, \
{ 0xa4, 0x77, 0xe0, 0x2f, 0xc3, 0x73, 0xf8, 0x71 } } { 0xbc, 0x2f, 0x5a, 0x1e, 0xd3, 0xe4, 0xb4, 0xff } }
/* This MUST match JSVERSION_DEFAULT. This version stuff if we don't /* This MUST match JSVERSION_DEFAULT. This version stuff if we don't
know what language we have is a little silly... */ know what language we have is a little silly... */
@ -265,6 +266,7 @@ public:
*/ */
virtual nsresult CreateNativeGlobalForInner( virtual nsresult CreateNativeGlobalForInner(
nsIScriptGlobalObject *aNewInner, nsIScriptGlobalObject *aNewInner,
nsIURI *aURI,
bool aIsChrome, bool aIsChrome,
nsIPrincipal *aPrincipal, nsIPrincipal *aPrincipal,
JSObject** aNativeGlobal, JSObject** aNativeGlobal,

View File

@ -2069,6 +2069,7 @@ nsJSContext::GetNativeGlobal()
nsresult nsresult
nsJSContext::CreateNativeGlobalForInner( nsJSContext::CreateNativeGlobalForInner(
nsIScriptGlobalObject *aNewInner, nsIScriptGlobalObject *aNewInner,
nsIURI *aURI,
bool aIsChrome, bool aIsChrome,
nsIPrincipal *aPrincipal, nsIPrincipal *aPrincipal,
JSObject** aNativeGlobal, nsISupports **aHolder) JSObject** aNativeGlobal, nsISupports **aHolder)
@ -2092,6 +2093,12 @@ nsJSContext::CreateNativeGlobalForInner(
} }
jsholder->GetJSObject(aNativeGlobal); jsholder->GetJSObject(aNativeGlobal);
jsholder.forget(aHolder); jsholder.forget(aHolder);
// Set the location information for the new global, so that tools like
// about:memory may use that information
MOZ_ASSERT(aNativeGlobal && *aNativeGlobal);
xpc::SetLocationForGlobal(*aNativeGlobal, aURI);
return NS_OK; return NS_OK;
} }

View File

@ -109,6 +109,7 @@ public:
virtual JSObject* GetNativeGlobal(); virtual JSObject* GetNativeGlobal();
virtual nsresult CreateNativeGlobalForInner( virtual nsresult CreateNativeGlobalForInner(
nsIScriptGlobalObject *aGlobal, nsIScriptGlobalObject *aGlobal,
nsIURI *aURI,
bool aIsChrome, bool aIsChrome,
nsIPrincipal *aPrincipal, nsIPrincipal *aPrincipal,
JSObject** aNativeGlobal, JSObject** aNativeGlobal,

View File

@ -569,6 +569,10 @@ mozJSComponentLoader::LoadModule(FileLocation &aFile)
// Cache this module for later // Cache this module for later
mModules.Put(spec, entry); mModules.Put(spec, entry);
// Set the location information for the new global, so that tools like
// about:memory may use that information
xpc::SetLocationForGlobal(entry->global, spec);
// The hash owns the ModuleEntry now, forget about it // The hash owns the ModuleEntry now, forget about it
return entry.forget(); return entry.forget();
} }
@ -1126,6 +1130,10 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation,
return NS_ERROR_FILE_NOT_FOUND; return NS_ERROR_FILE_NOT_FOUND;
} }
// Set the location information for the new global, so that tools like
// about:memory may use that information
xpc::SetLocationForGlobal(newEntry->global, aLocation);
mod = newEntry; mod = newEntry;
} }

View File

@ -3250,9 +3250,9 @@ xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, JSOb
} }
} }
xpc::CompartmentPrivate *compartmentPrivate = // Set the location information for the new global, so that tools like
static_cast<xpc::CompartmentPrivate*>(JS_GetCompartmentPrivate(compartment)); // about:memory may use that information
compartmentPrivate->location = sandboxName; xpc::SetLocationForGlobal(sandbox, sandboxName);
return NS_OK; return NS_OK;
} }

View File

@ -1175,11 +1175,12 @@ GetCompartmentName(JSCompartment *c, nsCString &name)
// (e.g. components owned by the system or null principal). // (e.g. components owned by the system or null principal).
xpc::CompartmentPrivate *compartmentPrivate = xpc::CompartmentPrivate *compartmentPrivate =
static_cast<xpc::CompartmentPrivate*>(JS_GetCompartmentPrivate(c)); static_cast<xpc::CompartmentPrivate*>(JS_GetCompartmentPrivate(c));
if (compartmentPrivate && if (compartmentPrivate) {
!compartmentPrivate->location.IsEmpty() && const nsACString& location = compartmentPrivate->GetLocation();
!compartmentPrivate->location.Equals(name)) { if (!location.IsEmpty() && !location.Equals(name)) {
name.AppendLiteral(", "); name.AppendLiteral(", ");
name.Append(compartmentPrivate->location); name.Append(location);
}
} }
// A hack: replace forward slashes with '\\' so they aren't // A hack: replace forward slashes with '\\' so they aren't

View File

@ -2610,6 +2610,36 @@ DumpJSHeap(FILE* file)
} }
#endif #endif
void
SetLocationForGlobal(JSObject *global, const nsACString& location)
{
MOZ_ASSERT(global);
JSCompartment *compartment = js::GetObjectCompartment(global);
MOZ_ASSERT(compartment, "No compartment for global");
xpc::CompartmentPrivate *priv =
static_cast<xpc::CompartmentPrivate *>(JS_GetCompartmentPrivate(compartment));
MOZ_ASSERT(priv, "No compartment private");
priv->SetLocation(location);
}
void
SetLocationForGlobal(JSObject *global, nsIURI *locationURI)
{
MOZ_ASSERT(global);
JSCompartment *compartment = js::GetObjectCompartment(global);
MOZ_ASSERT(compartment, "No compartment for global");
xpc::CompartmentPrivate *priv =
static_cast<xpc::CompartmentPrivate *>(JS_GetCompartmentPrivate(compartment));
MOZ_ASSERT(priv, "No compartment private");
priv->SetLocation(locationURI);
}
} // namespace xpc } // namespace xpc
NS_IMETHODIMP NS_IMETHODIMP

View File

@ -4342,8 +4342,9 @@ XPC_GetIdentityObject(JSContext *cx, JSObject *obj);
namespace xpc { namespace xpc {
struct CompartmentPrivate class CompartmentPrivate
{ {
public:
typedef nsDataHashtable<nsPtrHashKey<XPCWrappedNative>, JSObject *> ExpandoMap; typedef nsDataHashtable<nsPtrHashKey<XPCWrappedNative>, JSObject *> ExpandoMap;
typedef nsTHashtable<nsPtrHashKey<JSObject> > DOMExpandoMap; typedef nsTHashtable<nsPtrHashKey<JSObject> > DOMExpandoMap;
@ -4360,7 +4361,6 @@ struct CompartmentPrivate
// NB: we don't want this map to hold a strong reference to the wrapper. // NB: we don't want this map to hold a strong reference to the wrapper.
nsAutoPtr<ExpandoMap> expandoMap; nsAutoPtr<ExpandoMap> expandoMap;
nsAutoPtr<DOMExpandoMap> domExpandoMap; nsAutoPtr<DOMExpandoMap> domExpandoMap;
nsCString location;
bool RegisterExpandoObject(XPCWrappedNative *wn, JSObject *expando) { bool RegisterExpandoObject(XPCWrappedNative *wn, JSObject *expando) {
if (!expandoMap) { if (!expandoMap) {
@ -4403,6 +4403,33 @@ struct CompartmentPrivate
if (domExpandoMap) if (domExpandoMap)
domExpandoMap->RemoveEntry(expando); domExpandoMap->RemoveEntry(expando);
} }
const nsACString& GetLocation() {
if (locationURI) {
if (NS_FAILED(locationURI->GetSpec(location)))
location = NS_LITERAL_CSTRING("<unknown location>");
locationURI = nsnull;
}
return location;
}
void SetLocation(const nsACString& aLocation) {
if (aLocation.IsEmpty())
return;
if (!location.IsEmpty() || locationURI)
return;
location = aLocation;
}
void SetLocation(nsIURI *aLocationURI) {
if (!aLocationURI)
return;
if (!location.IsEmpty() || locationURI)
return;
locationURI = aLocationURI;
}
private:
nsCString location;
nsCOMPtr<nsIURI> locationURI;
}; };
} }

View File

@ -229,6 +229,9 @@ nsIPrincipal *GetCompartmentPrincipal(JSCompartment *compartment);
void DumpJSHeap(FILE* file); void DumpJSHeap(FILE* file);
#endif #endif
void SetLocationForGlobal(JSObject *global, const nsACString& location);
void SetLocationForGlobal(JSObject *global, nsIURI *locationURI);
/** /**
* Define quick stubs on the given object, @a proto. * Define quick stubs on the given object, @a proto.
* *