summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authoravi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-22 19:55:26 +0000
committeravi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-22 19:55:26 +0000
commitd2a10d134c864765e7015a62f20642f398adc721 (patch)
treecd5474cc66b613b9a4640e948fd1646da112c311 /base
parente05212240814d77101f6a677fc38411ec05fd177 (diff)
downloadchromium_src-d2a10d134c864765e7015a62f20642f398adc721.zip
chromium_src-d2a10d134c864765e7015a62f20642f398adc721.tar.gz
chromium_src-d2a10d134c864765e7015a62f20642f398adc721.tar.bz2
Impl of platform mime typing for Mac.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1238 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/scoped_cftyperef.h9
-rw-r--r--base/sys_string_conversions.h21
-rw-r--r--base/sys_string_conversions_mac.cc37
3 files changed, 62 insertions, 5 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