| Index: chrome/browser/download/download_path_reservation_tracker.cc
|
| diff --git a/chrome/browser/download/download_path_reservation_tracker.cc b/chrome/browser/download/download_path_reservation_tracker.cc
|
| index eab019053fa5920c5716aa998640c7d395f2df66..c4439acd29ead2fbb7a245883b8368d6a4ff6638 100644
|
| --- a/chrome/browser/download/download_path_reservation_tracker.cc
|
| +++ b/chrome/browser/download/download_path_reservation_tracker.cc
|
| @@ -12,6 +12,8 @@
|
| #include "base/logging.h"
|
| #include "base/path_service.h"
|
| #include "base/stl_util.h"
|
| +#include "base/string_util.h"
|
| +#include "base/sys_info.h"
|
| #include "chrome/browser/download/download_util.h"
|
| #include "chrome/common/chrome_paths.h"
|
| #include "content/public/browser/browser_thread.h"
|
| @@ -26,6 +28,10 @@ namespace {
|
|
|
| typedef std::map<content::DownloadId, FilePath> ReservationMap;
|
|
|
| +// The lower bound for file name truncation. If the truncation results in a name
|
| +// shorter than this limit, we give up automatic truncation and prompt the user.
|
| +static const int kTruncatedNameLengthLowerbound = 5;
|
| +
|
| // Map of download path reservations. Each reserved path is associated with a
|
| // DownloadId. This object is destroyed in |Revoke()| when there are no more
|
| // reservations.
|
| @@ -90,10 +96,27 @@ bool IsPathInUse(const FilePath& path) {
|
| return false;
|
| }
|
|
|
| +// Truncate the given name so that its becomes .size() <= limit. Returns an
|
| +// empty string if it is impossible due to unknown file-name encoding.
|
| +FilePath::StringType TruncateFileName(const FilePath::StringType& name,
|
| + size_t limit) {
|
| + FilePath::StringType truncated;
|
| +#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
|
| + TruncateUTF8ToByteSize(name, limit, &truncated);
|
| +#elif defined(OS_WIN)
|
| + // TODO(kinaba): Implement for Windows, where StringType == string16.
|
| +#else
|
| + // We cannot generally assume that the file name encoding is in UTF-8 (see
|
| + // the comment for FilePath::AsUTF8Unsafe), hence no safe way to truncate.
|
| +#endif
|
| + return truncated;
|
| +}
|
| +
|
| // 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.
|
| +// - Truncates the suggested name if it exceeds the filesystem's limit.
|
| // - 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.
|
| @@ -139,6 +162,33 @@ void CreateReservation(
|
| target_path = dir.Append(filename);
|
| }
|
|
|
| + // Check the limit of file name length if it could be obtained. When the
|
| + // suggested name exceeds the limit, truncate or prompt the user.
|
| + bool name_too_long = false;
|
| + if (is_path_writeable) {
|
| + int max_length = base::SysInfo::GetMaximumPathComponentLength(dir);
|
| + if (max_length > 0) {
|
| + // The extension should not be truncated, so we split it first.
|
| + FilePath::StringType ext = filename.Extension();
|
| + FilePath::StringType name = filename.RemoveExtension().value();
|
| +
|
| + // Reserve several characters we may append later.
|
| + const int kExtraChars = sizeof(" (100).crdownload") - 1;
|
| + int limit = max_length - ext.size() - kExtraChars;
|
| + if (static_cast<int>(name.size()) > limit) {
|
| + name_too_long = true;
|
| + if (limit >= kTruncatedNameLengthLowerbound) {
|
| + // Truncate the name to fit in |limit|.
|
| + FilePath::StringType truncated = TruncateFileName(name, limit);
|
| + if (!truncated.empty()) {
|
| + target_path = dir.Append(truncated + ext);
|
| + name_too_long = false;
|
| + }
|
| + }
|
| + }
|
| + }
|
| + }
|
| +
|
| if (is_path_writeable && should_uniquify && IsPathInUse(target_path)) {
|
| has_conflicts = true;
|
| for (int uniquifier = 1;
|
| @@ -154,9 +204,9 @@ void CreateReservation(
|
| }
|
| }
|
| reservations[download_id] = target_path;
|
| - BrowserThread::PostTask(
|
| - BrowserThread::UI, FROM_HERE,
|
| - base::Bind(callback, target_path, (is_path_writeable && !has_conflicts)));
|
| + bool verified = (is_path_writeable && !has_conflicts && !name_too_long);
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
| + base::Bind(callback, target_path, verified));
|
| }
|
|
|
| // Called on the FILE thread to update the path of the reservation associated
|
|
|