Sniff MIME type for files which have unknown extensions.
What I want to do:
Open text files which have unknown extensions(e.g. access.log.1) using packaged apps which can handle 'text/plain'(e.g. Text, Caret,...).
Current situation:
By following reasons, text files with unknow extension can't be opened.
1. FileManager guess the MIME type as empty, so 'text/plain'-supporting apps are not shown as handlers.
2. PlatformAppLauncher guess the MIME type as 'application/octet-stream', and it launch apps with no data because those apps don't handle 'application/octet-stream'.
What I changed:
Modified FileManager and PlatformAppLauncher to sniff MIME types if they are unknown based on extensions.
BUG=352250
[email protected], [email protected], [email protected]
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=264167
Review URL: https://codereview.chromium.org/224883008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@265239 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/apps/launcher.cc b/apps/launcher.cc
index ba7c829..8d31a01 100644
--- a/apps/launcher.cc
+++ b/apps/launcher.cc
@@ -29,6 +29,8 @@
#include "extensions/common/extension.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/manifest_handlers/kiosk_mode_info.h"
+#include "net/base/filename_util.h"
+#include "net/base/mime_sniffer.h"
#include "net/base/mime_util.h"
#include "net/base/net_util.h"
#include "url/gurl.h"
@@ -181,8 +183,21 @@
}
std::string mime_type;
- if (!net::GetMimeTypeFromFile(file_path_, &mime_type))
- mime_type = kFallbackMimeType;
+ if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) {
+ // If MIME type of the file can't be determined by its path,
+ // try to sniff it by its content.
+ std::vector<char> content(net::kMaxBytesToSniff);
+ int bytes_read = base::ReadFile(file_path_, &content[0], content.size());
+ if (bytes_read >= 0) {
+ net::SniffMimeType(&content[0],
+ bytes_read,
+ net::FilePathToFileURL(file_path_),
+ std::string(), // type_hint (passes no hint)
+ &mime_type);
+ }
+ if (mime_type.empty())
+ mime_type = kFallbackMimeType;
+ }
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type));