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