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 "sync/util/get_session_name_mac.h"
7 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
8 #include <sys/sysctl.h>
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/string_util.h"
12 #include "base/sys_string_conversions.h"
17 std::string GetHardwareModelName() {
18 // Do not use NSHost currentHost, as it's very slow. http://crbug.com/138570
19 SCDynamicStoreContext context = { 0, NULL, NULL, NULL };
20 base::mac::ScopedCFTypeRef<SCDynamicStoreRef> store(
21 SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("chrome_sync"),
23 base::mac::ScopedCFTypeRef<CFStringRef> machine_name(
24 SCDynamicStoreCopyLocalHostName(store.get()));
25 if (machine_name.get())
26 return base::SysCFStringRefToUTF8(machine_name.get());
28 // Fall back to get computer name.
29 base::mac::ScopedCFTypeRef<CFStringRef> computer_name(
30 SCDynamicStoreCopyComputerName(store.get(), NULL));
31 if (computer_name.get())
32 return base::SysCFStringRefToUTF8(computer_name.get());
34 // If all else fails, return to using a slightly nicer version of the
36 char modelBuffer[256];
37 size_t length = sizeof(modelBuffer);
38 if (!sysctlbyname("hw.model", modelBuffer, &length, NULL, 0)) {
39 for (size_t i = 0; i < length; i++) {
40 if (IsAsciiDigit(modelBuffer[i]))
41 return std::string(modelBuffer, 0, i);
43 return std::string(modelBuffer, 0, length);
48 } // namespace internal