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 "content/child/simple_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
14 using blink::WebString;
15 using blink::WebMimeRegistry;
16
17 namespace content {
18
19 //static
ToASCIIOrEmpty(const WebString & string)20 std::string SimpleWebMimeRegistryImpl::ToASCIIOrEmpty(const WebString& string) {
21 return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
22 : std::string();
23 }
24
supportsMIMEType(const WebString & mime_type)25 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMIMEType(
26 const WebString& mime_type) {
27 return net::IsSupportedMimeType(ToASCIIOrEmpty(mime_type)) ?
28 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
29 }
30
supportsImageMIMEType(const WebString & mime_type)31 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsImageMIMEType(
32 const WebString& mime_type) {
33 return net::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type)) ?
34 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
35 }
36
37 WebMimeRegistry::SupportsType
supportsImagePrefixedMIMEType(const WebString & mime_type)38 SimpleWebMimeRegistryImpl::supportsImagePrefixedMIMEType(
39 const WebString& mime_type) {
40 std::string ascii_mime_type = ToASCIIOrEmpty(mime_type);
41 return (net::IsSupportedImageMimeType(ascii_mime_type) ||
42 (StartsWithASCII(ascii_mime_type, "image/", true) &&
43 net::IsSupportedNonImageMimeType(ascii_mime_type))) ?
44 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
45 }
46
47 WebMimeRegistry::SupportsType
supportsJavaScriptMIMEType(const WebString & mime_type)48 SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType(
49 const WebString& mime_type) {
50 return net::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type)) ?
51 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
52 }
53
54 // When debugging layout tests failures in the test shell,
55 // see TestShellWebMimeRegistryImpl.
supportsMediaMIMEType(const WebString & mime_type,const WebString & codecs,const WebString & key_system)56 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType(
57 const WebString& mime_type,
58 const WebString& codecs,
59 const WebString& key_system) {
60 // Media features are only supported at the content/renderer/ layer.
61 return IsNotSupported;
62 }
63
supportsMediaSourceMIMEType(const WebString & mime_type,const WebString & codecs)64 bool SimpleWebMimeRegistryImpl::supportsMediaSourceMIMEType(
65 const WebString& mime_type,
66 const WebString& codecs) {
67 // Media features are only supported at the content/renderer layer.
68 return false;
69 }
70
supportsEncryptedMediaMIMEType(const blink::WebString & key_system,const blink::WebString & mime_type,const blink::WebString & codecs)71 bool SimpleWebMimeRegistryImpl::supportsEncryptedMediaMIMEType(
72 const blink::WebString& key_system,
73 const blink::WebString& mime_type,
74 const blink::WebString& codecs) {
75 // Media features are only supported at the content/renderer layer.
76 return false;
77 }
78
79 WebMimeRegistry::SupportsType
supportsNonImageMIMEType(const WebString & mime_type)80 SimpleWebMimeRegistryImpl::supportsNonImageMIMEType(
81 const WebString& mime_type) {
82 return net::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type)) ?
83 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
84 }
85
mimeTypeForExtension(const WebString & file_extension)86 WebString SimpleWebMimeRegistryImpl::mimeTypeForExtension(
87 const WebString& file_extension) {
88 std::string mime_type;
89 net::GetMimeTypeFromExtension(
90 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
91 return WebString::fromUTF8(mime_type);
92 }
93
wellKnownMimeTypeForExtension(const WebString & file_extension)94 WebString SimpleWebMimeRegistryImpl::wellKnownMimeTypeForExtension(
95 const WebString& file_extension) {
96 std::string mime_type;
97 net::GetWellKnownMimeTypeFromExtension(
98 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
99 return WebString::fromUTF8(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(base::FilePath::FromUTF16Unsafe(file_path),
106 &mime_type);
107 return WebString::fromUTF8(mime_type);
108 }
109
110 } // namespace content
111