1// Copyright 2014 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 "mojo/services/html_viewer/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
14namespace mojo {
15namespace {
16
17std::string ToASCIIOrEmpty(const blink::WebString& string) {
18  return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
19                                     : std::string();
20}
21
22}  // namespace
23
24blink::WebMimeRegistry::SupportsType WebMimeRegistryImpl::supportsMIMEType(
25    const blink::WebString& mime_type) {
26  return net::IsSupportedMimeType(ToASCIIOrEmpty(mime_type)) ?
27      blink::WebMimeRegistry::IsSupported :
28      blink::WebMimeRegistry::IsNotSupported;
29}
30
31blink::WebMimeRegistry::SupportsType WebMimeRegistryImpl::supportsImageMIMEType(
32    const blink::WebString& mime_type) {
33  return net::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type)) ?
34      blink::WebMimeRegistry::IsSupported :
35      blink::WebMimeRegistry::IsNotSupported;
36}
37
38blink::WebMimeRegistry::SupportsType
39WebMimeRegistryImpl::supportsImagePrefixedMIMEType(
40    const blink::WebString& mime_type) {
41  std::string ascii_mime_type = ToASCIIOrEmpty(mime_type);
42  return (net::IsSupportedImageMimeType(ascii_mime_type) ||
43          (StartsWithASCII(ascii_mime_type, "image/", true) &&
44           net::IsSupportedNonImageMimeType(ascii_mime_type)))
45             ? WebMimeRegistry::IsSupported
46             : WebMimeRegistry::IsNotSupported;
47}
48
49blink::WebMimeRegistry::SupportsType
50    WebMimeRegistryImpl::supportsJavaScriptMIMEType(
51    const blink::WebString& mime_type) {
52  return net::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type)) ?
53      blink::WebMimeRegistry::IsSupported :
54      blink::WebMimeRegistry::IsNotSupported;
55}
56
57blink::WebMimeRegistry::SupportsType WebMimeRegistryImpl::supportsMediaMIMEType(
58    const blink::WebString& mime_type,
59    const blink::WebString& codecs,
60    const blink::WebString& key_system) {
61  const std::string mime_type_ascii = ToASCIIOrEmpty(mime_type);
62  // Not supporting the container is a flat-out no.
63  if (!net::IsSupportedMediaMimeType(mime_type_ascii))
64    return IsNotSupported;
65
66  // Mojo does not currently support any key systems.
67  if (!key_system.isEmpty())
68    return IsNotSupported;
69
70  // Check list of strict codecs to see if it is supported.
71  if (net::IsStrictMediaMimeType(mime_type_ascii)) {
72    // Check if the codecs are a perfect match.
73    std::vector<std::string> strict_codecs;
74    net::ParseCodecString(ToASCIIOrEmpty(codecs), &strict_codecs, false);
75    return static_cast<WebMimeRegistry::SupportsType>(
76        net::IsSupportedStrictMediaMimeType(mime_type_ascii, strict_codecs));
77  }
78
79  // If we don't recognize the codec, it's possible we support it.
80  std::vector<std::string> parsed_codecs;
81  net::ParseCodecString(ToASCIIOrEmpty(codecs), &parsed_codecs, true);
82  if (!net::AreSupportedMediaCodecs(parsed_codecs))
83    return MayBeSupported;
84
85  // Otherwise we have a perfect match.
86  return IsSupported;
87}
88
89bool WebMimeRegistryImpl::supportsMediaSourceMIMEType(
90    const blink::WebString& mime_type,
91    const blink::WebString& codecs) {
92  NOTIMPLEMENTED();
93  return false;
94}
95
96bool WebMimeRegistryImpl::supportsEncryptedMediaMIMEType(
97    const blink::WebString& key_system,
98    const blink::WebString& mime_type,
99    const blink::WebString& codecs) {
100  NOTIMPLEMENTED();
101  return false;
102}
103
104blink::WebMimeRegistry::SupportsType
105    WebMimeRegistryImpl::supportsNonImageMIMEType(
106    const blink::WebString& mime_type) {
107  return net::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type)) ?
108      blink::WebMimeRegistry::IsSupported :
109      blink::WebMimeRegistry::IsNotSupported;
110}
111
112blink::WebString WebMimeRegistryImpl::mimeTypeForExtension(
113    const blink::WebString& file_extension) {
114  NOTIMPLEMENTED();
115  return blink::WebString();
116}
117
118blink::WebString WebMimeRegistryImpl::wellKnownMimeTypeForExtension(
119    const blink::WebString& file_extension) {
120  NOTIMPLEMENTED();
121  return blink::WebString();
122}
123
124blink::WebString WebMimeRegistryImpl::mimeTypeFromFile(
125    const blink::WebString& file_path) {
126  NOTIMPLEMENTED();
127  return blink::WebString();
128}
129
130}  // namespace mojo
131