Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/shell_dialogs/select_file_dialog.h" | 5 #include "ui/shell_dialogs/select_file_dialog.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "ui/shell_dialogs/select_file_dialog_factory.h" | 16 #include "ui/shell_dialogs/select_file_dialog_factory.h" |
| 16 #include "ui/shell_dialogs/select_file_policy.h" | 17 #include "ui/shell_dialogs/select_file_policy.h" |
| 17 #include "ui/shell_dialogs/selected_file_info.h" | 18 #include "ui/shell_dialogs/selected_file_info.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // Optional dialog factory. Leaked. | 22 // Optional dialog factory. Leaked. |
| 22 ui::SelectFileDialogFactory* dialog_factory_ = NULL; | 23 ui::SelectFileDialogFactory* dialog_factory_ = NULL; |
| 23 | 24 |
| 25 // Returns a file path with a base name at most 255 characters long. This | |
| 26 // is the limit on Windows and Linux, and on Windows the system file | |
| 27 // selection dialog will fail to open if the file name exceeds 255 characters. | |
| 28 base::FilePath GetShortenedFilePath(const base::FilePath& path) { | |
| 29 base::FilePath filename = path.BaseName(); | |
| 30 base::FilePath short_path = path; | |
| 31 const size_t kMaxNameLength = 255; | |
| 32 if (filename.value().length() <= kMaxNameLength) | |
| 33 return short_path; | |
| 34 base::FilePath::StringType extension = filename.FinalExtension(); | |
| 35 filename = filename.RemoveFinalExtension(); | |
| 36 // 1 for . plus 12 for longest known extension. | |
| 37 size_t max_extension_length = 13; | |
| 38 if (filename.value().length() < kMaxNameLength) { | |
| 39 max_extension_length = std::max(max_extension_length, | |
| 40 kMaxNameLength - filename.value().length()); | |
| 41 } | |
| 42 if (extension.length() >= max_extension_length) { | |
|
Lei Zhang
2017/04/13 21:53:09
Just > now?
rbpotter
2017/04/13 23:02:11
Done.
| |
| 43 // Take the first max_extension_length characters (this will be the | |
| 44 // leading '.' plus the next max_extension_length - 1). | |
| 45 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.
| |
| 46 } | |
| 47 short_path = path.DirName().Append( | |
| 48 filename.value().substr(0, kMaxNameLength - extension.length())); | |
| 49 short_path = short_path.AddExtension(extension); | |
| 50 return short_path; | |
| 51 } | |
| 52 | |
| 24 } // namespace | 53 } // namespace |
| 25 | 54 |
| 26 namespace ui { | 55 namespace ui { |
| 27 | 56 |
| 28 SelectFileDialog::FileTypeInfo::FileTypeInfo() | 57 SelectFileDialog::FileTypeInfo::FileTypeInfo() |
| 29 : include_all_files(false), allowed_paths(NATIVE_PATH) {} | 58 : include_all_files(false), allowed_paths(NATIVE_PATH) {} |
| 30 | 59 |
| 31 SelectFileDialog::FileTypeInfo::FileTypeInfo(const FileTypeInfo& other) = | 60 SelectFileDialog::FileTypeInfo::FileTypeInfo(const FileTypeInfo& other) = |
| 32 default; | 61 default; |
| 33 | 62 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 | 120 |
| 92 // Inform the listener that no file was selected. | 121 // Inform the listener that no file was selected. |
| 93 // Post a task rather than calling FileSelectionCanceled directly to ensure | 122 // Post a task rather than calling FileSelectionCanceled directly to ensure |
| 94 // that the listener is called asynchronously. | 123 // that the listener is called asynchronously. |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 124 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, | 125 FROM_HERE, |
| 97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); | 126 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); |
| 98 return; | 127 return; |
| 99 } | 128 } |
| 100 | 129 |
| 130 base::FilePath path = GetShortenedFilePath(default_path); | |
| 131 | |
| 101 // Call the platform specific implementation of the file selection dialog. | 132 // Call the platform specific implementation of the file selection dialog. |
| 102 SelectFileImpl(type, title, default_path, file_types, file_type_index, | 133 SelectFileImpl(type, title, path, file_types, file_type_index, |
| 103 default_extension, owning_window, params); | 134 default_extension, owning_window, params); |
| 104 } | 135 } |
| 105 | 136 |
| 106 bool SelectFileDialog::HasMultipleFileTypeChoices() { | 137 bool SelectFileDialog::HasMultipleFileTypeChoices() { |
| 107 return HasMultipleFileTypeChoicesImpl(); | 138 return HasMultipleFileTypeChoicesImpl(); |
| 108 } | 139 } |
| 109 | 140 |
| 110 SelectFileDialog::SelectFileDialog(Listener* listener, | 141 SelectFileDialog::SelectFileDialog(Listener* listener, |
| 111 ui::SelectFilePolicy* policy) | 142 ui::SelectFilePolicy* policy) |
| 112 : listener_(listener), | 143 : listener_(listener), |
| 113 select_file_policy_(policy) { | 144 select_file_policy_(policy) { |
| 114 DCHECK(listener_); | 145 DCHECK(listener_); |
| 115 } | 146 } |
| 116 | 147 |
| 117 SelectFileDialog::~SelectFileDialog() {} | 148 SelectFileDialog::~SelectFileDialog() {} |
| 118 | 149 |
| 119 void SelectFileDialog::CancelFileSelection(void* params) { | 150 void SelectFileDialog::CancelFileSelection(void* params) { |
| 120 if (listener_) | 151 if (listener_) |
| 121 listener_->FileSelectionCanceled(params); | 152 listener_->FileSelectionCanceled(params); |
| 122 } | 153 } |
| 123 | 154 |
| 124 } // namespace ui | 155 } // namespace ui |
| OLD | NEW |