Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(724)

Unified Diff: base/ios/device_util.mm

Issue 10818023: Implements iOS device util methods. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix a comment, add const to NSString* and fix a format Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b0d58dc350b9fa5bc69212f92ca9c1b6816abe4e
--- /dev/null
+++ b/base/ios/device_util.mm
@@ -0,0 +1,137 @@
+// 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>
Mark Mentovai 2012/07/25 14:00:23 #include this. It’s not Objective-C. #import is on
Chen Yu 2012/07/27 16:28:17 Done.
+#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* const kClientIdPreferenceKey = @"ChromiumClientID";
+// Default salt for device ids.
+const char* const kDefaultSalt = "Salt";
Mark Mentovai 2012/07/25 14:00:23 Why not const char kDefaultSalt[] = "Salt"; instea
Mark Mentovai 2012/07/25 14:00:23 What is special about this that an empty string ("
Chen Yu 2012/07/27 16:28:17 Nothing special actually. But "Salt" has been used
+
+} // 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();
Mark Mentovai 2012/07/25 14:00:23 Why the copy? Why not just pass result’s buffer to
Chen Yu 2012/07/27 16:28:17 Done.
+
+ 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);
Mark Mentovai 2012/07/25 14:00:23 The parentheses aren’t necessary.
Chen Yu 2012/07/27 16:28:17 Done.
+ }
+
+ return result;
+}
+
+bool IsSingleCoreDevice() {
+ static bool is_init_check = false;
Mark Mentovai 2012/07/25 14:00:23 Don’t do this thing with two statics. Use the comp
Chen Yu 2012/07/27 16:28:17 Changed not to use cache. On 2012/07/25 14:00:23,
+ 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 =
Mark Mentovai 2012/07/25 14:00:23 You can’t use something like hw.ncpu?
Chen Yu 2012/07/27 16:28:17 Thanks for the suggestions! The hardcoded-list mig
+ [NSArray arrayWithObjects:@"iPhone2,1", @"iPad1,1", @"iPhone3,1", nil];
+ is_single_core_device = [single_core_devices containsObject:
+ base::SysUTF8ToNSString(GetPlatform())];
Mark Mentovai 2012/07/25 14:00:23 Since GetPlatform() returns a std::string, I think
+ is_init_check = true;
+ }
+ return is_single_core_device;
+}
+
+std::string GetMacAddress(std::string ifName) {
Mark Mentovai 2012/07/25 14:00:23 interface_name, as before.
Chen Yu 2012/07/27 16:28:17 Done.
+ NSMutableString* mac_string = [NSMutableString string];
+
+ struct ifaddrs* addrs;
Mark Mentovai 2012/07/25 14:00:23 Don’t abbreviate. addresses.
Chen Yu 2012/07/27 16:28:17 Done.
+ if (getifaddrs(&addrs) == 0) {
+ struct ifaddrs* cursor = addrs;
Mark Mentovai 2012/07/25 14:00:23 cursor to what? Call this |address|.
Chen Yu 2012/07/27 16:28:17 Done.
+ while (cursor != 0) {
Mark Mentovai 2012/07/25 14:00:23 Use NULL for pointer comparisons to zero, or just
Mark Mentovai 2012/07/25 14:00:23 for (struct ifaddrs* address = addresses; address;
Chen Yu 2012/07/27 16:28:17 Done.
Chen Yu 2012/07/27 16:28:17 Done.
+ if ((cursor->ifa_addr->sa_family == AF_LINK) &&
+ strcmp(ifName.c_str(), cursor->ifa_name) == 0) {
+ const struct sockaddr_dl* dlAddr =
Mark Mentovai 2012/07/25 14:00:23 This, too, is namedImproperly.
Chen Yu 2012/07/27 16:28:17 Done.
+ (const struct sockaddr_dl*)cursor->ifa_addr;
Mark Mentovai 2012/07/25 14:00:23 Don’t use (c_style)casts. Use c_plus_plus<style>(c
Chen Yu 2012/07/27 16:28:17 Done.
+ const unsigned char* base =
Mark Mentovai 2012/07/25 14:00:23 base is a bad name. base of what?
Chen Yu 2012/07/27 16:28:17 Done.
+ (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen];
Mark Mentovai 2012/07/25 14:00:23 Casting again.
Chen Yu 2012/07/27 16:28:17 Done.
+ int sdl_alen = dlAddr->sdl_alen;
+ for (int i = 0; i < sdl_alen; ++i) {
+ if (i != 0)
+ [mac_string appendString:@":"];
Mark Mentovai 2012/07/25 14:00:23 Come on, we have a whole suite of StringPrintf uti
Chen Yu 2012/07/27 16:28:17 Done.
+ [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.
Mark Mentovai 2012/07/25 14:00:23 This comment doesn’t contribute anything.
Chen Yu 2012/07/27 16:28:17 Done.
+ base::mac::ScopedCFTypeRef<CFStringRef> uuid_string(
+ CFUUIDCreateString(kCFAllocatorDefault, uuid_object));
+ return base::SysCFStringRefToUTF8(uuid_string);
+}
+
+std::string GetDeviceIdentifier(const char* const salt) {
Mark Mentovai 2012/07/25 14:00:23 If you wouldn’t accept a const int argument or mak
Chen Yu 2012/07/27 16:28:17 Done.
+ 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);
Mark Mentovai 2012/07/25 14:00:23 This is a compile-time condition. It’s not somethi
Mark Mentovai 2012/07/25 14:00:23 Why do you care? What’s special about 16?
Chen Yu 2012/07/27 16:28:17 I removed this COMPILE_ASSERT. On 2012/07/25 14:00
+ 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

Powered by Google App Engine
This is Rietveld 408576698