Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: ui/shell_dialogs/select_file_dialog.cc

Issue 2804793002: Print Preview: Fix failure to save with long page title (Closed)
Patch Set: Use resize, simplify Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
Nico 2017/04/14 14:03:34 FWIW, on Windows, the limit for the full path (not
rbpotter 2017/04/14 22:04:39 It fails to save in the system dialog if the path
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();
Nico 2017/04/14 14:03:34 Move this below the early return, you currently al
rbpotter 2017/04/14 22:04:39 Done.
30 const size_t kMaxNameLength = 255;
31 if (filename.value().length() <= kMaxNameLength)
32 return path;
33 base::FilePath::StringType extension = filename.FinalExtension();
34 filename = filename.RemoveFinalExtension();
35 base::FilePath::StringType file_string = filename.value();
36 // 1 for . plus 12 for longest known extension.
37 size_t max_extension_length = 13;
38 if (file_string.length() < kMaxNameLength) {
39 max_extension_length =
40 std::max(max_extension_length, kMaxNameLength - file_string.length());
41 }
42 if (extension.length() > max_extension_length) {
43 // Take the first max_extension_length characters (this will be the
44 // leading '.' plus the next max_extension_length - 1).
45 extension.resize(max_extension_length);
46 }
47 file_string.resize(kMaxNameLength - extension.length());
48 return path.DirName().Append(file_string).AddExtension(extension);
49 }
50
24 } // namespace 51 } // namespace
25 52
26 namespace ui { 53 namespace ui {
27 54
28 SelectFileDialog::FileTypeInfo::FileTypeInfo() 55 SelectFileDialog::FileTypeInfo::FileTypeInfo()
29 : include_all_files(false), allowed_paths(NATIVE_PATH) {} 56 : include_all_files(false), allowed_paths(NATIVE_PATH) {}
30 57
31 SelectFileDialog::FileTypeInfo::FileTypeInfo(const FileTypeInfo& other) = 58 SelectFileDialog::FileTypeInfo::FileTypeInfo(const FileTypeInfo& other) =
32 default; 59 default;
33 60
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 118
92 // Inform the listener that no file was selected. 119 // Inform the listener that no file was selected.
93 // Post a task rather than calling FileSelectionCanceled directly to ensure 120 // Post a task rather than calling FileSelectionCanceled directly to ensure
94 // that the listener is called asynchronously. 121 // that the listener is called asynchronously.
95 base::ThreadTaskRunnerHandle::Get()->PostTask( 122 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE, 123 FROM_HERE,
97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); 124 base::Bind(&SelectFileDialog::CancelFileSelection, this, params));
98 return; 125 return;
99 } 126 }
100 127
128 base::FilePath path = GetShortenedFilePath(default_path);
129
101 // Call the platform specific implementation of the file selection dialog. 130 // Call the platform specific implementation of the file selection dialog.
102 SelectFileImpl(type, title, default_path, file_types, file_type_index, 131 SelectFileImpl(type, title, path, file_types, file_type_index,
103 default_extension, owning_window, params); 132 default_extension, owning_window, params);
104 } 133 }
105 134
106 bool SelectFileDialog::HasMultipleFileTypeChoices() { 135 bool SelectFileDialog::HasMultipleFileTypeChoices() {
107 return HasMultipleFileTypeChoicesImpl(); 136 return HasMultipleFileTypeChoicesImpl();
108 } 137 }
109 138
110 SelectFileDialog::SelectFileDialog(Listener* listener, 139 SelectFileDialog::SelectFileDialog(Listener* listener,
111 ui::SelectFilePolicy* policy) 140 ui::SelectFilePolicy* policy)
112 : listener_(listener), 141 : listener_(listener),
113 select_file_policy_(policy) { 142 select_file_policy_(policy) {
114 DCHECK(listener_); 143 DCHECK(listener_);
115 } 144 }
116 145
117 SelectFileDialog::~SelectFileDialog() {} 146 SelectFileDialog::~SelectFileDialog() {}
118 147
119 void SelectFileDialog::CancelFileSelection(void* params) { 148 void SelectFileDialog::CancelFileSelection(void* params) {
120 if (listener_) 149 if (listener_)
121 listener_->FileSelectionCanceled(params); 150 listener_->FileSelectionCanceled(params);
122 } 151 }
123 152
124 } // namespace ui 153 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698