Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions
nsPrefService.cpp File Reference
#include "nsPrefService.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceDefs.h"
#include "nsICategoryManager.h"
#include "nsCategoryManagerUtils.h"
#include "nsNetUtil.h"
#include "nsIFile.h"
#include "nsILocalFile.h"
#include "nsIObserverService.h"
#include "nsPrefBranch.h"
#include "nsXPIDLString.h"
#include "nsCRT.h"
#include "nsCOMArray.h"
#include "nsXPCOMCID.h"
#include "nsQuickSort.h"
#include "prmem.h"
#include "pldhash.h"
#include "prefapi.h"
#include "prefread.h"
#include "prefapi_private_data.h"
#include "nsITimelineService.h"

Go to the source code of this file.


#define PREF_READ_BUFFER_SIZE   4096


static nsresult openPrefFile (nsIFile *aFile)
static nsresult pref_InitInitialObjects (void)
static int pref_CompareFileNames (nsIFile *aFile1, nsIFile *aFile2, void *)
static nsresult pref_LoadPrefsInDir (nsIFile *aDir, char const *const *aSpecialFiles, PRUint32 aSpecialFilesCount)
 Load default pref files from a directory.

Define Documentation

Definition at line 70 of file nsPrefService.cpp.

Definition at line 71 of file nsPrefService.cpp.

Function Documentation

static nsresult openPrefFile ( nsIFile aFile) [static]

Definition at line 561 of file nsPrefService.cpp.

  nsCOMPtr<nsIInputStream> inStr;
  char      readBuf[PREF_READ_BUFFER_SIZE];

    nsCAutoString str;
    NS_TIMELINE_MARK_FUNCTION1("load pref file", str.get());

  nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), aFile);
  if (NS_FAILED(rv)) 
    return rv;        

  PrefParseState ps;
  PREF_InitParseState(&ps, PREF_ReaderCallback, NULL);
  for (;;) {
    PRUint32 amtRead = 0;
    rv = inStr->Read(readBuf, sizeof(readBuf), &amtRead);
    if (NS_FAILED(rv) || amtRead == 0)

    PREF_ParseBuf(&ps, readBuf, amtRead); 
  return rv;        

Here is the call graph for this function:

Here is the caller graph for this function:

static int pref_CompareFileNames ( nsIFile aFile1,
nsIFile aFile2,
) [static]

Definition at line 597 of file nsPrefService.cpp.

  nsCAutoString filename1, filename2;

  return Compare(filename2, filename1);

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult pref_InitInitialObjects ( void  ) [static]

Definition at line 710 of file nsPrefService.cpp.

  nsCOMPtr<nsIFile> aFile;
  nsCOMPtr<nsIFile> defaultPrefDir;
  nsresult          rv;

  // first we parse the GRE default prefs. This also works if we're not using a GRE, 

  rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(defaultPrefDir));

  rv = defaultPrefDir->AppendNative(NS_LITERAL_CSTRING("greprefs"));

  rv = pref_LoadPrefsInDir(defaultPrefDir, nsnull, 0);
  if (NS_FAILED(rv)) {
    NS_WARNING("Error parsing GRE default preferences. Is this an old-style embedding app?");

  // now parse the "application" default preferences
  rv = NS_GetSpecialDirectory(NS_APP_PREF_DEFAULTS_50_DIR, getter_AddRefs(defaultPrefDir));

  /* these pref file names should not be used: we process them after all other application pref files for backwards compatibility */
  static const char* specialFiles[] = {
#if defined(XP_MAC) || defined(XP_MACOSX)
#elif defined(XP_WIN)
#elif defined(XP_UNIX)
#if defined(VMS)
      , "openvms.js"
#elif defined(_AIX)
      , "aix.js"
#if defined(MOZ_WIDGET_PHOTON)
         , "photon.js"
#elif defined(XP_OS2)
#elif defined(XP_BEOS)

  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

  nsCOMPtr<nsIProperties> dirSvc(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsISimpleEnumerator> dirList;
  if (dirList) {
    PRBool hasMore;
    while (NS_SUCCEEDED(dirList->HasMoreElements(&hasMore)) && hasMore) {
      nsCOMPtr<nsISupports> 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;

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult pref_LoadPrefsInDir ( nsIFile aDir,
char const *const aSpecialFiles,
PRUint32  aSpecialFilesCount 
) [static]

Load default pref files from a directory.

The files in the directory are sorted reverse-alphabetically; a set of "special file names" may be specified which are loaded after all the others.

Definition at line 612 of file nsPrefService.cpp.

  nsresult rv, rv2;
  PRBool hasMoreElements;

  nsCOMPtr<nsISimpleEnumerator> dirIterator;

  // this may fail in some normal cases, such as embedders who do not use a GRE
  rv = aDir->GetDirectoryEntries(getter_AddRefs(dirIterator));
  if (NS_FAILED(rv)) {
    // If the directory doesn't exist, then we have no reason to complain.  We
    // loaded everything (and nothing) successfully.
    if (rv == NS_ERROR_FILE_NOT_FOUND)
      rv = NS_OK;
    return rv;

  rv = dirIterator->HasMoreElements(&hasMoreElements);

  nsCOMArray<nsIFile> prefFiles(INITIAL_PREF_FILES);
  nsCOMArray<nsIFile> specialFiles(aSpecialFilesCount);
  nsCOMPtr<nsIFile> prefFile;

  while (hasMoreElements && NS_SUCCEEDED(rv)) {
    nsCAutoString leafName;

    rv = dirIterator->GetNext(getter_AddRefs(prefFile));
    if (NS_FAILED(rv)) {

    NS_ASSERTION(!leafName.IsEmpty(), "Failure in default prefs: directory enumerator returned empty file?");

    // Skip non-js files
    if (StringEndsWith(leafName, NS_LITERAL_CSTRING(".js"),
                       nsCaseInsensitiveCStringComparator())) {
      PRBool shouldParse = PR_TRUE;
      // separate out special files
      for (PRUint32 i = 0; i < aSpecialFilesCount; ++i) {
        if (leafName.Equals(nsDependentCString(aSpecialFiles[i]))) {
          shouldParse = PR_FALSE;
          // special files should be process in order; we put them into
          // the array by index; this can make the array sparse
          specialFiles.ReplaceObjectAt(prefFile, i);

      if (shouldParse) {

    rv = dirIterator->HasMoreElements(&hasMoreElements);

  if (prefFiles.Count() + specialFiles.Count() == 0) {
    NS_WARNING("No default pref files found.");
    if (NS_SUCCEEDED(rv)) {
    return rv;

  prefFiles.Sort(pref_CompareFileNames, nsnull);
  PRUint32 arrayCount = prefFiles.Count();
  PRUint32 i;
  for (i = 0; i < arrayCount; ++i) {
    rv2 = openPrefFile(prefFiles[i]);
    if (NS_FAILED(rv2)) {
      NS_ERROR("Default pref file not parsed successfully.");
      rv = rv2;

  arrayCount = specialFiles.Count();
  for (i = 0; i < arrayCount; ++i) {
    // this may be a sparse array; test before parsing
    nsIFile* file = specialFiles[i];
    if (file) {
      rv2 = openPrefFile(file);
      if (NS_FAILED(rv2)) {
        NS_ERROR("Special default pref file not parsed successfully.");
        rv = rv2;

  return rv;

Here is the call graph for this function:

Here is the caller graph for this function: