Commit c8297369 authored by yawano's avatar yawano Committed by Commit bot

Implement ChromeOS mime type extension mappings.

Previously ChromeOS used same implementation with linux which returns mime type by reading mimetype-extension database in the OS.
However it doesn't worked since ChromeOS doesn't have it.
This CL implements ChromeOS specific logic which returns mime type based on hard-coded list.

BUG=472017, 477913, 488378
TEST=net_unittests:MimeUtilTest.ExtensionTest

Review URL: https://codereview.chromium.org/1134393002

Cr-Commit-Position: refs/heads/master@{#330482}
parent c49e7c55
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/mime_extension_chromeos.h"
#include "base/logging.h"
#include "net/base/mime_util.h"
namespace net {
namespace chromeos {
namespace {
static const net::MimeInfo mimetype_extension_mapping[] = {
{"application/epub+zip", "epub"},
{"application/zip", "zip"},
{"text/calendar", "ics"},
};
} // namespace
// On linux, chrome uses xdgmime to read extension-mimetype database (e.g.
// /usr/share/mime) and estimate mime type from extension. However ChromeOS does
// not have such database in it, we use |mimetype_extension_mapping| to resolve
// mime type on ChromeOS.
bool GetPlatformMimeTypeFromExtension(const base::FilePath::StringType& ext,
std::string* mime_type) {
base::FilePath path_ext(ext);
const std::string ext_narrow_str = path_ext.AsUTF8Unsafe();
const char* result = net::FindMimeType(mimetype_extension_mapping,
arraysize(mimetype_extension_mapping),
ext_narrow_str.c_str());
if (result) {
*mime_type = result;
return true;
}
return false;
}
} // namespace chromeos
} // namespace net
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_BASE_MIME_EXTENSION_CHROMEOS_H_
#define NET_BASE_MIME_EXTENSION_CHROMEOS_H_
#include <string>
#include "base/files/file_path.h"
namespace net {
namespace chromeos {
bool GetPlatformMimeTypeFromExtension(const base::FilePath::StringType& ext,
std::string* mime_type);
} // namespace chromeos
} // namespace net
#endif // NET_BASE_MIME_EXTENSION_CHROMEOS_H_
......@@ -59,11 +59,6 @@ class MimeUtil : public PlatformMimeUtil {
static base::LazyInstance<MimeUtil>::Leaky g_mime_util =
LAZY_INSTANCE_INITIALIZER;
struct MimeInfo {
const char* const mime_type;
const char* const extensions; // comma separated list
};
static const MimeInfo primary_mappings[] = {
{ "text/html", "html,htm,shtml,shtm" },
{ "text/css", "css" },
......@@ -111,12 +106,11 @@ static const MimeInfo secondary_mappings[] = {
{ "application/pkcs7-mime", "p7m,p7c,p7z" },
{ "application/pkcs7-signature", "p7s" },
{ "application/x-mpegurl", "m3u8" },
{ "application/epub+zip", "epub" },
};
static const char* FindMimeType(const MimeInfo* mappings,
size_t mappings_len,
const char* ext) {
const char* FindMimeType(const MimeInfo* mappings,
size_t mappings_len,
const char* ext) {
size_t ext_len = strlen(ext);
for (size_t i = 0; i < mappings_len; ++i) {
......@@ -163,6 +157,12 @@ bool MimeUtil::GetMimeTypeFromExtensionHelper(
if (ext.length() > kMaxFilePathSize)
return false;
// Reject a string which contains null character.
base::FilePath::StringType::size_type nul_pos =
ext.find(FILE_PATH_LITERAL('\0'));
if (nul_pos != base::FilePath::StringType::npos)
return false;
// We implement the same algorithm as Mozilla for mapping a file extension to
// a mime type. That is, we first check a hard-coded list (that cannot be
// overridden), and then if not found there, we defer to the system registry.
......
......@@ -110,6 +110,16 @@ NET_EXPORT void AddMultipartFinalDelimiterForUpload(
const std::string& mime_boundary,
std::string* post_data);
struct MimeInfo {
const char* const mime_type;
const char* const extensions; // comma separated list
};
// Finds mime type of |ext| from |mappings|.
const char* FindMimeType(const MimeInfo* mappings,
size_t mappings_len,
const char* ext);
} // namespace net
#endif // NET_BASE_MIME_UTIL_H__
......@@ -12,8 +12,14 @@
namespace net {
TEST(MimeUtilTest, ExtensionTest) {
// String: png\0css
base::FilePath::StringType containsNullByte;
containsNullByte.append(FILE_PATH_LITERAL("png"));
containsNullByte.append(1, FILE_PATH_LITERAL('\0'));
containsNullByte.append(FILE_PATH_LITERAL("css"));
const struct {
const base::FilePath::CharType* extension;
const base::FilePath::StringType extension;
const char* const mime_type;
bool valid;
} tests[] = {
......@@ -22,10 +28,16 @@ TEST(MimeUtilTest, ExtensionTest) {
{FILE_PATH_LITERAL("css"), "text/css", true},
{FILE_PATH_LITERAL("pjp"), "image/jpeg", true},
{FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true},
#if defined(OS_CHROMEOS)
// These two are test cases for testing platform mime types on Chrome OS.
{FILE_PATH_LITERAL("epub"), "application/epub+zip", true},
{FILE_PATH_LITERAL("ics"), "text/calendar", true},
#endif
#if defined(OS_ANDROID)
{FILE_PATH_LITERAL("m3u8"), "application/x-mpegurl", true},
#endif
{FILE_PATH_LITERAL("not an extension / for sure"), "", false},
{containsNullByte, "", false}
};
std::string mime_type;
......
......@@ -11,6 +11,8 @@
#if defined(OS_ANDROID)
#include "net/android/network_library.h"
#elif defined(OS_CHROMEOS)
#include "net/base/mime_extension_chromeos.h"
#else
#include "base/nix/mime_util_xdg.h"
#endif
......@@ -22,6 +24,12 @@ bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const base::FilePath::StringType& ext, std::string* result) const {
return android::GetMimeTypeFromExtension(ext, result);
}
#elif defined(OS_CHROMEOS)
bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const base::FilePath::StringType& ext,
std::string* result) const {
return chromeos::GetPlatformMimeTypeFromExtension(ext, result);
}
#else
bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const base::FilePath::StringType& ext, std::string* result) const {
......
......@@ -248,6 +248,8 @@
'base/load_flags_list.h',
'base/load_states.h',
'base/load_states_list.h',
'base/mime_extension_chromeos.cc',
'base/mime_extension_chromeos.h',
'base/mime_sniffer.cc',
'base/mime_sniffer.h',
'base/mime_util.cc',
......
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