chrome/browser/download/download_path_reservation_tracker.cc
// Called on the FILE thread to reserve a download path. This method:
// - Creates directory |default_download_path| if it doesn't exist.
// - Verifies that the parent directory of |suggested_path| exists and is
//   writeable.
// - Uniquifies |suggested_path| if |should_uniquify_path| is true.
// - Schedules |callback| on the UI thread with the reserved path and a flag
//   indicating whether the returned path has been successfully verified.
Called on the FILE thread to reserve a download path. This method:
Creates directory |default_download_path| if it doesn't exist.
Verifies that the parent directory of |suggested_path| exists and is
writeable.
Called on the FILE thread to reserve a download path . This method : Creates directory |default_download_path| if it doesn ' t exist . Verifies that the parent directory of |suggested_path| exists and is writeable .
void CreateReservation(
    DownloadId download_id,
    const FilePath& suggested_path,
    const FilePath& default_download_path,
    bool should_uniquify,
    const DownloadPathReservationTracker::ReservedPathCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  DCHECK(download_id.IsValid());
  DCHECK(suggested_path.IsAbsolute());
  if (g_reservation_map == NULL)
    g_reservation_map = new ReservationMap;
  ReservationMap& reservations = *g_reservation_map;
  DCHECK(!ContainsKey(reservations, download_id));
  FilePath target_path(suggested_path.NormalizePathSeparators());
  bool is_path_writeable = true;
  bool has_conflicts = false;
  if (!default_download_path.empty() &&
      default_download_path == target_path.DirName() &&
      !file_util::DirectoryExists(default_download_path)) {
    file_util::CreateDirectory(default_download_path);
  }
  FilePath dir = target_path.DirName();
  FilePath filename = target_path.BaseName();
  if (!file_util::PathIsWritable(dir)) {
    DVLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
    is_path_writeable = false;
    PathService::Get(chrome::DIR_USER_DOCUMENTS, &dir);
    target_path = dir.Append(filename);
  }
  if (is_path_writeable && should_uniquify && IsPathInUse(target_path)) {
    has_conflicts = true;
    for (int uniquifier = 1;
         uniquifier <= DownloadPathReservationTracker::kMaxUniqueFiles;
         ++uniquifier) {
      FilePath path_to_check(target_path.InsertBeforeExtensionASCII(
          StringPrintf(" (%d)", uniquifier)));
      if (!IsPathInUse(path_to_check)) {
        target_path = path_to_check;
        has_conflicts = false;
        break;
      }
    }
  }
  reservations[download_id] = target_path;
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(callback, target_path, (is_path_writeable && !has_conflicts)));
}
void CreateReservation ( DownloadId download_id , const FilePath & suggested_path , const FilePath & default_download_path , bool should_uniquify , const DownloadPathReservationTracker :: ReservedPathCallback & callback ) { DCHECK ( BrowserThread :: CurrentlyOn ( BrowserThread :: FILE ) ) ; DCHECK ( download_id . IsValid ( ) ) ; DCHECK ( suggested_path . IsAbsolute ( ) ) ; if ( g_reservation_map == NULL ) g_reservation_map = new ReservationMap ; ReservationMap & reservations = * g_reservation_map ; DCHECK ( ! ContainsKey ( reservations , download_id ) ) ; FilePath target_path ( suggested_path . NormalizePathSeparators ( ) ) ; bool is_path_writeable = true ; bool has_conflicts = false ; if ( ! default_download_path . empty ( ) && default_download_path == target_path . DirName ( ) && ! file_util :: DirectoryExists ( default_download_path ) ) { file_util :: CreateDirectory ( default_download_path ) ; } FilePath dir = target_path . DirName ( ) ; FilePath filename = target_path . BaseName ( ) ; if ( ! file_util :: PathIsWritable ( dir ) ) { DVLOG ( 1 ) << " \" " << dir . value ( ) << " \" " ; is_path_writeable = false ; PathService :: Get ( chrome :: DIR_USER_DOCUMENTS , & dir ) ; target_path = dir . Append ( filename ) ; } if ( is_path_writeable && should_uniquify && IsPathInUse ( target_path ) ) { has_conflicts = true ; for ( int uniquifier = 1 ; uniquifier <= DownloadPathReservationTracker :: kMaxUniqueFiles ; ++ uniquifier ) { FilePath path_to_check ( target_path . InsertBeforeExtensionASCII ( StringPrintf ( " " , uniquifier ) ) ) ; if ( ! IsPathInUse ( path_to_check ) ) { target_path = path_to_check ; has_conflicts = false ; break ; } } } reservations [ download_id ] = target_path ; BrowserThread :: PostTask ( BrowserThread :: UI , FROM_HERE , base :: Bind ( callback , target_path , ( is_path_writeable && ! has_conflicts ) ) ) ; }
Called on the FILE thread to reserve a download path.
Called on the FILE thread to reserve a download path .
// Create a reservation map if one doesn't exist. It will be automatically // deleted when all the reservations are revoked. // Create the default download path if it doesn't already exist and is where // we are going to create the downloaded file. |target_path| might point // elsewhere if this was a programmatic download. // Check writability of the suggested path. If we can't write to it, default // to the user's "My Documents" directory. We'll prompt them in this case.