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/system_monitor/media_transfer_protocol_device_observer_ chromeos.h" | 5 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ chromeos.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | |
| 7 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 10 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 12 #include "base/system_monitor/system_monitor.h" | |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h" | 14 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h" |
| 15 #include "chrome/browser/system_monitor/removable_device_constants.h" | 15 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 16 #include "chrome/browser/system_monitor/media_storage_util.h" | 16 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 17 #include "chromeos/dbus/mtp_storage_info.pb.h" | 17 #include "chromeos/dbus/mtp_storage_info.pb.h" |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 namespace mtp { | 20 namespace mtp { |
| 21 | 21 |
| 22 using base::SystemMonitor; | |
| 22 using chrome::MediaStorageUtil; | 23 using chrome::MediaStorageUtil; |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 27 static MediaTransferProtocolDeviceObserverCros* g_mtp_device_observer = NULL; | |
| 28 | |
| 26 // Device root path constant. | 29 // Device root path constant. |
| 27 const char kRootPath[] = "/"; | 30 const char kRootPath[] = "/"; |
| 28 | 31 |
| 32 // Constructs and returns the location of the device using the |storage_name|. | |
| 33 std::string GetDeviceLocationFromStorageName(const std::string& storage_name) { | |
| 34 // Construct a dummy device path using the storage name. This is only used | |
| 35 // for registering device media file system. | |
| 36 // E.g.: /usb:2,2:12345 | |
| 37 DCHECK(!storage_name.empty()); | |
| 38 return kRootPath + storage_name; | |
| 39 } | |
| 40 | |
| 29 // Returns the storage identifier of the device from the given |storage_name|. | 41 // Returns the storage identifier of the device from the given |storage_name|. |
| 30 // E.g. If the |storage_name| is "usb:2,2:65537", the storage identifier is | 42 // E.g. If the |storage_name| is "usb:2,2:65537", the storage identifier is |
| 31 // "65537". | 43 // "65537". |
| 32 std::string GetStorageIdFromStorageName(const std::string& storage_name) { | 44 std::string GetStorageIdFromStorageName(const std::string& storage_name) { |
| 33 std::vector<std::string> name_parts; | 45 std::vector<std::string> name_parts; |
| 34 base::SplitString(storage_name, ':', &name_parts); | 46 base::SplitString(storage_name, ':', &name_parts); |
| 35 return name_parts.size() == 3 ? name_parts[2] : std::string(); | 47 return name_parts.size() == 3 ? name_parts[2] : std::string(); |
| 36 } | 48 } |
| 37 | 49 |
| 38 // Returns a unique device id from the given |storage_info|. | 50 // Returns a unique device id from the given |storage_info|. |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 66 std::string device_label; | 78 std::string device_label; |
| 67 const std::string& vendor_name = storage_info.vendor(); | 79 const std::string& vendor_name = storage_info.vendor(); |
| 68 device_label = vendor_name; | 80 device_label = vendor_name; |
| 69 | 81 |
| 70 const std::string& product_name = storage_info.product(); | 82 const std::string& product_name = storage_info.product(); |
| 71 if (!product_name.empty()) { | 83 if (!product_name.empty()) { |
| 72 if (!device_label.empty()) | 84 if (!device_label.empty()) |
| 73 device_label += chrome::kSpaceDelim; | 85 device_label += chrome::kSpaceDelim; |
| 74 device_label += product_name; | 86 device_label += product_name; |
| 75 } | 87 } |
| 88 | |
| 89 // Add the data store id to the device label. | |
|
kmadhusu
2012/09/17 21:47:56
The following code appends the data store id to th
vandebo (ex-Chrome)
2012/09/17 22:29:44
The user won't understand the value anyway, so it'
kmadhusu
2012/09/17 23:32:23
Done.
| |
| 90 if (!device_label.empty()) { | |
| 91 const std::string& volume_id = storage_info.volume_identifier(); | |
| 92 if (volume_id.empty()) { | |
|
vandebo (ex-Chrome)
2012/09/17 22:29:44
nit: negate this:
if (!volume_id.empty()) {
dev
kmadhusu
2012/09/17 23:32:23
Done.
| |
| 93 const std::string data_store_id = | |
| 94 GetStorageIdFromStorageName(storage_info.storage_name()); | |
| 95 if (!data_store_id.empty()) | |
| 96 device_label += chrome::kNonSpaceDelim + data_store_id; | |
| 97 } else { | |
| 98 device_label += chrome::kNonSpaceDelim + volume_id; | |
| 99 } | |
| 100 } | |
| 76 return UTF8ToUTF16(device_label); | 101 return UTF8ToUTF16(device_label); |
| 77 } | 102 } |
| 78 | 103 |
| 79 // Helper function to get the device storage details such as device id, label | 104 // Helper function to get the device storage details such as device id, label |
| 80 // and location. On success and fills in |id|, |label| and |location|. | 105 // and location. On success and fills in |id|, |label| and |location|. |
| 81 void GetStorageInfo(const std::string& storage_name, | 106 void GetStorageInfo(const std::string& storage_name, |
| 82 std::string* id, | 107 std::string* id, |
| 83 string16* label, | 108 string16* label, |
| 84 std::string* location) { | 109 std::string* location) { |
| 85 DCHECK(!storage_name.empty()); | 110 DCHECK(!storage_name.empty()); |
| 86 MediaTransferProtocolManager* mtp_manager = | 111 MediaTransferProtocolManager* mtp_manager = |
| 87 MediaTransferProtocolManager::GetInstance(); | 112 MediaTransferProtocolManager::GetInstance(); |
| 88 DCHECK(mtp_manager); | 113 DCHECK(mtp_manager); |
| 89 const MtpStorageInfo* storage_info = | 114 const MtpStorageInfo* storage_info = |
| 90 mtp_manager->GetStorageInfo(storage_name); | 115 mtp_manager->GetStorageInfo(storage_name); |
| 91 | 116 |
| 92 if (!storage_info) | 117 if (!storage_info) |
| 93 return; | 118 return; |
| 94 | 119 |
| 95 if (id) | 120 if (id) |
| 96 *id = GetDeviceIdFromStorageInfo(*storage_info); | 121 *id = GetDeviceIdFromStorageInfo(*storage_info); |
| 97 | 122 |
| 98 if (label) | 123 if (label) |
| 99 *label = GetDeviceLabelFromStorageInfo(*storage_info); | 124 *label = GetDeviceLabelFromStorageInfo(*storage_info); |
| 100 | 125 |
| 101 // Construct a dummy device path using the storage name. This is only used | 126 |
| 102 // for registering device media file system. | |
| 103 // E.g.: /usb:2,2:12345 | |
| 104 if (location) | 127 if (location) |
| 105 *location = kRootPath + storage_name; | 128 *location = GetDeviceLocationFromStorageName(storage_name); |
| 106 } | 129 } |
| 107 | 130 |
| 108 } // namespace | 131 } // namespace |
| 109 | 132 |
| 110 MediaTransferProtocolDeviceObserverCros:: | 133 MediaTransferProtocolDeviceObserverCros:: |
| 111 MediaTransferProtocolDeviceObserverCros() | 134 MediaTransferProtocolDeviceObserverCros() |
| 112 : get_storage_info_func_(&GetStorageInfo) { | 135 : get_storage_info_func_(&GetStorageInfo) { |
| 136 DCHECK(!g_mtp_device_observer); | |
| 137 g_mtp_device_observer = this; | |
| 138 | |
| 113 MediaTransferProtocolManager* mtp_manager = | 139 MediaTransferProtocolManager* mtp_manager = |
| 114 MediaTransferProtocolManager::GetInstance(); | 140 MediaTransferProtocolManager::GetInstance(); |
| 115 if (mtp_manager) | 141 if (mtp_manager) |
| 116 mtp_manager->AddObserver(this); | 142 mtp_manager->AddObserver(this); |
| 117 EnumerateStorages(); | 143 EnumerateStorages(); |
| 118 } | 144 } |
| 119 | 145 |
| 120 // This constructor is only used by unit tests. | 146 // This constructor is only used by unit tests. |
| 121 MediaTransferProtocolDeviceObserverCros:: | 147 MediaTransferProtocolDeviceObserverCros:: |
| 122 MediaTransferProtocolDeviceObserverCros( | 148 MediaTransferProtocolDeviceObserverCros( |
| 123 GetStorageInfoFunc get_storage_info_func) | 149 GetStorageInfoFunc get_storage_info_func) |
| 124 : get_storage_info_func_(get_storage_info_func) { | 150 : get_storage_info_func_(get_storage_info_func) { |
| 125 // In unit tests, we don't have a media transfer protocol manager. | 151 // In unit tests, we don't have a media transfer protocol manager. |
| 126 DCHECK(!MediaTransferProtocolManager::GetInstance()); | 152 DCHECK(!MediaTransferProtocolManager::GetInstance()); |
| 153 DCHECK(!g_mtp_device_observer); | |
| 154 g_mtp_device_observer = this; | |
| 127 } | 155 } |
| 128 | 156 |
| 129 MediaTransferProtocolDeviceObserverCros:: | 157 MediaTransferProtocolDeviceObserverCros:: |
| 130 ~MediaTransferProtocolDeviceObserverCros() { | 158 ~MediaTransferProtocolDeviceObserverCros() { |
| 159 DCHECK_EQ(this, g_mtp_device_observer); | |
| 160 g_mtp_device_observer = NULL; | |
| 161 | |
| 131 MediaTransferProtocolManager* mtp_manager = | 162 MediaTransferProtocolManager* mtp_manager = |
| 132 MediaTransferProtocolManager::GetInstance(); | 163 MediaTransferProtocolManager::GetInstance(); |
| 133 if (mtp_manager) | 164 if (mtp_manager) |
| 134 mtp_manager->RemoveObserver(this); | 165 mtp_manager->RemoveObserver(this); |
| 135 } | 166 } |
| 136 | 167 |
| 168 // static | |
| 169 MediaTransferProtocolDeviceObserverCros* | |
| 170 MediaTransferProtocolDeviceObserverCros::GetInstance() { | |
| 171 DCHECK(g_mtp_device_observer != NULL); | |
| 172 return g_mtp_device_observer; | |
| 173 } | |
| 174 | |
| 175 bool MediaTransferProtocolDeviceObserverCros::GetStorageInfoForPath( | |
| 176 const FilePath& path, | |
| 177 SystemMonitor::RemovableStorageInfo* storage_info) const { | |
| 178 if (!path.IsAbsolute()) | |
| 179 return false; | |
| 180 | |
| 181 std::vector<FilePath::StringType> path_components; | |
| 182 path.GetComponents(&path_components); | |
| 183 if (path_components.size() < 2) | |
| 184 return false; | |
| 185 | |
| 186 // First and second component of the path specifies the device location. | |
| 187 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the | |
| 188 // device location. | |
| 189 StorageLocationToInfoMap::const_iterator info_it = | |
| 190 storage_map_.find(path_components[0] + path_components[1]); | |
| 191 if (info_it == storage_map_.end()) | |
| 192 return false; | |
| 193 | |
| 194 if (storage_info) | |
| 195 *storage_info = info_it->second; | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 137 // MediaTransferProtocolManager::Observer override. | 199 // MediaTransferProtocolManager::Observer override. |
| 138 void MediaTransferProtocolDeviceObserverCros::StorageChanged( | 200 void MediaTransferProtocolDeviceObserverCros::StorageChanged( |
| 139 bool is_attached, | 201 bool is_attached, |
| 140 const std::string& storage_name) { | 202 const std::string& storage_name) { |
| 141 DCHECK(!storage_name.empty()); | 203 DCHECK(!storage_name.empty()); |
| 142 | 204 |
| 143 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | 205 SystemMonitor* system_monitor = SystemMonitor::Get(); |
| 144 DCHECK(system_monitor); | 206 DCHECK(system_monitor); |
| 145 | 207 |
| 146 // New storage is attached. | 208 // New storage is attached. |
| 147 if (is_attached) { | 209 if (is_attached) { |
| 148 std::string device_id; | 210 std::string device_id; |
| 149 string16 device_name; | 211 string16 device_name; |
| 150 std::string location; | 212 std::string location; |
| 151 get_storage_info_func_(storage_name, &device_id, &device_name, &location); | 213 get_storage_info_func_(storage_name, &device_id, &device_name, &location); |
| 152 | 214 |
| 153 // Keep track of device id and device name to see how often we receive | 215 // Keep track of device id and device name to see how often we receive |
| 154 // empty values. | 216 // empty values. |
| 155 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable", | 217 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable", |
| 156 !device_id.empty()); | 218 !device_id.empty()); |
| 157 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable", | 219 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable", |
| 158 !device_name.empty()); | 220 !device_name.empty()); |
| 159 | 221 |
| 160 if (device_id.empty() || device_name.empty()) | 222 if (device_id.empty() || device_name.empty()) |
| 161 return; | 223 return; |
| 162 | 224 |
| 163 DCHECK(!ContainsKey(storage_map_, storage_name)); | 225 DCHECK(!ContainsKey(storage_map_, location)); |
| 164 storage_map_[storage_name] = device_id; | 226 |
| 227 SystemMonitor::RemovableStorageInfo storage_info(device_id, device_name, | |
| 228 location); | |
| 229 storage_map_[location] = storage_info; | |
| 165 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, | 230 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, |
| 166 location); | 231 location); |
| 167 } else { | 232 } else { |
| 168 // Existing storage is detached. | 233 // Existing storage is detached. |
| 169 StorageNameToIdMap::iterator it = storage_map_.find(storage_name); | 234 StorageLocationToInfoMap::iterator it = |
| 235 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); | |
| 170 if (it == storage_map_.end()) | 236 if (it == storage_map_.end()) |
| 171 return; | 237 return; |
| 172 system_monitor->ProcessRemovableStorageDetached(it->second); | 238 system_monitor->ProcessRemovableStorageDetached(it->second.device_id); |
| 173 storage_map_.erase(it); | 239 storage_map_.erase(it); |
| 174 } | 240 } |
| 175 } | 241 } |
| 176 | 242 |
| 177 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() { | 243 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() { |
| 178 typedef std::vector<std::string> StorageList; | 244 typedef std::vector<std::string> StorageList; |
| 179 MediaTransferProtocolManager* mtp_manager = | 245 MediaTransferProtocolManager* mtp_manager = |
| 180 MediaTransferProtocolManager::GetInstance(); | 246 MediaTransferProtocolManager::GetInstance(); |
| 181 DCHECK(mtp_manager); | 247 DCHECK(mtp_manager); |
| 182 StorageList storages = mtp_manager->GetStorages(); | 248 StorageList storages = mtp_manager->GetStorages(); |
| 183 for (StorageList::const_iterator storage_iter = storages.begin(); | 249 for (StorageList::const_iterator storage_iter = storages.begin(); |
| 184 storage_iter != storages.end(); ++storage_iter) { | 250 storage_iter != storages.end(); ++storage_iter) { |
| 185 StorageChanged(true, *storage_iter); | 251 StorageChanged(true, *storage_iter); |
| 186 } | 252 } |
| 187 } | 253 } |
| 188 | 254 |
| 189 } // namespace mtp | 255 } // namespace mtp |
| 190 } // namespace chromeos | 256 } // namespace chromeos |
| OLD | NEW |