Chromium Code Reviews| 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 <CommonCrypto/CommonDigest.h> | |
|
Mark Mentovai
2012/07/25 14:00:23
#include this. It’s not Objective-C. #import is on
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 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/mac/scoped_cftyperef.h" | |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/sys_string_conversions.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Client ID key in the user preferences. | |
| 24 NSString* const kClientIdPreferenceKey = @"ChromiumClientID"; | |
| 25 // Default salt for device ids. | |
| 26 const char* const kDefaultSalt = "Salt"; | |
|
Mark Mentovai
2012/07/25 14:00:23
Why not const char kDefaultSalt[] = "Salt"; instea
Mark Mentovai
2012/07/25 14:00:23
What is special about this that an empty string ("
Chen Yu
2012/07/27 16:28:17
Nothing special actually. But "Salt" has been used
| |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace ios { | |
| 31 namespace device_util { | |
| 32 | |
| 33 std::string GetPlatform() { | |
| 34 std::string result; | |
| 35 | |
| 36 size_t size = 0; | |
| 37 sysctlbyname("hw.machine", NULL, &size, NULL, 0); | |
| 38 scoped_array<char> machine(new char[size]); | |
| 39 if (sysctlbyname("hw.machine", machine.get(), &size, NULL, 0) == 0) | |
| 40 result = machine.get(); | |
|
Mark Mentovai
2012/07/25 14:00:23
Why the copy? Why not just pass result’s buffer to
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 41 | |
| 42 return result; | |
| 43 } | |
| 44 | |
| 45 bool IsRunningOnHighRamDevice() { | |
| 46 bool result = false; | |
| 47 | |
| 48 u_int64_t value = 0; | |
| 49 size_t size = sizeof(value); | |
| 50 if (sysctlbyname("hw.memsize", &value, &size, NULL, 0) == 0) { | |
| 51 // Anything >= 250M, call high ram. | |
| 52 result = (value >= 250 * 1024 * 1024); | |
|
Mark Mentovai
2012/07/25 14:00:23
The parentheses aren’t necessary.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 53 } | |
| 54 | |
| 55 return result; | |
| 56 } | |
| 57 | |
| 58 bool IsSingleCoreDevice() { | |
| 59 static bool is_init_check = false; | |
|
Mark Mentovai
2012/07/25 14:00:23
Don’t do this thing with two statics. Use the comp
Chen Yu
2012/07/27 16:28:17
Changed not to use cache.
On 2012/07/25 14:00:23,
| |
| 60 static bool is_single_core_device = false; | |
| 61 if (!is_init_check) { | |
| 62 // "iPhone2,1" is an iPhone 3GS, "iPad1,1" is an iPad 1, and "iPhone3,1" is | |
| 63 // an iPhone 4. | |
| 64 NSArray* single_core_devices = | |
|
Mark Mentovai
2012/07/25 14:00:23
You can’t use something like hw.ncpu?
Chen Yu
2012/07/27 16:28:17
Thanks for the suggestions! The hardcoded-list mig
| |
| 65 [NSArray arrayWithObjects:@"iPhone2,1", @"iPad1,1", @"iPhone3,1", nil]; | |
| 66 is_single_core_device = [single_core_devices containsObject: | |
| 67 base::SysUTF8ToNSString(GetPlatform())]; | |
|
Mark Mentovai
2012/07/25 14:00:23
Since GetPlatform() returns a std::string, I think
| |
| 68 is_init_check = true; | |
| 69 } | |
| 70 return is_single_core_device; | |
| 71 } | |
| 72 | |
| 73 std::string GetMacAddress(std::string ifName) { | |
|
Mark Mentovai
2012/07/25 14:00:23
interface_name, as before.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 74 NSMutableString* mac_string = [NSMutableString string]; | |
| 75 | |
| 76 struct ifaddrs* addrs; | |
|
Mark Mentovai
2012/07/25 14:00:23
Don’t abbreviate. addresses.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 77 if (getifaddrs(&addrs) == 0) { | |
| 78 struct ifaddrs* cursor = addrs; | |
|
Mark Mentovai
2012/07/25 14:00:23
cursor to what? Call this |address|.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 79 while (cursor != 0) { | |
|
Mark Mentovai
2012/07/25 14:00:23
Use NULL for pointer comparisons to zero, or just
Mark Mentovai
2012/07/25 14:00:23
for (struct ifaddrs* address = addresses; address;
Chen Yu
2012/07/27 16:28:17
Done.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 80 if ((cursor->ifa_addr->sa_family == AF_LINK) && | |
| 81 strcmp(ifName.c_str(), cursor->ifa_name) == 0) { | |
| 82 const struct sockaddr_dl* dlAddr = | |
|
Mark Mentovai
2012/07/25 14:00:23
This, too, is namedImproperly.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 83 (const struct sockaddr_dl*)cursor->ifa_addr; | |
|
Mark Mentovai
2012/07/25 14:00:23
Don’t use (c_style)casts. Use c_plus_plus<style>(c
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 84 const unsigned char* base = | |
|
Mark Mentovai
2012/07/25 14:00:23
base is a bad name. base of what?
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 85 (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen]; | |
|
Mark Mentovai
2012/07/25 14:00:23
Casting again.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 86 int sdl_alen = dlAddr->sdl_alen; | |
| 87 for (int i = 0; i < sdl_alen; ++i) { | |
| 88 if (i != 0) | |
| 89 [mac_string appendString:@":"]; | |
|
Mark Mentovai
2012/07/25 14:00:23
Come on, we have a whole suite of StringPrintf uti
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 90 [mac_string appendFormat:@"%02X", base[i]]; | |
| 91 } | |
| 92 break; | |
| 93 } | |
| 94 cursor = cursor->ifa_next; | |
| 95 } | |
| 96 freeifaddrs(addrs); | |
| 97 } | |
| 98 return std::string([mac_string UTF8String]);; | |
| 99 } | |
| 100 | |
| 101 std::string GetRandomId() { | |
| 102 base::mac::ScopedCFTypeRef<CFUUIDRef> | |
| 103 uuid_object(CFUUIDCreate(kCFAllocatorDefault)); | |
| 104 // Get the string representation of CFUUID object. | |
|
Mark Mentovai
2012/07/25 14:00:23
This comment doesn’t contribute anything.
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 105 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( | |
| 106 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | |
| 107 return base::SysCFStringRefToUTF8(uuid_string); | |
| 108 } | |
| 109 | |
| 110 std::string GetDeviceIdentifier(const char* const salt) { | |
|
Mark Mentovai
2012/07/25 14:00:23
If you wouldn’t accept a const int argument or mak
Chen Yu
2012/07/27 16:28:17
Done.
| |
| 111 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
| 112 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; | |
| 113 | |
| 114 if (!client_id) { | |
| 115 client_id = base::SysUTF8ToNSString(GetRandomId()); | |
| 116 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; | |
| 117 [defaults synchronize]; | |
| 118 } | |
| 119 | |
| 120 NSData* data = [[NSString stringWithFormat:@"%@%s", client_id, | |
| 121 salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding]; | |
| 122 | |
| 123 DCHECK(CC_SHA256_DIGEST_LENGTH >= 16); | |
|
Mark Mentovai
2012/07/25 14:00:23
This is a compile-time condition. It’s not somethi
Mark Mentovai
2012/07/25 14:00:23
Why do you care? What’s special about 16?
Chen Yu
2012/07/27 16:28:17
I removed this COMPILE_ASSERT.
On 2012/07/25 14:00
| |
| 124 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; | |
| 125 void* result = CC_SHA256([data bytes], [data length], hash); | |
| 126 DCHECK(result == hash); | |
| 127 CFUUIDBytes* uuid_bytes = static_cast<CFUUIDBytes*>(result); | |
| 128 | |
| 129 base::mac::ScopedCFTypeRef<CFUUIDRef> | |
| 130 uuid_object(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); | |
| 131 base::mac::ScopedCFTypeRef<CFStringRef> device_id( | |
| 132 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | |
| 133 return base::SysCFStringRefToUTF8(device_id); | |
| 134 } | |
| 135 | |
| 136 } // namespace device_util | |
| 137 } // namespace ios | |
| OLD | NEW |