blob: 3592c2be5c32f81f4706201688b8d96732938550 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]e16e8732012-08-07 11:03:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj51d26a42024-04-25 14:23:565#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7#pragma allow_unsafe_buffers
8#endif
9
[email protected]e16e8732012-08-07 11:03:1110#include "base/ios/device_util.h"
11
12#include <CommonCrypto/CommonDigest.h>
[email protected]aebcd0dd2012-10-05 17:48:5813#import <UIKit/UIKit.h>
[email protected]e16e8732012-08-07 11:03:1114#include <ifaddrs.h>
15#include <net/if_dl.h>
avi9b6f42932015-12-26 22:15:1416#include <stddef.h>
[email protected]e16e8732012-08-07 11:03:1117#include <string.h>
18#include <sys/socket.h>
19#include <sys/sysctl.h>
20
dcheng093de9b2016-04-04 21:25:5121#include <memory>
22
Avi Drissmana09d7dd2023-08-17 16:26:5823#include "base/apple/scoped_cftyperef.h"
Hans Wennborga47ddf82020-05-05 18:08:0724#include "base/check.h"
Peter Kasting2f61c8b2022-07-19 23:43:4625#include "base/numerics/safe_conversions.h"
Avi Drissman837d106b2023-09-12 14:51:2026#include "base/posix/sysctl.h"
[email protected]d1a5a2f2013-06-10 21:17:4027#include "base/strings/stringprintf.h"
[email protected]9fe1a5b2013-02-07 19:18:0328#include "base/strings/sys_string_conversions.h"
Asami Doi5fabc5252024-03-20 11:11:4629#include "base/system/sys_info.h"
[email protected]e16e8732012-08-07 11:03:1130
31namespace {
32
33// Client ID key in the user preferences.
[email protected]d724b5562012-12-13 17:39:4734NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID";
35NSString* const kClientIdPreferenceKey = @"ChromeClientID";
[email protected]af6a8c92012-12-20 16:41:3336// Current hardware type. This is used to detect that a device has been backed
37// up and restored to another device, and allows regenerating a new device id.
38NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType";
[email protected]e16e8732012-08-07 11:03:1139// Default salt for device ids.
40const char kDefaultSalt[] = "Salt";
[email protected]d724b5562012-12-13 17:39:4741// Zero UUID returned on buggy iOS devices.
42NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000";
43
44NSString* GenerateClientId() {
45 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
46
47 // Try to migrate from legacy client id.
48 NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey];
49
50 // Some iOS6 devices return a buggy identifierForVendor:
Avi Drissmanb34db7a2023-01-04 21:50:1551 // https://openradar.appspot.com/12377282. If this is the case, revert to
[email protected]d724b5562012-12-13 17:39:4752 // generating a new one.
53 if (!client_id || [client_id isEqualToString:kZeroUUID]) {
[email protected]d01f3112014-06-19 22:49:5554 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Peter Kasting134ef9af2024-12-28 02:30:0955 if ([client_id isEqualToString:kZeroUUID]) {
[email protected]d724b5562012-12-13 17:39:4756 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
Peter Kasting134ef9af2024-12-28 02:30:0957 }
[email protected]d724b5562012-12-13 17:39:4758 }
59 return client_id;
60}
[email protected]e16e8732012-08-07 11:03:1161
62} // namespace
63
Avi Drissman354864c2023-06-01 20:06:0364namespace ios::device_util {
[email protected]e16e8732012-08-07 11:03:1165
[email protected]4f150ec72014-03-18 21:54:4266bool RamIsAtLeast512Mb() {
67 // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe.
68 return RamIsAtLeast(450);
69}
70
71bool RamIsAtLeast1024Mb() {
72 // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe.
73 return RamIsAtLeast(900);
74}
75
76bool RamIsAtLeast(uint64_t ram_in_mb) {
[email protected]e16e8732012-08-07 11:03:1177 uint64_t memory_size = 0;
78 size_t size = sizeof(memory_size);
79 if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) {
[email protected]9317c2492013-12-11 22:18:4180 // Anything >= 500M, call high ram.
[email protected]4f150ec72014-03-18 21:54:4281 return memory_size >= ram_in_mb * 1024 * 1024;
[email protected]e16e8732012-08-07 11:03:1182 }
83 return false;
84}
85
86bool IsSingleCoreDevice() {
87 uint64_t cpu_number = 0;
88 size_t sizes = sizeof(cpu_number);
89 sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0);
90 return cpu_number == 1;
91}
92
93std::string GetMacAddress(const std::string& interface_name) {
94 std::string mac_string;
95 struct ifaddrs* addresses;
96 if (getifaddrs(&addresses) == 0) {
97 for (struct ifaddrs* address = addresses; address;
98 address = address->ifa_next) {
99 if ((address->ifa_addr->sa_family == AF_LINK) &&
100 strcmp(interface_name.c_str(), address->ifa_name) == 0) {
101 const struct sockaddr_dl* found_address_struct =
102 reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr);
103
104 // |found_address_struct->sdl_data| contains the interface name followed
105 // by the interface address. The address part can be accessed based on
106 // the length of the name, that is, |found_address_struct->sdl_nlen|.
107 const unsigned char* found_address =
108 reinterpret_cast<const unsigned char*>(
Peter Kasting134ef9af2024-12-28 02:30:09109 &found_address_struct
110 ->sdl_data[found_address_struct->sdl_nlen]);
[email protected]e16e8732012-08-07 11:03:11111
112 int found_address_length = found_address_struct->sdl_alen;
113 for (int i = 0; i < found_address_length; ++i) {
Peter Kasting134ef9af2024-12-28 02:30:09114 if (i != 0) {
[email protected]e16e8732012-08-07 11:03:11115 mac_string.push_back(':');
Peter Kasting134ef9af2024-12-28 02:30:09116 }
[email protected]e16e8732012-08-07 11:03:11117 base::StringAppendF(&mac_string, "%02X", found_address[i]);
118 }
119 break;
120 }
121 }
122 freeifaddrs(addresses);
123 }
124 return mac_string;
125}
126
127std::string GetRandomId() {
Avi Drissman28154a62023-08-22 04:06:45128 base::apple::ScopedCFTypeRef<CFUUIDRef> uuid_object(
[email protected]3df79f42013-06-24 18:49:05129 CFUUIDCreate(kCFAllocatorDefault));
Avi Drissman28154a62023-08-22 04:06:45130 base::apple::ScopedCFTypeRef<CFStringRef> uuid_string(
Avi Drissman2406f2a2023-11-06 14:32:40131 CFUUIDCreateString(kCFAllocatorDefault, uuid_object.get()));
132 return base::SysCFStringRefToUTF8(uuid_string.get());
[email protected]e16e8732012-08-07 11:03:11133}
134
135std::string GetDeviceIdentifier(const char* salt) {
136 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[email protected]af6a8c92012-12-20 16:41:33137
138 NSString* last_seen_hardware =
139 [defaults stringForKey:kHardwareTypePreferenceKey];
Asami Doi5fabc5252024-03-20 11:11:46140 NSString* current_hardware =
141 base::SysUTF8ToNSString(base::SysInfo::HardwareModelName());
[email protected]af6a8c92012-12-20 16:41:33142 if (!last_seen_hardware) {
143 last_seen_hardware = current_hardware;
144 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
145 [defaults synchronize];
146 }
147
[email protected]e16e8732012-08-07 11:03:11148 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
149
[email protected]af6a8c92012-12-20 16:41:33150 if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) {
[email protected]d724b5562012-12-13 17:39:47151 client_id = GenerateClientId();
[email protected]e16e8732012-08-07 11:03:11152 [defaults setObject:client_id forKey:kClientIdPreferenceKey];
[email protected]af6a8c92012-12-20 16:41:33153 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
[email protected]e16e8732012-08-07 11:03:11154 [defaults synchronize];
155 }
156
[email protected]0644db72014-06-05 20:05:43157 return GetSaltedString(base::SysNSStringToUTF8(client_id),
158 salt ? salt : kDefaultSalt);
[email protected]940b67d2014-06-03 14:27:18159}
160
Guillaume Jenkins74049b12020-12-01 18:53:46161std::string GetVendorId() {
162 return base::SysNSStringToUTF8(
163 [[[UIDevice currentDevice] identifierForVendor] UUIDString]);
164}
165
[email protected]940b67d2014-06-03 14:27:18166std::string GetSaltedString(const std::string& in_string,
167 const std::string& salt) {
[email protected]940b67d2014-06-03 14:27:18168 DCHECK(salt.length());
[email protected]0644db72014-06-05 20:05:43169 NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt)
170 dataUsingEncoding:NSUTF8StringEncoding];
[email protected]e16e8732012-08-07 11:03:11171
172 unsigned char hash[CC_SHA256_DIGEST_LENGTH];
Peter Kasting2f61c8b2022-07-19 23:43:46173 CC_SHA256([hash_data bytes], base::checked_cast<CC_LONG>([hash_data length]),
174 hash);
[email protected]e16e8732012-08-07 11:03:11175 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash);
176
Avi Drissman28154a62023-08-22 04:06:45177 base::apple::ScopedCFTypeRef<CFUUIDRef> uuid_object(
[email protected]3df79f42013-06-24 18:49:05178 CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes));
Avi Drissman28154a62023-08-22 04:06:45179 base::apple::ScopedCFTypeRef<CFStringRef> device_id(
Avi Drissman2406f2a2023-11-06 14:32:40180 CFUUIDCreateString(kCFAllocatorDefault, uuid_object.get()));
181 return base::SysCFStringRefToUTF8(device_id.get());
[email protected]e16e8732012-08-07 11:03:11182}
183
Alexandra Pereiraa675499b2025-04-03 16:52:50184base::expected<task_vm_info, kern_return_t> GetTaskVMInfo() {
185 task_vm_info task_vm_info_data = {0};
186 mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
187 kern_return_t result =
188 task_info(mach_task_self(), TASK_VM_INFO,
189 reinterpret_cast<task_info_t>(&task_vm_info_data), &count);
190 if (result == KERN_SUCCESS) {
191 return task_vm_info_data;
192 }
193 return base::unexpected(result);
194}
Avi Drissman354864c2023-06-01 20:06:03195} // namespace ios::device_util