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

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

Powered by Google App Engine
This is Rietveld 408576698