blob: 657a4a22b6281123940f3bdffcd3db4c66f77d33 [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
henrikaba35d052015-07-14 17:04:08 +020027NSString* NSStringFromStdString(const std::string& stdString) {
28 // std::string may contain null termination character so we construct
29 // using length.
30 return [[NSString alloc] initWithBytes:stdString.data()
31 length:stdString.length()
32 encoding:NSUTF8StringEncoding];
33}
34
35std::string StdStringFromNSString(NSString* nsString) {
36 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
37 return std::string(reinterpret_cast<const char*>([charData bytes]),
38 [charData length]);
39}
40
41bool CheckAndLogError(BOOL success, NSError* error) {
42 if (!success) {
43 NSString* msg =
44 [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code,
45 error.localizedDescription,
46 error.localizedFailureReason];
Mirko Bonadei675513b2017-11-09 11:09:25 +010047 RTC_LOG(LS_ERROR) << StdStringFromNSString(msg);
henrikaba35d052015-07-14 17:04:08 +020048 return false;
49 }
50 return true;
51}
52
53// TODO(henrika): see if it is possible to move to GetThreadName in
54// platform_thread.h and base it on pthread methods instead.
55std::string GetCurrentThreadDescription() {
56 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
57 return StdStringFromNSString(name);
58}
59
kthelgason1f16ee32017-02-14 03:07:57 -080060#if defined(WEBRTC_IOS)
henrikaba35d052015-07-14 17:04:08 +020061std::string GetSystemName() {
62 NSString* osName = [[UIDevice currentDevice] systemName];
63 return StdStringFromNSString(osName);
64}
65
henrikaab12c472016-03-03 16:59:50 +010066std::string GetSystemVersionAsString() {
henrikaba35d052015-07-14 17:04:08 +020067 NSString* osVersion = [[UIDevice currentDevice] systemVersion];
68 return StdStringFromNSString(osVersion);
69}
70
henrikaba35d052015-07-14 17:04:08 +020071std::string GetDeviceType() {
72 NSString* deviceModel = [[UIDevice currentDevice] model];
73 return StdStringFromNSString(deviceModel);
74}
Kári Tristan Helgason86f80472017-12-01 14:55:01 +010075
76bool GetLowPowerModeEnabled() {
77 return [NSProcessInfo processInfo].lowPowerModeEnabled;
78}
kthelgason1f16ee32017-02-14 03:07:57 -080079#endif
henrikaba35d052015-07-14 17:04:08 +020080
81std::string GetDeviceName() {
82 size_t size;
83 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
kwiberg22feaa32016-03-17 09:17:43 -070084 std::unique_ptr<char[]> machine;
henrikaba35d052015-07-14 17:04:08 +020085 machine.reset(new char[size]);
86 sysctlbyname("hw.machine", machine.get(), &size, NULL, 0);
kthelgasonb3df3852016-12-16 06:17:58 -080087 return std::string(machine.get());
henrika3e60bf02016-02-24 14:27:09 +010088}
89
90std::string GetProcessName() {
91 NSString* processName = [NSProcessInfo processInfo].processName;
92 return StdStringFromNSString(processName);
93}
94
95int GetProcessID() {
96 return [NSProcessInfo processInfo].processIdentifier;
97}
98
99std::string GetOSVersionString() {
100 NSString* osVersion =
101 [NSProcessInfo processInfo].operatingSystemVersionString;
102 return StdStringFromNSString(osVersion);
103}
104
105int GetProcessorCount() {
106 return [NSProcessInfo processInfo].processorCount;
107}
108
henrikaba35d052015-07-14 17:04:08 +0200109} // namespace ios
110} // namespace webrtc
111