local_device_info_util_mac.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
  5. #include <stddef.h>
  6. #include <sys/sysctl.h>
  7. #include <string>
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "base/strings/string_util.h"
  10. #include "base/strings/sys_string_conversions.h"
  11. namespace syncer {
  12. // Returns the Hardware model name, without trailing numbers, if
  13. // possible. See http://www.cocoadev.com/index.pl?MacintoshModels for
  14. // an example list of models. If an error occurs trying to read the
  15. // model, this simply returns "Unknown".
  16. std::string GetPersonalizableDeviceNameInternal() {
  17. // Do not use NSHost currentHost, as it's very slow. http://crbug.com/138570
  18. SCDynamicStoreContext context = {0, NULL, NULL, NULL};
  19. base::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate(
  20. kCFAllocatorDefault, CFSTR("chrome_sync"), NULL, &context));
  21. base::ScopedCFTypeRef<CFStringRef> machine_name(
  22. SCDynamicStoreCopyLocalHostName(store.get()));
  23. if (machine_name.get())
  24. return base::SysCFStringRefToUTF8(machine_name.get());
  25. // Fall back to get computer name.
  26. base::ScopedCFTypeRef<CFStringRef> computer_name(
  27. SCDynamicStoreCopyComputerName(store.get(), NULL));
  28. if (computer_name.get())
  29. return base::SysCFStringRefToUTF8(computer_name.get());
  30. // If all else fails, return to using a slightly nicer version of the
  31. // hardware model.
  32. char modelBuffer[256];
  33. size_t length = sizeof(modelBuffer);
  34. if (!sysctlbyname("hw.model", modelBuffer, &length, NULL, 0)) {
  35. for (size_t i = 0; i < length; i++) {
  36. if (base::IsAsciiDigit(modelBuffer[i]))
  37. return std::string(modelBuffer, 0, i);
  38. }
  39. return std::string(modelBuffer, 0, length);
  40. }
  41. return "Unknown";
  42. }
  43. } // namespace syncer