コード例 #1
0
/**
 * Delete the profile directory being reset after a backup and delete the local profile directory.
 */
nsresult
ProfileResetCleanup(nsIToolkitProfile* aOldProfile)
{
  nsresult rv;
  nsCOMPtr<nsIFile> profileDir;
  rv = aOldProfile->GetRootDir(getter_AddRefs(profileDir));
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsIFile> profileLocalDir;
  rv = aOldProfile->GetLocalDir(getter_AddRefs(profileLocalDir));
  if (NS_FAILED(rv)) return rv;

  // Get the friendly name for the backup directory.
  nsCOMPtr<nsIStringBundleService> sbs = mozilla::services::GetStringBundleService();
  if (!sbs) return NS_ERROR_FAILURE;

  nsCOMPtr<nsIStringBundle> sb;
  rv = sbs->CreateBundle(kProfileProperties, getter_AddRefs(sb));
  if (!sb) return NS_ERROR_FAILURE;

  NS_ConvertUTF8toUTF16 appName(gAppData->name);
  const PRUnichar* params[] = {appName.get(), appName.get()};

  nsXPIDLString resetBackupDirectoryName;

  static const PRUnichar* kResetBackupDirectory = NS_LITERAL_STRING("resetBackupDirectory").get();
  rv = sb->FormatStringFromName(kResetBackupDirectory, params, 2,
                                getter_Copies(resetBackupDirectoryName));

  // Get info to copy the old root profile dir to the desktop as a backup.
  nsCOMPtr<nsIFile> backupDest, uniqueDest;
  rv = NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(backupDest));
  if (NS_FAILED(rv)) {
    // Fall back to the home directory if the desktop is not available.
    rv = NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(backupDest));
    if (NS_FAILED(rv)) return rv;
  }

  // Try to get a unique backup directory name.
  backupDest->Clone(getter_AddRefs(uniqueDest));
  uniqueDest->Append(resetBackupDirectoryName);
  rv = uniqueDest->CreateUnique(nsIFile::DIRECTORY_TYPE, 0700);
  if (NS_FAILED(rv)) return rv;

  nsAutoString leafName;
  rv = uniqueDest->GetLeafName(leafName);
  if (NS_FAILED(rv)) return rv;
  // Delete the empty directory that CreateUnique just created.
  rv = uniqueDest->Remove(false);
  if (NS_FAILED(rv)) return rv;

  // Show a progress window while the cleanup happens since the disk I/O can take time.
  nsCOMPtr<nsIWindowWatcher> windowWatcher(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
  if (!windowWatcher) return NS_ERROR_FAILURE;

  nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID));
  if (!appStartup) return NS_ERROR_FAILURE;

  nsCOMPtr<nsIDOMWindow> progressWindow;
  rv = windowWatcher->OpenWindow(nullptr,
                                 kResetProgressURL,
                                 "_blank",
                                 "centerscreen,chrome,titlebar",
                                 NULL,
                                 getter_AddRefs(progressWindow));
  if (NS_FAILED(rv)) return rv;

  // Create a new thread to do the bulk of profile cleanup to stay responsive.
  nsCOMPtr<nsIThreadManager> tm = do_GetService(NS_THREADMANAGER_CONTRACTID);
  nsCOMPtr<nsIThread> cleanupThread;
  rv = tm->NewThread(0, 0, getter_AddRefs(cleanupThread));
  if (NS_SUCCEEDED(rv)) {
    nsCOMPtr<nsIRunnable> runnable = new ProfileResetCleanupAsyncTask(profileDir, profileLocalDir,
                                                                      backupDest, leafName);
    cleanupThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL);
    // The result callback will shut down the worker thread.

    nsIThread *thread = NS_GetCurrentThread();
    // Wait for the cleanup thread to complete.
    while(!gProfileResetCleanupCompleted) {
      NS_ProcessNextEvent(thread);
    }
  } else {
    gProfileResetCleanupCompleted = true;
    NS_WARNING("Cleanup thread creation failed");
    return rv;
  }
  // Close the progress window now that the cleanup thread is done.
  progressWindow->Close();

  // Delete the old profile from profiles.ini. The folder was already deleted above.
  rv = aOldProfile->Remove(false);
  if (NS_FAILED(rv)) NS_WARNING("Could not remove the profile");

  return rv;
}
コード例 #2
0
nsresult Stream::DownUpLoad(PRBool download)
{
  nsresult rv = NS_OK;

  if (!mController)
    return NS_ERROR_UNEXPECTED;

  /* Design

  I'd really like to
  - open a few channels (one per file) here in this function,
  - attach |nsIProgressEventSink|s to each and
  - let a Progress dialog track/reflect the progress sinks.
  That way, I would have a generic dialog for the UI somewhere
  and the logic (transfer) here in this file, where it belongs.

  Unfortunately, this doesn't work. I have to
  block this function until the transfer is done, but have the transfer
  itself and the progress UI working. darin says, I will get threading
  issues then. He suggested to initiate the transfer from the dialog
  (because that dialog uses another event quene) and let the dialog
  close itself when the transfer is done. Here in this function, I can
  block until the dialog is dismissed.
  (At least, that's what I understood from darin.)
  That's what I do, and it seems to work. It's still a poor design.

  Actually, it's really crap. It destroys my whole design with the
  pluggable "method"s (like stream, copy etc.). Basically everything
  contacting remote servers (i.e. needing a progress dialog) now has to
  either mess around in sroamingTransfer.js or duplicate a certain
  portion of code.
  */

  nsCOMPtr<nsIWindowWatcher> windowWatcher
              (do_GetService("@mozilla.org/embedcomp/window-watcher;1", &rv));
  NS_ENSURE_SUCCESS(rv, rv);

  /* nsIDialogParamBlock is a method to pass ints and strings
     to and from XUL dialogs.
     To dialog (upon open)
       Int array
         Item 0: 1 = download, 2 = upload
         Item 1: 1 = serial (transfer one file after the other),
                 2 = parallel (start all file transfers at once)
         Item 2: Number of files (n below)
         Item 3: Save password (should pw be stored by default when asked?)
                 1 = true
                 0 = false
       String array
         Item 0: unused
         Item 1: URL of profile dir
         Item 2: URL of remote dir
         Item 3: Password
         Item 4..(n+3): filenames
     From dialog (upon close)
       Int array
         Item 0: 0 = do nothing
                 1 = Save Password (user checked the box in the prompt)
                 2 = Set SavePassword pref to false, ignore PW/Usern. retval
       String array
         Item 0: Saved username (as entered in the prompt)
         Item 1: Saved password (as entered in the prompt)
  */
  nsCOMPtr<nsIDialogParamBlock> ioParamBlock
             (do_CreateInstance("@mozilla.org/embedcomp/dialogparam;1", &rv));
  NS_ENSURE_SUCCESS(rv, rv);

  // download/upload
  ioParamBlock->SetInt(0, download ? 1 : 2);
  ioParamBlock->SetInt(1, 2);

  const nsCStringArray* files = mController->GetFilesToRoam();
  ioParamBlock->SetInt(2, files->Count());

  ioParamBlock->SetInt(3, mSavePassword ? 1 : 0);

  nsXPIDLCString profile;
  mProfileDir->GetSpec(profile);
  ioParamBlock->SetString(1, NS_ConvertUTF8toUTF16(profile).get());
  ioParamBlock->SetString(2, NS_ConvertUTF8toUTF16(mRemoteBaseUrl).get());
  ioParamBlock->SetString(3, mSavePassword
                          ? mPassword.get()
                          : EmptyString().get());

  // filenames
  for (PRInt32 i = files->Count() - 1; i >= 0; i--)
  {
    NS_ConvertASCIItoUTF16 filename(*files->CStringAt(i));
    ioParamBlock->SetString(i + 4, filename.get());
                                                 // filenames start at item 4
  }

  nsCOMPtr<nsIDOMWindow> window;
  rv = windowWatcher->OpenWindow(nsnull,
                                 kXferDlg,
                                 nsnull,
                                 "centerscreen,chrome,modal,titlebar",
                                 ioParamBlock,
                                 getter_AddRefs(window));
  NS_ENSURE_SUCCESS(rv, rv);


  PRInt32 savepw = 0;
  ioParamBlock->GetInt(0, &savepw);
   	
  if (savepw == 1)
  {    
    nsXPIDLString password, username;
    ioParamBlock->GetString(0, getter_Copies(username));
    ioParamBlock->GetString(1, getter_Copies(password));

    mPassword = password;
    nsCOMPtr<nsIRegistry> registry;
    rv = mController->GetRegistry(registry);
    rv = registry->SetInt(mRegkeyStream, kRegKeySavePassword.get(),  1);
    rv = registry->SetString(mRegkeyStream, kRegKeyUsername.get(),
                             username.get());
    rv = registry->SetString(mRegkeyStream, kRegKeyPassword.get(),
                             mPassword.get());
    // failure is not fatal. then it's not saved *shrug*. ;-)
  }
    
  return NS_OK;
}