Bug 676054: part3: nsStyleSheetService::GetInstance. r=bz
authorGabor Krizsanits <gkrizsanits@mozilla.com>
Tue, 16 Oct 2012 21:45:56 +0200
changeset 110594 8c10557ef1b5078ce3af6d64b6157036a686d6ef
parent 110593 0ab2a9c94c911a07bb6333da4ec0503a579284f5
child 110595 47a7448b83c6cd5a262197fa02de8c8f7caaef9e
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersbz
bugs676054
milestone19.0a1
Bug 676054: part3: nsStyleSheetService::GetInstance. r=bz
content/base/src/nsDocument.cpp
layout/base/nsDocumentViewer.cpp
layout/base/nsStyleSheetService.cpp
layout/base/nsStyleSheetService.h
layout/style/nsStyleSet.cpp
--- a/content/base/src/nsDocument.cpp
+++ b/content/base/src/nsDocument.cpp
@@ -2335,35 +2335,30 @@ nsDocument::FillStyleSet(nsStyleSet* aSt
                   "Style set already has a preshint sheet?");
   NS_PRECONDITION(aStyleSet->SheetCount(nsStyleSet::eDocSheet) == 0,
                   "Style set already has document sheets?");
   NS_PRECONDITION(aStyleSet->SheetCount(nsStyleSet::eStyleAttrSheet) == 0,
                   "Style set already has style attr sheets?");
   NS_PRECONDITION(mStyleAttrStyleSheet, "No style attr stylesheet?");
   NS_PRECONDITION(mAttrStyleSheet, "No attr stylesheet?");
 
-  nsCOMPtr<nsIStyleSheetService> dummy =
-    do_GetService(NS_STYLESHEETSERVICE_CONTRACTID);
-
-  nsStyleSheetService *sheetService = nsStyleSheetService::gInstance;
-
-
   aStyleSet->AppendStyleSheet(nsStyleSet::ePresHintSheet, mAttrStyleSheet);
 
   aStyleSet->AppendStyleSheet(nsStyleSet::eStyleAttrSheet,
                               mStyleAttrStyleSheet);
 
   int32_t i;
   for (i = mStyleSheets.Count() - 1; i >= 0; --i) {
     nsIStyleSheet* sheet = mStyleSheets[i];
     if (sheet->IsApplicable()) {
       aStyleSet->AddDocStyleSheet(sheet, this);
     }
   }
 
+  nsStyleSheetService *sheetService = nsStyleSheetService::GetInstance();
   if (sheetService) {
     sheetService->AuthorStyleSheets()->EnumerateForwards(AppendAuthorSheet,
                                                          aStyleSet);
   }
 
   for (i = mCatalogSheets.Count() - 1; i >= 0; --i) {
     nsIStyleSheet* sheet = mCatalogSheets[i];
     if (sheet->IsApplicable()) {
--- a/layout/base/nsDocumentViewer.cpp
+++ b/layout/base/nsDocumentViewer.cpp
@@ -2227,20 +2227,17 @@ DocumentViewerImpl::CreateStyleSet(nsIDo
   }
   // quirk.css needs to come after the regular UA sheet (or more precisely,
   // after the html.css and so forth that the UA sheet imports).
   styleSet->PrependStyleSheet(nsStyleSet::eAgentSheet, quirkClone);
   styleSet->SetQuirkStyleSheet(quirkClone);
   styleSet->PrependStyleSheet(nsStyleSet::eAgentSheet,
                               nsLayoutStylesheetCache::UASheet());
 
-  nsCOMPtr<nsIStyleSheetService> dummy =
-    do_GetService(NS_STYLESHEETSERVICE_CONTRACTID);
-
-  nsStyleSheetService *sheetService = nsStyleSheetService::gInstance;
+  nsStyleSheetService *sheetService = nsStyleSheetService::GetInstance();
   if (sheetService) {
     sheetService->AgentStyleSheets()->EnumerateForwards(AppendAgentSheet,
                                                         styleSet);
     sheetService->UserStyleSheets()->EnumerateBackwards(PrependUserSheet,
                                                         styleSet);
   }
 
   // Caller will handle calling EndUpdate, per contract.
--- a/layout/base/nsStyleSheetService.cpp
+++ b/layout/base/nsStyleSheetService.cpp
@@ -235,16 +235,31 @@ nsStyleSheetService::UnregisterSheet(nsI
   nsCOMPtr<nsIObserverService> serv =
     mozilla::services::GetObserverService();
   if (serv)
     serv->NotifyObservers(sheet, message, nullptr);
 
   return NS_OK;
 }
 
+//static
+nsStyleSheetService *
+nsStyleSheetService::GetInstance()
+{
+  static bool first = true;
+  if (first) {
+    // make sure at first call that it's inited
+    nsCOMPtr<nsIStyleSheetService> dummy =
+      do_GetService(NS_STYLESHEETSERVICE_CONTRACTID);
+    first = false;
+  }
+
+  return gInstance;
+}
+
 size_t
 nsStyleSheetService::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
 {
   if (!nsStyleSheetService::gInstance) {
     return 0;
   }
 
   return nsStyleSheetService::gInstance->
--- a/layout/base/nsStyleSheetService.h
+++ b/layout/base/nsStyleSheetService.h
@@ -36,16 +36,17 @@ class nsStyleSheetService MOZ_FINAL : pu
   NS_HIDDEN_(nsresult) Init();
 
   nsCOMArray<nsIStyleSheet>* AgentStyleSheets() { return &mSheets[AGENT_SHEET]; }
   nsCOMArray<nsIStyleSheet>* UserStyleSheets() { return &mSheets[USER_SHEET]; }
   nsCOMArray<nsIStyleSheet>* AuthorStyleSheets() { return &mSheets[AUTHOR_SHEET]; }
 
   static size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf);
 
+  static nsStyleSheetService *GetInstance();
   static nsStyleSheetService *gInstance;
 
  private:
 
   NS_HIDDEN_(void) RegisterFromEnumerator(nsICategoryManager  *aManager,
                                           const char          *aCategory,
                                           nsISimpleEnumerator *aEnumerator,
                                           uint32_t             aSheetType);
--- a/layout/style/nsStyleSet.cpp
+++ b/layout/style/nsStyleSet.cpp
@@ -358,17 +358,17 @@ nsStyleSet::AddDocStyleSheet(nsIStyleShe
 {
   NS_PRECONDITION(aSheet && aDocument, "null arg");
   NS_ASSERTION(aSheet->IsApplicable(),
                "Inapplicable sheet being placed in style set");
 
   nsCOMArray<nsIStyleSheet>& docSheets = mSheets[eDocSheet];
 
   docSheets.RemoveObject(aSheet);
-  nsStyleSheetService *sheetService = nsStyleSheetService::gInstance;
+  nsStyleSheetService *sheetService = nsStyleSheetService::GetInstance();
 
   // lowest index first
   int32_t newDocIndex = aDocument->GetIndexOfStyleSheet(aSheet);
 
   int32_t count = docSheets.Count();
   int32_t index;
   for (index = 0; index < count; index++) {
     nsIStyleSheet* sheet = docSheets.ObjectAt(index);