| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/ios/device_util.h" | 5 #include "base/ios/device_util.h" |
| 6 | 6 |
| 7 #include <CommonCrypto/CommonDigest.h> | 7 #include <CommonCrypto/CommonDigest.h> |
| 8 #import <UIKit/UIKit.h> | 8 #import <UIKit/UIKit.h> |
| 9 | 9 |
| 10 #include <ifaddrs.h> | 10 #include <ifaddrs.h> |
| 11 #include <net/if_dl.h> | 11 #include <net/if_dl.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 #include <sys/socket.h> | 13 #include <sys/socket.h> |
| 14 #include <sys/sysctl.h> | 14 #include <sys/sysctl.h> |
| 15 | 15 |
| 16 #include "base/ios/ios_util.h" | 16 #include "base/ios/ios_util.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 19 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 20 #include "base/mac/scoped_cftyperef.h" | 20 #include "base/mac/scoped_cftyperef.h" |
| 21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 22 #include "base/sys_string_conversions.h" | 22 #include "base/sys_string_conversions.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Client ID key in the user preferences. | 26 // Client ID key in the user preferences. |
| 27 NSString* const kClientIdPreferenceKey = @"ChromiumClientID"; | 27 NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID"; |
| 28 NSString* const kClientIdPreferenceKey = @"ChromeClientID"; |
| 28 // Default salt for device ids. | 29 // Default salt for device ids. |
| 29 const char kDefaultSalt[] = "Salt"; | 30 const char kDefaultSalt[] = "Salt"; |
| 31 // Zero UUID returned on buggy iOS devices. |
| 32 NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000"; |
| 33 |
| 34 NSString* GenerateClientId() { |
| 35 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 36 |
| 37 // Try to migrate from legacy client id. |
| 38 NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey]; |
| 39 |
| 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; |
| 53 } |
| 30 | 54 |
| 31 } // namespace | 55 } // namespace |
| 32 | 56 |
| 33 namespace ios { | 57 namespace ios { |
| 34 namespace device_util { | 58 namespace device_util { |
| 35 | 59 |
| 36 std::string GetPlatform() { | 60 std::string GetPlatform() { |
| 37 std::string platform; | 61 std::string platform; |
| 38 size_t size = 0; | 62 size_t size = 0; |
| 39 sysctlbyname("hw.machine", NULL, &size, NULL, 0); | 63 sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( | 121 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( |
| 98 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | 122 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
| 99 return base::SysCFStringRefToUTF8(uuid_string); | 123 return base::SysCFStringRefToUTF8(uuid_string); |
| 100 } | 124 } |
| 101 | 125 |
| 102 std::string GetDeviceIdentifier(const char* salt) { | 126 std::string GetDeviceIdentifier(const char* salt) { |
| 103 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 127 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 104 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; | 128 NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey]; |
| 105 | 129 |
| 106 if (!client_id) { | 130 if (!client_id) { |
| 107 if (base::ios::IsRunningOnIOS6OrLater()) | 131 client_id = GenerateClientId(); |
| 108 client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; | |
| 109 else | |
| 110 client_id = base::SysUTF8ToNSString(GetRandomId()); | |
| 111 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; | 132 [defaults setObject:client_id forKey:kClientIdPreferenceKey]; |
| 112 [defaults synchronize]; | 133 [defaults synchronize]; |
| 113 } | 134 } |
| 114 | 135 |
| 115 NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id, | 136 NSData* hash_data = [[NSString stringWithFormat:@"%@%s", client_id, |
| 116 salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding]; | 137 salt ? salt : kDefaultSalt] dataUsingEncoding:NSUTF8StringEncoding]; |
| 117 | 138 |
| 118 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; | 139 unsigned char hash[CC_SHA256_DIGEST_LENGTH]; |
| 119 CC_SHA256([hash_data bytes], [hash_data length], hash); | 140 CC_SHA256([hash_data bytes], [hash_data length], hash); |
| 120 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); | 141 CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash); |
| 121 | 142 |
| 122 base::mac::ScopedCFTypeRef<CFUUIDRef> | 143 base::mac::ScopedCFTypeRef<CFUUIDRef> |
| 123 uuid_object(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); | 144 uuid_object(CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes)); |
| 124 base::mac::ScopedCFTypeRef<CFStringRef> device_id( | 145 base::mac::ScopedCFTypeRef<CFStringRef> device_id( |
| 125 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); | 146 CFUUIDCreateString(kCFAllocatorDefault, uuid_object)); |
| 126 return base::SysCFStringRefToUTF8(device_id); | 147 return base::SysCFStringRefToUTF8(device_id); |
| 127 } | 148 } |
| 128 | 149 |
| 129 } // namespace device_util | 150 } // namespace device_util |
| 130 } // namespace ios | 151 } // namespace ios |
| OLD | NEW |