Back to index

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

#include <nsProfileDirServiceProvider.h>

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

List of all members.

Public Member Functions

virtual nsresult SetProfileDir (nsIFile *aProfileDir, nsIFile *aLocalProfileDir=nsnull)
 SetProfileDir.
virtual nsresult Register ()
 Register.
virtual nsresult Shutdown ()
 Shutdown.
nsIFile getFile (in string prop, out PRBool persistent)
 getFile

Protected Member Functions

 nsProfileDirServiceProvider (PRBool aNotifyObservers=PR_TRUE)
virtual ~nsProfileDirServiceProvider ()
nsresult Initialize ()
nsresult InitProfileDir (nsIFile *profileDir)
nsresult InitNonSharedProfileDir ()
nsresult EnsureProfileFileExists (nsIFile *aFile, nsIFile *destDir)
nsresult UndefineFileLocations ()

Protected Attributes

nsCOMPtr< nsIFilemProfileDir
nsCOMPtr< nsIFilemLocalProfileDir
nsProfileLockmProfileDirLock
PRPackedBool mNotifyObservers
PRPackedBool mSharingEnabled
nsEmbedString mNonSharedDirName
nsCOMPtr< nsIFilemNonSharedProfileDir

Friends

NS_DECL_ISUPPORTS
NS_DECL_NSIDIRECTORYSERVICEPROVIDER
friend nsresult 
NS_NewProfileDirServiceProvider (PRBool, nsProfileDirServiceProvider **)
 Global method to create an instance of nsProfileDirServiceProvider.

Detailed Description

Definition at line 62 of file nsProfileDirServiceProvider.h.


Constructor & Destructor Documentation

Definition at line 75 of file nsProfileDirServiceProvider.cpp.

                                                                                :
#ifdef MOZ_PROFILELOCKING
  mProfileDirLock(nsnull),
#endif
  mNotifyObservers(aNotifyObservers),
  mSharingEnabled(PR_FALSE)
{
}

Definition at line 85 of file nsProfileDirServiceProvider.cpp.

{
#ifdef MOZ_PROFILELOCKING
  delete mProfileDirLock;
#endif
}

Member Function Documentation

Definition at line 463 of file nsProfileDirServiceProvider.cpp.

{
  nsresult rv;
  PRBool exists;

  rv = aFile->Exists(&exists);
  if (NS_FAILED(rv))
    return rv;
  if (exists)
    return NS_OK;

  nsCOMPtr<nsIFile> defaultsFile;

  // Attempt first to get the localized subdir of the defaults
  rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(defaultsFile));
  if (NS_FAILED(rv)) {
    // If that has not been defined, use the top level of the defaults
    rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR, getter_AddRefs(defaultsFile));
    if (NS_FAILED(rv))
      return rv;
  }

  nsCAutoString leafName;
  rv = aFile->GetNativeLeafName(leafName);
  if (NS_FAILED(rv))
    return rv;
  rv = defaultsFile->AppendNative(leafName);
  if (NS_FAILED(rv))
    return rv;
  
  return defaultsFile->CopyTo(destDir, EmptyString());
}

Here is the call graph for this function:

nsIFile nsIDirectoryServiceProvider::getFile ( in string  prop,
out PRBool  persistent 
) [inherited]

getFile

Directory Service calls this when it gets the first request for a prop or on every request if the prop is not persistent.

Parameters:
propThe symbolic name of the file.
persistentTRUE - The returned file will be cached by Directory Service. Subsequent requests for this prop will bypass the provider and use the cache. FALSE - The provider will be asked for this prop each time it is requested.
Returns:
The file represented by the property.

Definition at line 342 of file nsProfileDirServiceProvider.cpp.

