blob: 44eb4af19f828427f34c795ee89f75cbd272e091 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include <sstream>
13
14#include "webrtc/base/common.h"
15#include "webrtc/base/logging.h"
16#include "webrtc/base/macutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/stringutils.h"
18
19namespace rtc {
20
21///////////////////////////////////////////////////////////////////////////////
22
23bool ToUtf8(const CFStringRef str16, std::string* str8) {
24 if ((NULL == str16) || (NULL == str8)) {
25 return false;
26 }
27 size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16),
28 kCFStringEncodingUTF8) + 1;
jbauch555604a2016-04-26 03:13:22 -070029 std::unique_ptr<char[]> buffer(new char[maxlen]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 if (!buffer || !CFStringGetCString(str16, buffer.get(), maxlen,
31 kCFStringEncodingUTF8)) {
32 return false;
33 }
34 str8->assign(buffer.get());
35 return true;
36}
37
38bool ToUtf16(const std::string& str8, CFStringRef* str16) {
39 if (NULL == str16) {
40 return false;
41 }
42 *str16 = CFStringCreateWithBytes(kCFAllocatorDefault,
43 reinterpret_cast<const UInt8*>(str8.data()),
44 str8.length(), kCFStringEncodingUTF8,
45 false);
46 return NULL != *str16;
47}
48
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049void DecodeFourChar(UInt32 fc, std::string* out) {
50 std::stringstream ss;
51 ss << '\'';
52 bool printable = true;
53 for (int i = 3; i >= 0; --i) {
54 char ch = (fc >> (8 * i)) & 0xFF;
55 if (isprint(static_cast<unsigned char>(ch))) {
56 ss << ch;
57 } else {
58 printable = false;
59 break;
60 }
61 }
62 if (printable) {
63 ss << '\'';
64 } else {
65 ss.str("");
66 ss << "0x" << std::hex << fc;
67 }
68 out->append(ss.str());
69}
70
71static bool GetGestalt(OSType ostype, int* value) {
72 ASSERT(NULL != value);
73 SInt32 native_value;
74 OSStatus result = Gestalt(ostype, &native_value);
75 if (noErr == result) {
76 *value = native_value;
77 return true;
78 }
79 std::string str;
80 DecodeFourChar(ostype, &str);
81 LOG_E(LS_ERROR, OS, result) << "Gestalt(" << str << ")";
82 return false;
83}
84
thakis0cf208a2016-07-06 10:46:41 -070085static bool GetOSVersion(int* major, int* minor, int* bugfix) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 ASSERT(major && minor && bugfix);
87 if (!GetGestalt(gestaltSystemVersion, major)) {
88 return false;
89 }
90 if (*major < 0x1040) {
91 *bugfix = *major & 0xF;
92 *minor = (*major >> 4) & 0xF;
93 *major = (*major >> 8);
94 return true;
95 }
96 return GetGestalt(gestaltSystemVersionMajor, major) &&
97 GetGestalt(gestaltSystemVersionMinor, minor) &&
98 GetGestalt(gestaltSystemVersionBugFix, bugfix);
99}
100
101MacOSVersionName GetOSVersionName() {
102 int major = 0, minor = 0, bugfix = 0;
103 if (!GetOSVersion(&major, &minor, &bugfix)) {
104 return kMacOSUnknown;
105 }
106 if (major > 10) {
107 return kMacOSNewer;
108 }
109 if ((major < 10) || (minor < 3)) {
110 return kMacOSOlder;
111 }
112 switch (minor) {
113 case 3:
114 return kMacOSPanther;
115 case 4:
116 return kMacOSTiger;
117 case 5:
118 return kMacOSLeopard;
119 case 6:
120 return kMacOSSnowLeopard;
121 case 7:
122 return kMacOSLion;
123 case 8:
124 return kMacOSMountainLion;
125 case 9:
126 return kMacOSMavericks;
127 }
128 return kMacOSNewer;
129}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130} // namespace rtc