Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Private Attributes | Friends
nsPrefService Class Reference

#include <nsPrefService.h>

Inheritance diagram for nsPrefService:
Inheritance graph
[legend]
Collaboration diagram for nsPrefService:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSIPREFSERVICE
NS_DECL_NSIOBSERVER 
nsPrefService ()
virtual ~nsPrefService ()
nsresult Init ()
void readUserPrefs (in nsIFile aFile)
 Called to read in the preferences specified in a user preference file.
void resetPrefs ()
 Called to completely flush and re-initialize the preferences system.
void resetUserPrefs ()
 Called to reset all preferences with user set values back to the application default values.
void savePrefFile (in nsIFile aFile)
 Called to write current preferences state to a file.
nsIPrefBranch getBranch (in string aPrefRoot)
 Call to get a Preferences "Branch" which accesses user preference data.
nsIPrefBranch getDefaultBranch (in string aPrefRoot)
 Call to get a Preferences "Branch" which accesses only the default preference data.
void observe (in nsISupports aSubject, in string aTopic, in wstring aData)
 Observe will be called when there is a notification for the topic |aTopic|.
void addObserver (in string aDomain, in nsIObserver aObserver, in boolean aHoldWeak)
 Add a preference change observer.
void removeObserver (in string aDomain, in nsIObserver aObserver)
 Remove a preference change observer.
long getPrefType (in string aPrefName)
 Called to determine the type of a specific preference.
boolean getBoolPref (in string aPrefName)
 Called to get the state of an individual boolean preference.
void setBoolPref (in string aPrefName, in long aValue)
 Called to set the state of an individual boolean preference.
string getCharPref (in string aPrefName)
 Called to get the state of an individual string preference.
void setCharPref (in string aPrefName, in string aValue)
 Called to set the state of an individual string preference.
long getIntPref (in string aPrefName)
 Called to get the state of an individual integer preference.
void setIntPref (in string aPrefName, in long aValue)
 Called to set the state of an individual integer preference.
void getComplexValue (in string aPrefName, in nsIIDRef aType,[iid_is(aType), retval] out nsQIResult aValue)
 Called to get the state of an individual complex preference.
void setComplexValue (in string aPrefName, in nsIIDRef aType, in nsISupports aValue)
 Called to set the state of an individual complex preference.
void clearUserPref (in string aPrefName)
 Called to clear a user set value from a specific preference.
void lockPref (in string aPrefName)
 Called to lock a specific preference.
boolean prefHasUserValue (in string aPrefName)
 Called to check if a specific preference has a user value associated to it.
boolean prefIsLocked (in string aPrefName)
 Called to check if a specific preference is locked.
void unlockPref (in string aPrefName)
 Called to unlock a specific preference.
void deleteBranch (in string aStartingAt)
 Called to remove all of the preferences referenced by this branch.
void getChildList (in string aStartingAt, out unsigned long aCount,[array, size_is(aCount), retval] out string aChildArray)
 Returns an array of strings representing the child preferences of the root of this branch.
void resetBranch (in string aStartingAt)
 Called to reset all of the preferences referenced by this branch to their default values.

Public Attributes

const long PREF_INVALID = 0
 Values describing the basic preference types.
const long PREF_STRING = 32
const long PREF_INT = 64
const long PREF_BOOL = 128
readonly attribute string root
 Called to get the root on which this branch is based, such as "browser.startup.".

Protected Member Functions

nsresult NotifyServiceObservers (const char *aSubject)
nsresult UseDefaultPrefFile ()
nsresult UseUserPrefFile ()
nsresult ReadAndOwnUserPrefFile (nsIFile *aFile)
nsresult ReadAndOwnSharedUserPrefFile (nsIFile *aFile)
nsresult SavePrefFileInternal (nsIFile *aFile)
nsresult WritePrefFile (nsIFile *aFile)

Private Attributes

nsCOMPtr< nsIPrefBranch2mRootBranch
nsCOMPtr< nsIFilemCurrentFile
PRPackedBool mErrorOpeningUserPrefs
PRPackedBool mErrorOpeningSharedUserPrefs
nsCOMPtr< nsIFilemCurrentSharedFile

