• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 
5 #include "webkit/glue/simple_webmimeregistry_impl.h"
6 
7 #include "base/string_util.h"
8 #include "base/sys_string_conversions.h"
9 #include "base/utf_string_conversions.h"
10 #include "net/base/mime_util.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
12 #include "webkit/glue/webkit_glue.h"
13 
14 using WebKit::WebString;
15 using WebKit::WebMimeRegistry;
16 
17 namespace {
18 
19 // Convert a WebString to ASCII, falling back on an empty string in the case
20 // of a non-ASCII string.
ToASCIIOrEmpty(const WebString & string)21 std::string ToASCIIOrEmpty(const WebString& string) {
22   if (!IsStringASCII(string))
23     return std::string();
24   return UTF16ToASCII(string);
25 }
26 
27 }  // namespace
28 
29 namespace webkit_glue {
30 
supportsMIMEType(const WebString & mime_type)31 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMIMEType(
32     const WebString& mime_type) {
33   if (!net::IsSupportedMimeType(ToASCIIOrEmpty(mime_type).c_str()))
34     return WebMimeRegistry::IsNotSupported;
35   return WebMimeRegistry::IsSupported;
36 }
37 
supportsImageMIMEType(const WebString & mime_type)38 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsImageMIMEType(
39     const WebString& mime_type) {
40   if (!net::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type).c_str()))
41     return WebMimeRegistry::IsNotSupported;
42   return WebMimeRegistry::IsSupported;
43 }
44 
supportsJavaScriptMIMEType(const WebString & mime_type)45 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType(
46     const WebString& mime_type) {
47   if (!net::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type).c_str()))
48     return WebMimeRegistry::IsNotSupported;
49   return WebMimeRegistry::IsSupported;
50 }
51 
supportsMediaMIMEType(const WebString & mime_type,const WebString & codecs)52 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType(
53     const WebString& mime_type, const WebString& codecs) {
54   // Not supporting the container is a flat-out no.
55   if (!net::IsSupportedMediaMimeType(ToASCIIOrEmpty(mime_type).c_str()))
56     return IsNotSupported;
57 
58   // Check list of strict codecs to see if it is supported.
59   if (net::IsStrictMediaMimeType(ToASCIIOrEmpty(mime_type).c_str())) {
60     // We support the container, but no codecs were specified.
61     if (codecs.isNull())
62       return MayBeSupported;
63 
64     // Check if the codecs are a perfect match.
65     std::vector<std::string> strict_codecs;
66     net::ParseCodecString(ToASCIIOrEmpty(codecs).c_str(),
67                           &strict_codecs,
68                           false);
69     if (!net::IsSupportedStrictMediaMimeType(ToASCIIOrEmpty(mime_type).c_str(),
70                                              strict_codecs))
71       return IsNotSupported;
72 
73     // Good to go!
74     return IsSupported;
75   }
76 
77   // If we don't recognize the codec, it's possible we support it.
78   std::vector<std::string> parsed_codecs;
79   net::ParseCodecString(ToASCIIOrEmpty(codecs).c_str(), &parsed_codecs, true);
80   if (!net::AreSupportedMediaCodecs(parsed_codecs))
81     return MayBeSupported;
82 
83   // Otherwise we have a perfect match.
84   return IsSupported;
85 }
86 
supportsNonImageMIMEType(const WebString & mime_type)87 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsNonImageMIMEType(
88     const WebString& mime_type) {
89   if (!net::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type).c_str()))
90     return WebMimeRegistry::IsNotSupported;
91   return WebMimeRegistry::IsSupported;
92 }
93 
mimeTypeForExtension(const WebString & file_extension)94 WebString SimpleWebMimeRegistryImpl::mimeTypeForExtension(
95     const WebString& file_extension) {
96   std::string mime_type;
97   net::GetMimeTypeFromExtension(
98       WebStringToFilePathString(file_extension), &mime_type);
99   return ASCIIToUTF16(mime_type);
100 }
101 
mimeTypeFromFile(const WebString & file_path)102 WebString SimpleWebMimeRegistryImpl::mimeTypeFromFile(
103     const WebString& file_path) {
104   std::string mime_type;
105   net::GetMimeTypeFromFile(
106       FilePath(WebStringToFilePathString(file_path)), &mime_type);
107   return ASCIIToUTF16(mime_type);
108 }
109 
preferredExtensionForMIMEType(const WebString & mime_type)110 WebString SimpleWebMimeRegistryImpl::preferredExtensionForMIMEType(
111     const WebString& mime_type) {
112   FilePath::StringType file_extension;
113   net::GetPreferredExtensionForMimeType(ToASCIIOrEmpty(mime_type),
114                                         &file_extension);
115   return FilePathStringToWebString(file_extension);
116 }
117 
118 }  // namespace webkit_glue
119