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 #import <Foundation/Foundation.h> | |
8 #import <CommonCrypto/CommonDigest.h> | |
stuartmorgan
2012/07/24 13:38:49
Reverse these two lines.
Chen Yu
2012/07/24 14:34:20
Done.
| |
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/mac/scoped_cftyperef.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/sys_string_conversions.h" | |
20 | |
21 namespace { | |
22 // Client ID key in the user preferences. | |
23 NSString* kClientIdPreferenceKey = @"ChromiumClientID"; | |
24 // Default salt for device ids. | |
25 const char* const kDefaultSalt = "Salt"; | |
26 } // namespace | |
27 | |
28 namespace ios { | |
29 namespace device_util { | |
30 | |
31 std::string platform() { | |
32 std::string result; | |
33 | |
34 size_t size = 0; | |
35 sysctlbyname("hw.machine", NULL, &size, NULL, 0); | |
36 scoped_array<char> machine(new char[size]); | |
37 if (sysctlbyname("hw.machine", machine.get(), &size, NULL, 0) == 0) | |
38 result = machine.get(); | |
39 | |
40 return result; | |
41 } | |
42 | |
43 bool isRunningOnHighRamDevice() { | |
44 BOOL result = NO; | |
stuartmorgan
2012/07/24 13:38:49
s/BOOL/bool/
Chen Yu
2012/07/24 14:34:20
Done.
| |
45 | |
46 u_int64_t value = 0; | |
47 size_t size = sizeof(value); | |
48 if (sysctlbyname("hw.memsize", &value, &size, NULL, 0) == 0) { | |
49 // Anything >= 250M, call high ram. | |
50 result = (value >= 250 * 1024 * 1024); | |
51 } | |
52 | |
53 return result; | |
54 } | |
55 | |
56 bool isSingleCoreDevice() { | |
57 static BOOL initCheck; | |
58 static BOOL isSingleCoreDevice; | |
stuartmorgan
2012/07/24 13:38:49
Same for these, and both should be = false;
Also,
Chen Yu
2012/07/24 14:34:20
Done.
| |
59 if (!initCheck) { | |
60 NSArray* singleCoreDevices = | |
61 [NSArray arrayWithObjects:@"iPhone2,1", @"iPad1,1", @"iPhone3,1", nil]; | |
62 isSingleCoreDevice = | |
63 [singleCoreDevices containsObject: base::SysUTF8ToNSString(platform())]; | |
64 initCheck = YES; | |
stuartmorgan
2012/07/24 13:38:49
true
Chen Yu
2012/07/24 14:34:20
Done.
| |
65 } | |
66 return isSingleCoreDevice; | |
67 } | |
68 | |
69 std::string macAddress(std::string ifName) { | |
70 NSMutableString* macString = [NSMutableString string]; | |
stuartmorgan
2012/07/24 13:38:49
mac_string
Chen Yu
2012/07/24 14:34:20
Done.
| |
71 | |
72 struct ifaddrs* addrs; | |
73 if (getifaddrs(&addrs) == 0) { | |
74 struct ifaddrs* cursor = addrs; | |
75 while (cursor != 0) { | |
76 if ((cursor->ifa_addr->sa_family == AF_LINK) && | |
77 strcmp(ifName.c_str(), cursor->ifa_name) == 0) { | |
78 const struct sockaddr_dl* dlAddr = | |
79 (const struct sockaddr_dl*)cursor->ifa_addr; | |
80 const unsigned char* base = | |
81 (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen]; | |
82 int sdlAlen = dlAddr->sdl_alen; | |
stuartmorgan
2012/07/24 13:38:49
sdl_alen
Chen Yu
2012/07/24 14:34:20
Done.
| |
83 for (int i = 0; i < sdlAlen; ++i) { | |
84 if (i != 0) | |
85 [macString appendString:@":"]; | |
86 [macString appendFormat:@"%02X", base[i]]; | |
87 } | |
88 break; | |
89 } | |
90 cursor = cursor->ifa_next; | |
91 } | |
92 freeifaddrs(addrs); | |
93 } | |
94 return std::string([macString UTF8String]);; | |
95 } | |
96 | |
97 std::string randomId() { | |
98 base::mac::ScopedCFTypeRef<CFUUIDRef> | |
99 uuidObject(CFUUIDCreate(kCFAllocatorDefault)); | |
stuartmorgan
2012/07/24 13:38:49
uuid_object
Chen Yu
2012/07/24 14:34:20
Done.
| |
100 // Get the string representation of CFUUID object. | |
101 NSString* str = | |
stuartmorgan
2012/07/24 13:38:49
str is a terrible name; let's use uuid_string
Chen Yu
2012/07/24 14:34:20
Done.
| |
102 [(NSString*)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) | |
103 autorelease]; | |
stuartmorgan
2012/07/24 13:38:49
Use ScopedCFTypeRef
Chen Yu
2012/07/24 14:34:20
Done.
| |
104 return base::SysNSStringToUTF8(str); | |
stuartmorgan
2012/07/24 13:38:49
SysCFStringRefToUTF8
Chen Yu
2012/07/24 14:34:20
Done.
| |
105 } | |
106 | |
107 std::string deviceIdentifier(const char* const salt) { | |
108 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
109 NSString* clientId = [defaults stringForKey:kClientIdPreferenceKey]; | |
stuartmorgan
2012/07/24 13:38:49
client_id
Chen Yu
2012/07/24 14:34:20
Done.
| |
110 | |
111 if (!clientId) { | |
112 clientId = base::SysUTF8ToNSString(randomId()); | |
113 [defaults setObject:clientId forKey:kClientIdPreferenceKey]; | |
114 [defaults synchronize]; | |
115 } | |
116 | |
117 NSData* data = [[NSString stringWithFormat:@"%@%s", clientId, | |
118 salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding]; | |
119 | |
120 DCHECK(CC_SHA256_DIGEST_LENGTH >= 16); | |
121 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; | |
122 void* result = CC_SHA256([data bytes], [data length], hash); | |
123 DCHECK(result == hash); | |
124 CFUUIDBytes* bytes = static_cast<CFUUIDBytes*>(result); | |
stuartmorgan
2012/07/24 13:38:49
uuid_bytes
Chen Yu
2012/07/24 14:34:20
Done.
| |
125 | |
126 base::mac::ScopedCFTypeRef<CFUUIDRef> | |
127 uuidObject(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *bytes)); | |
128 NSString* str = | |
stuartmorgan
2012/07/24 13:38:49
s/str/device_id/
| |
129 [(NSString*)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) | |
130 autorelease]; | |
stuartmorgan
2012/07/24 13:38:49
Same here; scoped, and CF-based conversion.
Chen Yu
2012/07/24 14:34:20
Done.
| |
131 return base::SysNSStringToUTF8(str); | |
132 } | |
133 | |
134 } // namespace device_util | |
135 } // namespace ios | |
OLD | NEW |