[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/ios/device_util.h" |
| 6 | |
| 7 | #include <CommonCrypto/CommonDigest.h> |
[email protected] | aebcd0dd | 2012-10-05 17:48:58 | [diff] [blame] | 8 | #import <UIKit/UIKit.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 9 | |
| 10 | #include <ifaddrs.h> |
| 11 | #include <net/if_dl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 12 | #include <stddef.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/sysctl.h> |
| 16 | |
| 17 | #include "base/logging.h" |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 18 | #include "base/mac/scoped_cftyperef.h" |
| 19 | #include "base/memory/scoped_ptr.h" |
[email protected] | d1a5a2f | 2013-06-10 21:17:40 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
| 21 | #include "base/strings/stringprintf.h" |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 22 | #include "base/strings/sys_string_conversions.h" |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // Client ID key in the user preferences. |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 27 | NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID"; |
| 28 | NSString* const kClientIdPreferenceKey = @"ChromeClientID"; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 29 | // Current hardware type. This is used to detect that a device has been backed |
| 30 | // up and restored to another device, and allows regenerating a new device id. |
| 31 | NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType"; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 32 | // Default salt for device ids. |
| 33 | const char kDefaultSalt[] = "Salt"; |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 34 | // Zero UUID returned on buggy iOS devices. |
| 35 | NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000"; |
| 36 | |
| 37 | NSString* GenerateClientId() { |
| 38 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 39 | |
| 40 | // Try to migrate from legacy client id. |
| 41 | NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey]; |
| 42 | |
| 43 | // Some iOS6 devices return a buggy identifierForVendor: |
| 44 | // http://openradar.appspot.com/12377282. If this is the case, revert to |
| 45 | // generating a new one. |
| 46 | if (!client_id || [client_id isEqualToString:kZeroUUID]) { |
[email protected] | d01f311 | 2014-06-19 22:49:55 | [diff] [blame] | 47 | client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; |
| 48 | if ([client_id isEqualToString:kZeroUUID]) |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 49 | client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId()); |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 50 | } |
| 51 | return client_id; |
| 52 | } |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | namespace ios { |
| 57 | namespace device_util { |
| 58 | |
| 59 | std::string GetPlatform() { |
| 60 | std::string platform; |
| 61 | size_t size = 0; |
| 62 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 63 | sysctlbyname("hw.machine", base::WriteInto(&platform, size), &size, NULL, 0); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 64 | return platform; |
| 65 | } |
| 66 | |
[email protected] | 4f150ec7 | 2014-03-18 21:54:42 | [diff] [blame] | 67 | bool RamIsAtLeast512Mb() { |
| 68 | // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe. |
| 69 | return RamIsAtLeast(450); |
| 70 | } |
| 71 | |
| 72 | bool RamIsAtLeast1024Mb() { |
| 73 | // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe. |
| 74 | return RamIsAtLeast(900); |
| 75 | } |
| 76 | |
| 77 | bool RamIsAtLeast(uint64_t ram_in_mb) { |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 78 | uint64_t memory_size = 0; |
| 79 | size_t size = sizeof(memory_size); |
| 80 | if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) { |
[email protected] | 9317c249 | 2013-12-11 22:18:41 | [diff] [blame] | 81 | // Anything >= 500M, call high ram. |
[email protected] | 4f150ec7 | 2014-03-18 21:54:42 | [diff] [blame] | 82 | return memory_size >= ram_in_mb * 1024 * 1024; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | bool IsSingleCoreDevice() { |
| 88 | uint64_t cpu_number = 0; |
| 89 | size_t sizes = sizeof(cpu_number); |
| 90 | sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0); |
| 91 | return cpu_number == 1; |
| 92 | } |
| 93 | |
| 94 | std::string GetMacAddress(const std::string& interface_name) { |
| 95 | std::string mac_string; |
| 96 | struct ifaddrs* addresses; |
| 97 | if (getifaddrs(&addresses) == 0) { |
| 98 | for (struct ifaddrs* address = addresses; address; |
| 99 | address = address->ifa_next) { |
| 100 | if ((address->ifa_addr->sa_family == AF_LINK) && |
| 101 | strcmp(interface_name.c_str(), address->ifa_name) == 0) { |
| 102 | const struct sockaddr_dl* found_address_struct = |
| 103 | reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr); |
| 104 | |
| 105 | // |found_address_struct->sdl_data| contains the interface name followed |
| 106 | // by the interface address. The address part can be accessed based on |
| 107 | // the length of the name, that is, |found_address_struct->sdl_nlen|. |
| 108 | const unsigned char* found_address = |
| 109 | reinterpret_cast<const unsigned char*>( |
| 110 | &found_address_struct->sdl_data[ |
| 111 | found_address_struct->sdl_nlen]); |
| 112 | |
| 113 | int found_address_length = found_address_struct->sdl_alen; |
| 114 | for (int i = 0; i < found_address_length; ++i) { |
| 115 | if (i != 0) |
| 116 | mac_string.push_back(':'); |
| 117 | base::StringAppendF(&mac_string, "%02X", found_address[i]); |
| 118 | } |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | freeifaddrs(addresses); |
| 123 | } |
| 124 | return mac_string; |
| 125 | } |
| 126 | |
| 127 | std::string GetRandomId() { |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 128 | base::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
| 129 | CFUUIDCreate(kCFAllocatorDefault)); |
| 130 | base::ScopedCFTypeRef<CFStringRef> uuid_string( |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 131 | CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
| 132 | return base::SysCFStringRefToUTF8(uuid_string); |
| 133 | } |
| 134 | |
| 135 | std::string GetDeviceIdentifier(const char* salt) { |
| 136 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 137 | |
| 138 | NSString* last_seen_hardware = |
| 139 | [defaults stringForKey:kHardwareTypePreferenceKey]; |
| 140 | NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform()); |
| 141 | if (!last_seen_hardware) { |
| 142 | last_seen_hardware = current_hardware; |
| 143 | [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
| 144 | [defaults synchronize]; |
| 145 | } |
| 146 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 147 | NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; |
| 148 | |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 149 | if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) { |
[email protected] | d724b55 | 2012-12-13 17:39:47 | [diff] [blame] | 150 | client_id = GenerateClientId(); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 151 | [defaults setObject:client_id forKey:kClientIdPreferenceKey]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 152 | [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey]; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 153 | [defaults synchronize]; |
| 154 | } |
| 155 | |
[email protected] | 0644db7 | 2014-06-05 20:05:43 | [diff] [blame] | 156 | return GetSaltedString(base::SysNSStringToUTF8(client_id), |
| 157 | salt ? salt : kDefaultSalt); |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | std::string GetSaltedString(const std::string& in_string, |
| 161 | const std::string& salt) { |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 162 | DCHECK(salt.length()); |
[email protected] | 0644db7 | 2014-06-05 20:05:43 | [diff] [blame] | 163 | NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt) |
| 164 | dataUsingEncoding:NSUTF8StringEncoding]; |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 165 | |
| 166 | unsigned char hash[CC_SHA256_DIGEST_LENGTH]; |
| 167 | CC_SHA256([hash_data bytes], [hash_data length], hash); |
| 168 | CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); |
| 169 | |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 170 | base::ScopedCFTypeRef<CFUUIDRef> uuid_object( |
| 171 | CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); |
| 172 | base::ScopedCFTypeRef<CFStringRef> device_id( |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 173 | CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
| 174 | return base::SysCFStringRefToUTF8(device_id); |
| 175 | } |
| 176 | |
| 177 | } // namespace device_util |
| 178 | } // namespace ios |