summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/scoped_cftyperef.h9
-rw-r--r--base/sys_string_conversions.h21
-rw-r--r--base/sys_string_conversions_mac.cc37
-rw-r--r--net/base/platform_mime_util_mac.mm49
4 files changed, 105 insertions, 11 deletions
diff --git a/base/scoped_cftyperef.h b/base/scoped_cftyperef.h
index f4623fe..23d2690 100644
--- a/base/scoped_cftyperef.h
+++ b/base/scoped_cftyperef.h
@@ -27,10 +27,11 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#ifndef BASE_SCOPED_CFTYPEREF_H__
-#define BASE_SCOPED_CFTYPEREF_H__
+#ifndef BASE_SCOPED_CFTYPEREF_H_
+#define BASE_SCOPED_CFTYPEREF_H_
#include <CoreFoundation/CoreFoundation.h>
+#include "base/basictypes.h"
// scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership
// of a CoreFoundation object: any object that can be represented as a
@@ -91,7 +92,7 @@ class scoped_cftyperef {
private:
CFT object_;
- DISALLOW_EVIL_CONSTRUCTORS(scoped_cftyperef);
+ DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef);
};
-#endif // BASE_SCOPED_CFTYPEREF_H__
+#endif // BASE_SCOPED_CFTYPEREF_H_
diff --git a/base/sys_string_conversions.h b/base/sys_string_conversions.h
index dfca0f7..0896fea0 100644
--- a/base/sys_string_conversions.h
+++ b/base/sys_string_conversions.h
@@ -37,6 +37,10 @@
#include <string>
#include "base/basictypes.h"
+#if defined(OS_MACOSX)
+#include <CoreFoundation/CoreFoundation.h>
+#endif
+
class StringPiece;
namespace base {
@@ -64,6 +68,23 @@ std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page);
#endif // defined(OS_WIN)
+// Mac-specific ----------------------------------------------------------------
+
+#if defined(OS_MACOSX)
+
+// Converts between STL strings and CFStringRefs.
+
+// Creates a string, and returns it with a refcount of 1. You are responsible
+// for releasing it. Returns NULL on failure.
+CFStringRef SysUTF8ToCFStringRef(const std::string& utf8);
+CFStringRef SysWideToCFStringRef(const std::wstring& wide);
+
+// Converts a CFStringRef to an STL string. Returns an empty string on failure.
+std::string SysCFStringRefToUTF8(CFStringRef ref);
+std::wstring SysCFStringRefToWide(CFStringRef ref);
+
+#endif // defined(OS_MACOSX)
+
} // namespace base
#endif // BASE_SYS_STRING_CONVERSIONS_H_
diff --git a/base/sys_string_conversions_mac.cc b/base/sys_string_conversions_mac.cc
index 63533a4..2b89768 100644
--- a/base/sys_string_conversions_mac.cc
+++ b/base/sys_string_conversions_mac.cc
@@ -29,7 +29,6 @@
#include "base/sys_string_conversions.h"
-#include <CoreFoundation/CoreFoundation.h>
#include <vector>
#include "base/scoped_cftyperef.h"
@@ -116,6 +115,24 @@ static OutStringType STLStringToSTLStringWithEncodingsT(
out_encoding);
}
+// Given an STL string |in| with an encoding specified by |in_encoding|,
+// return it as a CFStringRef. Returns NULL on failure.
+template<typename StringType>
+static CFStringRef STLStringToCFStringWithEncodingsT(
+ const StringType& in,
+ CFStringEncoding in_encoding) {
+ typename StringType::size_type in_length = in.length();
+ if (in_length == 0)
+ return CFSTR("");
+
+ return CFStringCreateWithBytes(kCFAllocatorDefault,
+ reinterpret_cast<const UInt8*>(in.data()),
+ in_length *
+ sizeof(typename StringType::value_type),
+ in_encoding,
+ false);
+}
+
// Specify the byte ordering explicitly, otherwise CFString will be confused
// when strings don't carry BOMs, as they typically won't.
static const CFStringEncoding kNarrowStringEncoding = kCFStringEncodingUTF8;
@@ -155,4 +172,22 @@ std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
return SysUTF8ToWide(native_mb);
}
+CFStringRef SysUTF8ToCFStringRef(const std::string& utf8) {
+ return STLStringToCFStringWithEncodingsT(utf8, kNarrowStringEncoding);
+}
+
+CFStringRef SysWideToCFStringRef(const std::wstring& wide) {
+ return STLStringToCFStringWithEncodingsT(wide, kWideStringEncoding);
+}
+
+std::string SysCFStringRefToUTF8(CFStringRef ref) {
+ return CFStringToSTLStringWithEncodingT<std::string>(ref,
+ kNarrowStringEncoding);
+}
+
+std::wstring SysCFStringRefToWide(CFStringRef ref) {
+ return CFStringToSTLStringWithEncodingT<std::wstring>(ref,
+ kWideStringEncoding);
+}
+
} // namespace base
diff --git a/net/base/platform_mime_util_mac.mm b/net/base/platform_mime_util_mac.mm
index 8cc3794..36d8b62 100644
--- a/net/base/platform_mime_util_mac.mm
+++ b/net/base/platform_mime_util_mac.mm
@@ -27,24 +27,61 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#import <Cocoa/Cocoa.h>
+#include <CoreServices/CoreServices.h>
#include <string>
+#include "base/scoped_cftyperef.h"
+#include "base/sys_string_conversions.h"
#include "net/base/platform_mime_util.h"
-#include "base/notimplemented.h"
namespace net {
bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const std::wstring& ext, std::string* result) const {
- NOTIMPLEMENTED();
- return false;
+ std::wstring ext_nodot = ext;
+ if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.')
+ ext_nodot.erase(ext_nodot.begin());
+ scoped_cftyperef<CFStringRef> ext_ref(base::SysWideToCFStringRef(ext_nodot));
+ if (!ext_ref)
+ return false;
+ scoped_cftyperef<CFStringRef> uti(
+ UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
+ ext_ref,
+ NULL));
+ if (!uti)
+ return false;
+ scoped_cftyperef<CFStringRef> mime_ref(
+ UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
+ if (!mime_ref)
+ return false;
+
+ *result = base::SysCFStringRefToUTF8(mime_ref);
+ return true;
}
bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
const std::string& mime_type, std::wstring* ext) const {
- NOTIMPLEMENTED();
- return false;
+ scoped_cftyperef<CFStringRef> mime_ref(base::SysUTF8ToCFStringRef(mime_type));
+ if (!mime_ref)
+ return false;
+ scoped_cftyperef<CFStringRef> uti(
+ UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
+ mime_ref,
+ NULL));
+ if (!uti)
+ return false;
+ scoped_cftyperef<CFStringRef> ext_ref(
+ UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension));
+ if (!ext_ref)
+ return false;
+
+ ext_ref.reset(CFStringCreateWithFormat(kCFAllocatorDefault,
+ NULL,
+ CFSTR(".%@"),
+ ext_ref.get()));
+
+ *ext = base::SysCFStringRefToWide(ext_ref);
+ return true;
}
} // namespace net