Chromium Code Reviews| Index: chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.cc |
| diff --git a/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.cc b/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.cc |
| index 26c3db560f4e50625ccf73fa079cafbbdc6bb89f..09b46f9b5dd8da22d21997fa3de5fe89fffbe089 100644 |
| --- a/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.cc |
| +++ b/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.cc |
| @@ -4,16 +4,92 @@ |
| #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" |
| -#include <vector> |
| +#include <algorithm> |
| +#include <utility> |
| +#include "base/strings/string_split.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| #include "net/base/escape.h" |
| +#include "net/base/mime_util.h" |
| #include "storage/browser/fileapi/file_system_url.h" |
| #include "url/gurl.h" |
| namespace arc { |
| +namespace { |
| + |
| +struct MimeTypeToExtensions { |
| + const char* mime_type; |
| + const char* extensions; |
| +}; |
| + |
| +// The mapping from MIME types to file name extensions, taken from Android N. |
| +// See: frameworks/base/media/java/android/media/MediaFile.java |
| +constexpr MimeTypeToExtensions kAndroidMimeTypeMappings[] = { |
| + {"application/mspowerpoint", "ppt"}, |
| + {"application/msword", "doc"}, |
| + {"application/ogg", "ogg,oga"}, |
| + {"application/pdf", "pdf"}, |
| + {"application/vnd.apple.mpegurl", "m3u8"}, |
| + {"application/vnd.ms-excel", "xls"}, |
| + {"application/vnd.ms-wpl", "wpl"}, |
| + {"application/x-android-drm-fl", "fl"}, |
| + {"application/x-mpegurl", "m3u"}, |
| + {"application/zip", "zip"}, |
| + {"audio/aac", "aac"}, |
| + {"audio/aac-adts", "aac"}, |
| + {"audio/amr", "amr"}, |
| + {"audio/amr-wb", "awb"}, |
| + {"audio/flac", "flac"}, |
| + {"audio/imelody", "imy"}, |
| + {"audio/midi", "mid,midi,xmf,rtttl,rtx,ota,mxmf"}, |
| + {"audio/mp4", "m4a"}, |
| + {"audio/mpeg", "mp3,mpga"}, |
| + {"audio/mpegurl", "m3u8"}, |
| + {"audio/ogg", "ogg"}, |
| + {"audio/sp-midi", "smf"}, |
| + {"audio/x-matroska", "mka"}, |
| + {"audio/x-mpegurl", "m3u,m3u8"}, |
| + {"audio/x-ms-wma", "wma"}, |
| + {"audio/x-scpls", "pls"}, |
| + {"audio/x-wav", "wav"}, |
| + {"image/gif", "gif"}, |
| + {"image/jpeg", "jpg,jpeg"}, |
| + {"image/png", "png"}, |
| + {"image/vnd.wap.wbmp", "wbmp"}, |
| + {"image/webp", "webp"}, |
| + {"image/x-adobe-dng", "dng"}, |
| + {"image/x-canon-cr2", "cr2"}, |
| + {"image/x-fuji-raf", "raf"}, |
| + {"image/x-ms-bmp", "bmp"}, |
| + {"image/x-nikon-nef", "nef"}, |
| + {"image/x-nikon-nrw", "nrw"}, |
| + {"image/x-olympus-orf", "orf"}, |
| + {"image/x-panasonic-rw2", "rw2"}, |
| + {"image/x-pentax-pef", "pef"}, |
| + {"image/x-samsung-srw", "srw"}, |
| + {"image/x-sony-arw", "arw"}, |
| + {"text/html", "html,htm"}, |
| + {"text/plain", "txt"}, |
| + {"video/3gpp", "3gp,3gpp"}, |
| + {"video/3gpp2", "3g2,3gpp2"}, |
| + {"video/avi", "avi"}, |
| + {"video/mp2p", "mpg,mpeg"}, |
| + {"video/mp2ts", "ts"}, |
| + {"video/mp4", "mp4,m4v"}, |
| + {"video/mpeg", "mpg,mpeg"}, |
| + {"video/quicktime", "mov"}, |
| + {"video/webm", "webm"}, |
| + {"video/x-matroska", "mkv"}, |
| + {"video/x-ms-asf", "asf"}, |
| + {"video/x-ms-wmv", "wmv"}, |
| +}; |
| + |
| +constexpr char kApplicationOctetStreamMimeType[] = "application/octet-stream"; |
| + |
| +} // namespace |
| + |
| // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape. |
| // TODO(nya): Consider consolidating this function with EscapeFileSystemId() in |
| // chrome/browser/chromeos/file_system_provider/mount_path_util.cc. |
| @@ -99,4 +175,43 @@ GURL BuildDocumentUrl(const std::string& authority, |
| net::EscapeQueryParamValue(document_id, false /* use_plus */).c_str())); |
| } |
| +std::vector<base::FilePath::StringType> GetExtensionsForMimeTypeForArc( |
| + const std::string& mime_type) { |
| + std::vector<base::FilePath::StringType> extensions; |
| + |
| + // net::GetExtensionsForMimeType() returns unwanted extensions like |
| + // "exe,com,bin" for application/octet-stream. |
| + if (net::MatchesMimeType(kApplicationOctetStreamMimeType, mime_type)) |
| + return extensions; |
| + |
| + // Attempt net::GetExtensionsForMimeType(). |
| + net::GetExtensionsForMimeType(mime_type, &extensions); |
| + if (!extensions.empty()) { |
| + base::FilePath::StringType preferred_extension; |
| + if (net::GetPreferredExtensionForMimeType(mime_type, |
| + &preferred_extension)) { |
| + auto iter = |
| + std::find(extensions.begin(), extensions.end(), preferred_extension); |
| + if (iter == extensions.end()) |
| + extensions.insert(extensions.begin(), preferred_extension); |
|
hidehiko
2017/01/27 07:39:44
Just curious: Is there a case this is needed?
Thi
Shuhei Takahashi
2017/01/27 09:17:14
I'm not sure if this happens, but I could not find
|
| + else |
| + std::swap(extensions.front(), *iter); |
| + } |
| + return extensions; |
| + } |
| + |
| + // Fallback to our hard-coded list. |
| + for (const auto& entry : kAndroidMimeTypeMappings) { |
| + if (net::MatchesMimeType(entry.mime_type, mime_type)) { |
| + for (const auto& extension : |
| + base::SplitStringPiece(entry.extensions, ",", base::TRIM_WHITESPACE, |
|
hidehiko
2017/01/27 07:39:44
As you assume base::FilePath::StringType is std::s
Shuhei Takahashi
2017/01/27 09:17:14
Thanks, I'll use base::SplitString instead.
|
| + base::SPLIT_WANT_ALL)) { |
| + extensions.push_back(extension.as_string()); |
| + } |
| + break; |
| + } |
| + } |
| + return extensions; |
| +} |
| + |
| } // namespace arc |