blob: 4af8234988fc35b3f82fb72ef633f380a3ca43b0 [file] [log] [blame]
[email protected]e16e8732012-08-07 11:03:111// 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]aebcd0dd2012-10-05 17:48:588#import <UIKit/UIKit.h>
[email protected]e16e8732012-08-07 11:03:119
10#include <ifaddrs.h>
11#include <net/if_dl.h>
12#include <string.h>
13#include <sys/socket.h>
14#include <sys/sysctl.h>
15
16#include "base/logging.h"
[email protected]e16e8732012-08-07 11:03:1117#include "base/mac/scoped_cftyperef.h"
18#include "base/memory/scoped_ptr.h"
[email protected]d1a5a2f2013-06-10 21:17:4019#include "base/strings/string_util.h"
20#include "base/strings/stringprintf.h"
[email protected]9fe1a5b2013-02-07 19:18:0321#include "base/strings/sys_string_conversions.h"
[email protected]e16e8732012-08-07 11:03:1122
23namespace {
24
25// Client ID key in the user preferences.
[email protected]d724b552012-12-13 17:39:4726NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID";
27NSString* const kClientIdPreferenceKey = @"ChromeClientID";
[email protected]af6a8c92012-12-20 16:41:3328// Current hardware type. This is used to detect that a device has been backed
29// up and restored to another device, and allows regenerating a new device id.
30NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType";
[email protected]e16e8732012-08-07 11:03:1131// Default salt for device ids.
32const char kDefaultSalt[] = "Salt";
[email protected]d724b552012-12-13 17:39:4733// Zero UUID returned on buggy iOS devices.
34NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000";
35
36NSString* GenerateClientId() {
37 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
38
39 // Try to migrate from legacy client id.
40 NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey];
41
42 // Some iOS6 devices return a buggy identifierForVendor:
43 // http://openradar.appspot.com/12377282. If this is the case, revert to
44 // generating a new one.
45 if (!client_id || [client_id isEqualToString:kZeroUUID]) {
[email protected]d01f3112014-06-19 22:49:5546 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
47 if ([client_id isEqualToString:kZeroUUID])
[email protected]d724b552012-12-13 17:39:4748 client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
[email protected]d724b552012-12-13 17:39:4749 }
50 return client_id;
51}
[email protected]e16e8732012-08-07 11:03:1152
53} // namespace
54
55namespace ios {
56namespace device_util {
57
58std::string GetPlatform() {
59 std::string platform;
60 size_t size = 0;
61 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
Brett Wilsone3c4d1a2015-07-07 23:38:0962 sysctlbyname("hw.machine", base::WriteInto(&platform, size), &size, NULL, 0);
[email protected]e16e8732012-08-07 11:03:1163 return platform;
64}
65
[email protected]4f150ec2014-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]4f150ec2014-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*>(
109 &found_address_struct->sdl_data[
110 found_address_struct->sdl_nlen]);
111
112 int found_address_length = found_address_struct->sdl_alen;
113 for (int i = 0; i < found_address_length; ++i) {
114 if (i != 0)
115 mac_string.push_back(':');
116 base::StringAppendF(&mac_string, "%02X", found_address[i]);
117 }
118 break;
119 }
120 }
121 freeifaddrs(addresses);
122 }
123 return mac_string;
124}
125
126std::string GetRandomId() {
[email protected]3df79f42013-06-24 18:49:05127 base::ScopedCFTypeRef<CFUUIDRef> uuid_object(
128 CFUUIDCreate(kCFAllocatorDefault));
129 base::ScopedCFTypeRef<CFStringRef> uuid_string(
[email protected]e16e8732012-08-07 11:03:11130 CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
131 return base::SysCFStringRefToUTF8(uuid_string);
132}
133
134std::string GetDeviceIdentifier(const char* salt) {
135 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[email protected]af6a8c92012-12-20 16:41:33136
137 NSString* last_seen_hardware =
138 [defaults stringForKey:kHardwareTypePreferenceKey];
139 NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform());
140 if (!last_seen_hardware) {
141 last_seen_hardware = current_hardware;
142 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
143 [defaults synchronize];
144 }
145
[email protected]e16e8732012-08-07 11:03:11146 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
147
[email protected]af6a8c92012-12-20 16:41:33148 if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) {
[email protected]d724b552012-12-13 17:39:47149 client_id = GenerateClientId();
[email protected]e16e8732012-08-07 11:03:11150 [defaults setObject:client_id forKey:kClientIdPreferenceKey];
[email protected]af6a8c92012-12-20 16:41:33151 [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
[email protected]e16e8732012-08-07 11:03:11152 [defaults synchronize];
153 }
154
[email protected]0644db72014-06-05 20:05:43155 return GetSaltedString(base::SysNSStringToUTF8(client_id),
156 salt ? salt : kDefaultSalt);
[email protected]940b67d2014-06-03 14:27:18157}
158
159std::string GetSaltedString(const std::string& in_string,
160 const std::string& salt) {
[email protected]940b67d2014-06-03 14:27:18161 DCHECK(salt.length());
[email protected]0644db72014-06-05 20:05:43162 NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt)
163 dataUsingEncoding:NSUTF8StringEncoding];
[email protected]e16e8732012-08-07 11:03:11164
165 unsigned char hash[CC_SHA256_DIGEST_LENGTH];
166 CC_SHA256([hash_data bytes], [hash_data length], hash);
167 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash);
168
[email protected]3df79f42013-06-24 18:49:05169 base::ScopedCFTypeRef<CFUUIDRef> uuid_object(
170 CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes));
171 base::ScopedCFTypeRef<CFStringRef> device_id(
[email protected]e16e8732012-08-07 11:03:11172 CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
173 return base::SysCFStringRefToUTF8(device_id);
174}
175
176} // namespace device_util
177} // namespace ios