Bug 951948 - Separate out the unprivileged parts of nsXPCComponents into a separate interface and class. r=mrbkap

This commit is contained in:
Bobby Holley 2014-01-14 18:49:29 -08:00
parent 8cb0cd6465
commit 8850e08477
3 changed files with 101 additions and 40 deletions

View File

@ -544,17 +544,30 @@ interface nsIXPCComponents_Utils : nsISupports
};
/**
* interface of JavaScript's 'Components' object
* Interface for the 'Components' object.
*
* The first interface contains things that are available to non-chrome XBL code
* that runs in a scope with an nsExpandedPrincipal. The second interface
* includes members that are only exposed to chrome.
*/
[scriptable, uuid(e270f35d-dc63-42ed-9c8b-12ed14917c2c)]
interface nsIXPCComponents : nsISupports
[scriptable, uuid(eeeada2f-86c0-4609-b2bf-4bf2351b1ce6)]
interface nsIXPCComponentsBase : nsISupports
{
readonly attribute nsIXPCComponents_Interfaces interfaces;
readonly attribute nsIXPCComponents_InterfacesByID interfacesByID;
readonly attribute nsIXPCComponents_Results results;
boolean isSuccessCode(in nsresult result);
};
[scriptable, uuid(aa28aaf6-70ce-4b03-9514-afe43c7dfda8)]
interface nsIXPCComponents : nsIXPCComponentsBase
{
readonly attribute nsIXPCComponents_Classes classes;
readonly attribute nsIXPCComponents_ClassesByID classesByID;
readonly attribute nsIStackFrame stack;
readonly attribute nsIXPCComponents_Results results;
readonly attribute nsIComponentManager manager;
readonly attribute nsIXPCComponents_Utils utils;
@ -567,8 +580,6 @@ interface nsIXPCComponents : nsISupports
[implicit_jscontext]
attribute jsval returnCode;
boolean isSuccessCode(in nsresult result);
/* @deprecated Use Components.utils.reportError instead. */
[deprecated, implicit_jscontext] void reportError(in jsval error);
};

View File