Friends

class nsSharedPrefHandler

Detailed Description

Definition at line 51 of file nsPrefService.h.


Constructor & Destructor Documentation

Definition at line 87 of file nsPrefService.cpp.

: mErrorOpeningUserPrefs(PR_FALSE)
#if MOZ_PROFILESHARING
  , mErrorOpeningSharedUserPrefs(PR_FALSE)
#endif
{
}

Definition at line 95 of file nsPrefService.cpp.

{
  PREF_Cleanup();

#ifdef MOZ_PROFILESHARING
  NS_IF_RELEASE(gSharedPrefHandler);
#endif
}

Here is the call graph for this function:


Member Function Documentation

void nsIPrefBranch2::addObserver ( in string  aDomain,
in nsIObserver  aObserver,
in boolean  aHoldWeak 
) [inherited]

Add a preference change observer.

On preference changes, the following arguments will be passed to the nsIObserver.observe() method: aSubject - The nsIPrefBranch object (this) aTopic - The string defined by NS_PREFBRANCH_PREFCHANGE_TOPIC_ID aData - The preference which has changed

Parameters:
aDomainThe preference on which to listen for changes. This can be the name of an entire branch to observe. e.g. Holding the "root" prefbranch and calling addObserver("foo.bar.", ...) will observe changes to foo.bar.baz and foo.bar.bzip
aObserverThe object to be notified if the preference changes.
aHoldWeaktrue Hold a weak reference to |aObserver|. The object must implement the nsISupportsWeakReference interface or this will fail. false Hold a strong reference to |aObserver|.
Note:
Registering as a preference observer can open an object to potential cyclical references which will cause memory leaks. These cycles generally occur because an object both registers itself as an observer (causing the branch to hold a reference to the observer) and holds a reference to the branch object for the purpose of getting/setting preference values. There are 3 approaches which have been implemented in an attempt to avoid these situations. 1) The nsPrefBranch object supports nsISupportsWeakReference. Any consumer may hold a weak reference to it instead of a strong one. 2) The nsPrefBranch object listens for xpcom-shutdown and frees all of the objects currently in its observer list. This insures that long lived objects (services for example) will be freed correctly. 3) The observer can request to be held as a weak reference when it is registered. This insures that shorter lived objects (say one tied to an open window) will not fall into the cyclical reference trap.
The list of registered observers may be changed during the dispatch of nsPref:changed notification. However, the observers are not guaranteed to be notified in any particular order, so you can't be sure whether the added/removed observer will be called during the notification when it is added/removed.
It is possible to change preferences during the notification.
It is not safe to change observers during this callback in Gecko releases before 1.9. If you want a safe way to remove a pref observer, please use an nsITimer.
See also:
nsIObserver
removeObserver
void nsIPrefBranch::clearUserPref ( in string  aPrefName) [inherited]

Called to clear a user set value from a specific preference.

This will, in effect, reset the value to the default value. If no default value exists the preference will cease to exist.

Parameters:
aPrefNameThe preference to be cleared.
Note:
This method does nothing if this object is a default branch.
Returns:
NS_OK The user preference was successfully cleared.
Other The preference does not exist or have a user set value.
void nsIPrefBranch::deleteBranch ( in string  aStartingAt) [inherited]

Called to remove all of the preferences referenced by this branch.

Parameters:
aStartingAtThe point on the branch at which to start the deleting preferences. Pass in "" to remove all preferences referenced by this branch.
Note:
This method can be called on either a default or user branch but, in effect, always operates on both.
Returns:
NS_OK The preference(s) were successfully removed.
Other The preference(s) do not exist or an error occurred.
boolean nsIPrefBranch::getBoolPref ( in string  aPrefName) [inherited]

Called to get the state of an individual boolean preference.

Parameters:
aPrefNameThe boolean preference to get the state of.
Returns:
boolean The value of the requested boolean preference.
See also:
setBoolPref
nsIPrefBranch nsIPrefService::getBranch ( in string  aPrefRoot) [inherited]

Call to get a Preferences "Branch" which accesses user preference data.

