[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
2 | // source code is governed by a BSD-style license that can be found in the | ||||
3 | // LICENSE file. | ||||
4 | |||||
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 5 | #include "webkit/glue/simple_webmimeregistry_impl.h" |
6 | |||||
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 7 | #include "base/string_util.h" |
8 | #include "base/sys_string_conversions.h" | ||||
9 | #include "net/base/mime_util.h" | ||||
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 10 | #include "webkit/api/public/WebString.h" |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 11 | #include "webkit/glue/glue_util.h" |
[email protected] | 706f2dc | 2009-04-24 17:17:45 | [diff] [blame] | 12 | #include "webkit/glue/webkit_glue.h" |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 13 | |
14 | using WebKit::WebString; | ||||
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 15 | using WebKit::WebMimeRegistry; |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 16 | |
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 17 | namespace { |
18 | |||||
19 | // Convert a WebString to ASCII, falling back on an empty string in the case | ||||
20 | // of a non-ASCII string. | ||||
21 | std::string AsASCII(const WebString& string) { | ||||
22 | if (!IsStringASCII(string)) | ||||
23 | return EmptyString(); | ||||
24 | return UTF16ToASCII(string); | ||||
25 | } | ||||
26 | |||||
27 | } // namespace | ||||
28 | |||||
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 29 | namespace webkit_glue { |
30 | |||||
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 31 | WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsImageMIMEType( |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 32 | const WebString& mime_type) { |
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 33 | if (!net::IsSupportedImageMimeType(AsASCII(mime_type).c_str())) |
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 34 | return WebMimeRegistry::IsNotSupported; |
35 | return WebMimeRegistry::IsSupported; | ||||
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 36 | } |
37 | |||||
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 38 | WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType( |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 39 | const WebString& mime_type) { |
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 40 | if (!net::IsSupportedJavascriptMimeType(AsASCII(mime_type).c_str())) |
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 41 | return WebMimeRegistry::IsNotSupported; |
42 | return WebMimeRegistry::IsSupported; | ||||
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 43 | } |
44 | |||||
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 45 | WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType( |
46 | const WebString& mime_type, const WebString& codecs) { | ||||
47 | // Not supporting the container is a flat-out no. | ||||
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 48 | if (!net::IsSupportedMediaMimeType(AsASCII(mime_type).c_str())) |
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 49 | return IsNotSupported; |
50 | |||||
51 | // If we don't recognize the codec, it's possible we support it. | ||||
52 | std::vector<std::string> parsed_codecs; | ||||
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 53 | net::ParseCodecString(AsASCII(codecs).c_str(), &parsed_codecs); |
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 54 | if (!net::AreSupportedMediaCodecs(parsed_codecs)) |
55 | return MayBeSupported; | ||||
56 | |||||
57 | // Otherwise we have a perfect match. | ||||
58 | return IsSupported; | ||||
[email protected] | 4a93d16 | 2009-05-29 20:34:16 | [diff] [blame] | 59 | } |
60 | |||||
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 61 | WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsNonImageMIMEType( |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 62 | const WebString& mime_type) { |
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 63 | if (!net::IsSupportedNonImageMimeType(AsASCII(mime_type).c_str())) |
[email protected] | df407ea | 2009-07-25 01:34:57 | [diff] [blame] | 64 | return WebMimeRegistry::IsNotSupported; |
65 | return WebMimeRegistry::IsSupported; | ||||
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 66 | } |
67 | |||||
68 | WebString SimpleWebMimeRegistryImpl::mimeTypeForExtension( | ||||
69 | const WebString& file_extension) { | ||||
70 | std::string mime_type; | ||||
71 | net::GetMimeTypeFromExtension( | ||||
72 | WebStringToFilePathString(file_extension), &mime_type); | ||||
73 | return ASCIIToUTF16(mime_type); | ||||
74 | } | ||||
75 | |||||
76 | WebString SimpleWebMimeRegistryImpl::mimeTypeFromFile( | ||||
77 | const WebString& file_path) { | ||||
78 | std::string mime_type; | ||||
79 | net::GetMimeTypeFromFile( | ||||
80 | FilePath(WebStringToFilePathString(file_path)), &mime_type); | ||||
81 | return ASCIIToUTF16(mime_type); | ||||
82 | } | ||||
83 | |||||
84 | WebString SimpleWebMimeRegistryImpl::preferredExtensionForMIMEType( | ||||
85 | const WebString& mime_type) { | ||||
86 | FilePath::StringType file_extension; | ||||
[email protected] | cf3c5e9 | 2009-08-03 17:27:05 | [diff] [blame] | 87 | net::GetPreferredExtensionForMimeType(AsASCII(mime_type), |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 88 | &file_extension); |
89 | return FilePathStringToWebString(file_extension); | ||||
90 | } | ||||
91 | |||||
92 | } // namespace webkit_glue |