Add a CefGetMimeType function for retrieving the mime type of a file extension (issue #1098).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1577 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2014-01-28 17:26:59 +00:00
parent 0c8b381a46
commit 2f8b024797
6 changed files with 119 additions and 49 deletions

View File

@ -60,6 +60,14 @@ CEF_EXPORT int cef_parse_url(const cef_string_t* url,
CEF_EXPORT int cef_create_url(const struct _cef_urlparts_t* parts,
cef_string_t* url);
///
// Returns the mime type for the specified file extension or an NULL string if
// unknown.
///
// The resulting string must be freed by calling cef_string_userfree_free().
CEF_EXPORT cef_string_userfree_t cef_get_mime_type(
const cef_string_t* extension);
#ifdef __cplusplus
}
#endif

View File

@ -57,4 +57,11 @@ bool CefParseURL(const CefString& url,
bool CefCreateURL(const CefURLParts& parts,
CefString& url);
///
// Returns the mime type for the specified file extension or an empty string if
// unknown.
///
/*--cef()--*/
CefString CefGetMimeType(const CefString& extension);
#endif // CEF_INCLUDE_CEF_URL_H_

View File

@ -4,6 +4,7 @@
#include <sstream>
#include "include/cef_url.h"
#include "net/base/mime_util.h"
#include "url/gurl.h"
bool CefParseURL(const CefString& url,
@ -66,3 +67,9 @@ bool CefCreateURL(const CefURLParts& parts,
return false;
}
CefString CefGetMimeType(const CefString& extension) {
std::string mime_type;
net::GetMimeTypeFromExtension(extension, &mime_type);
return mime_type;
}

View File

@ -570,6 +570,23 @@ CEF_EXPORT int cef_create_url(const struct _cef_urlparts_t* parts,
return _retval;
}
CEF_EXPORT cef_string_userfree_t cef_get_mime_type(
const cef_string_t* extension) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Verify param: extension; type: string_byref_const
DCHECK(extension);
if (!extension)
return NULL;
// Execute
CefString _retval = CefGetMimeType(
CefString(extension));
// Return type: string
return _retval.DetachToUserFree();
}
CEF_EXPORT int cef_register_extension(const cef_string_t* extension_name,
const cef_string_t* javascript_code, struct _cef_v8handler_t* handler) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING

View File

@ -522,6 +522,24 @@ CEF_GLOBAL bool CefCreateURL(const CefURLParts& parts, CefString& url) {
return _retval?true:false;
}
CEF_GLOBAL CefString CefGetMimeType(const CefString& extension) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Verify param: extension; type: string_byref_const
DCHECK(!extension.empty());
if (extension.empty())
return CefString();
// Execute
cef_string_userfree_t _retval = cef_get_mime_type(
extension.GetStruct());
// Return type: string
CefString _retvalStr;
_retvalStr.AttachToUserFree(_retval);
return _retvalStr;
}
CEF_GLOBAL bool CefRegisterExtension(const CefString& extension_name,
const CefString& javascript_code, CefRefPtr<CefV8Handler> handler) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING

View File

@ -12,8 +12,8 @@ TEST(URLTest, CreateURL) {
CefString url;
CefString(&parts.spec).FromASCII(
"http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
ASSERT_TRUE(CefCreateURL(parts, url));
ASSERT_EQ(url,
EXPECT_TRUE(CefCreateURL(parts, url));
EXPECT_EQ(url,
"http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
}
@ -22,13 +22,13 @@ TEST(URLTest, CreateURL) {
CefURLParts parts;
CefString url;
CefString(&parts.scheme).FromASCII("http");
ASSERT_FALSE(CefCreateURL(parts, url));
EXPECT_FALSE(CefCreateURL(parts, url));
}
{
CefURLParts parts;
CefString url;
CefString(&parts.host).FromASCII("www.example.com");
ASSERT_FALSE(CefCreateURL(parts, url));
EXPECT_FALSE(CefCreateURL(parts, url));
}
// Create the URL using scheme and host.
@ -37,8 +37,8 @@ TEST(URLTest, CreateURL) {
CefString url;
CefString(&parts.scheme).FromASCII("http");
CefString(&parts.host).FromASCII("www.example.com");
ASSERT_TRUE(CefCreateURL(parts, url));
ASSERT_EQ(url, "http://www.example.com/");
EXPECT_TRUE(CefCreateURL(parts, url));
EXPECT_EQ(url, "http://www.example.com/");
}
// Create the URL using scheme, host and path.
@ -48,8 +48,8 @@ TEST(URLTest, CreateURL) {
CefString(&parts.scheme).FromASCII("http");
CefString(&parts.host).FromASCII("www.example.com");
CefString(&parts.path).FromASCII("/path/to.html");
ASSERT_TRUE(CefCreateURL(parts, url));
ASSERT_EQ(url, "http://www.example.com/path/to.html");
EXPECT_TRUE(CefCreateURL(parts, url));
EXPECT_EQ(url, "http://www.example.com/path/to.html");
}
// Create the URL using scheme, host, path and query.
@ -60,8 +60,8 @@ TEST(URLTest, CreateURL) {
CefString(&parts.host).FromASCII("www.example.com");
CefString(&parts.path).FromASCII("/path/to.html");
CefString(&parts.query).FromASCII("foo=test&bar=test2");
ASSERT_TRUE(CefCreateURL(parts, url));
ASSERT_EQ(url, "http://www.example.com/path/to.html?foo=test&bar=test2");
EXPECT_TRUE(CefCreateURL(parts, url));
EXPECT_EQ(url, "http://www.example.com/path/to.html?foo=test&bar=test2");
}
// Create the URL using all the various components.
@ -75,8 +75,8 @@ TEST(URLTest, CreateURL) {
CefString(&parts.port).FromASCII("88");
CefString(&parts.path).FromASCII("/path/to.html");
CefString(&parts.query).FromASCII("foo=test&bar=test2");
ASSERT_TRUE(CefCreateURL(parts, url));
ASSERT_EQ(url,
EXPECT_TRUE(CefCreateURL(parts, url));
EXPECT_EQ(url,
"http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
}
}
@ -87,20 +87,20 @@ TEST(URLTest, ParseURL) {
CefURLParts parts;
CefString url;
url.FromASCII("http://www.example.com");
ASSERT_TRUE(CefParseURL(url, parts));
EXPECT_TRUE(CefParseURL(url, parts));
CefString spec(&parts.spec);
ASSERT_EQ(spec, "http://www.example.com/");
ASSERT_EQ(parts.username.length, (size_t)0);
ASSERT_EQ(parts.password.length, (size_t)0);
EXPECT_EQ(spec, "http://www.example.com/");
EXPECT_EQ(parts.username.length, (size_t)0);
EXPECT_EQ(parts.password.length, (size_t)0);
CefString scheme(&parts.scheme);
ASSERT_EQ(scheme, "http");
EXPECT_EQ(scheme, "http");
CefString host(&parts.host);
ASSERT_EQ(host, "www.example.com");
ASSERT_EQ(parts.port.length, (size_t)0);
EXPECT_EQ(host, "www.example.com");
EXPECT_EQ(parts.port.length, (size_t)0);
CefString path(&parts.path);
ASSERT_EQ(path, "/");
ASSERT_EQ(parts.query.length, (size_t)0);
EXPECT_EQ(path, "/");
EXPECT_EQ(parts.query.length, (size_t)0);
}
// Parse the URL using scheme, host and path.
@ -108,20 +108,20 @@ TEST(URLTest, ParseURL) {
CefURLParts parts;
CefString url;
url.FromASCII("http://www.example.com/path/to.html");
ASSERT_TRUE(CefParseURL(url, parts));
EXPECT_TRUE(CefParseURL(url, parts));
CefString spec(&parts.spec);
ASSERT_EQ(spec, "http://www.example.com/path/to.html");
ASSERT_EQ(parts.username.length, (size_t)0);
ASSERT_EQ(parts.password.length, (size_t)0);
EXPECT_EQ(spec, "http://www.example.com/path/to.html");
EXPECT_EQ(parts.username.length, (size_t)0);
EXPECT_EQ(parts.password.length, (size_t)0);
CefString scheme(&parts.scheme);
ASSERT_EQ(scheme, "http");
EXPECT_EQ(scheme, "http");
CefString host(&parts.host);
ASSERT_EQ(host, "www.example.com");
ASSERT_EQ(parts.port.length, (size_t)0);
EXPECT_EQ(host, "www.example.com");
EXPECT_EQ(parts.port.length, (size_t)0);
CefString path(&parts.path);
ASSERT_EQ(path, "/path/to.html");
ASSERT_EQ(parts.query.length, (size_t)0);
EXPECT_EQ(path, "/path/to.html");
EXPECT_EQ(parts.query.length, (size_t)0);
}
// Parse the URL using scheme, host, path and query.
@ -129,21 +129,21 @@ TEST(URLTest, ParseURL) {
CefURLParts parts;
CefString url;
url.FromASCII("http://www.example.com/path/to.html?foo=test&bar=test2");
ASSERT_TRUE(CefParseURL(url, parts));
EXPECT_TRUE(CefParseURL(url, parts));
CefString spec(&parts.spec);
ASSERT_EQ(spec, "http://www.example.com/path/to.html?foo=test&bar=test2");
ASSERT_EQ(parts.username.length, (size_t)0);
ASSERT_EQ(parts.password.length, (size_t)0);
EXPECT_EQ(spec, "http://www.example.com/path/to.html?foo=test&bar=test2");
EXPECT_EQ(parts.username.length, (size_t)0);
EXPECT_EQ(parts.password.length, (size_t)0);
CefString scheme(&parts.scheme);
ASSERT_EQ(scheme, "http");
EXPECT_EQ(scheme, "http");
CefString host(&parts.host);
ASSERT_EQ(host, "www.example.com");
ASSERT_EQ(parts.port.length, (size_t)0);
EXPECT_EQ(host, "www.example.com");
EXPECT_EQ(parts.port.length, (size_t)0);
CefString path(&parts.path);
ASSERT_EQ(path, "/path/to.html");
EXPECT_EQ(path, "/path/to.html");
CefString query(&parts.query);
ASSERT_EQ(query, "foo=test&bar=test2");
EXPECT_EQ(query, "foo=test&bar=test2");
}
// Parse the URL using all the various components.
@ -152,25 +152,25 @@ TEST(URLTest, ParseURL) {
CefString url;
url.FromASCII(
"http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
ASSERT_TRUE(CefParseURL(url, parts));
EXPECT_TRUE(CefParseURL(url, parts));
CefString spec(&parts.spec);
ASSERT_EQ(spec,
EXPECT_EQ(spec,
"http://user:pass@www.example.com:88/path/to.html?foo=test&bar=test2");
CefString scheme(&parts.scheme);
ASSERT_EQ(scheme, "http");
EXPECT_EQ(scheme, "http");
CefString username(&parts.username);
ASSERT_EQ(username, "user");
EXPECT_EQ(username, "user");
CefString password(&parts.password);
ASSERT_EQ(password, "pass");
EXPECT_EQ(password, "pass");
CefString host(&parts.host);
ASSERT_EQ(host, "www.example.com");
EXPECT_EQ(host, "www.example.com");
CefString port(&parts.port);
ASSERT_EQ(port, "88");
EXPECT_EQ(port, "88");
CefString path(&parts.path);
ASSERT_EQ(path, "/path/to.html");
EXPECT_EQ(path, "/path/to.html");
CefString query(&parts.query);
ASSERT_EQ(query, "foo=test&bar=test2");
EXPECT_EQ(query, "foo=test&bar=test2");
}
// Parse an invalid URL.
@ -178,6 +178,19 @@ TEST(URLTest, ParseURL) {
CefURLParts parts;
CefString url;
url.FromASCII("www.example.com");
ASSERT_FALSE(CefParseURL(url, parts));
EXPECT_FALSE(CefParseURL(url, parts));
}
}
TEST(URLTest, GetMimeType) {
CefString mime_type;
mime_type = CefGetMimeType("html");
EXPECT_STREQ("text/html", mime_type.ToString().c_str());
mime_type = CefGetMimeType("txt");
EXPECT_STREQ("text/plain", mime_type.ToString().c_str());
mime_type = CefGetMimeType("gif");
EXPECT_STREQ("image/gif", mime_type.ToString().c_str());
}