Bug 392967: Load app and extension prefs separately. r=bsmedberg
authorthunder@mozilla.com
Fri, 31 Aug 2007 19:16:47 -0700
changeset 5534 70c14e34d7dff30524ff2fab0ea618f502196ac8
parent 5533 d62ac88f85b5625e202dd287af985378d459f123
child 5535 d8cb4f3a32dfce1ff6878c2845b2a3d56f32ab28
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbsmedberg
bugs392967
milestone1.9a8pre
Bug 392967: Load app and extension prefs separately. r=bsmedberg
modules/libpref/public/nsIPrefService.idl
modules/libpref/src/nsPrefService.cpp
toolkit/xre/nsXREDirProvider.cpp
xpcom/io/nsAppDirectoryServiceDefs.h
--- a/modules/libpref/public/nsIPrefService.idl
+++ b/modules/libpref/public/nsIPrefService.idl
@@ -174,9 +174,16 @@ interface nsIPrefService : nsISupports
 #define NS_PREFSERVICE_READ_TOPIC_ID "prefservice:before-read-userprefs"
 
 /**
  * Notification sent when resetPrefs has been called, but before the actual
  * reset process occurs.
  */
 #define NS_PREFSERVICE_RESET_TOPIC_ID "prefservice:before-reset"
 
+/**
+ * Notification sent when after reading app-provided default
+ * preferences, but before user profile override defaults or extension
+ * defaults are loaded.
+ */
+#define NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID "prefservice:after-app-defaults"
+
 %}
--- a/modules/libpref/src/nsPrefService.cpp
+++ b/modules/libpref/src/nsPrefService.cpp
@@ -712,16 +712,42 @@ pref_LoadPrefsInDir(nsIFile* aDir, char 
         rv = rv2;
       }
     }
   }
 
   return rv;
 }
 
