henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 11 | |
| 12 | #import <Foundation/Foundation.h> |
| 13 | #import <sys/sysctl.h> |
kthelgason | 1f16ee3 | 2017-02-14 03:07:57 -0800 | [diff] [blame] | 14 | #if defined(WEBRTC_IOS) |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 15 | #import <UIKit/UIKit.h> |
kthelgason | 1f16ee3 | 2017-02-14 03:07:57 -0800 | [diff] [blame] | 16 | #endif |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 17 | |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
| 22 | #include "sdk/objc/Framework/Classes/Common/helpers.h" |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace ios { |
| 26 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 27 | NSString* 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 | |
| 35 | std::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 | |
| 41 | bool 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 47 | RTC_LOG(LS_ERROR) << StdStringFromNSString(msg); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 48 | 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. |
| 55 | std::string GetCurrentThreadDescription() { |
| 56 | NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; |
| 57 | return StdStringFromNSString(name); |
| 58 | } |
| 59 | |
kthelgason | 1f16ee3 | 2017-02-14 03:07:57 -0800 | [diff] [blame] | 60 | #if defined(WEBRTC_IOS) |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 61 | std::string GetSystemName() { |
| 62 | NSString* osName = [[UIDevice currentDevice] systemName]; |
| 63 | return StdStringFromNSString(osName); |
| 64 | } |
| 65 | |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 66 | std::string GetSystemVersionAsString() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 67 | NSString* osVersion = [[UIDevice currentDevice] systemVersion]; |
| 68 | return StdStringFromNSString(osVersion); |
| 69 | } |
| 70 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 71 | std::string GetDeviceType() { |
| 72 | NSString* deviceModel = [[UIDevice currentDevice] model]; |
| 73 | return StdStringFromNSString(deviceModel); |
| 74 | } |
Kári Tristan Helgason | 86f8047 | 2017-12-01 14:55:01 +0100 | [diff] [blame] | 75 | |
| 76 | bool GetLowPowerModeEnabled() { |
| 77 | return [NSProcessInfo processInfo].lowPowerModeEnabled; |
| 78 | } |
kthelgason | 1f16ee3 | 2017-02-14 03:07:57 -0800 | [diff] [blame] | 79 | #endif |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 80 | |
| 81 | std::string GetDeviceName() { |
| 82 | size_t size; |
| 83 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 84 | std::unique_ptr<char[]> machine; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 85 | machine.reset(new char[size]); |
| 86 | sysctlbyname("hw.machine", machine.get(), &size, NULL, 0); |
kthelgason | b3df385 | 2016-12-16 06:17:58 -0800 | [diff] [blame] | 87 | return std::string(machine.get()); |
henrika | 3e60bf0 | 2016-02-24 14:27:09 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | std::string GetProcessName() { |
| 91 | NSString* processName = [NSProcessInfo processInfo].processName; |
| 92 | return StdStringFromNSString(processName); |
| 93 | } |
| 94 | |
| 95 | int GetProcessID() { |
| 96 | return [NSProcessInfo processInfo].processIdentifier; |
| 97 | } |
| 98 | |
| 99 | std::string GetOSVersionString() { |
| 100 | NSString* osVersion = |
| 101 | [NSProcessInfo processInfo].operatingSystemVersionString; |
| 102 | return StdStringFromNSString(osVersion); |
| 103 | } |
| 104 | |
| 105 | int GetProcessorCount() { |
| 106 | return [NSProcessInfo processInfo].processorCount; |
| 107 | } |
| 108 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 109 | } // namespace ios |
| 110 | } // namespace webrtc |
| 111 | |