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 #include <sys/sysctl.h>
8 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/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::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate(
21 kCFAllocatorDefault, CFSTR("chrome_sync"), NULL, &context));
22 base::ScopedCFTypeRef<CFStringRef> machine_name(
23 SCDynamicStoreCopyLocalHostName(store.get()));
24 if (machine_name.get())
25 return base::SysCFStringRefToUTF8(machine_name.get());
27 // Fall back to get computer name.
28 base::ScopedCFTypeRef<CFStringRef> computer_name(
29 SCDynamicStoreCopyComputerName(store.get(), NULL));
30 if (computer_name.get())
31 return base::SysCFStringRefToUTF8(computer_name.get());
33 // If all else fails, return to using a slightly nicer version of the
35 char modelBuffer[256];
36 size_t length = sizeof(modelBuffer);
37 if (!sysctlbyname("hw.model", modelBuffer, &length, NULL, 0)) {
38 for (size_t i = 0; i < length; i++) {
39 if (base::IsAsciiDigit(modelBuffer[i]))
40 return std::string(modelBuffer, 0, i);
42 return std::string(modelBuffer, 0, length);
47 } // namespace internal