Bug 667183 - Create a static nsGlobalWindow::Init method to do one-shot initializations currently in nsGlobalWindow ctor. r=jst
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 31 Aug 2011 16:54:39 +0200
changeset 76268 bc0b3ab327fa320000405b6b0aa4e76825c3f61b
parent 76267 69c025d6d230192ebea521a1d24fbd7b1e4ed9ef
child 76292 6c9f2fbaec1b4bd5a3e45c0fab707e48294e6167
child 76337 ba8bbef0fdf97da4fba62e57bbeb39150131a4db
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersjst
bugs667183
milestone9.0a1
Bug 667183 - Create a static nsGlobalWindow::Init method to do one-shot initializations currently in nsGlobalWindow ctor. r=jst
dom/base/nsGlobalWindow.cpp
dom/base/nsGlobalWindow.h
layout/build/nsLayoutStatics.cpp
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -925,68 +925,72 @@ nsGlobalWindow::nsGlobalWindow(nsGlobalW
       // if this fails to open, Dump() knows to just go to stdout
       // on null.
       gDumpFile = fopen(fname, "wb+");
     } else {
       gDumpFile = stdout;
     }
   }
 
-  if (!gEntropyCollector) {
-    CallGetService(NS_ENTROPYCOLLECTOR_CONTRACTID, &gEntropyCollector);
-  }
-
   mSerial = ++gSerialCounter;
 
 #ifdef DEBUG
   if (!PR_GetEnv("MOZ_QUIET")) {
     printf("++DOMWINDOW == %d (%p) [serial = %d] [outer = %p]\n", gRefCnt,
            static_cast<void*>(static_cast<nsIScriptGlobalObject*>(this)),
            gSerialCounter, static_cast<void*>(aOuterWindow));
   }
 #endif
 
 #ifdef PR_LOGGING
-  if (!gDOMLeakPRLog)
-    gDOMLeakPRLog = PR_NewLogModule("DOMLeak");
-
   if (gDOMLeakPRLog)
     PR_LOG(gDOMLeakPRLog, PR_LOG_DEBUG,
            ("DOMWINDOW %p created outer=%p", this, aOuterWindow));
 #endif
 
-  // TODO: could be moved to a ::Init() method, see bug 667183.
-  if (!sWindowsById) {
-    sWindowsById = new WindowByIdTable();
-    if (!sWindowsById->Init()) {
-      delete sWindowsById;
-      sWindowsById = nsnull;
-      NS_ERROR("sWindowsById initialization failed!");
-    }
-  }
-
-  if (sWindowsById) {
-    NS_ASSERTION(!sWindowsById->Get(mWindowID),
-                 "This window shouldn't be in the hash table yet!");
-    sWindowsById->Put(mWindowID, this);
-  }
+  NS_ASSERTION(sWindowsById, "Windows hash table must be created!");
+  NS_ASSERTION(!sWindowsById->Get(mWindowID),
+               "This window shouldn't be in the hash table yet!");
+  sWindowsById->Put(mWindowID, this);
+}
+
+/* static */
+void
+nsGlobalWindow::Init()
+{
+  CallGetService(NS_ENTROPYCOLLECTOR_CONTRACTID, &gEntropyCollector);
+  NS_ASSERTION(gEntropyCollector,
+               "gEntropyCollector should have been initialized!");
+
+#ifdef PR_LOGGING
+  gDOMLeakPRLog = PR_NewLogModule("DOMLeak");
+  NS_ASSERTION(gDOMLeakPRLog, "gDOMLeakPRLog should have been initialized!");
+#endif
+
+  sWindowsById = new WindowByIdTable();
+  // There are two reasons to have Init() failing: if we were not able to
+  // alloc the memory or if the size we want to init is too high. None of them
+  // should happen.
+#ifdef DEBUG
+  NS_ASSERTION(sWindowsById->Init(), "Init() should not fail!");
+#else
+  sWindowsById->Init();
+#endif
 }
 
 nsGlobalWindow::~nsGlobalWindow()
 {
   // We have to check if sWindowsById isn't null because ::Shutdown might have
   // been called.
   if (sWindowsById) {
     NS_ASSERTION(sWindowsById->Get(mWindowID),
                  "This window should be in the hash table");
     sWindowsById->Remove(mWindowID);
   }
-  if (!--gRefCnt) {
-    NS_IF_RELEASE(gEntropyCollector);
-  }
+
 #ifdef DEBUG
   if (!PR_GetEnv("MOZ_QUIET")) {
     nsCAutoString url;
     if (mLastOpenedURI) {
       mLastOpenedURI->GetSpec(url);
     }
 
     printf("--DOMWINDOW == %d (%p) [serial = %d] [outer = %p] [url = %s]\n",
@@ -1057,16 +1061,18 @@ nsGlobalWindow::ShutDown()
 {
   NS_IF_RELEASE(sGlobalStorageList);
 
   if (gDumpFile && gDumpFile != stdout) {
     fclose(gDumpFile);
   }
   gDumpFile = nsnull;
 
+  NS_IF_RELEASE(gEntropyCollector);
+
   delete sWindowsById;
   sWindowsById = nsnull;
 }
 
 // static
 void
 nsGlobalWindow::CleanupCachedXBLHandlers(nsGlobalWindow* aWindow)
 {
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -481,16 +481,17 @@ public:
   PRBool IsChromeWindow() const
   {
     return mIsChrome;
   }
 
   nsresult Observe(nsISupports* aSubject, const char* aTopic,
                    const PRUnichar* aData);
 
+  static void Init();
   static void ShutDown();
   static void CleanupCachedXBLHandlers(nsGlobalWindow* aWindow);
   static PRBool IsCallerChrome();
   static void CloseBlockScriptTerminationFunc(nsISupports *aRef);
 
   static void RunPendingTimeoutsRecursive(nsGlobalWindow *aTopWindow,
                                           nsGlobalWindow *aWindow);
 
--- a/layout/build/nsLayoutStatics.cpp
+++ b/layout/build/nsLayoutStatics.cpp
@@ -154,16 +154,18 @@ nsLayoutStatics::Initialize()
 
   nsJSRuntime::Startup();
   rv = nsRegion::InitStatic();
   if (NS_FAILED(rv)) {
     NS_ERROR("Could not initialize nsRegion");
     return rv;
   }
 
+  nsGlobalWindow::Init();
+
   rv = nsContentUtils::Init();
   if (NS_FAILED(rv)) {
     NS_ERROR("Could not initialize nsContentUtils");
     return rv;
   }
 
   rv = nsAttrValue::Init();
   if (NS_FAILED(rv)) {