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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 ui::SelectFilePolicy* policy) { | 68 ui::SelectFilePolicy* policy) { |
| 68 if (dialog_factory_) { | 69 if (dialog_factory_) { |
| 69 SelectFileDialog* dialog = dialog_factory_->Create(listener, policy); | 70 SelectFileDialog* dialog = dialog_factory_->Create(listener, policy); |
| 70 if (dialog) | 71 if (dialog) |
| 71 return dialog; | 72 return dialog; |
| 72 } | 73 } |
| 73 | 74 |
| 74 return CreateSelectFileDialog(listener, policy); | 75 return CreateSelectFileDialog(listener, policy); |
| 75 } | 76 } |
| 76 | 77 |
| 78 namespace { | |
|
Lei Zhang
2017/04/13 19:33:53
There's already one up on line 20.
rbpotter
2017/04/13 20:53:56
Done.
| |
| 79 | |
| 80 base::FilePath GetShortenedFilePath(const base::FilePath& path) { | |
|
Lei Zhang
2017/04/13 19:33:53
Can you add a comment to explain why we need this?
rbpotter
2017/04/13 20:53:56
Done.
| |
| 81 base::FilePath filename = path.BaseName(); | |
| 82 base::FilePath short_path = path; | |
| 83 const size_t kMaxNameLength = 255; | |
| 84 if (filename.value().length() > kMaxNameLength) { | |
|
Lei Zhang
2017/04/13 19:33:53
Flip to <=, and then return early.
rbpotter
2017/04/13 20:53:56
Done.
| |
| 85 base::FilePath::StringType extension = filename.FinalExtension(); | |
| 86 filename = filename.RemoveFinalExtension(); | |
| 87 // 1 for . plus 12 for longest known extension. | |
| 88 size_t max_extension_length = 13; | |
| 89 if (filename.value().length() < kMaxNameLength) { | |
|
Lei Zhang
2017/04/13 19:33:53
I think you can omit this check. If the |filename|
rbpotter
2017/04/13 20:53:56
Both kMaxNameLength and filename.value().length()
Lei Zhang
2017/04/13 21:13:44
Try writing 13U instead?
| |
| 90 max_extension_length = std::max( | |
| 91 max_extension_length, kMaxNameLength - filename.value().length()); | |
| 92 } | |
| 93 if (extension.length() >= max_extension_length) { | |
| 94 // Take the last max_extension_length - 1 characters (save 1 character | |
| 95 // for the '.'). | |
| 96 base::FilePath::StringType new_extension; | |
| 97 new_extension[0] = base::FilePath::kExtensionSeparator; | |
|
Lei Zhang
2017/04/13 19:33:53
Isn't this an out of bounds access since |new_exte
rbpotter
2017/04/13 20:53:56
That doesn't work since the extension separator is
| |
| 98 extension = new_extension.append( | |
| 99 extension.substr(extension.length() - max_extension_length + 1, | |
| 100 max_extension_length - 1)); | |
| 101 } | |
| 102 short_path = path.DirName().Append( | |
| 103 filename.value().substr(0, kMaxNameLength - extension.length())); | |
| 104 short_path = short_path.AddExtension(extension); | |
| 105 } | |
| 106 return short_path; | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 77 void SelectFileDialog::SelectFile( | 111 void SelectFileDialog::SelectFile( |
| 78 Type type, | 112 Type type, |
| 79 const base::string16& title, | 113 const base::string16& title, |
| 80 const base::FilePath& default_path, | 114 const base::FilePath& default_path, |
| 81 const FileTypeInfo* file_types, | 115 const FileTypeInfo* file_types, |
| 82 int file_type_index, | 116 int file_type_index, |
| 83 const base::FilePath::StringType& default_extension, | 117 const base::FilePath::StringType& default_extension, |
| 84 gfx::NativeWindow owning_window, | 118 gfx::NativeWindow owning_window, |
| 85 void* params) { | 119 void* params) { |
| 86 DCHECK(listener_); | 120 DCHECK(listener_); |
| 87 | 121 |
| 88 if (select_file_policy_.get() && | 122 if (select_file_policy_.get() && |
| 89 !select_file_policy_->CanOpenSelectFileDialog()) { | 123 !select_file_policy_->CanOpenSelectFileDialog()) { |
| 90 select_file_policy_->SelectFileDenied(); | 124 select_file_policy_->SelectFileDenied(); |
| 91 | 125 |
| 92 // Inform the listener that no file was selected. | 126 // Inform the listener that no file was selected. |
| 93 // Post a task rather than calling FileSelectionCanceled directly to ensure | 127 // Post a task rather than calling FileSelectionCanceled directly to ensure |
| 94 // that the listener is called asynchronously. | 128 // that the listener is called asynchronously. |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 129 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, | 130 FROM_HERE, |
| 97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); | 131 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); |
| 98 return; | 132 return; |
| 99 } | 133 } |
| 100 | 134 |
| 135 base::FilePath path = GetShortenedFilePath(default_path); | |
| 136 | |
| 101 // Call the platform specific implementation of the file selection dialog. | 137 // Call the platform specific implementation of the file selection dialog. |
| 102 SelectFileImpl(type, title, default_path, file_types, file_type_index, | 138 SelectFileImpl(type, title, path, file_types, file_type_index, |
| 103 default_extension, owning_window, params); | 139 default_extension, owning_window, params); |
| 104 } | 140 } |
| 105 | 141 |
| 106 bool SelectFileDialog::HasMultipleFileTypeChoices() { | 142 bool SelectFileDialog::HasMultipleFileTypeChoices() { |
| 107 return HasMultipleFileTypeChoicesImpl(); | 143 return HasMultipleFileTypeChoicesImpl(); |
| 108 } | 144 } |
| 109 | 145 |
| 110 SelectFileDialog::SelectFileDialog(Listener* listener, | 146 SelectFileDialog::SelectFileDialog(Listener* listener, |
| 111 ui::SelectFilePolicy* policy) | 147 ui::SelectFilePolicy* policy) |
| 112 : listener_(listener), | 148 : listener_(listener), |
| 113 select_file_policy_(policy) { | 149 select_file_policy_(policy) { |
| 114 DCHECK(listener_); | 150 DCHECK(listener_); |
| 115 } | 151 } |
| 116 | 152 |
| 117 SelectFileDialog::~SelectFileDialog() {} | 153 SelectFileDialog::~SelectFileDialog() {} |
| 118 | 154 |
| 119 void SelectFileDialog::CancelFileSelection(void* params) { | 155 void SelectFileDialog::CancelFileSelection(void* params) { |
| 120 if (listener_) | 156 if (listener_) |
| 121 listener_->FileSelectionCanceled(params); | 157 listener_->FileSelectionCanceled(params); |
| 122 } | 158 } |
| 123 | 159 |
| 124 } // namespace ui | 160 } // namespace ui |
| OLD | NEW |