Commit 6de1a7b8 authored by Alex Danilo's avatar Alex Danilo Committed by Commit Bot

[filesapp] Make HTML files prefer open in browser

CL:2332079 changed default application opening logic to prefer an
installed app over opening in the browser.

Change this logic to detect an HTML file extension and bypass the
installed application selection so that the browser remains the
preferred opening target to avoid user surprises. (N.B. Users can still
manually select the installed app as default for HTML).

Bug: 1121396
Tests: unit_tests --gtest_filter="*ChooseAndSetDefaultTask_FallbackHtmlTextApp"
Change-Id: I1359be9d06b2ff27c7928c59ba68a92a66430584
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2391944
Commit-Queue: Alex Danilo <adanilo@chromium.org>
Reviewed-by: default avatarBugs Nash <bugsnash@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804655}
parent df86c84c
...@@ -881,10 +881,17 @@ void ChooseAndSetDefaultTask(const PrefService& pref_service, ...@@ -881,10 +881,17 @@ void ChooseAndSetDefaultTask(const PrefService& pref_service,
} }
// Prefer a fallback app over viewing in the browser (crbug.com/1111399). // Prefer a fallback app over viewing in the browser (crbug.com/1111399).
// Unless it's HTML which should open in the browser (crbug.com/1121396).
for (size_t i = 0; i < tasks->size(); ++i) { for (size_t i = 0; i < tasks->size(); ++i) {
FullTaskDescriptor& task = (*tasks)[i]; FullTaskDescriptor& task = (*tasks)[i];
if (IsFallbackFileHandler(task) && if (IsFallbackFileHandler(task) &&
task.task_descriptor().action_id != "view-in-browser") { task.task_descriptor().action_id != "view-in-browser") {
const extensions::EntryInfo entry = entries[0];
const base::FilePath& file_path = entry.path;
if (IsHtmlFile(file_path)) {
break;
}
task.set_is_default(true); task.set_is_default(true);
return; return;
} }
...@@ -912,5 +919,15 @@ bool IsRawImage(const base::FilePath& path) { ...@@ -912,5 +919,15 @@ bool IsRawImage(const base::FilePath& path) {
return false; return false;
} }
bool IsHtmlFile(const base::FilePath& path) {
constexpr const char* kHtmlExtensions[] = {".htm", ".html", ".mhtml",
".xht", ".xhtm", ".xhtml"};
for (const char* extension : kHtmlExtensions) {
if (path.MatchesExtension(extension))
return true;
}
return false;
}
} // namespace file_tasks } // namespace file_tasks
} // namespace file_manager } // namespace file_manager
...@@ -346,6 +346,9 @@ void ChooseAndSetDefaultTask(const PrefService& pref_service, ...@@ -346,6 +346,9 @@ void ChooseAndSetDefaultTask(const PrefService& pref_service,
// using MimeTypeCollector. TODO(crbug/1030935): Remove this. // using MimeTypeCollector. TODO(crbug/1030935): Remove this.
bool IsRawImage(const base::FilePath& path); bool IsRawImage(const base::FilePath& path);
// Returns whether |path| is an HTML file according to its extension.
bool IsHtmlFile(const base::FilePath& path);
} // namespace file_tasks } // namespace file_tasks
} // namespace file_manager } // namespace file_manager
......
...@@ -266,6 +266,38 @@ TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_FallbackTextApp) { ...@@ -266,6 +266,38 @@ TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_FallbackTextApp) {
EXPECT_TRUE(tasks[1].is_default()); EXPECT_TRUE(tasks[1].is_default());
} }
// Test that browser is chosen as default for HTML files instead of the Text
// app even if nothing is set in the preferences.
TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_FallbackHtmlTextApp) {
TestingPrefServiceSimple pref_service;
RegisterDefaultTaskPreferences(&pref_service);
// Define the browser handler of the Files app for "foo.html".
TaskDescriptor files_app_task(
kFileManagerAppId, TASK_TYPE_FILE_BROWSER_HANDLER, "view-in-browser");
// Define the text editor app for "foo.html".
TaskDescriptor text_app_task(kTextEditorAppId, TASK_TYPE_FILE_HANDLER,
"Text");
std::vector<FullTaskDescriptor> tasks;
tasks.emplace_back(
files_app_task, "View in browser", Verb::VERB_OPEN_WITH,
GURL("http://example.com/some_icon.png"), false /* is_default */,
false /* is_generic_file_handler */, false /* is_file_extension_match */);
tasks.emplace_back(
text_app_task, "Text", Verb::VERB_OPEN_WITH,
GURL("chrome://extension-icon/mmfbcljfglbokpmkimbfghdkjmjhdgbg/16/1"),
false /* is_default */, false /* is_generic_file_handler */,
false /* is_file_extension_match */);
std::vector<extensions::EntryInfo> entries;
entries.emplace_back(base::FilePath::FromUTF8Unsafe("foo.html"), "text/html",
false);
// The internal file browser handler should be chosen as default,
// as it's a fallback file browser handler.
ChooseAndSetDefaultTask(pref_service, entries, &tasks);
EXPECT_TRUE(tasks[0].is_default());
}
// Test that Audio Player is chosen as default even if nothing is set in the // Test that Audio Player is chosen as default even if nothing is set in the
// preferences. // preferences.
TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_FallbackAudioPlayer) { TEST(FileManagerFileTasksTest, ChooseAndSetDefaultTask_FallbackAudioPlayer) {
......
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