Commit cd633fdb authored by Marijn Kruisselbrink's avatar Marijn Kruisselbrink Committed by Chromium LUCI CQ

Keep file types ordered in mac file dialog.

The first file type will be used as default extension in save dialogs.
Previously the mac implementation of the save dialog would arbitrarily
re-order the passed in file types (by adding them all to a set, and
using the resulting order), which makes it impossible for callers to
actually enforce what extension should be used in save dialogs. This
fixes this behavior by keeping the order of file types as passed in,
only using the set to avoid duplication.

Bug: 1103133
Change-Id: I08d9d944eee977a1c99c1b1ecd72f6bb2c9afdcd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2582823
Commit-Queue: Marijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835751}
parent 1d0a586c
......@@ -318,7 +318,7 @@ void SelectFileDialogBridge::SetAccessoryView(
// Populate file_type_lists.
// Set to store different extensions in the current extension group.
NSMutableSet* file_type_set = [NSMutableSet set];
NSMutableArray* file_type_array = [NSMutableArray array];
for (const base::FilePath::StringType& ext : ext_list) {
if (ext == default_extension)
default_extension_index = i;
......@@ -327,8 +327,11 @@ void SelectFileDialogBridge::SetAccessoryView(
// we nil check before adding to |file_type_set|. See crbug.com/630101 and
// rdar://27490414.
base::ScopedCFTypeRef<CFStringRef> uti(CreateUTIFromExtension(ext));
if (uti)
[file_type_set addObject:base::mac::CFToNSCast(uti.get())];
if (uti) {
NSString* uti_ns = base::mac::CFToNSCast(uti.get());
if (![file_type_array containsObject:uti_ns])
[file_type_array addObject:uti_ns];
}
// Always allow the extension itself, in case the UTI doesn't map
// back to the original extension correctly. This occurs with dynamic
......@@ -336,9 +339,11 @@ void SelectFileDialogBridge::SetAccessoryView(
// See http://crbug.com/148840, http://openradar.me/12316273
base::ScopedCFTypeRef<CFStringRef> ext_cf(
base::SysUTF8ToCFStringRef(ext));
[file_type_set addObject:base::mac::CFToNSCast(ext_cf.get())];
NSString* ext_ns = base::mac::CFToNSCast(ext_cf.get());
if (![file_type_array containsObject:ext_ns])
[file_type_array addObject:ext_ns];
}
[file_type_lists addObject:[file_type_set allObjects]];
[file_type_lists addObject:file_type_array];
}
if (file_types->include_all_files || file_types->extensions.empty()) {
......
......@@ -166,6 +166,9 @@ TEST_F(SelectFileDialogMacTest, ExtensionPopup) {
EXPECT_EQ(0, [popup indexOfSelectedItem]);
EXPECT_TRUE([[panel allowedFileTypes] containsObject:@"htm"]);
EXPECT_TRUE([[panel allowedFileTypes] containsObject:@"html"]);
// Extensions should appear in order of input.
EXPECT_LT([[panel allowedFileTypes] indexOfObject:@"html"],
[[panel allowedFileTypes] indexOfObject:@"htm"]);
EXPECT_FALSE([[panel allowedFileTypes] containsObject:@"jpg"]);
// Select the second item.
......@@ -173,6 +176,9 @@ TEST_F(SelectFileDialogMacTest, ExtensionPopup) {
EXPECT_EQ(1, [popup indexOfSelectedItem]);
EXPECT_TRUE([[panel allowedFileTypes] containsObject:@"jpg"]);
EXPECT_TRUE([[panel allowedFileTypes] containsObject:@"jpeg"]);
// Extensions should appear in order of input.
EXPECT_LT([[panel allowedFileTypes] indexOfObject:@"jpeg"],
[[panel allowedFileTypes] indexOfObject:@"jpg"]);
EXPECT_FALSE([[panel allowedFileTypes] containsObject:@"html"]);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment