blob: 8dc21f5de0b0749a59fbcbeed39388bb7f36384d [file] [log] [blame]
// 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.
#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#include <stddef.h>
#include <sys/sysctl.h>
#include <string>
#include "base/mac/scoped_cftyperef.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
namespace syncer {
// Returns the Hardware model name, without trailing numbers, if
// possible. See for
// an example list of models. If an error occurs trying to read the
// model, this simply returns "Unknown".
std::string GetPersonalizableDeviceNameInternal() {
// Do not use NSHost currentHost, as it's very slow.
SCDynamicStoreContext context = {0, NULL, NULL, NULL};
base::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate(
kCFAllocatorDefault, CFSTR("chrome_sync"), NULL, &context));
base::ScopedCFTypeRef<CFStringRef> machine_name(
if (machine_name.get())
return base::SysCFStringRefToUTF8(machine_name.get());
// Fall back to get computer name.
base::ScopedCFTypeRef<CFStringRef> computer_name(
SCDynamicStoreCopyComputerName(store.get(), NULL));
if (computer_name.get())
return base::SysCFStringRefToUTF8(computer_name.get());
// If all else fails, return to using a slightly nicer version of the
// hardware model.
char modelBuffer[256];
size_t length = sizeof(modelBuffer);
if (!sysctlbyname("hw.model", modelBuffer, &length, NULL, 0)) {
for (size_t i = 0; i < length; i++) {
if (base::IsAsciiDigit(modelBuffer[i]))
return std::string(modelBuffer, 0, i);
return std::string(modelBuffer, 0, length);
return "Unknown";
} // namespace syncer