diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-09 04:40:17 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-09 04:40:17 +0000 |
commit | 2f0f100b03ff0921362c75f9d8f61a44a45893c4 (patch) | |
tree | 1ba5674bef89d5a9a05538a8df4f878a9b8b893d /net | |
parent | 2984f1ca64a63ed179348f6a88133e06c117dd8b (diff) | |
download | chromium_src-2f0f100b03ff0921362c75f9d8f61a44a45893c4.zip chromium_src-2f0f100b03ff0921362c75f9d8f61a44a45893c4.tar.gz chromium_src-2f0f100b03ff0921362c75f9d8f61a44a45893c4.tar.bz2 |
Refactor mime_util into a singleton.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@626 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/SConscript | 2 | ||||
-rw-r--r-- | net/base/mime_util.cc | 150 | ||||
-rw-r--r-- | net/base/platform_mime_util.h | 53 | ||||
-rw-r--r-- | net/base/platform_mime_util_win.cc (renamed from net/base/mime_util_win.cc) | 13 | ||||
-rw-r--r-- | net/build/net.vcproj | 12 |
5 files changed, 175 insertions, 55 deletions
diff --git a/net/SConscript b/net/SConscript index 7a7fb14..e461568 100644 --- a/net/SConscript +++ b/net/SConscript @@ -73,10 +73,10 @@ input_files = [ 'base/listen_socket.cc',
'base/mime_sniffer.cc',
'base/mime_util.cc',
- 'base/mime_util_win.cc',
'base/net_errors.cc',
'base/net_module.cc',
'base/net_util.cc',
+ 'base/platform_mime_util_win.cc',
'base/registry_controlled_domain.cc',
'base/ssl_client_socket.cc',
'base/ssl_config_service.cc',
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc index 1277957..0221fc8 100644 --- a/net/base/mime_util.cc +++ b/net/base/mime_util.cc @@ -31,7 +31,10 @@ #include <string.h> #include "net/base/mime_util.h" +#include "net/base/platform_mime_util.h" + #include "base/logging.h" +#include "base/singleton.h" #include "base/string_util.h" using std::string; @@ -39,10 +42,41 @@ using std::wstring; namespace net { -// Helper used by GetMimeTypeFromExtension() to lookup the -// platform specific mappings. Defined in mime_util_{win,mac}.cc -bool GetPlatformMimeTypeFromExtension(const std::wstring& ext, - std::string* mime_type); +// Singleton utility class for mime types. +class MimeUtil : public PlatformMimeUtil { + public: + bool GetMimeTypeFromExtension(const std::wstring& ext, + std::string* mime_type) const; + + bool GetMimeTypeFromFile(const std::wstring& file_path, + std::string* mime_type) const; + + bool IsSupportedImageMimeType(const char* mime_type) const; + bool IsSupportedNonImageMimeType(const char* mime_type) const; + bool IsSupportedJavascriptMimeType(const char* mime_type) const; + + bool IsViewSourceMimeType(const char* mime_type) const; + + bool IsSupportedMimeType(const std::string& mime_type) const; + + bool MatchesMimeType(const std::string &mime_type_pattern, + const std::string &mime_type) const; + +private: + friend DefaultSingletonTraits<MimeUtil>; + MimeUtil() { + InitializeMimeTypeMaps(); + } + + // For faster lookup, keep hash sets. + void InitializeMimeTypeMaps(); + + typedef stdext::hash_set<std::string> MimeMappings; + MimeMappings image_map_; + MimeMappings non_image_map_; + MimeMappings javascript_map_; + MimeMappings view_source_map_; +}; // class MimeUtil struct MimeInfo { const char* mime_type; @@ -81,7 +115,8 @@ static const MimeInfo secondary_mappings[] = { { "application/x-shockwave-flash", "swf,swl" } }; -static const char* FindMimeType(const MimeInfo* mappings, size_t mappings_len, +static const char* FindMimeType(const MimeInfo* mappings, + size_t mappings_len, const char* ext) { size_t ext_len = strlen(ext); @@ -100,7 +135,8 @@ static const char* FindMimeType(const MimeInfo* mappings, size_t mappings_len, return NULL; } -bool GetMimeTypeFromExtension(const wstring& ext, string* result) { +bool MimeUtil::GetMimeTypeFromExtension(const wstring& 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 // overridden), and then if not found there, we defer to the system registry. @@ -130,7 +166,8 @@ bool GetMimeTypeFromExtension(const wstring& ext, string* result) { return false; } -bool GetMimeTypeFromFile(const wstring& file_path, string* result) { +bool MimeUtil::GetMimeTypeFromFile(const wstring& file_path, + string* result) const { wstring::size_type dot = file_path.find_last_of('.'); if (dot == wstring::npos) return false; @@ -192,60 +229,41 @@ static const char* view_source_types[] = { "image/svg+xml" }; -// For faster lookup -static stdext::hash_set<string>* image_map = NULL; -static stdext::hash_set<string>* non_image_map = NULL; -static stdext::hash_set<string>* javascript_map = NULL; -static stdext::hash_set<string>* view_source_map = NULL; - -static void InitializeMimeTypeMaps() { - image_map = new stdext::hash_set<string>; - non_image_map = new stdext::hash_set<string>; - javascript_map = new stdext::hash_set<string>; - view_source_map = new stdext::hash_set<string>; - +void MimeUtil::InitializeMimeTypeMaps() { for (int i = 0; i < arraysize(supported_image_types); ++i) - image_map->insert(supported_image_types[i]); + image_map_.insert(supported_image_types[i]); // Initialize the supported non-image types for (int i = 0; i < arraysize(supported_non_image_types); ++i) - non_image_map->insert(supported_non_image_types[i]); + non_image_map_.insert(supported_non_image_types[i]); for (int i = 0; i < arraysize(supported_javascript_types); ++i) - non_image_map->insert(supported_javascript_types[i]); + non_image_map_.insert(supported_javascript_types[i]); for (int i = 0; i < arraysize(supported_javascript_types); ++i) - javascript_map->insert(supported_javascript_types[i]); + javascript_map_.insert(supported_javascript_types[i]); for (int i = 0; i < arraysize(view_source_types); ++i) - view_source_map->insert(view_source_types[i]); + view_source_map_.insert(view_source_types[i]); } -bool IsSupportedImageMimeType(const char* mime_type) { - if (!image_map) - InitializeMimeTypeMaps(); - return image_map->find(mime_type) != image_map->end(); +bool MimeUtil::IsSupportedImageMimeType(const char* mime_type) const { + return image_map_.find(mime_type) != image_map_.end(); } -bool IsSupportedNonImageMimeType(const char* mime_type) { - if (!non_image_map) - InitializeMimeTypeMaps(); - return non_image_map->find(mime_type) != non_image_map->end(); +bool MimeUtil::IsSupportedNonImageMimeType(const char* mime_type) const { + return non_image_map_.find(mime_type) != non_image_map_.end(); } -bool IsSupportedJavascriptMimeType(const char* mime_type) { - if (!javascript_map) - InitializeMimeTypeMaps(); - return javascript_map->find(mime_type) != javascript_map->end(); +bool MimeUtil::IsSupportedJavascriptMimeType(const char* mime_type) const { + return javascript_map_.find(mime_type) != javascript_map_.end(); } -bool IsViewSourceMimeType(const char* mime_type) { - if (!view_source_map) - InitializeMimeTypeMaps(); - return view_source_map->find(mime_type) != view_source_map->end(); +bool MimeUtil::IsViewSourceMimeType(const char* mime_type) const { + return view_source_map_.find(mime_type) != view_source_map_.end(); } // Mirrors WebViewImpl::CanShowMIMEType() -bool IsSupportedMimeType(const std::string& mime_type) { +bool MimeUtil::IsSupportedMimeType(const std::string& mime_type) const { if (mime_type.compare(0, 5, "text/") == 0 || (mime_type.compare(0, 6, "image/") == 0 && IsSupportedImageMimeType(mime_type.c_str())) || @@ -254,8 +272,8 @@ bool IsSupportedMimeType(const std::string& mime_type) { return false; } -bool MatchesMimeType(const std::string &mime_type_pattern, - const std::string &mime_type) { +bool MimeUtil::MatchesMimeType(const std::string &mime_type_pattern, + const std::string &mime_type) const { // verify caller is passing lowercase DCHECK(mime_type_pattern == StringToLowerASCII(mime_type_pattern)); DCHECK(mime_type == StringToLowerASCII(mime_type)); @@ -291,4 +309,50 @@ bool MatchesMimeType(const std::string &mime_type_pattern, return true; } +//---------------------------------------------------------------------------- +// Wrappers for the singleton +//---------------------------------------------------------------------------- + +static MimeUtil* GetMimeUtil() { + return Singleton<MimeUtil>::get(); +} + +bool GetMimeTypeFromExtension(const std::wstring& ext, std::string* mime_type) { + return GetMimeUtil()->GetMimeTypeFromExtension(ext, mime_type); +} + +bool GetMimeTypeFromFile(const std::wstring& file_path, std::string* mime_type) { + return GetMimeUtil()->GetMimeTypeFromFile(file_path, mime_type); +} + +bool GetPreferredExtensionForMimeType(const std::string& mime_type, + std::wstring* extension) { + return GetMimeUtil()->GetPreferredExtensionForMimeType(mime_type, extension); +} + +bool IsSupportedImageMimeType(const char* mime_type) { + return GetMimeUtil()->IsSupportedImageMimeType(mime_type); +} + +bool IsSupportedNonImageMimeType(const char* mime_type) { + return GetMimeUtil()->IsSupportedNonImageMimeType(mime_type); +} + +bool IsSupportedJavascriptMimeType(const char* mime_type) { + return GetMimeUtil()->IsSupportedJavascriptMimeType(mime_type); +} + +bool IsViewSourceMimeType(const char* mime_type) { + return GetMimeUtil()->IsViewSourceMimeType(mime_type); +} + +bool IsSupportedMimeType(const std::string& mime_type) { + return GetMimeUtil()->IsSupportedMimeType(mime_type); +} + +bool MatchesMimeType(const std::string &mime_type_pattern, + const std::string &mime_type) { + return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); +} + } // namespace net diff --git a/net/base/platform_mime_util.h b/net/base/platform_mime_util.h new file mode 100644 index 0000000..c045557 --- /dev/null +++ b/net/base/platform_mime_util.h @@ -0,0 +1,53 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef NET_BASE_PLATFORM_MIME_UTIL_H_ +#define NET_BASE_PLATFORM_MIME_UTIL_H_ + +#include <string> + +namespace net { + +// Encapsulates the platform-specific functionality in mime_util +class PlatformMimeUtil { + public: + // See documentation for base::GetPreferredExtensionForMimeType [mime_util.h] + bool GetPreferredExtensionForMimeType(const std::string& mime_type, + std::wstring* 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, + std::string* mime_type) const; +}; + +} // namespace net + +#endif // NET_BASE_PLATFORM_MIME_UTIL_H_ diff --git a/net/base/mime_util_win.cc b/net/base/platform_mime_util_win.cc index dae5238..325c4e96 100644 --- a/net/base/mime_util_win.cc +++ b/net/base/platform_mime_util_win.cc @@ -29,15 +29,15 @@ #include <string.h> +#include "net/base/platform_mime_util.h" + #include "base/registry.h" #include "base/string_util.h" namespace net { -// Helper used by GetMimeTypeFromExtension() to lookup the -// platform specific mappings. Declared in mime_util.cc -bool GetPlatformMimeTypeFromExtension(const std::wstring& ext, - std::string* result) { +bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( + const std::wstring& 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; @@ -46,12 +46,11 @@ bool GetPlatformMimeTypeFromExtension(const std::wstring& ext, *result = WideToUTF8(value); return true; } - return false; } -bool GetPreferredExtensionForMimeType(const std::string& mime_type, - std::wstring* ext) { +bool PlatformMimeUtil::GetPreferredExtensionForMimeType( + const std::string& mime_type, std::wstring* ext) const { std::wstring key(L"MIME\\Database\\Content Type\\" + UTF8ToWide(mime_type)); return RegKey(HKEY_CLASSES_ROOT, key.c_str()).ReadValue(L"Extension", ext); } diff --git a/net/build/net.vcproj b/net/build/net.vcproj index b605946..4fe72e1 100644 --- a/net/build/net.vcproj +++ b/net/build/net.vcproj @@ -325,10 +325,6 @@ > </File> <File - RelativePath="..\base\mime_util_win.cc" - > - </File> - <File RelativePath="..\base\mime_util.h" > </File> @@ -389,6 +385,14 @@ > </File> <File + RelativePath="..\base\platform_mime_util.h" + > + </File> + <File + RelativePath="..\base\platform_mime_util_win.cc" + > + </File> + <File RelativePath="..\base\registry_controlled_domain.cc" > </File> |