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