WebApps: Register MIME types when web app installed on Linux.
In Linux, an association between an app and a particular file is made
on the basis of the file's MIME type. Where an app supports handling
MIME types that aren't known to the OS, those types must be registered
calling `xdg-mime` [1] on an XML file containing a mapping of MIME types
to file extensions per the freedesktop.org Shared MIME-info Database
specification [2]. This CL implements methods for generating a compliant
XML file from a set of file handlers, writing it to a temp file, and
calling `xdg-mime` on that file to register the new MIME types. These
are invoked as part of the web app install process on Linux.
[1] https://linux.die.net/man/1/xdg-mime
[2] https://freedesktop.org/standards/shared-mime-info
Bug: 938103
Change-Id: If7e4cb0063e1688cb69d2b2b503235658c087366
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2101398
Commit-Queue: Robert Woods <[email protected]>
Reviewed-by: Alan Cutter <[email protected]>
Reviewed-by: Thomas Anderson <[email protected]>
Cr-Commit-Position: refs/heads/master@{#751938}
diff --git a/chrome/browser/shell_integration_linux.cc b/chrome/browser/shell_integration_linux.cc
index 55be2dc..8f8f6b8 100644
--- a/chrome/browser/shell_integration_linux.cc
+++ b/chrome/browser/shell_integration_linux.cc
@@ -12,6 +12,7 @@
#include <unistd.h>
#include <memory>
+#include <sstream>
#include <string>
#include <utility>
#include <vector>
@@ -32,6 +33,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread.h"
@@ -645,6 +647,43 @@
#endif
}
+base::FilePath GetMimeTypesRegistrationFilename(
+ const base::FilePath& profile_path,
+ const web_app::AppId& app_id) {
+ DCHECK(!profile_path.empty() && !app_id.empty());
+
+ // Use a prefix to clearly group files created by Chrome.
+ std::string filename = base::StringPrintf(
+ "%s-%s-%s%s", chrome::kBrowserProcessExecutableName, app_id.c_str(),
+ profile_path.BaseName().value().c_str(), ".xml");
+
+ // Replace illegal characters and spaces in |filename|.
+ base::i18n::ReplaceIllegalCharactersInPath(&filename, '_');
+ base::ReplaceChars(filename, " ", "_", &filename);
+
+ return base::FilePath(filename);
+}
+
+std::string GetMimeTypesRegistrationFileContents(
+ const apps::FileHandlers& file_handlers) {
+ std::stringstream ss;
+ ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<mime-info "
+ "xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n";
+
+ for (const auto& file_handler : file_handlers) {
+ for (const auto& accept_entry : file_handler.accept) {
+ ss << " <mime-type type=\"" << accept_entry.mime_type + "\">\n";
+ for (const auto& file_extension : accept_entry.file_extensions)
+ ss << " <glob pattern=\"*" << file_extension << "\"/>\n";
+ ss << " </mime-type>\n";
+ }
+ }
+
+ ss << "</mime-info>\n";
+ return ss.str();
+}
+
} // namespace shell_integration_linux
namespace shell_integration {