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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 | 92 |
| 92 // Inform the listener that no file was selected. | 93 // Inform the listener that no file was selected. |
| 93 // Post a task rather than calling FileSelectionCanceled directly to ensure | 94 // Post a task rather than calling FileSelectionCanceled directly to ensure |
| 94 // that the listener is called asynchronously. | 95 // that the listener is called asynchronously. |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 96 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, | 97 FROM_HERE, |
| 97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); | 98 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); |
| 98 return; | 99 return; |
| 99 } | 100 } |
| 100 | 101 |
| 102 // Shorten filename if necessary. | |
|
Lei Zhang
2017/04/13 01:34:51
This is getting long-ish. Can we split it off into
rbpotter
2017/04/13 19:21:02
Done.
| |
| 103 base::FilePath filename = default_path.BaseName(); | |
| 104 base::FilePath path = default_path; | |
| 105 const size_t kMaxNameLength = 255; | |
| 106 if (filename.value().length() > kMaxNameLength) { | |
| 107 base::FilePath::StringType extension = filename.FinalExtension(); | |
| 108 filename = filename.RemoveFinalExtension(); | |
| 109 // 1 for . plus 12 for longest known extension. | |
| 110 size_t max_extension_length = 13; | |
| 111 if (kMaxNameLength > filename.value().length()) { | |
|
Lei Zhang
2017/04/13 01:34:51
This reads more naturally if you flip the equation
rbpotter
2017/04/13 19:21:02
Done.
| |
| 112 max_extension_length = std::max( | |
| 113 max_extension_length, kMaxNameLength - filename.value().length()); | |
| 114 } | |
| 115 size_t extension_length = extension.length(); | |
| 116 if (extension.length() >= max_extension_length) { | |
| 117 // Take the last max_extension_length - 1 characters (save 1 character | |
| 118 // for the '.'). | |
| 119 extension = | |
|
Lei Zhang
2017/04/13 01:34:50
This is a bit weird. If the code does not go throu
rbpotter
2017/04/13 19:21:02
Done.
| |
| 120 extension.substr(extension.length() - max_extension_length + 1, | |
| 121 max_extension_length - 1); | |
| 122 extension_length = max_extension_length; | |
| 123 } | |
| 124 path = default_path.DirName().Append( | |
| 125 filename.value().substr(0, kMaxNameLength - extension_length)); | |
| 126 path = path.AddExtension(extension); | |
| 127 } | |
| 128 | |
| 101 // Call the platform specific implementation of the file selection dialog. | 129 // Call the platform specific implementation of the file selection dialog. |
| 102 SelectFileImpl(type, title, default_path, file_types, file_type_index, | 130 SelectFileImpl(type, title, path, file_types, file_type_index, |
| 103 default_extension, owning_window, params); | 131 default_extension, owning_window, params); |
| 104 } | 132 } |
| 105 | 133 |
| 106 bool SelectFileDialog::HasMultipleFileTypeChoices() { | 134 bool SelectFileDialog::HasMultipleFileTypeChoices() { |
| 107 return HasMultipleFileTypeChoicesImpl(); | 135 return HasMultipleFileTypeChoicesImpl(); |
| 108 } | 136 } |
| 109 | 137 |
| 110 SelectFileDialog::SelectFileDialog(Listener* listener, | 138 SelectFileDialog::SelectFileDialog(Listener* listener, |
| 111 ui::SelectFilePolicy* policy) | 139 ui::SelectFilePolicy* policy) |
| 112 : listener_(listener), | 140 : listener_(listener), |
| 113 select_file_policy_(policy) { | 141 select_file_policy_(policy) { |
| 114 DCHECK(listener_); | 142 DCHECK(listener_); |
| 115 } | 143 } |
| 116 | 144 |
| 117 SelectFileDialog::~SelectFileDialog() {} | 145 SelectFileDialog::~SelectFileDialog() {} |
| 118 | 146 |
| 119 void SelectFileDialog::CancelFileSelection(void* params) { | 147 void SelectFileDialog::CancelFileSelection(void* params) { |
| 120 if (listener_) | 148 if (listener_) |
| 121 listener_->FileSelectionCanceled(params); | 149 listener_->FileSelectionCanceled(params); |
| 122 } | 150 } |
| 123 | 151 |
| 124 } // namespace ui | 152 } // namespace ui |
| OLD | NEW |