Commit 8ba9ebda authored by Christopher Cameron's avatar Christopher Cameron Committed by Commit Bot

MacPWAs: Fix file handlers

In r750774, web_app::ShortcutInfo::file_handler_extensions was
changed from stripping off the leading `.` to not stripping
off the leading `.`. This broke the macOS implementation, because
these values are listed directly in CFBundleTypeExtensions in the
Info.plist.

Fix this by stripping the leading `.` in a helper function, and
updating the unit tests to verify this behavior.

Bug: 1067368
Change-Id: I222fbd8a342cc1f13bc271bfe59a1aad8989b889
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2136470
Commit-Queue: ccameron <ccameron@chromium.org>
Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756581}
parent 4d42e3d1
...@@ -211,6 +211,19 @@ bool IsImageValidForIcon(const gfx::Image& image) { ...@@ -211,6 +211,19 @@ bool IsImageValidForIcon(const gfx::Image& image) {
return false; return false;
} }
// Remove the leading . from the entries of |extensions|. Any items that do not
// have a leading . are removed.
std::set<std::string> GetFileHandlerExtensionsWithoutDot(
const std::set<std::string>& file_extensions) {
std::set<std::string> result;
for (const auto& file_extension : file_extensions) {
if (file_extension.length() <= 1 || file_extension[0] != '.')
continue;
result.insert(file_extension.substr(1));
}
return result;
}
bool AppShimCreationDisabledForTest() { bool AppShimCreationDisabledForTest() {
// Disable app shims in tests because shims created in ~/Applications will not // Disable app shims in tests because shims created in ~/Applications will not
// be cleaned up. // be cleaned up.
...@@ -1024,17 +1037,19 @@ bool WebAppShortcutCreator::UpdatePlist(const base::FilePath& app_path) const { ...@@ -1024,17 +1037,19 @@ bool WebAppShortcutCreator::UpdatePlist(const base::FilePath& app_path) const {
forKey:app_mode::kNSHighResolutionCapableKey]; forKey:app_mode::kNSHighResolutionCapableKey];
// 3. Fill in file handlers. // 3. Fill in file handlers.
if (!info_->file_handler_extensions.empty() || const auto file_handler_extensions =
GetFileHandlerExtensionsWithoutDot(info_->file_handler_extensions);
if (!file_handler_extensions.empty() ||
!info_->file_handler_mime_types.empty()) { !info_->file_handler_mime_types.empty()) {
base::scoped_nsobject<NSMutableArray> doc_types_value( base::scoped_nsobject<NSMutableArray> doc_types_value(
[[NSMutableArray alloc] init]); [[NSMutableArray alloc] init]);
base::scoped_nsobject<NSMutableDictionary> doc_types_dict( base::scoped_nsobject<NSMutableDictionary> doc_types_dict(
[[NSMutableDictionary alloc] init]); [[NSMutableDictionary alloc] init]);
if (!info_->file_handler_extensions.empty()) { if (!file_handler_extensions.empty()) {
base::scoped_nsobject<NSMutableArray> extensions( base::scoped_nsobject<NSMutableArray> extensions(
[[NSMutableArray alloc] init]); [[NSMutableArray alloc] init]);
for (const auto& extension : info_->file_handler_extensions) for (const auto& file_extension : file_handler_extensions)
[extensions addObject:base::SysUTF8ToNSString(extension)]; [extensions addObject:base::SysUTF8ToNSString(file_extension)];
[doc_types_dict setObject:extensions [doc_types_dict setObject:extensions
forKey:app_mode::kCFBundleTypeExtensionsKey]; forKey:app_mode::kCFBundleTypeExtensionsKey];
} }
......
...@@ -219,8 +219,10 @@ TEST_F(WebAppShortcutCreatorTest, FileHandlers) { ...@@ -219,8 +219,10 @@ TEST_F(WebAppShortcutCreatorTest, FileHandlers) {
} }
EXPECT_TRUE(base::DeleteFileRecursively(shim_path_)); EXPECT_TRUE(base::DeleteFileRecursively(shim_path_));
// Register 2 mime types. We should now have kCFBundleTypeMIMETypesKey but // Register 2 mime types (and 2 invalid extensions). We should now have
// not kCFBundleTypeExtensionsKey. // kCFBundleTypeMIMETypesKey but not kCFBundleTypeExtensionsKey.
info_->file_handler_extensions.insert("byobb");
info_->file_handler_extensions.insert(".");
info_->file_handler_mime_types.insert("foo/bar"); info_->file_handler_mime_types.insert("foo/bar");
info_->file_handler_mime_types.insert("moo/cow"); info_->file_handler_mime_types.insert("moo/cow");
EXPECT_TRUE(shortcut_creator.CreateShortcuts(SHORTCUT_CREATION_AUTOMATED, EXPECT_TRUE(shortcut_creator.CreateShortcuts(SHORTCUT_CREATION_AUTOMATED,
...@@ -249,10 +251,10 @@ TEST_F(WebAppShortcutCreatorTest, FileHandlers) { ...@@ -249,10 +251,10 @@ TEST_F(WebAppShortcutCreatorTest, FileHandlers) {
} }
EXPECT_TRUE(base::DeleteFileRecursively(shim_path_)); EXPECT_TRUE(base::DeleteFileRecursively(shim_path_));
// Register 3 extensions with the 2 mime types. // Register 3 valid extensions (and 2 invalid ones) with the 2 mime types.
info_->file_handler_extensions.insert("cow"); info_->file_handler_extensions.insert(".cow");
info_->file_handler_extensions.insert("pig"); info_->file_handler_extensions.insert(".pig");
info_->file_handler_extensions.insert("bbq"); info_->file_handler_extensions.insert(".bbq");
EXPECT_TRUE(shortcut_creator.CreateShortcuts(SHORTCUT_CREATION_AUTOMATED, EXPECT_TRUE(shortcut_creator.CreateShortcuts(SHORTCUT_CREATION_AUTOMATED,
ShortcutLocations())); ShortcutLocations()));
{ {
......
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