+static nsresult pref_LoadPrefsInDirList(const char *listId)
+{
+  nsresult rv;
+  nsCOMPtr<nsIProperties> dirSvc(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
+  if (NS_FAILED(rv)) return rv;
+
+  nsCOMPtr<nsISimpleEnumerator> dirList;
+  dirSvc->Get(listId,
+              NS_GET_IID(nsISimpleEnumerator),
+              getter_AddRefs(dirList));
+  if (dirList) {
+    PRBool hasMore;
+    while (NS_SUCCEEDED(dirList->HasMoreElements(&hasMore)) && hasMore) {
+      nsCOMPtr<nsISupports> elem;
+      dirList->GetNext(getter_AddRefs(elem));
+      if (elem) {
+        nsCOMPtr<nsIFile> dir = do_QueryInterface(elem);
+        if (dir) {
+          // Do we care if a file provided by this process fails to load?
+          pref_LoadPrefsInDir(dir, nsnull, 0); 
+        }
+      }
+    }
+  }
+  return NS_OK;
+}
 
 //----------------------------------------------------------------------------------------
 // Initialize default preference JavaScript buffers from
 // appropriate TEXT resources
 //----------------------------------------------------------------------------------------
 static nsresult pref_InitInitialObjects()
 {
   nsCOMPtr<nsIFile> aFile;
@@ -768,35 +794,24 @@ static nsresult pref_InitInitialObjects(
 #endif
   };
 
   rv = pref_LoadPrefsInDir(defaultPrefDir, specialFiles, NS_ARRAY_LENGTH(specialFiles));
   if (NS_FAILED(rv)) {
     NS_WARNING("Error parsing application default preferences.");
   }
 
-  // xxxbsmedberg: TODO load default prefs from a category
-  // but the architecture is not quite there yet
+  rv = pref_LoadPrefsInDirList(NS_APP_PREFS_DEFAULTS_DIR_LIST);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIProperties> dirSvc(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
-  if (NS_FAILED(rv)) return rv;
+  NS_CreateServicesFromCategory(NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID,
+                                nsnull, NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID);
 
-  nsCOMPtr<nsISimpleEnumerator> dirList;
-  dirSvc->Get(NS_APP_PREFS_DEFAULTS_DIR_LIST,
-              NS_GET_IID(nsISimpleEnumerator),
-              getter_AddRefs(dirList));
-  if (dirList) {
-    PRBool hasMore;
-    while (NS_SUCCEEDED(dirList->HasMoreElements(&hasMore)) && hasMore) {
-      nsCOMPtr<nsISupports> elem;
-      dirList->GetNext(getter_AddRefs(elem));
-      if (elem) {
-        nsCOMPtr<nsIFile> dir = do_QueryInterface(elem);
-        if (dir) {
-          // Do we care if a file provided by this process fails to load?
-          pref_LoadPrefsInDir(dir, nsnull, 0); 
-        }
-      }
-    }
-  }
+  nsCOMPtr<nsIObserverService> observerService = 
+    do_GetService("@mozilla.org/observer-service;1", &rv);
+  
+  if (NS_FAILED(rv) || !observerService)
+    return rv;
 
-  return NS_OK;
+  observerService->NotifyObservers(nsnull, NS_PREFSERVICE_APPDEFAULTS_TOPIC_ID, nsnull);
+
+  return pref_LoadPrefsInDirList(NS_EXT_PREFS_DEFAULTS_DIR_LIST);
 }
--- a/toolkit/xre/nsXREDirProvider.cpp
+++ b/toolkit/xre/nsXREDirProvider.cpp
@@ -635,17 +635,24 @@ nsXREDirProvider::GetFilesInternal(const
       mXULAppDir->Clone(getter_AddRefs(file));
       file->AppendNative(NS_LITERAL_CSTRING("defaults"));
       file->AppendNative(NS_LITERAL_CSTRING("preferences"));
       if (NS_SUCCEEDED(file->Exists(&exists)) && exists)
         directories.AppendObject(file);
 
        LoadAppPlatformDirIntoArray(mXULAppDir, kAppendPrefDir, directories);
     }
-    
+
+    rv = NS_NewArrayEnumerator(aResult, directories);
+  }
+  else if (!strcmp(aProperty, NS_EXT_PREFS_DEFAULTS_DIR_LIST)) {
+    static const char *const kAppendPrefDir[] = { "defaults", "preferences", nsnull };
+    nsCOMArray<nsIFile> directories;
+    PRBool exists;
+
     if (mProfileDir) {
       nsCOMPtr<nsIFile> overrideFile;
       mProfileDir->Clone(getter_AddRefs(overrideFile));
       overrideFile->AppendNative(NS_LITERAL_CSTRING(PREF_OVERRIDE_DIRNAME));
       if (NS_SUCCEEDED(overrideFile->Exists(&exists)) && exists)
         directories.AppendObject(overrideFile);
 
       if (!gSafeMode) {
--- a/xpcom/io/nsAppDirectoryServiceDefs.h
+++ b/xpcom/io/nsAppDirectoryServiceDefs.h
@@ -91,16 +91,17 @@
 // other users of the profile. Without this prefix, the consumer
 // has exclusive access to this location.
  
 #define NS_SHARED                               "SHARED"
 
 #define NS_APP_PREFS_50_DIR                     "PrefD"         // Directory which contains user prefs       
 #define NS_APP_PREFS_50_FILE                    "PrefF"
 #define NS_APP_PREFS_DEFAULTS_DIR_LIST          "PrefDL"
+#define NS_EXT_PREFS_DEFAULTS_DIR_LIST          "ExtPrefDL"
 #define NS_APP_PREFS_OVERRIDE_DIR               "PrefDOverride" // Directory for per-profile defaults
         
 #define NS_APP_USER_PROFILE_50_DIR              "ProfD"
 #define NS_APP_USER_PROFILE_LOCAL_50_DIR        "ProfLD"
         
 #define NS_APP_USER_CHROME_DIR                  "UChrm"
 #define NS_APP_USER_SEARCH_DIR                  "UsrSrchPlugns"