Bug 993772 - Remove nsXBLDocGlobalObject. r=mrbkap
authorBobby Holley <bobbyholley@gmail.com>
Sun, 20 Apr 2014 11:48:14 -0700
changeset 191401 63818195fa63b2b517fd679d5817203a64138fc3
parent 191400 3c13321cab204019759d46d7962c5e2a744d4e82
child 191402 10d5e3c5b0e423a65ef426b91b32f34895be24f0
push idunknown
push userunknown
push dateunknown
reviewersmrbkap
bugs993772
milestone31.0a1
Bug 993772 - Remove nsXBLDocGlobalObject. r=mrbkap
dom/xbl/nsXBLDocumentInfo.cpp
dom/xbl/nsXBLDocumentInfo.h
--- a/dom/xbl/nsXBLDocumentInfo.cpp
+++ b/dom/xbl/nsXBLDocumentInfo.cpp
@@ -6,17 +6,16 @@
 
 #include "mozilla/DebugOnly.h"
 
 #include "nsXBLDocumentInfo.h"
 #include "nsHashtable.h"
 #include "nsIDocument.h"
 #include "nsXBLPrototypeBinding.h"
 #include "nsIScriptObjectPrincipal.h"
-#include "nsIScriptGlobalObject.h"
 #include "nsIScriptContext.h"
 #include "nsIDOMDocument.h"
 #include "nsIDOMScriptObjectFactory.h"
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "nsIURI.h"
 #include "nsIConsoleService.h"
 #include "nsIScriptError.h"
@@ -36,196 +35,16 @@
 #include "mozilla/dom/URL.h"
 
 using namespace mozilla;
 using namespace mozilla::scache;
 using namespace mozilla::dom;
 
 static const char kXBLCachePrefix[] = "xblcache";
 
