Index: base/ios/device_util.mm |
diff --git a/base/ios/device_util.mm b/base/ios/device_util.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8f04e856acf88c2e9aa1b8b2fec9e53bf634c53d |
--- /dev/null |
+++ b/base/ios/device_util.mm |
@@ -0,0 +1,135 @@ |
+// 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" |
+ |
+#import <Foundation/Foundation.h> |
+#import <CommonCrypto/CommonDigest.h> |
stuartmorgan
2012/07/24 13:38:49
Reverse these two lines.
Chen Yu
2012/07/24 14:34:20
Done.
|
+ |
+#include <ifaddrs.h> |
+#include <net/if_dl.h> |
+#include <string.h> |
+#include <sys/socket.h> |
+#include <sys/sysctl.h> |
+ |
+#include "base/logging.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* kClientIdPreferenceKey = @"ChromiumClientID"; |
+// Default salt for device ids. |
+const char* const kDefaultSalt = "Salt"; |
+} // namespace |
+ |
+namespace ios { |
+namespace device_util { |
+ |
+std::string platform() { |
+ std::string result; |
+ |
+ size_t size = 0; |
+ sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
+ scoped_array<char> machine(new char[size]); |
+ if (sysctlbyname("hw.machine", machine.get(), &size, NULL, 0) == 0) |
+ result = machine.get(); |
+ |
+ return result; |
+} |
+ |
+bool isRunningOnHighRamDevice() { |
+ BOOL result = NO; |
stuartmorgan
2012/07/24 13:38:49
s/BOOL/bool/
Chen Yu
2012/07/24 14:34:20
Done.
|
+ |
+ u_int64_t value = 0; |
+ size_t size = sizeof(value); |
+ if (sysctlbyname("hw.memsize", &value, &size, NULL, 0) == 0) { |
+ // Anything >= 250M, call high ram. |
+ result = (value >= 250 * 1024 * 1024); |
+ } |
+ |
+ return result; |
+} |
+ |
+bool isSingleCoreDevice() { |
+ static BOOL initCheck; |
+ 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.
|
+ if (!initCheck) { |
+ NSArray* singleCoreDevices = |
+ [NSArray arrayWithObjects:@"iPhone2,1", @"iPad1,1", @"iPhone3,1", nil]; |
+ isSingleCoreDevice = |
+ [singleCoreDevices containsObject: base::SysUTF8ToNSString(platform())]; |
+ initCheck = YES; |
stuartmorgan
2012/07/24 13:38:49
true
Chen Yu
2012/07/24 14:34:20
Done.
|
+ } |
+ return isSingleCoreDevice; |
+} |
+ |
+std::string macAddress(std::string ifName) { |
+ NSMutableString* macString = [NSMutableString string]; |
stuartmorgan
2012/07/24 13:38:49
mac_string
Chen Yu
2012/07/24 14:34:20
Done.
|
+ |
+ struct ifaddrs* addrs; |
+ if (getifaddrs(&addrs) == 0) { |
+ struct ifaddrs* cursor = addrs; |
+ while (cursor != 0) { |
+ if ((cursor->ifa_addr->sa_family == AF_LINK) && |
+ strcmp(ifName.c_str(), cursor->ifa_name) == 0) { |
+ const struct sockaddr_dl* dlAddr = |
+ (const struct sockaddr_dl*)cursor->ifa_addr; |
+ const unsigned char* base = |
+ (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen]; |
+ int sdlAlen = dlAddr->sdl_alen; |
stuartmorgan
2012/07/24 13:38:49
sdl_alen
Chen Yu
2012/07/24 14:34:20
Done.
|
+ for (int i = 0; i < sdlAlen; ++i) { |
+ if (i != 0) |
+ [macString appendString:@":"]; |
+ [macString appendFormat:@"%02X", base[i]]; |
+ } |
+ break; |
+ } |
+ cursor = cursor->ifa_next; |
+ } |
+ freeifaddrs(addrs); |
+ } |
+ return std::string([macString UTF8String]);; |
+} |
+ |
+std::string randomId() { |
+ base::mac::ScopedCFTypeRef<CFUUIDRef> |
+ uuidObject(CFUUIDCreate(kCFAllocatorDefault)); |
stuartmorgan
2012/07/24 13:38:49
uuid_object
Chen Yu
2012/07/24 14:34:20
Done.
|
+ // Get the string representation of CFUUID object. |
+ 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.
|
+ [(NSString*)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) |
+ autorelease]; |
stuartmorgan
2012/07/24 13:38:49
Use ScopedCFTypeRef
Chen Yu
2012/07/24 14:34:20
Done.
|
+ return base::SysNSStringToUTF8(str); |
stuartmorgan
2012/07/24 13:38:49
SysCFStringRefToUTF8
Chen Yu
2012/07/24 14:34:20
Done.
|
+} |
+ |
+std::string deviceIdentifier(const char* const salt) { |
+ NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
+ NSString* clientId = [defaults stringForKey:kClientIdPreferenceKey]; |
stuartmorgan
2012/07/24 13:38:49
client_id
Chen Yu
2012/07/24 14:34:20
Done.
|
+ |
+ if (!clientId) { |
+ clientId = base::SysUTF8ToNSString(randomId()); |
+ [defaults setObject:clientId forKey:kClientIdPreferenceKey]; |
+ [defaults synchronize]; |
+ } |
+ |
+ NSData* data = [[NSString stringWithFormat:@"%@%s", clientId, |
+ salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding]; |
+ |
+ DCHECK(CC_SHA256_DIGEST_LENGTH >= 16); |
+ unsigned char hash[CC_SHA256_DIGEST_LENGTH]; |
+ void* result = CC_SHA256([data bytes], [data length], hash); |
+ DCHECK(result == hash); |
+ CFUUIDBytes* bytes = static_cast<CFUUIDBytes*>(result); |
stuartmorgan
2012/07/24 13:38:49
uuid_bytes
Chen Yu
2012/07/24 14:34:20
Done.
|
+ |
+ base::mac::ScopedCFTypeRef<CFUUIDRef> |
+ uuidObject(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *bytes)); |
+ NSString* str = |
stuartmorgan
2012/07/24 13:38:49
s/str/device_id/
|
+ [(NSString*)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) |
+ autorelease]; |
stuartmorgan
2012/07/24 13:38:49
Same here; scoped, and CF-based conversion.
Chen Yu
2012/07/24 14:34:20
Done.
|
+ return base::SysNSStringToUTF8(str); |
+} |
+ |
+} // namespace device_util |
+} // namespace ios |