1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/ios/device_util.h"
#include <CommonCrypto/CommonDigest.h>
#import <UIKit/UIKit.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include "base/ios/ios_util.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_ptr.h"
#include "base/sys_string_conversions.h"
namespace {
// Client ID key in the user preferences.
NSString* const kClientIdPreferenceKey = @"ChromiumClientID";
// Default salt for device ids.
const char kDefaultSalt[] = "Salt";
} // namespace
namespace ios {
namespace device_util {
std::string GetPlatform() {
std::string platform;
size_t size = 0;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
sysctlbyname("hw.machine", WriteInto(&platform, size), &size, NULL, 0);
return platform;
}
bool IsRunningOnHighRamDevice() {
uint64_t memory_size = 0;
size_t size = sizeof(memory_size);
if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) {
// Anything >= 250M, call high ram.
return memory_size >= 250 * 1024 * 1024;
}
return false;
}
bool IsSingleCoreDevice() {
uint64_t cpu_number = 0;
size_t sizes = sizeof(cpu_number);
sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0);
return cpu_number == 1;
}
std::string GetMacAddress(const std::string& interface_name) {
std::string mac_string;
struct ifaddrs* addresses;
if (getifaddrs(&addresses) == 0) {
for (struct ifaddrs* address = addresses; address;
address = address->ifa_next) {
if ((address->ifa_addr->sa_family == AF_LINK) &&
strcmp(interface_name.c_str(), address->ifa_name) == 0) {
const struct sockaddr_dl* found_address_struct =
reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr);
// |found_address_struct->sdl_data| contains the interface name followed
// by the interface address. The address part can be accessed based on
// the length of the name, that is, |found_address_struct->sdl_nlen|.
const unsigned char* found_address =
reinterpret_cast<const unsigned char*>(
&found_address_struct->sdl_data[
found_address_struct->sdl_nlen]);
int found_address_length = found_address_struct->sdl_alen;
for (int i = 0; i < found_address_length; ++i) {
if (i != 0)
mac_string.push_back(':');
base::StringAppendF(&mac_string, "%02X", found_address[i]);
}
break;
}
}
freeifaddrs(addresses);
}
return mac_string;
}
std::string GetRandomId() {
base::mac::ScopedCFTypeRef<CFUUIDRef>
uuid_object(CFUUIDCreate(kCFAllocatorDefault));
base::mac::ScopedCFTypeRef<CFStringRef> uuid_string(
CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
return base::SysCFStringRefToUTF8(uuid_string);
}
std::string GetDeviceIdentifier(const char* salt) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
if (!client_id) {
if (base::ios::IsRunningOnIOS6OrLater())
client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
else
client_id = base::SysUTF8ToNSString(GetRandomId());
[defaults setObject:client_id forKey:kClientIdPreferenceKey];
[defaults synchronize];
}
NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id,
salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding];
unsigned char hash[CC_SHA256_DIGEST_LENGTH];
CC_SHA256([hash_data bytes], [hash_data length], hash);
CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash);
base::mac::ScopedCFTypeRef<CFUUIDRef>
uuid_object(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes));
base::mac::ScopedCFTypeRef<CFStringRef> device_id(
CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
return base::SysCFStringRefToUTF8(device_id);
}
} // namespace device_util
} // namespace ios
|