OLD | NEW |
---|---|
(Empty) | |
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> | |
8 #import <Foundation/Foundation.h> | |
9 | |
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" | |
17 #include "base/string_util.h" | |
18 #include "base/stringprintf.h" | |
19 #include "base/mac/scoped_cftyperef.h" | |
20 #include "base/memory/scoped_ptr.h" | |
21 #include "base/sys_string_conversions.h" | |
22 | |
23 namespace { | |
24 | |
25 // Client ID key in the user preferences. | |
26 NSString* const kClientIdPreferenceKey = @"ChromiumClientID"; | |
27 // Default salt for device ids. | |
28 const char kDefaultSalt[] = "Salt"; | |
29 | |
30 } // namespace | |
31 | |
32 namespace ios { | |
33 namespace device_util { | |
34 | |
35 std::string GetPlatform() { | |
36 std::string platform; | |
37 size_t size = 0; | |
38 sysctlbyname("hw.machine", NULL, &size, NULL, 0); | |
39 sysctlbyname("hw.machine", WriteInto(&platform, size), &size, NULL, 0); | |
40 return platform; | |
41 } | |
42 | |
43 bool IsRunningOnHighRamDevice() { | |
44 u_int64_t memory_size = 0; | |
45 size_t size = sizeof(memory_size); | |
46 if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) { | |
47 // Anything >= 250M, call high ram. | |
48 return memory_size >= 250 * 1024 * 1024; | |
49 } | |
50 return false; | |
51 } | |
52 | |
53 bool IsSingleCoreDevice() { | |
54 u_int64_t cpu_number = 0; | |
55 size_t sizes = sizeof(cpu_number); | |
56 sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0); | |
57 return cpu_number == 1; | |
58 } | |
59 | |
60 std::string GetMacAddress(const std::string& interface_name) { | |
61 std::string mac_string; | |
62 struct ifaddrs* addresses; | |
63 if (getifaddrs(&addresses) == 0) { | |
64 for (struct ifaddrs* address = addresses; address; | |
65 address = address->ifa_next) { | |
66 if ((address->ifa_addr->sa_family == AF_LINK) && | |
67 strcmp(interface_name.c_str(), address->ifa_name) == 0) { | |
stuartmorgan
2012/07/30 14:28:54
This should be indented one less.
Chen Yu
2012/07/30 16:15:45
Done.
| |
68 const struct sockaddr_dl* found_address_struct = | |
69 reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr); | |
70 | |
71 // |found_address_struct->sdl_data| contains the interface name followed | |
72 // by the interface address. The address part can be accessed based on | |
73 // the length of the name, that is, |found_address_struct->sdl_nlen|. | |
74 const unsigned char* found_address = | |
75 reinterpret_cast<const unsigned char*>( | |
76 &found_address_struct->sdl_data[ | |
77 found_address_struct->sdl_nlen]); | |
78 | |
79 int found_address_length = found_address_struct->sdl_alen; | |
80 for (int i = 0; i < found_address_length; ++i) { | |
81 if (i != 0) | |
82 base::StringAppendF(&mac_string, ":"); | |
83 base::StringAppendF(&mac_string, "%02X", found_address[i]); | |
84 } | |
85 break; | |
86 } | |
87 } | |
88 freeifaddrs(addresses); | |
89 } | |
90 return mac_string; | |
91 } | |
92 | |
93 std::string GetRandomId() { | |
94 base::mac::ScopedCFTypeRef<CFUUIDRef> | |
95 uuid_object(CFUUIDCreate(kCFAllocatorDefault)); | |
96 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( | |
97 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | |
98 return base::SysCFStringRefToUTF8(uuid_string); | |
99 } | |
100 | |
101 std::string GetDeviceIdentifier(const char* salt) { | |
102 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
103 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; | |
104 | |
105 if (!client_id) { | |
106 client_id = base::SysUTF8ToNSString(GetRandomId()); | |
107 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; | |
108 [defaults synchronize]; | |
109 } | |
110 | |
111 NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id, | |
112 salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding]; | |
113 | |
114 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; | |
115 void* result = CC_SHA256([hash_data bytes], [hash_data length], hash); | |
116 DCHECK(result == hash); | |
stuartmorgan
2012/07/30 14:28:54
DCHECK_EQ (although why are we DCHECKing this? It'
Chen Yu
2012/07/30 16:15:45
DCHECK removed; also changed the way to access sha
| |
117 CFUUIDBytes* uuid_bytes = static_cast<CFUUIDBytes*>(result); | |
118 | |
119 base::mac::ScopedCFTypeRef<CFUUIDRef> | |
120 uuid_object(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); | |
121 base::mac::ScopedCFTypeRef<CFStringRef> device_id( | |
122 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | |
123 return base::SysCFStringRefToUTF8(device_id); | |
124 } | |
125 | |
126 } // namespace device_util | |
127 } // namespace ios | |
OLD | NEW |