{
#ifdef MOZ_PROFILELOCKING
  mProfileDirLock = new nsProfileLock;
  if (!mProfileDirLock)
    return NS_ERROR_OUT_OF_MEMORY;
#endif

#ifdef MOZ_PROFILESHARING
  nsCOMPtr<nsIProfileSharingSetup> sharingSetup =
      do_GetService("@mozilla.org/embedcomp/profile-sharing-setup;1");
  if (sharingSetup) {
    PRBool tempBool;
    if (NS_SUCCEEDED(sharingSetup->GetIsSharingEnabled(&tempBool)))
      mSharingEnabled = tempBool;
    if (mSharingEnabled)
      sharingSetup->GetClientName(mNonSharedDirName);
  }
#endif

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 428 of file nsProfileDirServiceProvider.cpp.

{
  nsresult rv;

  NS_ENSURE_STATE(mProfileDir);
  NS_ENSURE_STATE(mNonSharedDirName.Length());

  nsCOMPtr<nsIFile> localDir;
  rv = mProfileDir->Clone(getter_AddRefs(localDir));
  if (NS_SUCCEEDED(rv)) {
    rv = localDir->Append(mNonSharedDirName);
    if (NS_SUCCEEDED(rv)) {
      PRBool exists;
      rv = localDir->Exists(&exists);
      if (NS_SUCCEEDED(rv)) {
        if (!exists) {
          rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
        }
        else {
          PRBool isDir;
          rv = localDir->IsDirectory(&isDir);
          if (NS_SUCCEEDED(rv)) {
            if (!isDir)
              rv = NS_ERROR_FILE_NOT_DIRECTORY;
          }
        }
        if (NS_SUCCEEDED(rv))
          mNonSharedProfileDir = localDir;
      }
    }
  }
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 366 of file nsProfileDirServiceProvider.cpp.

{
  // Make sure our "Profile" folder exists.
  // If it does not, copy the profile defaults to its location.

  nsresult rv;
  PRBool exists;
  rv = profileDir->Exists(&exists);
  if (NS_FAILED(rv))
    return rv;

  if (!exists) {
    nsCOMPtr<nsIFile> profileDefaultsDir;
    nsCOMPtr<nsIFile> profileDirParent;
    nsCAutoString profileDirName;

    (void)profileDir->GetParent(getter_AddRefs(profileDirParent));
    if (!profileDirParent)
      return NS_ERROR_FAILURE;
    rv = profileDir->GetNativeLeafName(profileDirName);
    if (NS_FAILED(rv))
      return rv;

    rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR, getter_AddRefs(profileDefaultsDir));
    if (NS_FAILED(rv)) {
      rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR, getter_AddRefs(profileDefaultsDir));
      if (NS_FAILED(rv))
        return rv;
    }
    rv = profileDefaultsDir->CopyToNative(profileDirParent, profileDirName);
    if (NS_FAILED(rv)) {
        // if copying failed, lets just ensure that the profile directory exists.
        profileDirParent->AppendNative(profileDirName);
        rv = profileDirParent->Create(nsIFile::DIRECTORY_TYPE, 0700);
        if (NS_FAILED(rv))
            return rv;
    }

#if !defined(XP_MAC) && !defined(WINCE)
    rv = profileDir->SetPermissions(0700);
    if (NS_FAILED(rv))
      return rv;
#endif

  }
  else {
    PRBool isDir;
    rv = profileDir->IsDirectory(&isDir);

    if (NS_FAILED(rv))
      return rv;
    if (!isDir)
      return NS_ERROR_FILE_NOT_DIRECTORY;
  }

  if (mNonSharedDirName.Length())
    rv = InitNonSharedProfileDir();

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Register.

Convenience method to register the provider with directory service. The service holds strong references to registered providers so consumers don't need to hold a reference to this object after calling Register().

Definition at line 170 of file nsProfileDirServiceProvider.cpp.

{
  nsCOMPtr<nsIDirectoryService> directoryService =
          do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
  if (!directoryService)
    return NS_ERROR_FAILURE;
  return directoryService->RegisterProvider(this);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsProfileDirServiceProvider::SetProfileDir ( nsIFile aProfileDir,
nsIFile aLocalProfileDir = nsnull 
) [virtual]

SetProfileDir.

Parameters:
aProfileDirThe directory containing the profile files. It does not need to exist before calling this method. If it does not, it will be created and defaults will be copied to it.
aLocalProfileDirDirectory for local profile data, e.g. Cache. If null, aProfileDir will be used for this purpose.

Definition at line 93 of file nsProfileDirServiceProvider.cpp.

{
  if (!aLocalProfileDir)
    aLocalProfileDir = aProfileDir;
  if (mProfileDir) {
    PRBool isEqual;
    if (aProfileDir &&
        NS_SUCCEEDED(aProfileDir->Equals(mProfileDir, &isEqual)) && isEqual) {
      NS_WARNING("Setting profile dir to same as current");
      return NS_OK;
    }
#ifdef MOZ_PROFILELOCKING
    mProfileDirLock->Unlock();
#endif
    UndefineFileLocations();
  }
  mProfileDir = aProfileDir;
  mLocalProfileDir = aLocalProfileDir;
  if (!mProfileDir)
    return NS_OK;

  nsresult rv = InitProfileDir(mProfileDir);
  if (NS_FAILED(rv))
    return rv;

  // Make sure that the local profile dir exists
  // we just try to create it - if it exists already, that'll fail; ignore
  // errors
  mLocalProfileDir->Create(nsIFile::DIRECTORY_TYPE, 0700);

#ifdef MOZ_PROFILESHARING
  if (mSharingEnabled) {
    nsCOMPtr<ipcITransactionService> transServ =
        do_GetService(IPC_TRANSACTIONSERVICE_CONTRACTID, &rv);
    if (NS_SUCCEEDED(rv)) {
      nsCAutoString nativePath;
      rv = mProfileDir->GetNativePath(nativePath);
      if (NS_SUCCEEDED(rv))
        rv = transServ->Init(nativePath);
    }
    if (NS_FAILED(rv)) {
      NS_WARNING("Unable to initialize transaction service");
    }
  }
#endif

#ifdef MOZ_PROFILELOCKING
  // Lock the non-shared sub-dir if we are sharing,
  // the whole profile dir if we are not.
  nsCOMPtr<nsILocalFile> dirToLock;
  if (mSharingEnabled)
    dirToLock = do_QueryInterface(mNonSharedProfileDir);
  else
    dirToLock = do_QueryInterface(mProfileDir);
  rv = mProfileDirLock->Lock(dirToLock, nsnull);
  if (NS_FAILED(rv))
    return rv;
#endif

  if (mNotifyObservers) {
    nsCOMPtr<nsIObserverService> observerService =
             do_GetService("@mozilla.org/observer-service;1");
    if (!observerService)
      return NS_ERROR_FAILURE;

    NS_NAMED_LITERAL_STRING(context, "startup");
    // Notify observers that the profile has changed - Here they respond to new profile
    observerService->NotifyObservers(nsnull, "profile-do-change", context.get());
    // Now observers can respond to something another observer did on "profile-do-change"
    observerService->NotifyObservers(nsnull, "profile-after-change", context.get());
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Shutdown.

This method must be called before shutting down XPCOM if this object was created with aNotifyObservers == PR_TRUE. If this object was created with aNotifyObservers == PR_FALSE, this method is a no-op.

Definition at line 180 of file nsProfileDirServiceProvider.cpp.

{
  if (!mNotifyObservers)
    return NS_OK;

  nsCOMPtr<nsIObserverService> observerService =
           do_GetService("@mozilla.org/observer-service;1");
  if (!observerService)
    return NS_ERROR_FAILURE;

  NS_NAMED_LITERAL_STRING(context, "shutdown-persist");
  observerService->NotifyObservers(nsnull, "profile-before-change", context.get());
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 497 of file nsProfileDirServiceProvider.cpp.

{
  nsresult rv;

  nsCOMPtr<nsIProperties> directoryService =
           do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
  NS_ENSURE_TRUE(directoryService, NS_ERROR_FAILURE);

  (void) directoryService->Undefine(NS_APP_PREFS_50_DIR);
  (void) directoryService->Undefine(NS_APP_PREFS_50_FILE);
  (void) directoryService->Undefine(NS_APP_USER_PROFILE_50_DIR);
  (void) directoryService->Undefine(NS_APP_USER_CHROME_DIR);
  (void) directoryService->Undefine(NS_APP_LOCALSTORE_50_FILE);
  (void) directoryService->Undefine(NS_APP_HISTORY_50_FILE);
  (void) directoryService->Undefine(NS_APP_USER_PANELS_50_FILE);
  (void) directoryService->Undefine(NS_APP_USER_MIMETYPES_50_FILE);
  (void) directoryService->Undefine(NS_APP_BOOKMARKS_50_FILE);
  (void) directoryService->Undefine(NS_APP_DOWNLOADS_50_FILE);
  (void) directoryService->Undefine(NS_APP_SEARCH_50_FILE);
  (void) directoryService->Undefine(NS_APP_MAIL_50_DIR);
  (void) directoryService->Undefine(NS_APP_IMAP_MAIL_50_DIR);
  (void) directoryService->Undefine(NS_APP_NEWS_50_DIR);
  (void) directoryService->Undefine(NS_APP_MESSENGER_FOLDER_CACHE_50_DIR);

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

NS_DECL_ISUPPORTS NS_DECL_NSIDIRECTORYSERVICEPROVIDER friend nsresult NS_NewProfileDirServiceProvider ( PRBool  aNotifyObservers,
nsProfileDirServiceProvider **  aProvider 
) [friend]

Global method to create an instance of nsProfileDirServiceProvider.

Parameters:
aNotifyObserversIf true, will send out profile startup notifications when the profile directory is set. See nsIProfileChangeStatus.

Definition at line 528 of file nsProfileDirServiceProvider.cpp.

{
  NS_ENSURE_ARG_POINTER(aProvider);
  *aProvider = nsnull;

  nsProfileDirServiceProvider *prov = new nsProfileDirServiceProvider(aNotifyObservers);
  if (!prov)
    return NS_ERROR_OUT_OF_MEMORY;
  nsresult rv = prov->Initialize();
  if (NS_FAILED(rv)) {
    delete prov;
    return rv;
  }
  NS_ADDREF(*aProvider = prov);
  return NS_OK;
}

Member Data Documentation

Definition at line 119 of file nsProfileDirServiceProvider.h.

Definition at line 125 of file nsProfileDirServiceProvider.h.

Definition at line 129 of file nsProfileDirServiceProvider.h.

Definition at line 121 of file nsProfileDirServiceProvider.h.

Definition at line 118 of file nsProfileDirServiceProvider.h.

Definition at line 120 of file nsProfileDirServiceProvider.h.

Definition at line 123 of file nsProfileDirServiceProvider.h.


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