blob: a2b792dcbbb22c7217bb5ada278661e90c0c284a [file] [log] [blame]
[email protected]8d86fce2009-02-26 23:37:551// Copyright (c) 2009 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
[email protected]8d86fce2009-02-26 23:37:555#include "webkit/glue/simple_webmimeregistry_impl.h"
6
[email protected]8d86fce2009-02-26 23:37:557#include "base/string_util.h"
8#include "base/sys_string_conversions.h"
9#include "net/base/mime_util.h"
[email protected]afdcf5c2009-05-10 20:30:4110#include "webkit/api/public/WebString.h"
[email protected]8d86fce2009-02-26 23:37:5511#include "webkit/glue/glue_util.h"
[email protected]706f2dc2009-04-24 17:17:4512#include "webkit/glue/webkit_glue.h"
[email protected]8d86fce2009-02-26 23:37:5513
14using WebKit::WebString;
[email protected]df407ea2009-07-25 01:34:5715using WebKit::WebMimeRegistry;
[email protected]8d86fce2009-02-26 23:37:5516
[email protected]cf3c5e92009-08-03 17:27:0517namespace {
18
19// Convert a WebString to ASCII, falling back on an empty string in the case
20// of a non-ASCII string.
21std::string AsASCII(const WebString& string) {
22 if (!IsStringASCII(string))
23 return EmptyString();
24 return UTF16ToASCII(string);
25}
26
27} // namespace
28
[email protected]8d86fce2009-02-26 23:37:5529namespace webkit_glue {
30
[email protected]df407ea2009-07-25 01:34:5731WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsImageMIMEType(
[email protected]8d86fce2009-02-26 23:37:5532 const WebString& mime_type) {
[email protected]cf3c5e92009-08-03 17:27:0533 if (!net::IsSupportedImageMimeType(AsASCII(mime_type).c_str()))
[email protected]df407ea2009-07-25 01:34:5734 return WebMimeRegistry::IsNotSupported;
35 return WebMimeRegistry::IsSupported;
[email protected]8d86fce2009-02-26 23:37:5536}
37
[email protected]df407ea2009-07-25 01:34:5738WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType(
[email protected]8d86fce2009-02-26 23:37:5539 const WebString& mime_type) {
[email protected]cf3c5e92009-08-03 17:27:0540 if (!net::IsSupportedJavascriptMimeType(AsASCII(mime_type).c_str()))
[email protected]df407ea2009-07-25 01:34:5741 return WebMimeRegistry::IsNotSupported;
42 return WebMimeRegistry::IsSupported;
[email protected]8d86fce2009-02-26 23:37:5543}
44
[email protected]df407ea2009-07-25 01:34:5745WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType(
46 const WebString& mime_type, const WebString& codecs) {
47 // Not supporting the container is a flat-out no.
[email protected]cf3c5e92009-08-03 17:27:0548 if (!net::IsSupportedMediaMimeType(AsASCII(mime_type).c_str()))
[email protected]df407ea2009-07-25 01:34:5749 return IsNotSupported;
50
51 // If we don't recognize the codec, it's possible we support it.
52 std::vector<std::string> parsed_codecs;
[email protected]cf3c5e92009-08-03 17:27:0553 net::ParseCodecString(AsASCII(codecs).c_str(), &parsed_codecs);
[email protected]df407ea2009-07-25 01:34:5754 if (!net::AreSupportedMediaCodecs(parsed_codecs))
55 return MayBeSupported;
56
57 // Otherwise we have a perfect match.
58 return IsSupported;
[email protected]4a93d162009-05-29 20:34:1659}
60
[email protected]df407ea2009-07-25 01:34:5761WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsNonImageMIMEType(
[email protected]8d86fce2009-02-26 23:37:5562 const WebString& mime_type) {
[email protected]cf3c5e92009-08-03 17:27:0563 if (!net::IsSupportedNonImageMimeType(AsASCII(mime_type).c_str()))
[email protected]df407ea2009-07-25 01:34:5764 return WebMimeRegistry::IsNotSupported;
65 return WebMimeRegistry::IsSupported;
[email protected]8d86fce2009-02-26 23:37:5566}
67
68WebString SimpleWebMimeRegistryImpl::mimeTypeForExtension(
69 const WebString& file_extension) {
70 std::string mime_type;
71 net::GetMimeTypeFromExtension(
72 WebStringToFilePathString(file_extension), &mime_type);
73 return ASCIIToUTF16(mime_type);
74}
75
76WebString SimpleWebMimeRegistryImpl::mimeTypeFromFile(
77 const WebString& file_path) {
78 std::string mime_type;
79 net::GetMimeTypeFromFile(
80 FilePath(WebStringToFilePathString(file_path)), &mime_type);
81 return ASCIIToUTF16(mime_type);
82}
83
84WebString SimpleWebMimeRegistryImpl::preferredExtensionForMIMEType(
85 const WebString& mime_type) {
86 FilePath::StringType file_extension;
[email protected]cf3c5e92009-08-03 17:27:0587 net::GetPreferredExtensionForMimeType(AsASCII(mime_type),
[email protected]8d86fce2009-02-26 23:37:5588 &file_extension);
89 return FilePathStringToWebString(file_extension);
90}
91
92} // namespace webkit_glue