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..f58a9adc5d462e10da467effd287d35da1cc3633 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,34 @@ 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 |
| +// 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(); |
| + base::FilePath short_path = path; |
| + const size_t kMaxNameLength = 255; |
| + if (filename.value().length() <= kMaxNameLength) |
| + return short_path; |
| + base::FilePath::StringType extension = filename.FinalExtension(); |
| + filename = filename.RemoveFinalExtension(); |
| + // 1 for . plus 12 for longest known extension. |
| + size_t max_extension_length = 13; |
| + if (filename.value().length() < kMaxNameLength) { |
| + max_extension_length = std::max(max_extension_length, |
| + kMaxNameLength - filename.value().length()); |
| + } |
| + if (extension.length() >= max_extension_length) { |
|
Lei Zhang
2017/04/13 21:53:09
Just > now?
rbpotter
2017/04/13 23:02:11
Done.
|
| + // Take the first max_extension_length characters (this will be the |
| + // leading '.' plus the next max_extension_length - 1). |
| + extension = extension.substr(0, max_extension_length); |
|
Lei Zhang
2017/04/13 21:53:09
Can we trim by using resize()? Ditto on line 48.
rbpotter
2017/04/13 23:02:10
Done.
|
| + } |
| + short_path = path.DirName().Append( |
| + filename.value().substr(0, kMaxNameLength - extension.length())); |
| + short_path = short_path.AddExtension(extension); |
| + return short_path; |
| +} |
| + |
| } // namespace |
| namespace ui { |
| @@ -98,8 +127,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); |
| } |