Chromium Code Reviews| Index: ui/shell_dialogs/select_file_dialog.cc |
| diff --git a/ui/shell_dialogs/select_file_dialog.cc b/ui/shell_dialogs/select_file_dialog.cc |
| index 1aa7a0e26164079e0563943180353d03cdb6c861..fe8ab4dc24c614de287b8554b89ff63dfc84d339 100644 |
| --- a/ui/shell_dialogs/select_file_dialog.cc |
| +++ b/ui/shell_dialogs/select_file_dialog.cc |
| @@ -5,6 +5,7 @@ |
| #include "ui/shell_dialogs/select_file_dialog.h" |
| #include <stddef.h> |
| +#include <algorithm> |
| #include "base/bind.h" |
| #include "base/location.h" |
| @@ -74,6 +75,33 @@ scoped_refptr<SelectFileDialog> SelectFileDialog::Create( |
| return CreateSelectFileDialog(listener, policy); |
| } |
| +// Returns a file path with a base name at most 255 characters long. This |
|
Lei Zhang
2017/04/15 22:19:34
Comment goes in the header now.
rbpotter
2017/04/17 17:48:49
Done.
|
| +// is the limit on Windows and Linux, and on Windows the system file |
| +// selection dialog will fail to open if the file name exceeds 255 characters. |
| +base::FilePath SelectFileDialog::GetShortenedFilePath( |
| + const base::FilePath& path) { |
| + const size_t kMaxNameLength = 255; |
| + if (path.BaseName().value().length() <= kMaxNameLength) |
| + return path; |
| + base::FilePath filename = path.BaseName(); |
| + base::FilePath::StringType extension = filename.FinalExtension(); |
| + filename = filename.RemoveFinalExtension(); |
| + base::FilePath::StringType file_string = filename.value(); |
| + // 1 for . plus 12 for longest known extension. |
| + size_t max_extension_length = 13; |
| + if (file_string.length() < kMaxNameLength) { |
| + max_extension_length = |
| + std::max(max_extension_length, kMaxNameLength - file_string.length()); |
| + } |
| + if (extension.length() > max_extension_length) { |
| + // Take the first max_extension_length characters (this will be the |
| + // leading '.' plus the next max_extension_length - 1). |
| + extension.resize(max_extension_length); |
| + } |
| + file_string.resize(kMaxNameLength - extension.length()); |
| + return path.DirName().Append(file_string).AddExtension(extension); |
| +} |
| + |
| void SelectFileDialog::SelectFile( |
| Type type, |
| const base::string16& title, |
| @@ -98,8 +126,10 @@ void SelectFileDialog::SelectFile( |
| return; |
| } |
| + base::FilePath path = GetShortenedFilePath(default_path); |
| + |
| // Call the platform specific implementation of the file selection dialog. |
| - SelectFileImpl(type, title, default_path, file_types, file_type_index, |
| + SelectFileImpl(type, title, path, file_types, file_type_index, |
| default_extension, owning_window, params); |
| } |