blob: 4afa15d11d5101ad663f35df60d7d697023bf7f2 [file] [log] [blame]
henrikaba35d052015-07-14 17:04:08 +02001/*
2 * Copyright (c) 2015 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
henrikaba35d052015-07-14 17:04:08 +020011
12#import <Foundation/Foundation.h>
13#import <sys/sysctl.h>
kthelgason1f16ee32017-02-14 03:07:57 -080014#if defined(WEBRTC_IOS)
henrikaba35d052015-07-14 17:04:08 +020015#import <UIKit/UIKit.h>
kthelgason1f16ee32017-02-14 03:07:57 -080016#endif
henrikaba35d052015-07-14 17:04:08 +020017
kwiberg22feaa32016-03-17 09:17:43 -070018#include <memory>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22#include "sdk/objc/Framework/Classes/Common/helpers.h"
henrikaba35d052015-07-14 17:04:08 +020023
24namespace webrtc {
25namespace ios {
26
kthelgason1f16ee32017-02-14 03:07:57 -080027#if defined(WEBRTC_IOS)
henrikaab12c472016-03-03 16:59:50 +010028bool isOperatingSystemAtLeastVersion(double version) {
29 return GetSystemVersion() >= version;
30}
kthelgason1f16ee32017-02-14 03:07:57 -080031#endif
henrikaab12c472016-03-03 16:59:50 +010032
henrikaba35d052015-07-14 17:04:08 +020033NSString* NSStringFromStdString(const std::string& stdString) {
34 // std::string may contain null termination character so we construct
35 // using length.
36 return [[NSString alloc] initWithBytes:stdString.data()
37 length:stdString.length()
38 encoding:NSUTF8StringEncoding];
39}
40
41std::string StdStringFromNSString(NSString* nsString) {
42 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
43 return std::string(reinterpret_cast<const char*>([charData bytes]),
44 [charData length]);
45}
46
47bool CheckAndLogError(BOOL success, NSError* error) {
48 if (!success) {
49 NSString* msg =
50 [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code,
51 error.localizedDescription,
52 error.localizedFailureReason];
53 LOG(LS_ERROR) << StdStringFromNSString(msg);
54 return false;
55 }
56 return true;
57}
58
59// TODO(henrika): see if it is possible to move to GetThreadName in
60// platform_thread.h and base it on pthread methods instead.
61std::string GetCurrentThreadDescription() {
62 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
63 return StdStringFromNSString(name);
64}
65
kthelgason1f16ee32017-02-14 03:07:57 -080066#if defined(WEBRTC_IOS)
henrikaba35d052015-07-14 17:04:08 +020067std::string GetSystemName() {
68 NSString* osName = [[UIDevice currentDevice] systemName];
69 return StdStringFromNSString(osName);
70}
71
henrikaab12c472016-03-03 16:59:50 +010072std::string GetSystemVersionAsString() {
henrikaba35d052015-07-14 17:04:08 +020073 NSString* osVersion = [[UIDevice currentDevice] systemVersion];
74 return StdStringFromNSString(osVersion);
75}
76
henrikaab12c472016-03-03 16:59:50 +010077double GetSystemVersion() {
78 static dispatch_once_t once_token;
79 static double system_version;
80 dispatch_once(&once_token, ^{
81 system_version = [UIDevice currentDevice].systemVersion.doubleValue;
82 });
83 return system_version;
henrikaba35d052015-07-14 17:04:08 +020084}
85
86std::string GetDeviceType() {
87 NSString* deviceModel = [[UIDevice currentDevice] model];
88 return StdStringFromNSString(deviceModel);
89}
kthelgason1f16ee32017-02-14 03:07:57 -080090#endif
henrikaba35d052015-07-14 17:04:08 +020091
92std::string GetDeviceName() {
93 size_t size;
94 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
kwiberg22feaa32016-03-17 09:17:43 -070095 std::unique_ptr<char[]> machine;
henrikaba35d052015-07-14 17:04:08 +020096 machine.reset(new char[size]);
97 sysctlbyname("hw.machine", machine.get(), &size, NULL, 0);
kthelgasonb3df3852016-12-16 06:17:58 -080098 return std::string(machine.get());
henrika3e60bf02016-02-24 14:27:09 +010099}
100
101std::string GetProcessName() {
102 NSString* processName = [NSProcessInfo processInfo].processName;
103 return StdStringFromNSString(processName);
104}
105
106int GetProcessID() {
107 return [NSProcessInfo processInfo].processIdentifier;
108}
109
110std::string GetOSVersionString() {
111 NSString* osVersion =
112 [NSProcessInfo processInfo].operatingSystemVersionString;
113 return StdStringFromNSString(osVersion);
114}
115
116int GetProcessorCount() {
117 return [NSProcessInfo processInfo].processorCount;
118}
119
kwiberg77eab702016-09-28 17:42:01 -0700120#if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) \
121 && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
henrika3e60bf02016-02-24 14:27:09 +0100122bool GetLowPowerModeEnabled() {
henrikaab12c472016-03-03 16:59:50 +0100123 if (isOperatingSystemAtLeastVersion(9.0)) {
124 // lowPoweredModeEnabled is only available on iOS9+.
125 return [NSProcessInfo processInfo].lowPowerModeEnabled;
tkchinfc59c442016-02-26 00:25:45 -0800126 }
henrikaab12c472016-03-03 16:59:50 +0100127 LOG(LS_WARNING) << "webrtc::ios::GetLowPowerModeEnabled() is not "
128 "supported. Requires at least iOS 9.0";
tkchinfc59c442016-02-26 00:25:45 -0800129 return false;
henrikaba35d052015-07-14 17:04:08 +0200130}
henrikaab12c472016-03-03 16:59:50 +0100131#endif
henrikaba35d052015-07-14 17:04:08 +0200132
133} // namespace ios
134} // namespace webrtc
135