Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / system_monitor / disk_info_mac.mm
blobe8d67de6f96178739a9b2167aa6af6a249606b5f
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 "chrome/browser/system_monitor/disk_info_mac.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/sys_string_conversions.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/system_monitor/media_device_notifications_utils.h"
11 #include "chrome/browser/system_monitor/media_storage_util.h"
12 #include "content/public/browser/browser_thread.h"
14 namespace chrome {
15 namespace {
17 string16 GetUTF16FromDictionary(CFDictionaryRef dictionary, CFStringRef key) {
18   CFStringRef value =
19       base::mac::GetValueFromDictionary<CFStringRef>(dictionary, key);
20   if (!value)
21     return string16();
22   return base::SysCFStringRefToUTF16(value);
25 string16 JoinName(const string16& name, const string16& addition) {
26   if (addition.empty())
27     return name;
28   if (name.empty())
29     return addition;
30   return name + static_cast<char16>(' ') + addition;
33 MediaStorageUtil::Type GetDeviceType(bool is_removable, bool has_dcim) {
34   if (!is_removable)
35     return MediaStorageUtil::FIXED_MASS_STORAGE;
36   if (has_dcim)
37     return MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM;
38   return MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
41 }  // namespace
43 DiskInfoMac::DiskInfoMac() {
46 DiskInfoMac::~DiskInfoMac() {
49 // static
50 DiskInfoMac DiskInfoMac::BuildDiskInfoOnFileThread(CFDictionaryRef dict) {
51   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
52   DiskInfoMac info;
54   CFStringRef bsd_name = base::mac::GetValueFromDictionary<CFStringRef>(
55       dict, kDADiskDescriptionMediaBSDNameKey);
56   if (bsd_name)
57     info.bsd_name_ = base::SysCFStringRefToUTF8(bsd_name);
59   CFURLRef url = base::mac::GetValueFromDictionary<CFURLRef>(
60       dict, kDADiskDescriptionVolumePathKey);
61   NSURL* nsurl = base::mac::CFToNSCast(url);
62   info.mount_point_ = base::mac::NSStringToFilePath([nsurl path]);
64   string16 vendor_name = GetUTF16FromDictionary(
65       dict, kDADiskDescriptionDeviceVendorKey);
66   string16 model_name = GetUTF16FromDictionary(
67       dict, kDADiskDescriptionDeviceModelKey);
68   string16 volume_name = GetUTF16FromDictionary(
69       dict, kDADiskDescriptionVolumeNameKey);
70   info.display_name_ = vendor_name;
71   info.display_name_ = JoinName(info.display_name_, model_name);
72   info.display_name_ = JoinName(info.display_name_, volume_name);
73   info.model_name_ = UTF16ToUTF8(model_name);
75   CFUUIDRef uuid = base::mac::GetValueFromDictionary<CFUUIDRef>(
76       dict, kDADiskDescriptionVolumeUUIDKey);
77   std::string unique_id;
78   if (uuid) {
79     base::mac::ScopedCFTypeRef<CFStringRef> uuid_string(
80         CFUUIDCreateString(NULL, uuid));
81     if (uuid_string.get())
82       unique_id = base::SysCFStringRefToUTF8(uuid_string);
83   }
84   if (unique_id.empty()) {
85     string16 revision = GetUTF16FromDictionary(
86         dict, kDADiskDescriptionDeviceRevisionKey);
87     string16 unique_id2 = vendor_name;
88     unique_id2 = JoinName(unique_id2, model_name);
89     unique_id2 = JoinName(unique_id2, revision);
90     unique_id = UTF16ToUTF8(unique_id2);
91   }
93   CFBooleanRef is_removable_ref =
94       base::mac::GetValueFromDictionary<CFBooleanRef>(
95           dict, kDADiskDescriptionMediaRemovableKey);
96   bool is_removable = is_removable_ref && CFBooleanGetValue(is_removable_ref);
97   // Checking for DCIM only matters on removable devices.
98   bool has_dcim = is_removable && IsMediaDevice(info.mount_point_.value());
99   info.type_ = GetDeviceType(is_removable, has_dcim);
100   if (!unique_id.empty())
101     info.device_id_ = MediaStorageUtil::MakeDeviceId(info.type_, unique_id);
103   return info;
106 }  // namesapce chrome