OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/storage_monitor/removable_device_notifications_mac.h" | 5 #include "chrome/browser/storage_monitor/storage_monitor_mac.h" |
6 | 6 |
7 #include "chrome/browser/storage_monitor/media_device_notifications_utils.h" | 7 #include "chrome/browser/storage_monitor/media_device_notifications_utils.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 | 9 |
10 namespace chrome { | 10 namespace chrome { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 const char kDiskImageModelName[] = "Disk Image"; | 14 const char kDiskImageModelName[] = "Disk Image"; |
15 | 15 |
16 void GetDiskInfoAndUpdateOnFileThread( | 16 void GetDiskInfoAndUpdateOnFileThread( |
17 const scoped_refptr<RemovableDeviceNotificationsMac>& notifications, | 17 const scoped_refptr<StorageMonitorMac>& monitor, |
18 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict, | 18 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict, |
19 RemovableDeviceNotificationsMac::UpdateType update_type) { | 19 StorageMonitorMac::UpdateType update_type) { |
20 DiskInfoMac info = DiskInfoMac::BuildDiskInfoOnFileThread(dict); | 20 DiskInfoMac info = DiskInfoMac::BuildDiskInfoOnFileThread(dict); |
21 if (info.device_id().empty()) | 21 if (info.device_id().empty()) |
22 return; | 22 return; |
23 | 23 |
24 content::BrowserThread::PostTask( | 24 content::BrowserThread::PostTask( |
25 content::BrowserThread::UI, | 25 content::BrowserThread::UI, |
26 FROM_HERE, | 26 FROM_HERE, |
27 base::Bind(&RemovableDeviceNotificationsMac::UpdateDisk, | 27 base::Bind(&StorageMonitorMac::UpdateDisk, |
28 notifications, | 28 monitor, |
29 info, | 29 info, |
30 update_type)); | 30 update_type)); |
31 } | 31 } |
32 | 32 |
33 void GetDiskInfoAndUpdate( | 33 void GetDiskInfoAndUpdate(const scoped_refptr<StorageMonitorMac>& monitor, |
34 const scoped_refptr<RemovableDeviceNotificationsMac>& notifications, | 34 DADiskRef disk, |
35 DADiskRef disk, | 35 StorageMonitorMac::UpdateType update_type) { |
36 RemovableDeviceNotificationsMac::UpdateType update_type) { | |
37 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk)); | 36 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk)); |
38 content::BrowserThread::PostTask( | 37 content::BrowserThread::PostTask( |
39 content::BrowserThread::FILE, | 38 content::BrowserThread::FILE, |
40 FROM_HERE, | 39 FROM_HERE, |
41 base::Bind(GetDiskInfoAndUpdateOnFileThread, | 40 base::Bind(GetDiskInfoAndUpdateOnFileThread, |
42 notifications, | 41 monitor, |
43 dict, | 42 dict, |
44 update_type)); | 43 update_type)); |
45 } | 44 } |
46 | 45 |
47 } // namespace | 46 } // namespace |
48 | 47 |
49 RemovableDeviceNotificationsMac::RemovableDeviceNotificationsMac() { | 48 StorageMonitorMac::StorageMonitorMac() { |
50 session_.reset(DASessionCreate(NULL)); | 49 session_.reset(DASessionCreate(NULL)); |
51 | 50 |
52 DASessionScheduleWithRunLoop( | 51 DASessionScheduleWithRunLoop( |
53 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); | 52 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); |
54 | 53 |
55 // Register for callbacks for attached, changed, and removed devices. | 54 // Register for callbacks for attached, changed, and removed devices. |
56 // This will send notifications for existing devices too. | 55 // This will send notifications for existing devices too. |
57 DARegisterDiskAppearedCallback( | 56 DARegisterDiskAppearedCallback( |
58 session_, | 57 session_, |
59 kDADiskDescriptionMatchVolumeMountable, | 58 kDADiskDescriptionMatchVolumeMountable, |
60 DiskAppearedCallback, | 59 DiskAppearedCallback, |
61 this); | 60 this); |
62 DARegisterDiskDisappearedCallback( | 61 DARegisterDiskDisappearedCallback( |
63 session_, | 62 session_, |
64 kDADiskDescriptionMatchVolumeMountable, | 63 kDADiskDescriptionMatchVolumeMountable, |
65 DiskDisappearedCallback, | 64 DiskDisappearedCallback, |
66 this); | 65 this); |
67 DARegisterDiskDescriptionChangedCallback( | 66 DARegisterDiskDescriptionChangedCallback( |
68 session_, | 67 session_, |
69 kDADiskDescriptionMatchVolumeMountable, | 68 kDADiskDescriptionMatchVolumeMountable, |
70 kDADiskDescriptionWatchVolumePath, | 69 kDADiskDescriptionWatchVolumePath, |
71 DiskDescriptionChangedCallback, | 70 DiskDescriptionChangedCallback, |
72 this); | 71 this); |
73 } | 72 } |
74 | 73 |
75 RemovableDeviceNotificationsMac::~RemovableDeviceNotificationsMac() { | 74 StorageMonitorMac::~StorageMonitorMac() { |
76 DASessionUnscheduleFromRunLoop( | 75 DASessionUnscheduleFromRunLoop( |
77 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); | 76 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); |
78 } | 77 } |
79 | 78 |
80 void RemovableDeviceNotificationsMac::UpdateDisk( | 79 void StorageMonitorMac::UpdateDisk(const DiskInfoMac& info, |
81 const DiskInfoMac& info, | 80 UpdateType update_type) { |
82 UpdateType update_type) { | |
83 if (info.bsd_name().empty()) | 81 if (info.bsd_name().empty()) |
84 return; | 82 return; |
85 | 83 |
86 std::map<std::string, DiskInfoMac>::iterator it = | 84 std::map<std::string, DiskInfoMac>::iterator it = |
87 disk_info_map_.find(info.bsd_name()); | 85 disk_info_map_.find(info.bsd_name()); |
88 if (it != disk_info_map_.end()) { | 86 if (it != disk_info_map_.end()) { |
89 // If an attached notification was previously posted then post a detached | 87 // If an attached notification was previously posted then post a detached |
90 // notification now. This is used for devices that are being removed or | 88 // notification now. This is used for devices that are being removed or |
91 // devices that have changed. | 89 // devices that have changed. |
92 if (ShouldPostNotificationForDisk(it->second)) { | 90 if (ShouldPostNotificationForDisk(it->second)) { |
(...skipping 10 matching lines...) Expand all Loading... |
103 info.device_name()); | 101 info.device_name()); |
104 if (ShouldPostNotificationForDisk(info)) { | 102 if (ShouldPostNotificationForDisk(info)) { |
105 string16 display_name = GetDisplayNameForDevice( | 103 string16 display_name = GetDisplayNameForDevice( |
106 info.total_size_in_bytes(), info.device_name()); | 104 info.total_size_in_bytes(), info.device_name()); |
107 receiver()->ProcessAttach(StorageInfo( | 105 receiver()->ProcessAttach(StorageInfo( |
108 info.device_id(), display_name, info.mount_point().value())); | 106 info.device_id(), display_name, info.mount_point().value())); |
109 } | 107 } |
110 } | 108 } |
111 } | 109 } |
112 | 110 |
113 bool RemovableDeviceNotificationsMac::GetStorageInfoForPath( | 111 bool StorageMonitorMac::GetStorageInfoForPath(const base::FilePath& path, |
114 const base::FilePath& path, | 112 StorageInfo* device_info) const { |
115 StorageInfo* device_info) const { | |
116 if (!path.IsAbsolute()) | 113 if (!path.IsAbsolute()) |
117 return false; | 114 return false; |
118 | 115 |
119 base::FilePath current = path; | 116 base::FilePath current = path; |
120 const base::FilePath root(base::FilePath::kSeparators); | 117 const base::FilePath root(base::FilePath::kSeparators); |
121 while (current != root) { | 118 while (current != root) { |
122 DiskInfoMac info; | 119 DiskInfoMac info; |
123 if (FindDiskWithMountPoint(current, &info)) { | 120 if (FindDiskWithMountPoint(current, &info)) { |
124 device_info->device_id = info.device_id(); | 121 device_info->device_id = info.device_id(); |
125 device_info->name = info.device_name(); | 122 device_info->name = info.device_name(); |
126 device_info->location = info.mount_point().value(); | 123 device_info->location = info.mount_point().value(); |
127 return true; | 124 return true; |
128 } | 125 } |
129 current = current.DirName(); | 126 current = current.DirName(); |
130 } | 127 } |
131 | 128 |
132 return false; | 129 return false; |
133 } | 130 } |
134 | 131 |
135 uint64 RemovableDeviceNotificationsMac::GetStorageSize( | 132 uint64 StorageMonitorMac::GetStorageSize(const std::string& location) const { |
136 const std::string& location) const { | |
137 DiskInfoMac info; | 133 DiskInfoMac info; |
138 if (!FindDiskWithMountPoint(base::FilePath(location), &info)) | 134 if (!FindDiskWithMountPoint(base::FilePath(location), &info)) |
139 return 0; | 135 return 0; |
140 return info.total_size_in_bytes(); | 136 return info.total_size_in_bytes(); |
141 } | 137 } |
142 | 138 |
143 // static | 139 // static |
144 void RemovableDeviceNotificationsMac::DiskAppearedCallback( | 140 void StorageMonitorMac::DiskAppearedCallback(DADiskRef disk, void* context) { |
145 DADiskRef disk, | 141 StorageMonitorMac* monitor = static_cast<StorageMonitorMac*>(context); |
146 void* context) { | 142 GetDiskInfoAndUpdate(monitor, disk, UPDATE_DEVICE_ADDED); |
147 RemovableDeviceNotificationsMac* notifications = | |
148 static_cast<RemovableDeviceNotificationsMac*>(context); | |
149 GetDiskInfoAndUpdate(notifications, | |
150 disk, | |
151 UPDATE_DEVICE_ADDED); | |
152 } | 143 } |
153 | 144 |
154 // static | 145 // static |
155 void RemovableDeviceNotificationsMac::DiskDisappearedCallback( | 146 void StorageMonitorMac::DiskDisappearedCallback(DADiskRef disk, void* context) { |
156 DADiskRef disk, | 147 StorageMonitorMac* monitor = static_cast<StorageMonitorMac*>(context); |
157 void* context) { | 148 GetDiskInfoAndUpdate(monitor, disk, UPDATE_DEVICE_REMOVED); |
158 RemovableDeviceNotificationsMac* notifications = | |
159 static_cast<RemovableDeviceNotificationsMac*>(context); | |
160 GetDiskInfoAndUpdate(notifications, | |
161 disk, | |
162 UPDATE_DEVICE_REMOVED); | |
163 } | 149 } |
164 | 150 |
165 // static | 151 // static |
166 void RemovableDeviceNotificationsMac::DiskDescriptionChangedCallback( | 152 void StorageMonitorMac::DiskDescriptionChangedCallback(DADiskRef disk, |
167 DADiskRef disk, | 153 CFArrayRef keys, |
168 CFArrayRef keys, | 154 void *context) { |
169 void *context) { | 155 StorageMonitorMac* monitor = static_cast<StorageMonitorMac*>(context); |
170 RemovableDeviceNotificationsMac* notifications = | 156 GetDiskInfoAndUpdate(monitor, disk, UPDATE_DEVICE_CHANGED); |
171 static_cast<RemovableDeviceNotificationsMac*>(context); | |
172 GetDiskInfoAndUpdate(notifications, | |
173 disk, | |
174 UPDATE_DEVICE_CHANGED); | |
175 } | 157 } |
176 | 158 |
177 bool RemovableDeviceNotificationsMac::ShouldPostNotificationForDisk( | 159 bool StorageMonitorMac::ShouldPostNotificationForDisk( |
178 const DiskInfoMac& info) const { | 160 const DiskInfoMac& info) const { |
179 // Only post notifications about disks that have no empty fields and | 161 // Only post notifications about disks that have no empty fields and |
180 // are removable. Also exclude disk images (DMGs). | 162 // are removable. Also exclude disk images (DMGs). |
181 return !info.bsd_name().empty() && | 163 return !info.bsd_name().empty() && |
182 !info.device_id().empty() && | 164 !info.device_id().empty() && |
183 !info.device_name().empty() && | 165 !info.device_name().empty() && |
184 !info.mount_point().empty() && | 166 !info.mount_point().empty() && |
185 info.model_name() != kDiskImageModelName && | 167 info.model_name() != kDiskImageModelName && |
186 (info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM || | 168 (info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM || |
187 info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); | 169 info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); |
188 } | 170 } |
189 | 171 |
190 bool RemovableDeviceNotificationsMac::FindDiskWithMountPoint( | 172 bool StorageMonitorMac::FindDiskWithMountPoint( |
191 const base::FilePath& mount_point, | 173 const base::FilePath& mount_point, |
192 DiskInfoMac* info) const { | 174 DiskInfoMac* info) const { |
193 for (std::map<std::string, DiskInfoMac>::const_iterator | 175 for (std::map<std::string, DiskInfoMac>::const_iterator |
194 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) { | 176 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) { |
195 if (it->second.mount_point() == mount_point) { | 177 if (it->second.mount_point() == mount_point) { |
196 *info = it->second; | 178 *info = it->second; |
197 return true; | 179 return true; |
198 } | 180 } |
199 } | 181 } |
200 return false; | 182 return false; |
201 } | 183 } |
202 | 184 |
203 } // namespace chrome | 185 } // namespace chrome |
OLD | NEW |