Using a Set method on this object will always create or set a user preference value. When using a Get method a user set value will be returned if one exists, otherwise a default value will be returned.

Parameters:
aPrefRootThe preference "root" on which to base this "branch". For example, if the root "browser.startup." is used, the branch will be able to easily access the preferences "browser.startup.page", "browser.startup.homepage", or "browser.startup.homepage_override" by simply requesting "page", "homepage", or "homepage_override". nsnull or "" may be used to access to the entire preference "tree".
Returns:
nsIPrefBranch The object representing the requested branch.
See also:
getDefaultBranch
string nsIPrefBranch::getCharPref ( in string  aPrefName) [inherited]

Called to get the state of an individual string preference.

Parameters:
aPrefNameThe string preference to retrieve.
Returns:
string The value of the requested string preference.
See also:
setCharPref
void nsIPrefBranch::getChildList ( in string  aStartingAt,
out unsigned long  aCount,
[array, size_is(aCount), retval] out string  aChildArray 
) [inherited]

Returns an array of strings representing the child preferences of the root of this branch.

Parameters:
aStartingAtThe point on the branch at which to start enumerating the child preferences. Pass in "" to enumerate all preferences referenced by this branch.
aCountReceives the number of elements in the array.
aChildArrayReceives the array of child preferences.
Note:
This method can be called on either a default or user branch but, in effect, always operates on both.
Returns:
NS_OK The preference list was successfully retrieved.
Other The preference(s) do not exist or an error occurred.
void nsIPrefBranch::getComplexValue ( in string  aPrefName,
in nsIIDRef  aType,
[iid_is(aType), retval] out nsQIResult  aValue 
) [inherited]

Called to get the state of an individual complex preference.

A complex preference is a preference which represents an XPCOM object that can not be easily represented using a standard boolean, integer or string value.

Parameters:
aPrefNameThe complex preference to get the value of.
aTypeThe XPCOM interface that this complex preference represents. Interfaces currently supported are:
aValueThe XPCOM object into which to the complex preference value should be retrieved.
Returns:
NS_OK The value was successfully retrieved.
Other The value does not exist or is the wrong type.
See also:
setComplexValue

Call to get a Preferences "Branch" which accesses only the default preference data.

Using a Set method on this object will always create or set a default preference value. When using a Get method a default value will always be returned.

Parameters:
aPrefRootThe preference "root" on which to base this "branch". For example, if the root "browser.startup." is used, the branch will be able to easily access the preferences "browser.startup.page", "browser.startup.homepage", or "browser.startup.homepage_override" by simply requesting "page", "homepage", or "homepage_override". nsnull or "" may be used to access to the entire preference "tree".
Note:
Few consumers will want to create default branch objects. Many of the branch methods do nothing on a default branch because the operations only make sense when applied to user set preferences.
Returns:
nsIPrefBranch The object representing the requested default branch.
See also:
getBranch
long nsIPrefBranch::getIntPref ( in string  aPrefName) [inherited]

Called to get the state of an individual integer preference.

Parameters:
aPrefNameThe integer preference to get the value of.
Returns:
long The value of the requested integer preference.
See also:
setIntPref
long nsIPrefBranch::getPrefType ( in string  aPrefName) [inherited]

Called to determine the type of a specific preference.

Parameters:
aPrefNameThe preference to get the type of.
Returns:
long A value representing the type of the preference. This value will be PREF_STRING, PREF_INT, or PREF_BOOL.

Definition at line 127 of file nsPrefService.cpp.