-class nsXBLDocGlobalObject : public nsISupports
-{
-public:
-  nsXBLDocGlobalObject(nsXBLDocumentInfo *aGlobalObjectOwner);
-
-  // nsISupports interface
-  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
-  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsXBLDocGlobalObject)
-
-  JSObject *GetCompilationGlobal();
-  void UnmarkCompilationGlobal();
-  void Destroy();
-  nsIPrincipal* GetPrincipal();
-
-  void ClearGlobalObjectOwner();
-
-  static const JSClass gSharedGlobalClass;
-
-protected:
-  virtual ~nsXBLDocGlobalObject();
-
-  JS::Heap<JSObject*> mJSObject;
-  nsXBLDocumentInfo* mGlobalObjectOwner; // weak reference
-  bool mDestroyed; // Probably not necessary, but let's be safe.
-};
-
-static void
-nsXBLDocGlobalObject_finalize(JSFreeOp *fop, JSObject *obj)
-{
-  nsISupports *nativeThis = (nsISupports*)JS_GetPrivate(obj);
-  nsXBLDocGlobalObject* dgo = static_cast<nsXBLDocGlobalObject*>(nativeThis);
-
-  if (dgo)
-    dgo->Destroy();
-
-  // The addref was part of JSObject construction. Note that this effectively
-  // just calls release later on.
-  nsContentUtils::DeferredFinalize(nativeThis);
-}
-
-static bool
-nsXBLDocGlobalObject_resolve(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id)
-{
-  bool did_resolve = false;
-  return JS_ResolveStandardClass(cx, obj, id, &did_resolve);
-}
-
-
-const JSClass nsXBLDocGlobalObject::gSharedGlobalClass = {
-    "nsXBLPrototypeScript compilation scope",
-    JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS |
-    JSCLASS_IMPLEMENTS_BARRIERS | JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(0),
-    JS_PropertyStub,  JS_DeletePropertyStub,
-    JS_PropertyStub, JS_StrictPropertyStub,
-    JS_EnumerateStub, nsXBLDocGlobalObject_resolve,
-    JS_ConvertStub, nsXBLDocGlobalObject_finalize,
-    nullptr, nullptr, nullptr, JS_GlobalObjectTraceHook
-};
-
-//----------------------------------------------------------------------
-//
-// nsXBLDocGlobalObject
-//
-
-nsXBLDocGlobalObject::nsXBLDocGlobalObject(nsXBLDocumentInfo *aGlobalObjectOwner)
-    : mJSObject(nullptr)
-    , mGlobalObjectOwner(aGlobalObjectOwner) // weak reference
-    , mDestroyed(false)
-
-{
-}
-
-
-nsXBLDocGlobalObject::~nsXBLDocGlobalObject()
-{
-  MOZ_ASSERT(!mJSObject);
-}
-
-NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLDocGlobalObject)
-
-NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXBLDocGlobalObject)
-  NS_INTERFACE_MAP_ENTRY(nsISupports)
-NS_INTERFACE_MAP_END
-
-NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsXBLDocGlobalObject)
-  NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJSObject)
-NS_IMPL_CYCLE_COLLECTION_TRACE_END
-
-NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLDocGlobalObject)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
-NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
-
-NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLDocGlobalObject)
-  tmp->Destroy();
-NS_IMPL_CYCLE_COLLECTION_UNLINK_END
-
-
-NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXBLDocGlobalObject)
-NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXBLDocGlobalObject)
-
-void
-nsXBLDocGlobalObject::ClearGlobalObjectOwner()
-{
-  mGlobalObjectOwner = nullptr;
-}
-
-void
-nsXBLDocGlobalObject::UnmarkCompilationGlobal()
-{
-  if (mJSObject) {
-    JS::ExposeObjectToActiveJS(mJSObject);
-  }
-}
-
-JSObject *
-nsXBLDocGlobalObject::GetCompilationGlobal()
-{
-  // The prototype document has its own special secret script object
-  // that can be used to compile scripts and event handlers.
-  if (mJSObject || mDestroyed) {
-    // We've been initialized before - what we have is what you get.
-    return mJSObject;
-  }
-
-  AutoSafeJSContext cx;
-  JS::CompartmentOptions options;
-  options.setZone(JS::SystemZone)
-         .setInvisibleToDebugger(true);
-  mJSObject = JS_NewGlobalObject(cx, &gSharedGlobalClass,
-                                 nsJSPrincipals::get(GetPrincipal()),
-                                 JS::DontFireOnNewGlobalHook,
-                                 options);
-  if (!mJSObject)
-      return nullptr;
-
-  mozilla::HoldJSObjects(this);
-
-  // 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);
-
-  // Add an owning reference from JS back to us. This'll be
-  // released when the JSObject is finalized.
-  ::JS_SetPrivate(mJSObject, this);
-  NS_ADDREF(this);
-  return mJSObject;
-}
-
-void
-nsXBLDocGlobalObject::Destroy()
-{
-  // Maintain indempotence.
-  mDestroyed = true;
-  if (!mJSObject)
-    return;
-  mJSObject = nullptr;
-  mozilla::DropJSObjects(this);
-}
-
-
-nsIPrincipal*
-nsXBLDocGlobalObject::GetPrincipal()
-{
-  if (!mGlobalObjectOwner) {
-    // XXXbz this should really save the principal when
-    // ClearGlobalObjectOwner() happens.
-    return nullptr;
-  }
-
-  nsRefPtr<nsXBLDocumentInfo> docInfo =
-    static_cast<nsXBLDocumentInfo*>(mGlobalObjectOwner);
-
-  nsCOMPtr<nsIDocument> document = docInfo->GetDocument();
-  if (!document)
-    return nullptr;
-
-  return document->NodePrincipal();
-}
-
 /* Implementation file */
 
 static PLDHashOperator
 TraverseProtos(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure)
 {
   nsCycleCollectionTraversalCallback *cb =
     static_cast<nsCycleCollectionTraversalCallback*>(aClosure);
   aProto->Traverse(*cb);
@@ -255,29 +74,27 @@ TraceProtos(const nsACString &aKey, nsXB
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLDocumentInfo)
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLDocumentInfo)
   if (tmp->mBindingTable) {
     tmp->mBindingTable->EnumerateRead(UnlinkProtoJSObjects, nullptr);
   }
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
-  NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobalObject)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLDocumentInfo)
   if (tmp->mDocument &&
       nsCCUncollectableMarker::InGeneration(cb, tmp->mDocument->GetMarkedCCGeneration())) {
     NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
     return NS_SUCCESS_INTERRUPTED_TRAVERSE;
   }
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
   if (tmp->mBindingTable) {
     tmp->mBindingTable->EnumerateRead(TraverseProtos, &cb);
   }
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobalObject)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsXBLDocumentInfo)
   if (tmp->mBindingTable) {
     ProtoTracer closure = { aCallbacks, aClosure };
     tmp->mBindingTable->EnumerateRead(TraceProtos, &closure);
   }
 NS_IMPL_CYCLE_COLLECTION_TRACE_END
