blob: c05526affb73fb36d0f8f28113ebc951849b7f98 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 10:13:2211#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:2612#include <sstream>
13
kthelgasond5472242016-09-09 10:19:4814#include <CoreServices/CoreServices.h>
15
henrike@webrtc.orgf0488722014-05-13 18:00:2616#include "webrtc/base/common.h"
17#include "webrtc/base/logging.h"
18#include "webrtc/base/macutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2619#include "webrtc/base/stringutils.h"
20
21namespace rtc {
22
23///////////////////////////////////////////////////////////////////////////////
24
25bool 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;
jbauch555604a2016-04-26 10:13:2231 std::unique_ptr<char[]> buffer(new char[maxlen]);
henrike@webrtc.orgf0488722014-05-13 18:00:2632 if (!buffer || !CFStringGetCString(str16, buffer.get(), maxlen,
33 kCFStringEncodingUTF8)) {
34 return false;
35 }
36 str8->assign(buffer.get());
37 return true;
38}
39
40bool 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.orgf0488722014-05-13 18:00:2651void 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
73static 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
thakis0cf208a2016-07-06 17:46:4187static bool GetOSVersion(int* major, int* minor, int* bugfix) {
henrike@webrtc.orgf0488722014-05-13 18:00:2688 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
103MacOSVersionName 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.orgf0488722014-05-13 18:00:26132} // namespace rtc