| 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 // chromeos::RemovableDeviceNotificationsCros implementation. | 5 // chromeos::RemovableDeviceNotificationsCros implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos.
h" | 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos.
h" |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 16 #include "chrome/browser/system_monitor/media_storage_util.h" | 16 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 17 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 | 19 |
| 19 namespace chromeos { | 20 namespace chromeos { |
| 20 | 21 |
| 22 using base::SystemMonitor; |
| 23 |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 26 static RemovableDeviceNotificationsCros* |
| 27 g_removable_device_notifications_chromeos = NULL; |
| 28 |
| 23 // Returns true if the requested device is valid, else false. On success, fills | 29 // Returns true if the requested device is valid, else false. On success, fills |
| 24 // in |unique_id| and |device_label| | 30 // in |unique_id| and |device_label| |
| 25 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, | 31 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, |
| 26 string16* device_label) { | 32 string16* device_label) { |
| 27 // Get the media device uuid and label if exists. | 33 // Get the media device uuid and label if exists. |
| 28 const disks::DiskMountManager::Disk* disk = | 34 const disks::DiskMountManager::Disk* disk = |
| 29 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); | 35 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); |
| 30 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) | 36 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) |
| 31 return false; | 37 return false; |
| 32 | 38 |
| 33 *unique_id = disk->fs_uuid(); | 39 *unique_id = disk->fs_uuid(); |
| 34 | 40 |
| 35 // TODO(kmadhusu): If device label is empty, extract vendor and model details | 41 // TODO(kmadhusu): If device label is empty, extract vendor and model details |
| 36 // and use them as device_label. | 42 // and use them as device_label. |
| 37 *device_label = UTF8ToUTF16(disk->device_label().empty() ? | 43 *device_label = UTF8ToUTF16(disk->device_label().empty() ? |
| 38 FilePath(source_path).BaseName().value() : | 44 FilePath(source_path).BaseName().value() : |
| 39 disk->device_label()); | 45 disk->device_label()); |
| 40 return true; | 46 return true; |
| 41 } | 47 } |
| 42 | 48 |
| 43 } // namespace | 49 } // namespace |
| 44 | 50 |
| 45 using content::BrowserThread; | 51 using content::BrowserThread; |
| 46 | 52 |
| 47 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { | 53 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { |
| 48 DCHECK(disks::DiskMountManager::GetInstance()); | 54 DCHECK(disks::DiskMountManager::GetInstance()); |
| 55 DCHECK(!g_removable_device_notifications_chromeos); |
| 56 g_removable_device_notifications_chromeos = this; |
| 49 disks::DiskMountManager::GetInstance()->AddObserver(this); | 57 disks::DiskMountManager::GetInstance()->AddObserver(this); |
| 50 CheckExistingMountPointsOnUIThread(); | 58 CheckExistingMountPointsOnUIThread(); |
| 51 } | 59 } |
| 52 | 60 |
| 53 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() { | 61 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() { |
| 62 DCHECK_EQ(this, g_removable_device_notifications_chromeos); |
| 63 g_removable_device_notifications_chromeos = NULL; |
| 54 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); | 64 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); |
| 55 if (manager) { | 65 if (manager) { |
| 56 manager->RemoveObserver(this); | 66 manager->RemoveObserver(this); |
| 57 } | 67 } |
| 58 } | 68 } |
| 59 | 69 |
| 70 // static |
| 71 RemovableDeviceNotificationsCros* |
| 72 RemovableDeviceNotificationsCros::GetInstance() { |
| 73 return g_removable_device_notifications_chromeos; |
| 74 } |
| 75 |
| 60 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() { | 76 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 62 const disks::DiskMountManager::MountPointMap& mount_point_map = | 78 const disks::DiskMountManager::MountPointMap& mount_point_map = |
| 63 disks::DiskMountManager::GetInstance()->mount_points(); | 79 disks::DiskMountManager::GetInstance()->mount_points(); |
| 64 for (disks::DiskMountManager::MountPointMap::const_iterator it = | 80 for (disks::DiskMountManager::MountPointMap::const_iterator it = |
| 65 mount_point_map.begin(); it != mount_point_map.end(); ++it) { | 81 mount_point_map.begin(); it != mount_point_map.end(); ++it) { |
| 66 BrowserThread::PostTask( | 82 BrowserThread::PostTask( |
| 67 BrowserThread::FILE, FROM_HERE, | 83 BrowserThread::FILE, FROM_HERE, |
| 68 base::Bind( | 84 base::Bind( |
| 69 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | 85 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 BrowserThread::FILE, FROM_HERE, | 123 BrowserThread::FILE, FROM_HERE, |
| 108 base::Bind( | 124 base::Bind( |
| 109 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | 125 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, |
| 110 this, mount_info)); | 126 this, mount_info)); |
| 111 break; | 127 break; |
| 112 } | 128 } |
| 113 case disks::DiskMountManager::UNMOUNTING: { | 129 case disks::DiskMountManager::UNMOUNTING: { |
| 114 MountMap::iterator it = mount_map_.find(mount_info.mount_path); | 130 MountMap::iterator it = mount_map_.find(mount_info.mount_path); |
| 115 if (it == mount_map_.end()) | 131 if (it == mount_map_.end()) |
| 116 return; | 132 return; |
| 117 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(it->second); | 133 SystemMonitor::Get()->ProcessRemovableStorageDetached( |
| 134 it->second.device_id); |
| 118 mount_map_.erase(it); | 135 mount_map_.erase(it); |
| 119 break; | 136 break; |
| 120 } | 137 } |
| 121 } | 138 } |
| 122 } | 139 } |
| 123 | 140 |
| 141 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( |
| 142 const FilePath& path, |
| 143 SystemMonitor::RemovableStorageInfo* device_info) const { |
| 144 if (!path.IsAbsolute()) |
| 145 return false; |
| 146 |
| 147 FilePath current = path; |
| 148 while (!ContainsKey(mount_map_, current.value()) && |
| 149 current != current.DirName()) { |
| 150 current = current.DirName(); |
| 151 } |
| 152 |
| 153 MountMap::const_iterator info_it = mount_map_.find(current.value()); |
| 154 if (info_it == mount_map_.end()) |
| 155 return false; |
| 156 |
| 157 if (device_info) |
| 158 *device_info = info_it->second; |
| 159 return true; |
| 160 } |
| 161 |
| 124 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( | 162 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( |
| 125 const disks::DiskMountManager::MountPointInfo& mount_info) { | 163 const disks::DiskMountManager::MountPointInfo& mount_info) { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 127 | 165 |
| 128 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); | 166 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); |
| 129 | 167 |
| 130 BrowserThread::PostTask( | 168 BrowserThread::PostTask( |
| 131 BrowserThread::UI, FROM_HERE, | 169 BrowserThread::UI, FROM_HERE, |
| 132 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, | 170 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, |
| 133 this, mount_info, has_dcim)); | 171 this, mount_info, has_dcim)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 153 !unique_id.empty()); | 191 !unique_id.empty()); |
| 154 if (unique_id.empty()) | 192 if (unique_id.empty()) |
| 155 return; | 193 return; |
| 156 | 194 |
| 157 chrome::MediaStorageUtil::Type type = has_dcim ? | 195 chrome::MediaStorageUtil::Type type = has_dcim ? |
| 158 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : | 196 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : |
| 159 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | 197 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 160 | 198 |
| 161 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, | 199 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, |
| 162 unique_id); | 200 unique_id); |
| 163 mount_map_.insert(std::make_pair(mount_info.mount_path, device_id)); | 201 SystemMonitor::RemovableStorageInfo info(device_id, device_label, |
| 164 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | 202 mount_info.mount_path); |
| 203 mount_map_.insert(std::make_pair(mount_info.mount_path, info)); |
| 204 SystemMonitor::Get()->ProcessRemovableStorageAttached( |
| 165 device_id, | 205 device_id, |
| 166 device_label, | 206 device_label, |
| 167 mount_info.mount_path); | 207 mount_info.mount_path); |
| 168 } | 208 } |
| 169 | 209 |
| 170 } // namespace chrome | 210 } // namespace chrome |
| OLD | NEW |