| 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 base::FilePath SelectFileDialog::GetShortenedFilePath( |
| 79 const base::FilePath& path) { |
| 80 const size_t kMaxNameLength = 255; |
| 81 if (path.BaseName().value().length() <= kMaxNameLength) |
| 82 return path; |
| 83 base::FilePath filename = path.BaseName(); |
| 84 base::FilePath::StringType extension = filename.FinalExtension(); |
| 85 filename = filename.RemoveFinalExtension(); |
| 86 base::FilePath::StringType file_string = filename.value(); |
| 87 // 1 for . plus 12 for longest known extension. |
| 88 size_t max_extension_length = 13; |
| 89 if (file_string.length() < kMaxNameLength) { |
| 90 max_extension_length = |
| 91 std::max(max_extension_length, kMaxNameLength - file_string.length()); |
| 92 } |
| 93 if (extension.length() > max_extension_length) { |
| 94 // Take the first max_extension_length characters (this will be the |
| 95 // leading '.' plus the next max_extension_length - 1). |
| 96 extension.resize(max_extension_length); |
| 97 } |
| 98 file_string.resize(kMaxNameLength - extension.length()); |
| 99 return path.DirName().Append(file_string).AddExtension(extension); |
| 100 } |
| 101 |
| 77 void SelectFileDialog::SelectFile( | 102 void SelectFileDialog::SelectFile( |
| 78 Type type, | 103 Type type, |
| 79 const base::string16& title, | 104 const base::string16& title, |
| 80 const base::FilePath& default_path, | 105 const base::FilePath& default_path, |
| 81 const FileTypeInfo* file_types, | 106 const FileTypeInfo* file_types, |
| 82 int file_type_index, | 107 int file_type_index, |
| 83 const base::FilePath::StringType& default_extension, | 108 const base::FilePath::StringType& default_extension, |
| 84 gfx::NativeWindow owning_window, | 109 gfx::NativeWindow owning_window, |
| 85 void* params) { | 110 void* params) { |
| 86 DCHECK(listener_); | 111 DCHECK(listener_); |
| 87 | 112 |
| 88 if (select_file_policy_.get() && | 113 if (select_file_policy_.get() && |
| 89 !select_file_policy_->CanOpenSelectFileDialog()) { | 114 !select_file_policy_->CanOpenSelectFileDialog()) { |
| 90 select_file_policy_->SelectFileDenied(); | 115 select_file_policy_->SelectFileDenied(); |
| 91 | 116 |
| 92 // Inform the listener that no file was selected. | 117 // Inform the listener that no file was selected. |
| 93 // Post a task rather than calling FileSelectionCanceled directly to ensure | 118 // Post a task rather than calling FileSelectionCanceled directly to ensure |
| 94 // that the listener is called asynchronously. | 119 // that the listener is called asynchronously. |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 120 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, | 121 FROM_HERE, |
| 97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); | 122 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); |
| 98 return; | 123 return; |
| 99 } | 124 } |
| 100 | 125 |
| 126 base::FilePath path = GetShortenedFilePath(default_path); |
| 127 |
| 101 // Call the platform specific implementation of the file selection dialog. | 128 // Call the platform specific implementation of the file selection dialog. |
| 102 SelectFileImpl(type, title, default_path, file_types, file_type_index, | 129 SelectFileImpl(type, title, path, file_types, file_type_index, |
| 103 default_extension, owning_window, params); | 130 default_extension, owning_window, params); |
| 104 } | 131 } |
| 105 | 132 |
| 106 bool SelectFileDialog::HasMultipleFileTypeChoices() { | 133 bool SelectFileDialog::HasMultipleFileTypeChoices() { |
| 107 return HasMultipleFileTypeChoicesImpl(); | 134 return HasMultipleFileTypeChoicesImpl(); |
| 108 } | 135 } |
| 109 | 136 |
| 110 SelectFileDialog::SelectFileDialog(Listener* listener, | 137 SelectFileDialog::SelectFileDialog(Listener* listener, |
| 111 ui::SelectFilePolicy* policy) | 138 ui::SelectFilePolicy* policy) |
| 112 : listener_(listener), | 139 : listener_(listener), |
| 113 select_file_policy_(policy) { | 140 select_file_policy_(policy) { |
| 114 DCHECK(listener_); | 141 DCHECK(listener_); |
| 115 } | 142 } |
| 116 | 143 |
| 117 SelectFileDialog::~SelectFileDialog() {} | 144 SelectFileDialog::~SelectFileDialog() {} |
| 118 | 145 |
| 119 void SelectFileDialog::CancelFileSelection(void* params) { | 146 void SelectFileDialog::CancelFileSelection(void* params) { |
| 120 if (listener_) | 147 if (listener_) |
| 121 listener_->FileSelectionCanceled(params); | 148 listener_->FileSelectionCanceled(params); |
| 122 } | 149 } |
| 123 | 150 |
| 124 } // namespace ui | 151 } // namespace ui |
| OLD | NEW |