Home | History | Annotate | Download | only in util
      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 
      5 #include "sync/util/get_session_name_mac.h"
      6 
      7 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
      8 #include <sys/sysctl.h>
      9 
     10 #include "base/mac/scoped_cftyperef.h"
     11 #include "base/strings/string_util.h"
     12 #include "base/strings/sys_string_conversions.h"
     13 
     14 namespace syncer {
     15 namespace internal {
     16 
     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());
     26 
     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());
     32 
     33   // If all else fails, return to using a slightly nicer version of the
     34   // hardware model.
     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 (IsAsciiDigit(modelBuffer[i]))
     40         return std::string(modelBuffer, 0, i);
     41     }
     42     return std::string(modelBuffer, 0, length);
     43   }
     44   return "Unknown";
     45 }
     46 
     47 }  // namespace internal
     48 }  // namespace syncer
     49