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

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: Address comments 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
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();
30 base::FilePath short_path = path;
31 const size_t kMaxNameLength = 255;
32 if (filename.value().length() <= kMaxNameLength)
33 return short_path;
34 base::FilePath::StringType extension = filename.FinalExtension();
35 filename = filename.RemoveFinalExtension();
36 // 1 for . plus 12 for longest known extension.
37 size_t max_extension_length = 13;
38 if (filename.value().length() < kMaxNameLength) {
39 max_extension_length = std::max(max_extension_length,
40 kMaxNameLength - filename.value().length());
41 }
42 if (extension.length() >= max_extension_length) {
43 // Take the last max_extension_length - 1 characters (save 1 character
44 // for the '.').
45 base::FilePath::StringType new_extension;
Lei Zhang 2017/04/13 21:13:44 Just an idea: would this be clearer if we instead
rbpotter 2017/04/13 21:48:37 Don't need this if we truncate since FinalExtensio
46 new_extension.push_back(base::FilePath::kExtensionSeparator);
47 extension = new_extension.append(
48 extension.substr(extension.length() - max_extension_length + 1,
Lei Zhang 2017/04/13 21:13:44 We should truncate the extension instead of taking
rbpotter 2017/04/13 21:48:37 Done.
49 max_extension_length - 1));
50 }
51 short_path = path.DirName().Append(
52 filename.value().substr(0, kMaxNameLength - extension.length()));
53 short_path = short_path.AddExtension(extension);
54 return short_path;
55 }
56
24 } // namespace 57 } // namespace
25 58
26 namespace ui { 59 namespace ui {
27 60
28 SelectFileDialog::FileTypeInfo::FileTypeInfo() 61 SelectFileDialog::FileTypeInfo::FileTypeInfo()
29 : include_all_files(false), allowed_paths(NATIVE_PATH) {} 62 : include_all_files(false), allowed_paths(NATIVE_PATH) {}
30 63
31 SelectFileDialog::FileTypeInfo::FileTypeInfo(const FileTypeInfo& other) = 64 SelectFileDialog::FileTypeInfo::FileTypeInfo(const FileTypeInfo& other) =
32 default; 65 default;
33 66
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 124
92 // Inform the listener that no file was selected. 125 // Inform the listener that no file was selected.
93 // Post a task rather than calling FileSelectionCanceled directly to ensure 126 // Post a task rather than calling FileSelectionCanceled directly to ensure
94 // that the listener is called asynchronously. 127 // that the listener is called asynchronously.
95 base::ThreadTaskRunnerHandle::Get()->PostTask( 128 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE, 129 FROM_HERE,
97 base::Bind(&SelectFileDialog::CancelFileSelection, this, params)); 130 base::Bind(&SelectFileDialog::CancelFileSelection, this, params));
98 return; 131 return;
99 } 132 }
100 133
134 base::FilePath path = GetShortenedFilePath(default_path);
135
101 // Call the platform specific implementation of the file selection dialog. 136 // Call the platform specific implementation of the file selection dialog.
102 SelectFileImpl(type, title, default_path, file_types, file_type_index, 137 SelectFileImpl(type, title, path, file_types, file_type_index,
103 default_extension, owning_window, params); 138 default_extension, owning_window, params);
104 } 139 }
105 140
106 bool SelectFileDialog::HasMultipleFileTypeChoices() { 141 bool SelectFileDialog::HasMultipleFileTypeChoices() {
107 return HasMultipleFileTypeChoicesImpl(); 142 return HasMultipleFileTypeChoicesImpl();
108 } 143 }
109 144
110 SelectFileDialog::SelectFileDialog(Listener* listener, 145 SelectFileDialog::SelectFileDialog(Listener* listener,
111 ui::SelectFilePolicy* policy) 146 ui::SelectFilePolicy* policy)
112 : listener_(listener), 147 : listener_(listener),
113 select_file_policy_(policy) { 148 select_file_policy_(policy) {
114 DCHECK(listener_); 149 DCHECK(listener_);
115 } 150 }
116 151
117 SelectFileDialog::~SelectFileDialog() {} 152 SelectFileDialog::~SelectFileDialog() {}
118 153
119 void SelectFileDialog::CancelFileSelection(void* params) { 154 void SelectFileDialog::CancelFileSelection(void* params) {
120 if (listener_) 155 if (listener_)
121 listener_->FileSelectionCanceled(params); 156 listener_->FileSelectionCanceled(params);
122 } 157 }
123 158
124 } // namespace ui 159 } // 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