1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "webkit/glue/simple_webmimeregistry_impl.h" 6 7 #include "base/files/file_path.h" 8 #include "base/strings/string_util.h" 9 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "net/base/mime_util.h" 12 #include "third_party/WebKit/public/platform/WebString.h" 13 14 using WebKit::WebString; 15 using WebKit::WebMimeRegistry; 16 17 namespace webkit_glue { 18 19 //static 20 std::string SimpleWebMimeRegistryImpl::ToASCIIOrEmpty(const WebString& string) { 21 return IsStringASCII(string) ? UTF16ToASCII(string) : std::string(); 22 } 23 24 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMIMEType( 25 const WebString& mime_type) { 26 return net::IsSupportedMimeType(ToASCIIOrEmpty(mime_type)) ? 27 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported; 28 } 29 30 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsImageMIMEType( 31 const WebString& mime_type) { 32 return net::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type)) ? 33 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported; 34 } 35 36 WebMimeRegistry::SupportsType 37 SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType( 38 const WebString& mime_type) { 39 return net::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type)) ? 40 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported; 41 } 42 43 // When debugging layout tests failures in the test shell, 44 // see TestShellWebMimeRegistryImpl. 45 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType( 46 const WebString& mime_type, const WebString& codecs) { 47 // Media features are only supported at the content/ layer. 48 return IsNotSupported; 49 } 50 51 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType( 52 const WebString& mime_type, 53 const WebString& codecs, 54 const WebString& key_system) { 55 // Media features are only supported at the content/ layer. 56 return IsNotSupported; 57 } 58 59 bool SimpleWebMimeRegistryImpl::supportsMediaSourceMIMEType( 60 const WebString& mime_type, 61 const WebString& codecs) { 62 // Media features are only supported at the content/ layer. 63 return IsNotSupported; 64 } 65 66 WebMimeRegistry::SupportsType 67 SimpleWebMimeRegistryImpl::supportsNonImageMIMEType( 68 const WebString& mime_type) { 69 return net::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type)) ? 70 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported; 71 } 72 73 WebString SimpleWebMimeRegistryImpl::mimeTypeForExtension( 74 const WebString& file_extension) { 75 std::string mime_type; 76 net::GetMimeTypeFromExtension( 77 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type); 78 return WebString::fromUTF8(mime_type); 79 } 80 81 WebString SimpleWebMimeRegistryImpl::wellKnownMimeTypeForExtension( 82 const WebString& file_extension) { 83 std::string mime_type; 84 net::GetWellKnownMimeTypeFromExtension( 85 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type); 86 return WebString::fromUTF8(mime_type); 87 } 88 89 WebString SimpleWebMimeRegistryImpl::mimeTypeFromFile( 90 const WebString& file_path) { 91 std::string mime_type; 92 net::GetMimeTypeFromFile(base::FilePath::FromUTF16Unsafe(file_path), 93 &mime_type); 94 return WebString::fromUTF8(mime_type); 95 } 96 97 WebString SimpleWebMimeRegistryImpl::preferredExtensionForMIMEType( 98 const WebString& mime_type) { 99 base::FilePath::StringType file_extension; 100 net::GetPreferredExtensionForMimeType(ToASCIIOrEmpty(mime_type), 101 &file_extension); 102 return base::FilePath(file_extension).AsUTF16Unsafe(); 103 } 104 105 } // namespace webkit_glue 106