diff --git a/include/capi/cef_url_capi.h b/include/capi/cef_url_capi.h index b14c6479c..3254931af 100644 --- a/include/capi/cef_url_capi.h +++ b/include/capi/cef_url_capi.h @@ -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 diff --git a/include/cef_url.h b/include/cef_url.h index c5cb21aaf..a3863a44b 100644 --- a/include/cef_url.h +++ b/include/cef_url.h @@ -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_ diff --git a/libcef/common/url_impl.cc b/libcef/common/url_impl.cc index 671b0aa6f..dda4e3edd 100644 --- a/libcef/common/url_impl.cc +++ b/libcef/common/url_impl.cc @@ -4,6 +4,7 @@ #include #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; +} diff --git a/libcef_dll/libcef_dll.cc b/libcef_dll/libcef_dll.cc index 7d935ea60..5eccc717a 100644 --- a/libcef_dll/libcef_dll.cc +++ b/libcef_dll/libcef_dll.cc @@ -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 diff --git a/libcef_dll/wrapper/libcef_dll_wrapper.cc b/libcef_dll/wrapper/libcef_dll_wrapper.cc index f73c0cdc5..3bbd9fc2c 100644 --- a/libcef_dll/wrapper/libcef_dll_wrapper.cc +++ b/libcef_dll/wrapper/libcef_dll_wrapper.cc @@ -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 handler) { // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING diff --git a/tests/unittests/url_unittest.cc b/tests/unittests/url_unittest.cc index 44557ac5c..12918b68e 100644 --- a/tests/unittests/url_unittest.cc +++ b/tests/unittests/url_unittest.cc @@ -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()); +}