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
14using WebKit::WebString;
15using WebKit::WebMimeRegistry;
16
17namespace {
18
19// Convert a WebString to ASCII, falling back on an empty string in the case
20// of a non-ASCII string.
21std::string ToASCIIOrEmpty(const WebString& string) {
22  if (!IsStringASCII(string))
23    return std::string();
24  return UTF16ToASCII(string);
25}
26
27}  // namespace
28
29namespace webkit_glue {
30
31WebMimeRegistry::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
38WebMimeRegistry::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
45WebMimeRegistry::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
52WebMimeRegistry::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
87WebMimeRegistry::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
94WebString 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
102WebString 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
110WebString 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