{
  nsPrefBranch *rootBranch = new nsPrefBranch("", PR_FALSE); 
  if (!rootBranch)
    return NS_ERROR_OUT_OF_MEMORY;

  mRootBranch = (nsIPrefBranch2 *)rootBranch;
  
  nsXPIDLCString lockFileName;
  nsresult rv;

  rv = PREF_Init();
  NS_ENSURE_SUCCESS(rv, rv);

  rv = pref_InitInitialObjects();
  NS_ENSURE_SUCCESS(rv, rv);

  /*
   * The following is a small hack which will allow us to only load the library
   * which supports the netscape.cfg file if the preference is defined. We
   * test for the existence of the pref, set in the all.js (mozilla) or
   * all-ns.js (netscape 6), and if it exists we startup the pref config
   * category which will do the rest.
   */

  rv = mRootBranch->GetCharPref("general.config.filename", getter_Copies(lockFileName));
  if (NS_SUCCEEDED(rv))
    NS_CreateServicesFromCategory("pref-config-startup",
                                  NS_STATIC_CAST(nsISupports *, NS_STATIC_CAST(void *, this)),
                                  "pref-config-startup");    

  nsCOMPtr<nsIObserverService> observerService = 
           do_GetService("@mozilla.org/observer-service;1", &rv);
  if (observerService) {
    rv = observerService->AddObserver(this, "profile-before-change", PR_TRUE);
    if (NS_SUCCEEDED(rv)) {
      rv = observerService->AddObserver(this, "profile-do-change", PR_TRUE);
    }
  }

#ifdef MOZ_PROFILESHARING  
  rv = NS_CreateSharedPrefHandler(this);
#endif

  return(rv);
}

Here is the call graph for this function:

void nsIPrefBranch::lockPref ( in string  aPrefName) [inherited]

Called to lock a specific preference.

Locking a preference will cause the preference service to always return the default value regardless of whether there is a user set value or not.

Parameters:
aPrefNameThe preference to be locked.
Note:
This method can be called on either a default or user branch but, in effect, always operates on the default branch.
Returns:
NS_OK The preference was successfully locked.
Other The preference does not exist or an error occurred.
See also:
unlockPref
nsresult nsPrefService::NotifyServiceObservers ( const char *  aSubject) [protected]

Definition at line 285 of file nsPrefService.cpp.

{
  nsresult rv;
  nsCOMPtr<nsIObserverService> observerService = 
    do_GetService("@mozilla.org/observer-service;1", &rv);
  
  if (NS_FAILED(rv) || !observerService)
    return rv;

  nsISupports *subject = (nsISupports *)((nsIPrefService *)this);
  observerService->NotifyObservers(subject, aTopic, nsnull);
  
  return NS_OK;
}

Here is the call graph for this function:

void nsIObserver::observe ( in nsISupports  aSubject,
in string  aTopic,
in wstring  aData 
) [inherited]

Observe will be called when there is a notification for the topic |aTopic|.

This assumes that the object implementing this interface has been registered with an observer service such as the nsIObserverService.

If you expect multiple topics/subjects, the impl is responsible for filtering.

You should not modify, add, remove, or enumerate notifications in the implemention of observe.

Parameters:
aSubject: Notification specific interface pointer.
aTopic: The notification topic or subject.
aData: Notification specific wide string. subject event.
boolean nsIPrefBranch::prefHasUserValue ( in string  aPrefName) [inherited]

Called to check if a specific preference has a user value associated to it.

Parameters:
aPrefNameThe preference to be tested.
Note:
This method can be called on either a default or user branch but, in effect, always operates on the user branch.
Returns:
boolean true The preference has a user set value. false The preference only has a default value.
boolean nsIPrefBranch::prefIsLocked ( in string  aPrefName) [inherited]

Called to check if a specific preference is locked.

If a preference is locked calling its Get method will always return the default value.

Parameters:
aPrefNameThe preference to be tested.
Note:
This method can be called on either a default or user branch but, in effect, always operates on the default branch.
Returns:
boolean true The preference is locked. false The preference is not locked.
See also:
lockPref
unlockPref

Here is the caller graph for this function:

Definition at line 361 of file nsPrefService.cpp.

