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..18f2f586db1cc08baa1625b151a8549e900ca0a3 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" |
| @@ -21,6 +22,32 @@ namespace { |
| // Optional dialog factory. Leaked. |
| ui::SelectFileDialogFactory* dialog_factory_ = NULL; |
| +// Returns a file path with a base name at most 255 characters long. This |
| +// is the limit on Windows and Linux, and on Windows the system file |
|
Nico
2017/04/14 14:03:34
FWIW, on Windows, the limit for the full path (not
rbpotter
2017/04/14 22:04:39
It fails to save in the system dialog if the path
|
| +// selection dialog will fail to open if the file name exceeds 255 characters. |
| +base::FilePath GetShortenedFilePath(const base::FilePath& path) { |
| + base::FilePath filename = path.BaseName(); |
|
Nico
2017/04/14 14:03:34
Move this below the early return, you currently al
rbpotter
2017/04/14 22:04:39
Done.
|
| + const size_t kMaxNameLength = 255; |
| + if (filename.value().length() <= kMaxNameLength) |
| + return path; |
| + 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); |
| +} |
| + |
| } // namespace |
| namespace ui { |
| @@ -98,8 +125,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); |
| } |