@@ -300,19 +117,16 @@ nsXBLDocumentInfo::MarkInCCGeneration(ui
 {
   if (mDocument) {
     mDocument->MarkUncollectableForCCGeneration(aGeneration);
   }
   // Unmark any JS we hold
   if (mBindingTable) {
     mBindingTable->EnumerateRead(UnmarkProtos, nullptr);
   }
-  if (mGlobalObject) {
-    mGlobalObject->UnmarkCompilationGlobal();
-  }
 }
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXBLDocumentInfo)
   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXBLDocumentInfo)
@@ -353,20 +167,16 @@ nsXBLDocumentInfo::nsXBLDocumentInfo(nsI
     nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
     nsresult rv = ssm->PolicyAllowsScript(uri, &allow);
     mScriptAccess = NS_SUCCEEDED(rv) && allow;
   }
 }
 
 nsXBLDocumentInfo::~nsXBLDocumentInfo()
 {
-  /* destructor code */
-  if (mGlobalObject) {
-    mGlobalObject->ClearGlobalObjectOwner(); // just in case
-  }
   mozilla::DropJSObjects(this);
 }
 
 nsXBLPrototypeBinding*
 nsXBLDocumentInfo::GetPrototypeBinding(const nsACString& aRef)
 {
   if (!mBindingTable)
     return nullptr;
@@ -541,31 +351,16 @@ FlushScopedSkinSheets(const nsACString &
 void
 nsXBLDocumentInfo::FlushSkinStylesheets()
 {
   if (mBindingTable) {
     mBindingTable->EnumerateRead(FlushScopedSkinSheets, nullptr);
   }
 }
 
-JSObject*
-nsXBLDocumentInfo::GetCompilationGlobal()
-{
-  EnsureGlobalObject();
-  return mGlobalObject->GetCompilationGlobal();
-}
-
-void
-nsXBLDocumentInfo::EnsureGlobalObject()
-{
-  if (!mGlobalObject) {
-    mGlobalObject = new nsXBLDocGlobalObject(this);
-  }
-}
-
 #ifdef DEBUG
 void
 AssertInCompilationScope()
 {
   AutoJSContext cx;
   // Note - Inverting the order of these operands is a rooting hazard.
   MOZ_ASSERT(xpc::GetCompilationScope() == JS::CurrentGlobalOrNull(cx));
 }
--- a/dom/xbl/nsXBLDocumentInfo.h
+++ b/dom/xbl/nsXBLDocumentInfo.h
@@ -40,36 +40,31 @@ public:
   nsresult WritePrototypeBindings();
 
   void SetFirstPrototypeBinding(nsXBLPrototypeBinding* aBinding);
 
   void FlushSkinStylesheets();
 
   bool IsChrome() { return mIsChrome; }
 
-  JSObject* GetCompilationGlobal();
-
   void MarkInCCGeneration(uint32_t aGeneration);
 
   static nsresult ReadPrototypeBindings(nsIURI* aURI, nsXBLDocumentInfo** aDocInfo);
 
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsXBLDocumentInfo)
 
 private:
-  void EnsureGlobalObject();
   nsCOMPtr<nsIDocument> mDocument;
   bool mScriptAccess;
   bool mIsChrome;
   // the binding table owns each nsXBLPrototypeBinding
   nsAutoPtr<nsClassHashtable<nsCStringHashKey, nsXBLPrototypeBinding>> mBindingTable;
 
   // non-owning pointer to the first binding in the table
   nsXBLPrototypeBinding* mFirstBinding;
-
-  nsRefPtr<nsXBLDocGlobalObject> mGlobalObject;
 };
 
 #ifdef DEBUG
 void AssertInCompilationScope();
 #else
 inline void AssertInCompilationScope() {}
 #endif