Update .DEPS.git
[chromium-blink-merge.git] / base / ios / device_util.mm
blobb538ea858a5558987e268e2e7aed78ecaee805a0
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.
5 #include "base/ios/device_util.h"
7 #include <CommonCrypto/CommonDigest.h>
8 #import <UIKit/UIKit.h>
10 #include <ifaddrs.h>
11 #include <net/if_dl.h>
12 #include <string.h>
13 #include <sys/socket.h>
14 #include <sys/sysctl.h>
16 #include "base/ios/ios_util.h"
17 #include "base/logging.h"
18 #include "base/string_util.h"
19 #include "base/stringprintf.h"
20 #include "base/mac/scoped_cftyperef.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/sys_string_conversions.h"
24 namespace {
26 // Client ID key in the user preferences.
27 NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID";
28 NSString* const kClientIdPreferenceKey = @"ChromeClientID";
29 // Default salt for device ids.
30 const char kDefaultSalt[] = "Salt";
31 // Zero UUID returned on buggy iOS devices.
32 NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000";
34 NSString* GenerateClientId() {
35   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
37   // Try to migrate from legacy client id.
38   NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey];
40   // Some iOS6 devices return a buggy identifierForVendor:
41   // http://openradar.appspot.com/12377282. If this is the case, revert to
42   // generating a new one.
43   if (!client_id || [client_id isEqualToString:kZeroUUID]) {
44     if (base::ios::IsRunningOnIOS6OrLater()) {
45       client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
46       if ([client_id isEqualToString:kZeroUUID])
47         client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
48     } else {
49       client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
50     }
51   }
52   return client_id;
55 }  // namespace
57 namespace ios {
58 namespace device_util {
60 std::string GetPlatform() {
61   std::string platform;
62   size_t size = 0;
63   sysctlbyname("hw.machine", NULL, &size, NULL, 0);
64   sysctlbyname("hw.machine", WriteInto(&platform, size), &size, NULL, 0);
65   return platform;
68 bool IsRunningOnHighRamDevice() {
69   uint64_t memory_size = 0;
70   size_t size = sizeof(memory_size);
71   if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) {
72     // Anything >= 250M, call high ram.
73     return memory_size >= 250 * 1024 * 1024;
74   }
75   return false;
78 bool IsSingleCoreDevice() {
79   uint64_t cpu_number = 0;
80   size_t sizes = sizeof(cpu_number);
81   sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0);
82   return cpu_number == 1;
85 std::string GetMacAddress(const std::string& interface_name) {
86   std::string mac_string;
87   struct ifaddrs* addresses;
88   if (getifaddrs(&addresses) == 0) {
89     for (struct ifaddrs* address = addresses; address;
90          address = address->ifa_next) {
91       if ((address->ifa_addr->sa_family == AF_LINK) &&
92           strcmp(interface_name.c_str(), address->ifa_name) == 0) {
93         const struct sockaddr_dl* found_address_struct =
94             reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr);
96         // |found_address_struct->sdl_data| contains the interface name followed
97         // by the interface address. The address part can be accessed based on
98         // the length of the name, that is, |found_address_struct->sdl_nlen|.
99         const unsigned char* found_address =
100             reinterpret_cast<const unsigned char*>(
101                 &found_address_struct->sdl_data[
102                     found_address_struct->sdl_nlen]);
104         int found_address_length = found_address_struct->sdl_alen;
105         for (int i = 0; i < found_address_length; ++i) {
106           if (i != 0)
107             mac_string.push_back(':');
108           base::StringAppendF(&mac_string, "%02X", found_address[i]);
109         }
110         break;
111       }
112     }
113     freeifaddrs(addresses);
114   }
115   return mac_string;
118 std::string GetRandomId() {
119   base::mac::ScopedCFTypeRef<CFUUIDRef>
120       uuid_object(CFUUIDCreate(kCFAllocatorDefault));
121   base::mac::ScopedCFTypeRef<CFStringRef> uuid_string(
122       CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
123   return base::SysCFStringRefToUTF8(uuid_string);
126 std::string GetDeviceIdentifier(const char* salt) {
127   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
128   NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
130   if (!client_id) {
131     client_id = GenerateClientId();
132     [defaults setObject:client_id forKey:kClientIdPreferenceKey];
133     [defaults synchronize];
134   }
136   NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id,
137       salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding];
139   unsigned char hash[CC_SHA256_DIGEST_LENGTH];
140   CC_SHA256([hash_data bytes], [hash_data length], hash);
141   CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash);
143   base::mac::ScopedCFTypeRef<CFUUIDRef>
144       uuid_object(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes));
145   base::mac::ScopedCFTypeRef<CFStringRef> device_id(
146       CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
147   return base::SysCFStringRefToUTF8(device_id);
150 }  // namespace device_util
151 }  // namespace ios