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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 Type type, | 79 Type type, |
| 79 const base::string16& title, | 80 const base::string16& title, |
| 80 const base::FilePath& default_path, | 81 const base::FilePath& default_path, |
| 81 const FileTypeInfo* file_types, | 82 const FileTypeInfo* file_types, |
| 82 int file_type_index, | 83 int file_type_index, |
| 83 const base::FilePath::StringType& default_extension, | 84 const base::FilePath::StringType& default_extension, |
| 84 gfx::NativeWindow owning_window, | 85 gfx::NativeWindow owning_window, |
| 85 void* params) { | 86 void* params) { |
| 86 DCHECK(listener_); | 87 DCHECK(listener_); |
| 87 | 88 |
| 89 // Shorten filename if necessary. | |
|
Lei Zhang
2017/04/06 00:47:05
Do this right before the SelectFileImpl() call. Ot
rbpotter
2017/04/12 17:25:19
Done.
| |
| 90 base::FilePath filename = default_path.BaseName(); | |
| 91 base::FilePath path = default_path.DirName(); | |
| 92 int max_name_length = 255; | |
|
Lei Zhang
2017/04/06 00:47:05
Can this be size_t to avoid all the casts below.
rbpotter
2017/04/12 17:25:19
Done.
| |
| 93 if (filename.value().length() > max_name_length || | |
| 94 default_path.value().length() > MAX_PATH - 1) { | |
|
Lei Zhang
2017/04/06 00:47:05
Does MAX_PATH work on POSIX?
rbpotter
2017/04/12 17:25:19
Removed path length check so not an issue any more
| |
| 95 base::FilePath::StringType extension = filename.Extension(); | |
| 96 // Path name no longer than MAX_PATH - 2 - length of rest of path (remove | |
| 97 // 1 for separator and 1 for null character). | |
| 98 int max_path_length = | |
| 99 MAX_PATH - 2 - static_cast<int>(path.value().length()); | |
| 100 int new_length = std::max(1, std::min(max_name_length, max_path_length) - | |
| 101 static_cast<int>(extension.length())); | |
| 102 path = path.Append(filename.value().substr(0, new_length)); | |
| 103 path = path.AddExtension(extension); | |
| 104 } else { | |
| 105 path = path.Append(filename); | |
|
Lei Zhang
2017/04/06 00:47:05
Isn't this the same as: path = default_path; ?
rbpotter
2017/04/12 17:25:19
Done.
| |
| 106 } | |
| 107 | |
| 88 if (select_file_policy_.get() && | 108 if (select_file_policy_.get() && |
| 89 !select_file_policy_->CanOpenSelectFileDialog()) { | 109 !select_file_policy_->CanOpenSelectFileDialog()) { |
| 90 select_file_policy_->SelectFileDenied(); | 110 select_file_policy_->SelectFileDenied(); |
| 91 | 111 |
| 92 // Inform the listener that no file was selected. | 112 // Inform the listener that no file was selected. |
| 93 // Post a task rather than calling FileSelectionCanceled directly to ensure | 113 // Post a task rather than calling FileSelectionCanceled directly to ensure |
| 94 // that the listener is called asynchronously. | 114 // that the listener is called asynchronously. |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 115 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, | 116 FROM_HERE, |
| 97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); | 117 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); |
| 98 return; | 118 return; |
| 99 } | 119 } |
| 100 | 120 |
| 101 // Call the platform specific implementation of the file selection dialog. | 121 // Call the platform specific implementation of the file selection dialog. |
| 102 SelectFileImpl(type, title, default_path, file_types, file_type_index, | 122 SelectFileImpl(type, title, path, file_types, file_type_index, |
| 103 default_extension, owning_window, params); | 123 default_extension, owning_window, params); |
| 104 } | 124 } |
| 105 | 125 |
| 106 bool SelectFileDialog::HasMultipleFileTypeChoices() { | 126 bool SelectFileDialog::HasMultipleFileTypeChoices() { |
| 107 return HasMultipleFileTypeChoicesImpl(); | 127 return HasMultipleFileTypeChoicesImpl(); |
| 108 } | 128 } |
| 109 | 129 |
| 110 SelectFileDialog::SelectFileDialog(Listener* listener, | 130 SelectFileDialog::SelectFileDialog(Listener* listener, |
| 111 ui::SelectFilePolicy* policy) | 131 ui::SelectFilePolicy* policy) |
| 112 : listener_(listener), | 132 : listener_(listener), |
| 113 select_file_policy_(policy) { | 133 select_file_policy_(policy) { |
| 114 DCHECK(listener_); | 134 DCHECK(listener_); |
| 115 } | 135 } |
| 116 | 136 |
| 117 SelectFileDialog::~SelectFileDialog() {} | 137 SelectFileDialog::~SelectFileDialog() {} |
| 118 | 138 |
| 119 void SelectFileDialog::CancelFileSelection(void* params) { | 139 void SelectFileDialog::CancelFileSelection(void* params) { |
| 120 if (listener_) | 140 if (listener_) |
| 121 listener_->FileSelectionCanceled(params); | 141 listener_->FileSelectionCanceled(params); |
| 122 } | 142 } |
| 123 | 143 |
| 124 } // namespace ui | 144 } // namespace ui |
| OLD | NEW |