@ -3466,47 +3466,55 @@ nsXPCComponents_Utils::GetWatchdogTimestamp(const nsAString& aCategory, PRTime *
/***************************************************************************/
/***************************************************************************/
nsXPCComponents::nsXPCComponents(XPCWrappedNativeScope* aScope)
nsXPCComponentsBase::nsXPCComponentsBase(XPCWrappedNativeScope* aScope)
: mScope(aScope),
mInterfaces(nullptr),
mInterfacesByID(nullptr),
mResults(nullptr)
{
MOZ_ASSERT(aScope, "aScope must not be null");
}
nsXPCComponents::nsXPCComponents(XPCWrappedNativeScope* aScope)
: nsXPCComponentsBase(aScope),
mClasses(nullptr),
mClassesByID(nullptr),
mResults(nullptr),
mID(nullptr),
mException(nullptr),
mConstructor(nullptr),
mUtils(nullptr)
{
MOZ_ASSERT(aScope, "aScope must not be null");
}
nsXPCComponents::~nsXPCComponents()
void
nsXPCComponentsBase::ClearMembers()
{
ClearMembers();
NS_IF_RELEASE(mInterfaces);
NS_IF_RELEASE(mInterfacesByID);
NS_IF_RELEASE(mResults);
}
void
nsXPCComponents::ClearMembers()
{
NS_IF_RELEASE(mInterfaces);
NS_IF_RELEASE(mInterfacesByID);
NS_IF_RELEASE(mClasses);
NS_IF_RELEASE(mClassesByID);
NS_IF_RELEASE(mResults);
NS_IF_RELEASE(mID);
NS_IF_RELEASE(mException);
NS_IF_RELEASE(mConstructor);
NS_IF_RELEASE(mUtils);
nsXPCComponentsBase::ClearMembers();
}
/*******************************************/
#define XPC_IMPL_GET_OBJ_METHOD(_n) \
NS_IMETHODIMP nsXPCComponents::Get##_n(nsIXPCComponents_##_n * *a##_n) { \
#define XPC_IMPL_GET_OBJ_METHOD(_class, _n) \
NS_IMETHODIMP _class::Get##_n(nsIXPCComponents_##_n * *a##_n) { \
NS_ENSURE_ARG_POINTER(a##_n); \
if (!m##_n) { \
if (!(m##_n = new nsXPCComponents_##_n())) { \
*a##_n = nullptr; \
*a##_n = nullptr; \
return NS_ERROR_OUT_OF_MEMORY; \
} \
NS_ADDREF(m##_n); \
@ -3516,21 +3524,21 @@ NS_IMETHODIMP nsXPCComponents::Get##_n(nsIXPCComponents_##_n * *a##_n) { \
return NS_OK; \
}
XPC_IMPL_GET_OBJ_METHOD(Interfaces)
XPC_IMPL_GET_OBJ_METHOD(InterfacesByID)
XPC_IMPL_GET_OBJ_METHOD(Classes)
XPC_IMPL_GET_OBJ_METHOD(ClassesByID)
XPC_IMPL_GET_OBJ_METHOD(Results)
XPC_IMPL_GET_OBJ_METHOD(ID)
XPC_IMPL_GET_OBJ_METHOD(Exception)
XPC_IMPL_GET_OBJ_METHOD(Constructor)
XPC_IMPL_GET_OBJ_METHOD(Utils)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponentsBase, Interfaces)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponentsBase, InterfacesByID)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponents, Classes)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponents, ClassesByID)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponentsBase, Results)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponents, ID)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponents, Exception)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponents, Constructor)
XPC_IMPL_GET_OBJ_METHOD(nsXPCComponents, Utils)
#undef XPC_IMPL_GET_OBJ_METHOD
/*******************************************/
NS_IMETHODIMP
nsXPCComponents::IsSuccessCode(nsresult result, bool *out)
nsXPCComponentsBase::IsSuccessCode(nsresult result, bool *out)
{
*out = NS_SUCCEEDED(result);
return NS_OK;
@ -3638,12 +3646,27 @@ NS_INTERFACE_MAP_BEGIN(ComponentsSH)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
#define NSXPCCOMPONENTSBASE_CID \
{ 0xc62998e5, 0x95f1, 0x4058, \
{ 0xa5, 0x09, 0xec, 0x21, 0x66, 0x18, 0x92, 0xb9 } }
#define NSXPCCOMPONENTS_CID \
{ 0x3649f405, 0xf0ec, 0x4c28, \
{ 0xae, 0xb0, 0xaf, 0x9a, 0x51, 0xe4, 0x4c, 0x81 } }
NS_IMPL_CLASSINFO(nsXPCComponentsBase, &ComponentsSH::Get, nsIClassInfo::DOM_OBJECT, NSXPCCOMPONENTSBASE_CID)
NS_IMPL_ISUPPORTS1_CI(nsXPCComponentsBase, nsIXPCComponentsBase)
NS_IMPL_CLASSINFO(nsXPCComponents, &ComponentsSH::Get, nsIClassInfo::DOM_OBJECT, NSXPCCOMPONENTS_CID)
NS_IMPL_ISUPPORTS1_CI(nsXPCComponents, nsIXPCComponents)
// Below is more or less what NS_IMPL_ISUPPORTS_CI_INHERITED1 would look like
// if it existed.
NS_IMPL_ADDREF_INHERITED(nsXPCComponents, nsXPCComponentsBase)
NS_IMPL_RELEASE_INHERITED(nsXPCComponents, nsXPCComponentsBase)
NS_INTERFACE_MAP_BEGIN(nsXPCComponents)
NS_INTERFACE_MAP_ENTRY(nsIXPCComponents)
NS_IMPL_QUERY_CLASSINFO(nsXPCComponents)
NS_INTERFACE_MAP_END_INHERITING(nsXPCComponentsBase)
NS_IMPL_CI_INTERFACE_GETTER1(nsXPCComponents, nsIXPCComponents)
// The nsIXPCScriptable map declaration that will generate stubs for us
#define XPC_MAP_CLASSNAME ComponentsSH
@ -3654,7 +3677,7 @@ NS_IMPL_ISUPPORTS1_CI(nsXPCComponents, nsIXPCComponents)
NS_IMETHODIMP
ComponentsSH::PreCreate(nsISupports *nativeObj, JSContext *cx, JSObject *globalObj, JSObject **parentObj)
{
nsXPCComponents *self = static_cast<nsXPCComponents*>(nativeObj);
nsXPCComponentsBase *self = static_cast<nsXPCComponentsBase*>(nativeObj);
// this should never happen
if (!self->GetScope()) {
NS_WARNING("mScope must not be null when nsXPCComponents::PreCreate is called");

View File

@ -2920,36 +2920,63 @@ private:
};
/***************************************************************************/
// 'Components' object
// 'Components' object implementations. nsXPCComponentsBase has the
// less-privileged stuff that we're willing to expose to XBL.
class nsXPCComponents : public nsIXPCComponents
class nsXPCComponentsBase : public nsIXPCComponentsBase
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIXPCCOMPONENTS
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCCOMPONENTSBASE
public:
void SystemIsBeingShutDown() { ClearMembers(); }
virtual ~nsXPCComponents();
virtual ~nsXPCComponentsBase() { ClearMembers(); }
XPCWrappedNativeScope *GetScope() { return mScope; }
private:
nsXPCComponents(XPCWrappedNativeScope* aScope);
void ClearMembers();
protected:
nsXPCComponentsBase(XPCWrappedNativeScope* aScope);
virtual void ClearMembers();
private:
friend class XPCWrappedNativeScope;
XPCWrappedNativeScope* mScope;
// Unprivileged members from nsIXPCComponentsBase.
nsXPCComponents_Interfaces* mInterfaces;
nsXPCComponents_InterfacesByID* mInterfacesByID;
nsXPCComponents_Results* mResults;
friend class XPCWrappedNativeScope;
};
class nsXPCComponents : public nsXPCComponentsBase,
public nsIXPCComponents
{
public:
NS_DECL_ISUPPORTS
NS_FORWARD_NSIXPCCOMPONENTSBASE(nsXPCComponentsBase::)
NS_DECL_NSIXPCCOMPONENTS
protected:
nsXPCComponents(XPCWrappedNativeScope* aScope);
// One might think we could rely on the superclass destructor invoking
// the virtual cleanup function. But by the time we hit the superclass
// destructor, the derived class will be gone and the vtable pointer
// will be updated to point to that of the superclass, giving us only
// the superclass' cleanup.
virtual ~nsXPCComponents() { ClearMembers(); }
virtual void ClearMembers() MOZ_OVERRIDE;
// Privileged members added by nsIXPCComponents.
nsXPCComponents_Classes* mClasses;
nsXPCComponents_ClassesByID* mClassesByID;
nsXPCComponents_Results* mResults;
nsXPCComponents_ID* mID;
nsXPCComponents_Exception* mException;
nsXPCComponents_Constructor* mConstructor;
nsXPCComponents_Utils* mUtils;
friend class XPCWrappedNativeScope;
};