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..5949cfaa261ebe5a81d62a5072aa04c2486f11d8 |
--- /dev/null |
+++ b/base/ios/device_util.mm |
@@ -0,0 +1,138 @@ |
+// 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 <CommonCrypto/CommonDigest.h> |
+#import <Foundation/Foundation.h> |
+ |
+#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"; |
stuartmorgan
2012/07/25 11:48:59
NSString* const
Chen Yu
2012/07/25 12:44:55
Done.
|
+// Default salt for device ids. |
+const char* const kDefaultSalt = "Salt"; |
+ |
+} // namespace |
+ |
+namespace ios { |
+namespace device_util { |
+ |
+std::string GetPlatform() { |
+ 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 = false; |
+ |
+ 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 is_init_check = false; |
+ static bool is_single_core_device = false; |
+ if (!is_init_check) { |
+ // "iPhone2,1" is an iPhone 3GS, "iPad1,1" is an iPad 1, and "iPhone3,1" is |
+ // an iPhone 4. |
+ NSArray* single_core_devices = |
+ [NSArray arrayWithObjects:@"iPhone2,1", @"iPad1,1", @"iPhone3,1", nil]; |
+ is_single_core_device = |
+ [single_core_devices containsObject: |
stuartmorgan
2012/07/25 11:48:59
Move this up to the previous line, and reduce the
Chen Yu
2012/07/25 12:44:55
Done.
|
+ base::SysUTF8ToNSString(GetPlatform())]; |
+ is_init_check = true; |
+ } |
+ return is_single_core_device; |
+} |
+ |
+std::string GetMacAddress(std::string ifName) { |
+ NSMutableString* mac_string = [NSMutableString string]; |
+ |
+ 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 sdl_alen = dlAddr->sdl_alen; |
+ for (int i = 0; i < sdl_alen; ++i) { |
+ if (i != 0) |
+ [mac_string appendString:@":"]; |
+ [mac_string appendFormat:@"%02X", base[i]]; |
+ } |
+ break; |
+ } |
+ cursor = cursor->ifa_next; |
+ } |
+ freeifaddrs(addrs); |
+ } |
+ return std::string([mac_string UTF8String]);; |
+} |
+ |
+std::string GetRandomId() { |
+ base::mac::ScopedCFTypeRef<CFUUIDRef> |
+ uuid_object(CFUUIDCreate(kCFAllocatorDefault)); |
+ // Get the string representation of CFUUID object. |
+ base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( |
+ CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
+ return base::SysCFStringRefToUTF8(uuid_string); |
+} |
+ |
+std::string GetDeviceIdentifier(const char* const salt) { |
+ NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
+ NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; |
+ |
+ if (!client_id) { |
+ client_id = base::SysUTF8ToNSString(GetRandomId()); |
+ [defaults setObject:client_id forKey:kClientIdPreferenceKey]; |
+ [defaults synchronize]; |
+ } |
+ |
+ NSData* data = [[NSString stringWithFormat:@"%@%s", client_id, |
+ 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* uuid_bytes = static_cast<CFUUIDBytes*>(result); |
+ |
+ 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 |