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 63a5c4284b
commit 35431284b1
12 changed files with 107 additions and 16 deletions

View File

@ -44,7 +44,7 @@ class nsXBLDocGlobalObject : public nsIScriptGlobalObject,
public nsIScriptObjectPrincipal
{
public:
nsXBLDocGlobalObject(nsIScriptGlobalObjectOwner *aGlobalObjectOwner);
nsXBLDocGlobalObject(nsXBLDocumentInfo *aGlobalObjectOwner);
// nsISupports interface
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@ -82,7 +82,7 @@ protected:
nsCOMPtr<nsIScriptContext> mScriptContext;
JSObject *mJSObject;
nsIScriptGlobalObjectOwner* mGlobalObjectOwner; // weak reference
nsXBLDocumentInfo* mGlobalObjectOwner; // weak reference
static JSClass gSharedGlobalClass;
};
@ -181,7 +181,7 @@ JSClass nsXBLDocGlobalObject::gSharedGlobalClass = {
// nsXBLDocGlobalObject
//
nsXBLDocGlobalObject::nsXBLDocGlobalObject(nsIScriptGlobalObjectOwner *aGlobalObjectOwner)
nsXBLDocGlobalObject::nsXBLDocGlobalObject(nsXBLDocumentInfo *aGlobalObjectOwner)
: mJSObject(nsnull),
mGlobalObjectOwner(aGlobalObjectOwner) // weak reference
{
@ -281,6 +281,11 @@ nsXBLDocGlobalObject::EnsureScriptEnvironment()
false, &mJSObject, &compartment);
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);
// Add an owning reference from JS back to us. This'll be

View File

@ -701,6 +701,11 @@ nsXULPDGlobalObject::EnsureScriptEnvironment()
mContext = ctxNew;
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;
}

View File

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

View File

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

View File

@ -2069,6 +2069,7 @@ nsJSContext::GetNativeGlobal()
nsresult
nsJSContext::CreateNativeGlobalForInner(
nsIScriptGlobalObject *aNewInner,
nsIURI *aURI,
bool aIsChrome,
nsIPrincipal *aPrincipal,
JSObject** aNativeGlobal, nsISupports **aHolder)
@ -2092,6 +2093,12 @@ nsJSContext::CreateNativeGlobalForInner(
}
jsholder->GetJSObject(aNativeGlobal);
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;
}

View File

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

View File

@ -569,6 +569,10 @@ mozJSComponentLoader::LoadModule(FileLocation &aFile)
// Cache this module for later
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
return entry.forget();
}
@ -1126,6 +1130,10 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation,
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;
}

View File

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

View File

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

View File

@ -2610,6 +2610,36 @@ DumpJSHeap(FILE* file)
}
#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
NS_IMETHODIMP

View File

@ -4342,8 +4342,9 @@ XPC_GetIdentityObject(JSContext *cx, JSObject *obj);
namespace xpc {
struct CompartmentPrivate
class CompartmentPrivate
{
public:
typedef nsDataHashtable<nsPtrHashKey<XPCWrappedNative>, JSObject *> ExpandoMap;
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.
nsAutoPtr<ExpandoMap> expandoMap;
nsAutoPtr<DOMExpandoMap> domExpandoMap;
nsCString location;
bool RegisterExpandoObject(XPCWrappedNative *wn, JSObject *expando) {
if (!expandoMap) {
@ -4403,6 +4403,33 @@ struct CompartmentPrivate
if (domExpandoMap)
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);
#endif
void SetLocationForGlobal(JSObject *global, const nsACString& location);
void SetLocationForGlobal(JSObject *global, nsIURI *locationURI);
/**
* Define quick stubs on the given object, @a proto.
*