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.
5 #include "base/ios/device_util.h"
7 #include <CommonCrypto/CommonDigest.h>
8 #import <UIKit/UIKit.h>
11 #include <net/if_dl.h>
13 #include <sys/socket.h>
14 #include <sys/sysctl.h>
16 #include "base/ios/ios_util.h"
17 #include "base/logging.h"
18 #include "base/mac/scoped_cftyperef.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/sys_string_conversions.h"
26 // Client ID key in the user preferences.
27 NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID";
28 NSString* const kClientIdPreferenceKey = @"ChromeClientID";
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";
32 // Default salt for device ids.
33 const char kDefaultSalt[] = "Salt";
34 // Zero UUID returned on buggy iOS devices.
35 NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000";
37 NSString* GenerateClientId() {
38 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
40 // Try to migrate from legacy client id.
41 NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey];
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]) {
47 if (base::ios::IsRunningOnIOS6OrLater()) {
48 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
49 if ([client_id isEqualToString:kZeroUUID])
50 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
52 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
61 namespace device_util {
63 std::string GetPlatform() {
66 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
67 sysctlbyname("hw.machine", WriteInto(&platform, size), &size, NULL, 0);
71 bool RamIsAtLeast512Mb() {
72 // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe.
73 return RamIsAtLeast(450);
76 bool RamIsAtLeast1024Mb() {
77 // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe.
78 return RamIsAtLeast(900);
81 bool RamIsAtLeast(uint64_t ram_in_mb) {
82 uint64_t memory_size = 0;
83 size_t size = sizeof(memory_size);
84 if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) {
85 // Anything >= 500M, call high ram.
86 return memory_size >= ram_in_mb * 1024 * 1024;
91 bool IsSingleCoreDevice() {
92 uint64_t cpu_number = 0;
93 size_t sizes = sizeof(cpu_number);
94 sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0);
95 return cpu_number == 1;
98 std::string GetMacAddress(const std::string& interface_name) {
99 std::string mac_string;
100 struct ifaddrs* addresses;
101 if (getifaddrs(&addresses) == 0) {
102 for (struct ifaddrs* address = addresses; address;
103 address = address->ifa_next) {
104 if ((address->ifa_addr->sa_family == AF_LINK) &&
105 strcmp(interface_name.c_str(), address->ifa_name) == 0) {
106 const struct sockaddr_dl* found_address_struct =
107 reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr);
109 // |found_address_struct->sdl_data| contains the interface name followed
110 // by the interface address. The address part can be accessed based on
111 // the length of the name, that is, |found_address_struct->sdl_nlen|.
112 const unsigned char* found_address =
113 reinterpret_cast<const unsigned char*>(
114 &found_address_struct->sdl_data[
115 found_address_struct->sdl_nlen]);
117 int found_address_length = found_address_struct->sdl_alen;
118 for (int i = 0; i < found_address_length; ++i) {
120 mac_string.push_back(':');
121 base::StringAppendF(&mac_string, "%02X", found_address[i]);
126 freeifaddrs(addresses);
131 std::string GetRandomId() {
132 base::ScopedCFTypeRef<CFUUIDRef> uuid_object(
133 CFUUIDCreate(kCFAllocatorDefault));
134 base::ScopedCFTypeRef<CFStringRef> uuid_string(
135 CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
136 return base::SysCFStringRefToUTF8(uuid_string);
139 std::string GetDeviceIdentifier(const char* salt) {
140 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
142 NSString* last_seen_hardware =
143 [defaults stringForKey:kHardwareTypePreferenceKey];
144 NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform());
145 if (!last_seen_hardware) {
146 last_seen_hardware = current_hardware;
147 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
148 [defaults synchronize];
151 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
153 if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) {
154 client_id = GenerateClientId();
155 [defaults setObject:client_id forKey:kClientIdPreferenceKey];
156 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
157 [defaults synchronize];
160 NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id,
161 salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding];
163 unsigned char hash[CC_SHA256_DIGEST_LENGTH];
164 CC_SHA256([hash_data bytes], [hash_data length], hash);
165 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash);
167 base::ScopedCFTypeRef<CFUUIDRef> uuid_object(
168 CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes));
169 base::ScopedCFTypeRef<CFStringRef> device_id(
170 CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
171 return base::SysCFStringRefToUTF8(device_id);
174 } // namespace device_util