Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/system_monitor/removable_device_notifications_mac.h" | |
| 6 | |
| 7 #include "base/system_monitor/system_monitor.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 | |
| 10 namespace chrome { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 void GetDiskInfoAndUpdateOnFileThread( | |
| 15 const base::WeakPtr<RemovableDeviceNotificationsMac>& notifications, | |
| 16 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict, | |
| 17 RemovableDeviceNotificationsMac::UpdateAction update_action) { | |
| 18 DiskInfoMac info = DiskInfoMac::BuildDiskInfoOnFileThread(dict); | |
| 19 if (info.device_id().empty()) | |
| 20 return; | |
| 21 | |
| 22 content::BrowserThread::PostTask( | |
| 23 content::BrowserThread::UI, | |
| 24 FROM_HERE, | |
| 25 base::Bind(&RemovableDeviceNotificationsMac::UpdateDisk, | |
| 26 notifications, | |
| 27 info, | |
| 28 update_action)); | |
| 29 } | |
| 30 | |
| 31 void GetDiskInfoAndUpdate( | |
| 32 const base::WeakPtr<RemovableDeviceNotificationsMac>& notifications, | |
| 33 DADiskRef disk, | |
| 34 RemovableDeviceNotificationsMac::UpdateAction update_action) { | |
| 35 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk)); | |
| 36 content::BrowserThread::PostTask( | |
| 37 content::BrowserThread::FILE, | |
| 38 FROM_HERE, | |
| 39 base::Bind(GetDiskInfoAndUpdateOnFileThread, | |
| 40 notifications, | |
| 41 dict, | |
| 42 update_action)); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 RemovableDeviceNotificationsMac::RemovableDeviceNotificationsMac() { | |
| 48 session_.reset(DASessionCreate(NULL)); | |
| 49 DASessionScheduleWithRunLoop( | |
| 50 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); | |
| 51 | |
| 52 // Register for callbacks for attached, changed, and removed devices. | |
| 53 // This will send notifications for existing devices too. | |
| 54 DARegisterDiskAppearedCallback( | |
| 55 session_, | |
| 56 kDADiskDescriptionMatchVolumeMountable, | |
| 57 DiskAppearedCallback, | |
| 58 this); | |
| 59 DARegisterDiskDisappearedCallback( | |
| 60 session_, | |
| 61 kDADiskDescriptionMatchVolumeMountable, | |
| 62 DiskDisappearedCallback, | |
| 63 this); | |
| 64 DARegisterDiskDescriptionChangedCallback( | |
| 65 session_, | |
| 66 kDADiskDescriptionMatchVolumeMountable, | |
| 67 kDADiskDescriptionWatchVolumePath, | |
| 68 DiskDescriptionChangedCallback, | |
| 69 this); | |
| 70 } | |
| 71 | |
| 72 RemovableDeviceNotificationsMac::~RemovableDeviceNotificationsMac() { | |
| 73 DASessionUnscheduleFromRunLoop( | |
| 74 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); | |
| 75 } | |
| 76 | |
| 77 void RemovableDeviceNotificationsMac::UpdateDisk( | |
| 78 const DiskInfoMac& info, | |
| 79 UpdateAction update_action) { | |
| 80 if (info.bsd_name().empty()) | |
| 81 return; | |
| 82 | |
| 83 std::map<std::string, DiskInfoMac>::iterator it = | |
| 84 disk_info_map_.find(info.bsd_name()); | |
| 85 if (it != disk_info_map_.end()) { | |
| 86 if (ShouldPostNotificationForDisk(it->second)) { | |
|
vandebo (ex-Chrome)
2012/09/11 00:45:50
Should we check to see that one of the properties
sail
2012/09/11 03:02:00
Currently the DescriptionChanged event is fired fo
| |
| 87 base::SystemMonitor::Get()->ProcessRemovableStorageDetached( | |
| 88 it->second.device_id()); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 if (update_action == UPDATE_SHOULD_REMOVE) { | |
| 93 if (it != disk_info_map_.end()) | |
| 94 disk_info_map_.erase(it); | |
| 95 } else { | |
| 96 disk_info_map_[info.bsd_name()] = info; | |
| 97 if (ShouldPostNotificationForDisk(info)) { | |
| 98 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | |
| 99 info.device_id(), info.display_name(), info.mount_point().value()); | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 // static | |
| 105 void RemovableDeviceNotificationsMac::DiskAppearedCallback( | |
| 106 DADiskRef disk, | |
| 107 void* context) { | |
| 108 RemovableDeviceNotificationsMac* notifications = | |
| 109 static_cast<RemovableDeviceNotificationsMac*>(context); | |
| 110 GetDiskInfoAndUpdate(notifications->AsWeakPtr(), | |
| 111 disk, | |
| 112 UPDATE_SHOULD_NOT_REMOVE); | |
| 113 } | |
| 114 | |
| 115 // static | |
| 116 void RemovableDeviceNotificationsMac::DiskDisappearedCallback( | |
| 117 DADiskRef disk, | |
| 118 void* context) { | |
| 119 RemovableDeviceNotificationsMac* notifications = | |
| 120 static_cast<RemovableDeviceNotificationsMac*>(context); | |
| 121 GetDiskInfoAndUpdate(notifications->AsWeakPtr(), | |
| 122 disk, | |
| 123 UPDATE_SHOULD_REMOVE); | |
| 124 } | |
| 125 | |
| 126 // static | |
| 127 void RemovableDeviceNotificationsMac::DiskDescriptionChangedCallback( | |
| 128 DADiskRef disk, | |
| 129 CFArrayRef keys, | |
| 130 void *context) { | |
| 131 RemovableDeviceNotificationsMac* notifications = | |
| 132 static_cast<RemovableDeviceNotificationsMac*>(context); | |
| 133 GetDiskInfoAndUpdate(notifications->AsWeakPtr(), | |
| 134 disk, | |
| 135 UPDATE_SHOULD_NOT_REMOVE); | |
|
vandebo (ex-Chrome)
2012/09/11 00:45:50
Since you're using an enum, it might make things c
sail
2012/09/11 03:02:00
Done.
| |
| 136 } | |
| 137 | |
| 138 bool RemovableDeviceNotificationsMac::ShouldPostNotificationForDisk( | |
| 139 const DiskInfoMac& info) const { | |
| 140 // Only post notifications about disks that have no empty fields and | |
| 141 // are removable. | |
| 142 return !info.bsd_name().empty() && | |
| 143 !info.device_id().empty() && | |
| 144 !info.display_name().empty() && | |
| 145 !info.mount_point().empty() && | |
| 146 (info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM || | |
| 147 info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); | |
| 148 } | |
| 149 | |
| 150 } // namespace chrome | |
| OLD | NEW |