diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-14 01:20:41 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-14 01:20:41 +0000 |
commit | bae0ea1f95c5c3a02761d942c0802ec877c1a106 (patch) | |
tree | 456d3b2eb7cc4fc497870385223c88312db8a914 /net | |
parent | 8a5deb2ef0a4cc84b06a3466b32e94375110106c (diff) | |
download | chromium_src-bae0ea1f95c5c3a02761d942c0802ec877c1a106.zip chromium_src-bae0ea1f95c5c3a02761d942c0802ec877c1a106.tar.gz chromium_src-bae0ea1f95c5c3a02761d942c0802ec877c1a106.tar.bz2 |
Change mime type utils to operate on platform-specific string types for filenames/file extensions.
Review URL: http://codereview.chromium.org/21327
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9809 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/mime_util.cc | 34 | ||||
-rw-r--r-- | net/base/mime_util.h | 9 | ||||
-rw-r--r-- | net/base/mime_util_unittest.cc | 29 | ||||
-rw-r--r-- | net/base/platform_mime_util.h | 8 | ||||
-rw-r--r-- | net/base/platform_mime_util_linux.cc | 4 | ||||
-rw-r--r-- | net/base/platform_mime_util_mac.cc | 10 | ||||
-rw-r--r-- | net/base/platform_mime_util_win.cc | 4 | ||||
-rw-r--r-- | net/url_request/url_request_file_job.cc | 2 |
8 files changed, 54 insertions, 46 deletions
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc index 03d09ad..1528d3a 100644 --- a/net/base/mime_util.cc +++ b/net/base/mime_util.cc @@ -13,17 +13,16 @@ #include "base/string_util.h" using std::string; -using std::wstring; namespace net { // Singleton utility class for mime types. class MimeUtil : public PlatformMimeUtil { public: - bool GetMimeTypeFromExtension(const std::wstring& ext, + bool GetMimeTypeFromExtension(const FilePath::StringType& ext, std::string* mime_type) const; - bool GetMimeTypeFromFile(const std::wstring& file_path, + bool GetMimeTypeFromFile(const FilePath& file_path, std::string* mime_type) const; bool IsSupportedImageMimeType(const char* mime_type) const; @@ -111,7 +110,7 @@ static const char* FindMimeType(const MimeInfo* mappings, return NULL; } -bool MimeUtil::GetMimeTypeFromExtension(const wstring& ext, +bool MimeUtil::GetMimeTypeFromExtension(const FilePath::StringType& ext, string* result) const { // 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 @@ -119,11 +118,15 @@ bool MimeUtil::GetMimeTypeFromExtension(const wstring& ext, // Finally, we scan a secondary hard-coded list to catch types that we can // deduce but that we also want to allow the OS to override. - string ext_utf8 = WideToUTF8(ext); +#if defined(OS_WIN) + string ext_narrow_str = WideToUTF8(ext); +#elif defined(OS_POSIX) + const string& ext_narrow_str = ext; +#endif const char* mime_type; mime_type = FindMimeType(primary_mappings, arraysize(primary_mappings), - ext_utf8.c_str()); + ext_narrow_str.c_str()); if (mime_type) { *result = mime_type; return true; @@ -133,7 +136,7 @@ bool MimeUtil::GetMimeTypeFromExtension(const wstring& ext, return true; mime_type = FindMimeType(secondary_mappings, arraysize(secondary_mappings), - ext_utf8.c_str()); + ext_narrow_str.c_str()); if (mime_type) { *result = mime_type; return true; @@ -142,14 +145,12 @@ bool MimeUtil::GetMimeTypeFromExtension(const wstring& ext, return false; } -bool MimeUtil::GetMimeTypeFromFile(const wstring& file_path, +bool MimeUtil::GetMimeTypeFromFile(const FilePath& file_path, string* result) const { - // TODO(ericroman): this doesn't work properly with paths like - // /home/foo/.ssh/known_hosts - wstring::size_type dot = file_path.find_last_of('.'); - if (dot == wstring::npos) + FilePath::StringType file_name_str = file_path.Extension(); + if (file_name_str.empty()) return false; - return GetMimeTypeFromExtension(file_path.substr(dot + 1), result); + return GetMimeTypeFromExtension(file_name_str.substr(1), result); } // From WebKit's WebCore/platform/MIMETypeRegistry.cpp: @@ -292,16 +293,17 @@ static MimeUtil* GetMimeUtil() { return Singleton<MimeUtil>::get(); } -bool GetMimeTypeFromExtension(const std::wstring& ext, std::string* mime_type) { +bool GetMimeTypeFromExtension(const FilePath::StringType& ext, + std::string* mime_type) { return GetMimeUtil()->GetMimeTypeFromExtension(ext, mime_type); } -bool GetMimeTypeFromFile(const std::wstring& file_path, std::string* mime_type) { +bool GetMimeTypeFromFile(const FilePath& file_path, std::string* mime_type) { return GetMimeUtil()->GetMimeTypeFromFile(file_path, mime_type); } bool GetPreferredExtensionForMimeType(const std::string& mime_type, - std::wstring* extension) { + FilePath::StringType* extension) { return GetMimeUtil()->GetPreferredExtensionForMimeType(mime_type, extension); } diff --git a/net/base/mime_util.h b/net/base/mime_util.h index 7bfe1c3..d442e0f 100644 --- a/net/base/mime_util.h +++ b/net/base/mime_util.h @@ -7,21 +7,24 @@ #include <string> +#include "base/file_path.h" + namespace net { // Get the mime type (if any) that is associated with the given file extension. // Returns true if a corresponding mime type exists. -bool GetMimeTypeFromExtension(const std::wstring& ext, std::string* mime_type); +bool GetMimeTypeFromExtension(const FilePath::StringType& ext, + std::string* mime_type); // Get the mime type (if any) that is associated with the given file. Returns // true if a corresponding mime type exists. -bool GetMimeTypeFromFile(const std::wstring& file_path, std::string* mime_type); +bool GetMimeTypeFromFile(const FilePath& file_path, std::string* mime_type); // Get the preferred extension (if any) associated with the given mime type. // Returns true if a corresponding file extension exists. The extension is // returned without a prefixed dot, ex "html". bool GetPreferredExtensionForMimeType(const std::string& mime_type, - std::wstring* extension); + FilePath::StringType* extension); // Check to see if a particular MIME type is in our list. bool IsSupportedImageMimeType(const char* mime_type); diff --git a/net/base/mime_util_unittest.cc b/net/base/mime_util_unittest.cc index a49dfe3..bd7f820 100644 --- a/net/base/mime_util_unittest.cc +++ b/net/base/mime_util_unittest.cc @@ -13,15 +13,15 @@ namespace { TEST(MimeUtilTest, ExtensionTest) { const struct { - const wchar_t* extension; + const FilePath::CharType* extension; const char* mime_type; bool valid; } tests[] = { - { L"png", "image/png", true }, - { L"css", "text/css", true }, - { L"pjp", "image/jpeg", true }, - { L"pjpeg", "image/jpeg", true }, - { L"not an extension / for sure", "", false }, + { FILE_PATH_LITERAL("png"), "image/png", true }, + { FILE_PATH_LITERAL("css"), "text/css", true }, + { FILE_PATH_LITERAL("pjp"), "image/jpeg", true }, + { FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true }, + { FILE_PATH_LITERAL("not an extension / for sure"), "", false }, }; std::string mime_type; @@ -37,23 +37,24 @@ TEST(MimeUtilTest, ExtensionTest) { TEST(MimeUtilTest, FileTest) { const struct { - const wchar_t* file_path; + const FilePath::CharType* file_path; const char* mime_type; bool valid; } tests[] = { - { L"c:\\foo\\bar.css", "text/css", true }, - { L"c:\\blah", "", false }, - { L"/usr/local/bin/mplayer", "", false }, - { L"/home/foo/bar.css", "text/css", true }, - { L"/blah.", "", false }, - { L"c:\\blah.", "", false }, + { FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true }, + { FILE_PATH_LITERAL("c:\\blah"), "", false }, + { FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false }, + { FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true }, + { FILE_PATH_LITERAL("/blah."), "", false }, + { FILE_PATH_LITERAL("c:\\blah."), "", false }, }; std::string mime_type; bool rv; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { - rv = net::GetMimeTypeFromFile(tests[i].file_path, &mime_type); + rv = net::GetMimeTypeFromFile(FilePath(tests[i].file_path), + &mime_type); EXPECT_EQ(rv, tests[i].valid); if (rv) EXPECT_EQ(mime_type, tests[i].mime_type); diff --git a/net/base/platform_mime_util.h b/net/base/platform_mime_util.h index 6b90994..7be386a 100644 --- a/net/base/platform_mime_util.h +++ b/net/base/platform_mime_util.h @@ -7,6 +7,8 @@ #include <string> +#include "base/file_path.h" + namespace net { // Encapsulates the platform-specific functionality in mime_util @@ -14,12 +16,12 @@ class PlatformMimeUtil { public: // See documentation for base::GetPreferredExtensionForMimeType [mime_util.h] bool GetPreferredExtensionForMimeType(const std::string& mime_type, - std::wstring* extension) const; + FilePath::StringType* extension) const; protected: - + // Get the mime type (if any) that is associated with the file extension. // Returns true if a corresponding mime type exists. - bool GetPlatformMimeTypeFromExtension(const std::wstring& ext, + bool GetPlatformMimeTypeFromExtension(const FilePath::StringType& ext, std::string* mime_type) const; }; diff --git a/net/base/platform_mime_util_linux.cc b/net/base/platform_mime_util_linux.cc index 1d7dccc..874f6af 100644 --- a/net/base/platform_mime_util_linux.cc +++ b/net/base/platform_mime_util_linux.cc @@ -9,7 +9,7 @@ namespace net { bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( - const std::wstring& ext, std::string* result) const { + const FilePath::StringType& ext, std::string* result) const { // The correct thing to do is to interact somehow with the freedesktop shared // mime info cache. Since Linux (and other traditional *IX systems) don't use // file extensions; they use mime types and have multiple ways of detecting @@ -24,7 +24,7 @@ bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( } bool PlatformMimeUtil::GetPreferredExtensionForMimeType( - const std::string& mime_type, std::wstring* ext) const { + const std::string& mime_type, FilePath::StringType* ext) const { // Unlike GetPlatformMimeTypeFromExtension, this method doesn't have a // default list that it uses, but for now we are also returning false since // this doesn't really matter as much under Linux. diff --git a/net/base/platform_mime_util_mac.cc b/net/base/platform_mime_util_mac.cc index 221cf51..bf1cdee 100644 --- a/net/base/platform_mime_util_mac.cc +++ b/net/base/platform_mime_util_mac.cc @@ -12,11 +12,11 @@ namespace net { bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( - const std::wstring& ext, std::string* result) const { - std::wstring ext_nodot = ext; + const FilePath::StringType& ext, std::string* result) const { + std::string ext_nodot = ext; if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.') ext_nodot.erase(ext_nodot.begin()); - scoped_cftyperef<CFStringRef> ext_ref(base::SysWideToCFStringRef(ext_nodot)); + scoped_cftyperef<CFStringRef> ext_ref(base::SysUTF8ToCFStringRef(ext_nodot)); if (!ext_ref) return false; scoped_cftyperef<CFStringRef> uti( @@ -35,7 +35,7 @@ bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( } bool PlatformMimeUtil::GetPreferredExtensionForMimeType( - const std::string& mime_type, std::wstring* ext) const { + const std::string& mime_type, FilePath::StringType* ext) const { scoped_cftyperef<CFStringRef> mime_ref(base::SysUTF8ToCFStringRef(mime_type)); if (!mime_ref) return false; @@ -55,7 +55,7 @@ bool PlatformMimeUtil::GetPreferredExtensionForMimeType( CFSTR(".%@"), ext_ref.get())); - *ext = base::SysCFStringRefToWide(ext_ref); + *ext = base::SysCFStringRefToUTF8(ext_ref); return true; } diff --git a/net/base/platform_mime_util_win.cc b/net/base/platform_mime_util_win.cc index 53600cf..b6946fd 100644 --- a/net/base/platform_mime_util_win.cc +++ b/net/base/platform_mime_util_win.cc @@ -12,7 +12,7 @@ namespace net { bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( - const std::wstring& ext, std::string* result) const { + const FilePath::StringType& ext, std::string* result) const { // check windows registry for file extension's mime type (registry key // names are not case-sensitive). std::wstring value, key = L"." + ext; @@ -25,7 +25,7 @@ bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( } bool PlatformMimeUtil::GetPreferredExtensionForMimeType( - const std::string& mime_type, std::wstring* ext) const { + const std::string& mime_type, FilePath::StringType* ext) const { std::wstring key(L"MIME\\Database\\Content Type\\" + UTF8ToWide(mime_type)); if (!RegKey(HKEY_CLASSES_ROOT, key.c_str()).ReadValue(L"Extension", ext)) return false; diff --git a/net/url_request/url_request_file_job.cc b/net/url_request/url_request_file_job.cc index 2cdd109..baf94a2 100644 --- a/net/url_request/url_request_file_job.cc +++ b/net/url_request/url_request_file_job.cc @@ -151,7 +151,7 @@ bool URLRequestFileJob::ReadRawData(net::IOBuffer* dest, int dest_size, bool URLRequestFileJob::GetMimeType(std::string* mime_type) { DCHECK(request_); - return net::GetMimeTypeFromFile(file_path_.ToWStringHack(), mime_type); + return net::GetMimeTypeFromFile(file_path_, mime_type); } void URLRequestFileJob::DidResolve( |