henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | #include <sstream> |
| 13 | |
kthelgason | d547224 | 2016-09-09 10:19:48 | [diff] [blame^] | 14 | #include <CoreServices/CoreServices.h> |
| 15 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 16 | #include "webrtc/base/common.h" |
| 17 | #include "webrtc/base/logging.h" |
| 18 | #include "webrtc/base/macutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 19 | #include "webrtc/base/stringutils.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | |
| 25 | bool ToUtf8(const CFStringRef str16, std::string* str8) { |
| 26 | if ((NULL == str16) || (NULL == str8)) { |
| 27 | return false; |
| 28 | } |
| 29 | size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16), |
| 30 | kCFStringEncodingUTF8) + 1; |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 31 | std::unique_ptr<char[]> buffer(new char[maxlen]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 32 | if (!buffer || !CFStringGetCString(str16, buffer.get(), maxlen, |
| 33 | kCFStringEncodingUTF8)) { |
| 34 | return false; |
| 35 | } |
| 36 | str8->assign(buffer.get()); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | bool ToUtf16(const std::string& str8, CFStringRef* str16) { |
| 41 | if (NULL == str16) { |
| 42 | return false; |
| 43 | } |
| 44 | *str16 = CFStringCreateWithBytes(kCFAllocatorDefault, |
| 45 | reinterpret_cast<const UInt8*>(str8.data()), |
| 46 | str8.length(), kCFStringEncodingUTF8, |
| 47 | false); |
| 48 | return NULL != *str16; |
| 49 | } |
| 50 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 51 | void DecodeFourChar(UInt32 fc, std::string* out) { |
| 52 | std::stringstream ss; |
| 53 | ss << '\''; |
| 54 | bool printable = true; |
| 55 | for (int i = 3; i >= 0; --i) { |
| 56 | char ch = (fc >> (8 * i)) & 0xFF; |
| 57 | if (isprint(static_cast<unsigned char>(ch))) { |
| 58 | ss << ch; |
| 59 | } else { |
| 60 | printable = false; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | if (printable) { |
| 65 | ss << '\''; |
| 66 | } else { |
| 67 | ss.str(""); |
| 68 | ss << "0x" << std::hex << fc; |
| 69 | } |
| 70 | out->append(ss.str()); |
| 71 | } |
| 72 | |
| 73 | static bool GetGestalt(OSType ostype, int* value) { |
| 74 | ASSERT(NULL != value); |
| 75 | SInt32 native_value; |
| 76 | OSStatus result = Gestalt(ostype, &native_value); |
| 77 | if (noErr == result) { |
| 78 | *value = native_value; |
| 79 | return true; |
| 80 | } |
| 81 | std::string str; |
| 82 | DecodeFourChar(ostype, &str); |
| 83 | LOG_E(LS_ERROR, OS, result) << "Gestalt(" << str << ")"; |
| 84 | return false; |
| 85 | } |
| 86 | |
thakis | 0cf208a | 2016-07-06 17:46:41 | [diff] [blame] | 87 | static bool GetOSVersion(int* major, int* minor, int* bugfix) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 88 | ASSERT(major && minor && bugfix); |
| 89 | if (!GetGestalt(gestaltSystemVersion, major)) { |
| 90 | return false; |
| 91 | } |
| 92 | if (*major < 0x1040) { |
| 93 | *bugfix = *major & 0xF; |
| 94 | *minor = (*major >> 4) & 0xF; |
| 95 | *major = (*major >> 8); |
| 96 | return true; |
| 97 | } |
| 98 | return GetGestalt(gestaltSystemVersionMajor, major) && |
| 99 | GetGestalt(gestaltSystemVersionMinor, minor) && |
| 100 | GetGestalt(gestaltSystemVersionBugFix, bugfix); |
| 101 | } |
| 102 | |
| 103 | MacOSVersionName GetOSVersionName() { |
| 104 | int major = 0, minor = 0, bugfix = 0; |
| 105 | if (!GetOSVersion(&major, &minor, &bugfix)) { |
| 106 | return kMacOSUnknown; |
| 107 | } |
| 108 | if (major > 10) { |
| 109 | return kMacOSNewer; |
| 110 | } |
| 111 | if ((major < 10) || (minor < 3)) { |
| 112 | return kMacOSOlder; |
| 113 | } |
| 114 | switch (minor) { |
| 115 | case 3: |
| 116 | return kMacOSPanther; |
| 117 | case 4: |
| 118 | return kMacOSTiger; |
| 119 | case 5: |
| 120 | return kMacOSLeopard; |
| 121 | case 6: |
| 122 | return kMacOSSnowLeopard; |
| 123 | case 7: |
| 124 | return kMacOSLion; |
| 125 | case 8: |
| 126 | return kMacOSMountainLion; |
| 127 | case 9: |
| 128 | return kMacOSMavericks; |
| 129 | } |
| 130 | return kMacOSNewer; |
| 131 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 132 | } // namespace rtc |