device_util.mm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include "base/ios/device_util.h"
  5. #include <CommonCrypto/CommonDigest.h>
  6. #import <UIKit/UIKit.h>
  7. #include <ifaddrs.h>
  8. #include <net/if_dl.h>
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/sysctl.h>
  13. #include <memory>
  14. #include "base/check.h"
  15. #include "base/mac/scoped_cftyperef.h"
  16. #include "base/numerics/safe_conversions.h"
  17. #include "base/strings/string_util.h"
  18. #include "base/strings/stringprintf.h"
  19. #include "base/strings/sys_string_conversions.h"
  20. namespace {
  21. // Client ID key in the user preferences.
  22. NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID";
  23. NSString* const kClientIdPreferenceKey = @"ChromeClientID";
  24. // Current hardware type. This is used to detect that a device has been backed
  25. // up and restored to another device, and allows regenerating a new device id.
  26. NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType";
  27. // Default salt for device ids.
  28. const char kDefaultSalt[] = "Salt";
  29. // Zero UUID returned on buggy iOS devices.
  30. NSString* const kZeroUUID = @"00000000-0000-0000-0000-000000000000";
  31. NSString* GenerateClientId() {
  32. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  33. // Try to migrate from legacy client id.
  34. NSString* client_id = [defaults stringForKey:kLegacyClientIdPreferenceKey];
  35. // Some iOS6 devices return a buggy identifierForVendor:
  36. // http://openradar.appspot.com/12377282. If this is the case, revert to
  37. // generating a new one.
  38. if (!client_id || [client_id isEqualToString:kZeroUUID]) {
  39. client_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  40. if ([client_id isEqualToString:kZeroUUID])
  41. client_id = base::SysUTF8ToNSString(ios::device_util::GetRandomId());
  42. }
  43. return client_id;
  44. }
  45. } // namespace
  46. namespace ios {
  47. namespace device_util {
  48. std::string GetPlatform() {
  49. std::string platform;
  50. size_t size = 0;
  51. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  52. sysctlbyname("hw.machine", base::WriteInto(&platform, size), &size, NULL, 0);
  53. return platform;
  54. }
  55. bool RamIsAtLeast512Mb() {
  56. // 512MB devices report anywhere from 502-504 MB, use 450 MB just to be safe.
  57. return RamIsAtLeast(450);
  58. }
  59. bool RamIsAtLeast1024Mb() {
  60. // 1GB devices report anywhere from 975-999 MB, use 900 MB just to be safe.
  61. return RamIsAtLeast(900);
  62. }
  63. bool RamIsAtLeast(uint64_t ram_in_mb) {
  64. uint64_t memory_size = 0;
  65. size_t size = sizeof(memory_size);
  66. if (sysctlbyname("hw.memsize", &memory_size, &size, NULL, 0) == 0) {
  67. // Anything >= 500M, call high ram.
  68. return memory_size >= ram_in_mb * 1024 * 1024;
  69. }
  70. return false;
  71. }
  72. bool IsSingleCoreDevice() {
  73. uint64_t cpu_number = 0;
  74. size_t sizes = sizeof(cpu_number);
  75. sysctlbyname("hw.physicalcpu", &cpu_number, &sizes, NULL, 0);
  76. return cpu_number == 1;
  77. }
  78. std::string GetMacAddress(const std::string& interface_name) {
  79. std::string mac_string;
  80. struct ifaddrs* addresses;
  81. if (getifaddrs(&addresses) == 0) {
  82. for (struct ifaddrs* address = addresses; address;
  83. address = address->ifa_next) {
  84. if ((address->ifa_addr->sa_family == AF_LINK) &&
  85. strcmp(interface_name.c_str(), address->ifa_name) == 0) {
  86. const struct sockaddr_dl* found_address_struct =
  87. reinterpret_cast<const struct sockaddr_dl*>(address->ifa_addr);
  88. // |found_address_struct->sdl_data| contains the interface name followed
  89. // by the interface address. The address part can be accessed based on
  90. // the length of the name, that is, |found_address_struct->sdl_nlen|.
  91. const unsigned char* found_address =
  92. reinterpret_cast<const unsigned char*>(
  93. &found_address_struct->sdl_data[
  94. found_address_struct->sdl_nlen]);
  95. int found_address_length = found_address_struct->sdl_alen;
  96. for (int i = 0; i < found_address_length; ++i) {
  97. if (i != 0)
  98. mac_string.push_back(':');
  99. base::StringAppendF(&mac_string, "%02X", found_address[i]);
  100. }
  101. break;
  102. }
  103. }
  104. freeifaddrs(addresses);
  105. }
  106. return mac_string;
  107. }
  108. std::string GetRandomId() {
  109. base::ScopedCFTypeRef<CFUUIDRef> uuid_object(
  110. CFUUIDCreate(kCFAllocatorDefault));
  111. base::ScopedCFTypeRef<CFStringRef> uuid_string(
  112. CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
  113. return base::SysCFStringRefToUTF8(uuid_string);
  114. }
  115. std::string GetDeviceIdentifier(const char* salt) {
  116. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  117. NSString* last_seen_hardware =
  118. [defaults stringForKey:kHardwareTypePreferenceKey];
  119. NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform());
  120. if (!last_seen_hardware) {
  121. last_seen_hardware = current_hardware;
  122. [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
  123. [defaults synchronize];
  124. }
  125. NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
  126. if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) {
  127. client_id = GenerateClientId();
  128. [defaults setObject:client_id forKey:kClientIdPreferenceKey];
  129. [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
  130. [defaults synchronize];
  131. }
  132. return GetSaltedString(base::SysNSStringToUTF8(client_id),
  133. salt ? salt : kDefaultSalt);
  134. }
  135. std::string GetVendorId() {
  136. return base::SysNSStringToUTF8(
  137. [[[UIDevice currentDevice] identifierForVendor] UUIDString]);
  138. }
  139. std::string GetSaltedString(const std::string& in_string,
  140. const std::string& salt) {
  141. DCHECK(salt.length());
  142. NSData* hash_data = [base::SysUTF8ToNSString(in_string + salt)
  143. dataUsingEncoding:NSUTF8StringEncoding];
  144. unsigned char hash[CC_SHA256_DIGEST_LENGTH];
  145. CC_SHA256([hash_data bytes], base::checked_cast<CC_LONG>([hash_data length]),
  146. hash);
  147. CFUUIDBytes* uuid_bytes = reinterpret_cast<CFUUIDBytes*>(hash);
  148. base::ScopedCFTypeRef<CFUUIDRef> uuid_object(
  149. CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *uuid_bytes));
  150. base::ScopedCFTypeRef<CFStringRef> device_id(
  151. CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
  152. return base::SysCFStringRefToUTF8(device_id);
  153. }
  154. } // namespace device_util
  155. } // namespace ios