{
  NS_ENSURE_ARG(aFile);
  
  if (mCurrentFile == aFile)
    return NS_OK;
  mCurrentFile = aFile;

#ifdef MOZ_PROFILESHARING
  // We don't want prefs set here to cause transactions
  gSharedPrefHandler->ReadingUserPrefs(PR_TRUE);
#endif

  // We need to track errors in reading the shared and the
  // non-shared files independently. 
  // Set the appropriate member variable from it after reading.
  nsresult rv = openPrefFile(mCurrentFile);
  mErrorOpeningUserPrefs = NS_FAILED(rv);

#ifdef MOZ_PROFILESHARING
  gSharedPrefHandler->ReadingUserPrefs(PR_FALSE);
#endif

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIPrefService::readUserPrefs ( in nsIFile  aFile) [inherited]

Called to read in the preferences specified in a user preference file.

Parameters:
aFileThe file to be read.
Note:
If nsnull is passed in for the aFile parameter the default preferences file(s) [prefs.js, user.js] will be read and processed.
Returns:
NS_OK File was read and processed.
Other File failed to read or contained invalid data.
See also:
savePrefFile
nsIFile
void nsIPrefBranch2::removeObserver ( in string  aDomain,
in nsIObserver  aObserver 
) [inherited]

Remove a preference change observer.

Parameters:
aDomainThe preference which is being observed for changes.
aObserverAn observer previously registered with addObserver().
See also:
nsIObserver
addObserver
void nsIPrefBranch::resetBranch ( in string  aStartingAt) [inherited]

Called to reset all of the preferences referenced by this branch to their default values.

Parameters:
aStartingAtThe point on the branch at which to start the resetting preferences to their default values. Pass in "" to reset all preferences referenced by this branch.
Note:
This method can be called on either a default or user branch but, in effect, always operates on the user branch.
Returns:
NS_OK The preference(s) were successfully reset.
Other The preference(s) do not exist or an error occurred.

Called to completely flush and re-initialize the preferences system.

Returns:
NS_OK The preference service was re-initialized correctly.
Other The preference service failed to restart correctly.

Called to reset all preferences with user set values back to the application default values.

Returns:
NS_OK Always.
void nsIPrefService::savePrefFile ( in nsIFile  aFile) [inherited]

Called to write current preferences state to a file.

Parameters:
aFileThe file to be written.
Note:
If nsnull is passed in for the aFile parameter the preference data is written out to the current preferences file (usually prefs.js.)
Returns:
NS_OK File was written.
Other File failed to write.
See also:
readUserPrefs
nsIFile

Definition at line 415 of file nsPrefService.cpp.

{
  if (nsnull == aFile) {
    // the gDirty flag tells us if we should write to mCurrentFile
    // we only check this flag when the caller wants to write to the default
    if (!gDirty)
      return NS_OK;
    
    // It's possible that we never got a prefs file.
    nsresult rv = NS_OK;
    if (mCurrentFile)
      rv = WritePrefFile(mCurrentFile);

#ifdef MOZ_PROFILESHARING
    if (mCurrentSharedFile) {
      nsresult rv2 = WritePrefFile(mCurrentSharedFile);
      if (NS_SUCCEEDED(rv))
        rv = rv2;
    }
#endif

    return rv;
  } else {
    return WritePrefFile(aFile);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIPrefBranch::setBoolPref ( in string  aPrefName,
in long  aValue 
) [inherited]

Called to set the state of an individual boolean preference.

Parameters:
aPrefNameThe boolean preference to set the state of.
aValueThe boolean value to set the preference to.
Returns:
NS_OK The value was successfully set.
Other The value was not set or is the wrong type.
See also:
getBoolPref
void nsIPrefBranch::setCharPref ( in string  aPrefName,
in string  aValue 
) [inherited]

Called to set the state of an individual string preference.

Parameters:
aPrefNameThe string preference to set.
aValueThe string value to set the preference to.
Returns:
NS_OK The value was successfully set.
Other The value was not set or is the wrong type.
See also:
getCharPref
void nsIPrefBranch::setComplexValue ( in string  aPrefName,
in nsIIDRef  aType,
in nsISupports  aValue 
) [inherited]

Called to set the state of an individual complex preference.

A complex preference is a preference which represents an XPCOM object that can not be easily represented using a standard boolean, integer or string value.

Parameters:
aPrefNameThe complex preference to set the value of.
aTypeThe XPCOM interface that this complex preference represents. Interfaces currently supported are:
aValueThe XPCOM object from which to set the complex preference value.
Returns:
NS_OK The value was successfully set.
Other The value was not set or is the wrong type.
See also:
getComplexValue
void nsIPrefBranch::setIntPref ( in string  aPrefName,
in long  aValue 
) [inherited]

Called to set the state of an individual integer preference.

Parameters:
aPrefNameThe integer preference to set the value of.
aValueThe integer value to set the preference to.
Returns:
NS_OK The value was successfully set.
Other The value was not set or is the wrong type.
See also:
getIntPref
void nsIPrefBranch::unlockPref ( in string  aPrefName) [inherited]

Called to unlock a specific preference.

Unlocking a previously locked preference allows the preference service to once again return the user set value of the preference.

Parameters:
aPrefNameThe preference to be unlocked.
Note:
This method can be called on either a default or user branch but, in effect, always operates on the default branch.
Returns:
NS_OK The preference was successfully unlocked.
Other The preference does not exist or an error occurred.
See also:
lockPref

Definition at line 300 of file nsPrefService.cpp.

{
  nsresult rv, rv2;
  nsCOMPtr<nsIFile> aFile;

#ifdef MOZ_PROFILESHARING
  // First, read the shared file.
  if (isSharingEnabled()) {
    rv = NS_GetSpecialDirectory(NS_SHARED NS_APP_PREFS_50_FILE, getter_AddRefs(aFile));
    if (NS_SUCCEEDED(rv)) {
      rv = ReadAndOwnSharedUserPrefFile(aFile);
      // Most likely cause of failure here is that the file didn't
      // exist, so save a new one. mSharedUserPrefReadFailed will be
      // used to catch an error in actually reading the file.
      if (NS_FAILED(rv)) {
        rv2 = SavePrefFileInternal(aFile);
        NS_ASSERTION(NS_SUCCEEDED(rv2), "Failed to save new shared pref file");
      }
    }
  }
  // Continue on to read the nonshared file.
#endif

  rv = NS_GetSpecialDirectory(NS_APP_PREFS_50_FILE, getter_AddRefs(aFile));
  if (NS_SUCCEEDED(rv)) {
    rv = ReadAndOwnUserPrefFile(aFile);
    // Most likely cause of failure here is that the file didn't
    // exist, so save a new one. mUserPrefReadFailed will be
    // used to catch an error in actually reading the file.
    if (NS_FAILED(rv)) {
      rv2 = SavePrefFileInternal(aFile);
      NS_ASSERTION(NS_SUCCEEDED(rv2), "Failed to save new shared pref file");
    }
  }
  
  return rv;
}

Here is the call graph for this function:

Definition at line 338 of file nsPrefService.cpp.

{
  nsresult rv = NS_OK;
  nsCOMPtr<nsIFile> aFile;

#ifdef MOZ_PROFILESHARING
  nsCAutoString prefsDirProp(NS_APP_PREFS_50_DIR);
  if (isSharingEnabled())
    prefsDirProp.Insert(NS_SHARED, 0); // Prepend modifier so we get shared file
#else
  nsDependentCString prefsDirProp(NS_APP_PREFS_50_DIR);
#endif

  rv = NS_GetSpecialDirectory(prefsDirProp.get(), getter_AddRefs(aFile));
  if (NS_SUCCEEDED(rv) && aFile) {
    rv = aFile->AppendNative(NS_LITERAL_CSTRING("user.js"));
    if (NS_SUCCEEDED(rv)) {
      rv = openPrefFile(aFile);
    }
  }
  return rv;
}

Here is the call graph for this function:

nsresult nsPrefService::WritePrefFile ( nsIFile aFile) [protected]

Definition at line 442 of file nsPrefService.cpp.

{
  const char                outHeader[] =
    "# Mozilla User Preferences"
    NS_LINEBREAK
    NS_LINEBREAK
    "/* Do not edit this file."
    NS_LINEBREAK
    " *"
    NS_LINEBREAK
    " * If you make changes to this file while the application is running,"
    NS_LINEBREAK
    " * the changes will be overwritten when the application exits."
    NS_LINEBREAK
    " *"
    NS_LINEBREAK
    " * To make a manual change to preferences, you can visit the URL about:config"
    NS_LINEBREAK
    " * For more information, see http://www.mozilla.org/unix/customizing.html#prefs"
    NS_LINEBREAK
    " */"
    NS_LINEBREAK
    NS_LINEBREAK;

  nsCOMPtr<nsIOutputStream> outStreamSink;
  nsCOMPtr<nsIOutputStream> outStream;
  PRUint32                  writeAmount;
  nsresult                  rv;

  if (!gHashTable.ops)
    return NS_ERROR_NOT_INITIALIZED;

  /* ?! Don't save (blank) user prefs if there was an error reading them */
  if (aFile == mCurrentFile && mErrorOpeningUserPrefs)
    return NS_OK;
#if MOZ_PROFILESHARING
  if (aFile == mCurrentSharedFile && mErrorOpeningSharedUserPrefs)
    return NS_OK;
#endif

  // execute a "safe" save by saving through a tempfile
  rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink),
                                       aFile,
                                       -1,
                                       0600);
  if (NS_FAILED(rv)) 
      return rv;
  rv = NS_NewBufferedOutputStream(getter_AddRefs(outStream), outStreamSink, 4096);
  if (NS_FAILED(rv)) 
      return rv;  

  char** valueArray = (char **)PR_Calloc(sizeof(char *), gHashTable.entryCount);
  if (!valueArray)
    return NS_ERROR_OUT_OF_MEMORY;
  
  pref_saveArgs saveArgs;
  saveArgs.prefArray = valueArray;
  saveArgs.saveTypes = SAVE_ALL;
  
#if MOZ_PROFILESHARING
  if (isSharingEnabled()) {
    if (aFile == mCurrentSharedFile)
      saveArgs.saveTypes = SAVE_SHARED;
    else if (aFile == mCurrentFile)
      saveArgs.saveTypes = SAVE_NONSHARED;
  }
#endif
  
  // get the lines that we're supposed to be writing to the file
  PL_DHashTableEnumerate(&gHashTable, pref_savePref, &saveArgs);
    
  /* Sort the preferences to make a readable file on disk */
  NS_QuickSort(valueArray, gHashTable.entryCount, sizeof(char *), pref_CompareStrings, NULL);
  
  // write out the file header
  outStream->Write(outHeader, sizeof(outHeader) - 1, &writeAmount);

  char** walker = valueArray;
  for (PRUint32 valueIdx = 0; valueIdx < gHashTable.entryCount; valueIdx++, walker++) {
    if (*walker) {
      outStream->Write(*walker, strlen(*walker), &writeAmount);
      outStream->Write(NS_LINEBREAK, NS_LINEBREAK_LEN, &writeAmount);
      PR_Free(*walker);
    }
  }
  PR_Free(valueArray);

  // tell the safe output stream to overwrite the real prefs file
  // (it'll abort if there were any errors during writing)
  nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(outStream);
  NS_ASSERTION(safeStream, "expected a safe output stream!");
  if (safeStream) {
    rv = safeStream->Finish();
    if (NS_FAILED(rv)) {
      NS_WARNING("failed to save prefs file! possible dataloss");
      return rv;
    }
  }

  gDirty = PR_FALSE;
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class nsSharedPrefHandler [friend]

Definition at line 56 of file nsPrefService.h.


Member Data Documentation

Definition at line 81 of file nsPrefService.h.

Definition at line 85 of file nsPrefService.h.

Definition at line 84 of file nsPrefService.h.

Definition at line 82 of file nsPrefService.h.

Definition at line 80 of file nsPrefService.h.

const long nsIPrefBranch::PREF_BOOL = 128 [inherited]

Definition at line 72 of file nsIPrefBranch.idl.

Definition at line 71 of file nsIPrefBranch.idl.

Values describing the basic preference types.

See also:
getPrefType

Definition at line 69 of file nsIPrefBranch.idl.

Definition at line 70 of file nsIPrefBranch.idl.

readonly attribute string nsIPrefBranch::root [inherited]

Called to get the root on which this branch is based, such as "browser.startup.".

Definition at line 78 of file nsIPrefBranch.idl.


The documentation for this class was generated from the following files: