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 | |
|
Lei Zhang
2012/09/18 00:28:02
Is this the example input or output?
kmadhusu
2012/09/18 00:50:05
Fixed comment.
| |
| 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. | |
| 90 if (!device_label.empty()) { | |
| 91 const std::string& volume_id = storage_info.volume_identifier(); | |
| 92 if (!volume_id.empty()) { | |
| 93 device_label += chrome::kLeftParen + volume_id + chrome::kRightParen; | |
|
Lei Zhang
2012/09/18 00:28:02
nit: StringPrintf here and on line 98?
kmadhusu
2012/09/18 00:50:05
I will do this in a separate clean up CL.
| |
| 94 } else { | |
| 95 const std::string data_store_id = | |
| 96 GetStorageIdFromStorageName(storage_info.storage_name()); | |
| 97 if (!data_store_id.empty()) | |
|
Lei Zhang
2012/09/18 00:28:02
nit: Add {}s
kmadhusu
2012/09/18 00:50:05
Done.
| |
| 98 device_label += chrome::kLeftParen + data_store_id + | |
| 99 chrome::kRightParen; | |
| 100 } | |
| 101 } | |
| 76 return UTF8ToUTF16(device_label); | 102 return UTF8ToUTF16(device_label); |
| 77 } | 103 } |
| 78 | 104 |
| 79 // Helper function to get the device storage details such as device id, label | 105 // 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|. | 106 // and location. On success and fills in |id|, |label| and |location|. |
| 81 void GetStorageInfo(const std::string& storage_name, | 107 void GetStorageInfo(const std::string& storage_name, |
| 82 std::string* id, | 108 std::string* id, |
| 83 string16* label, | 109 string16* label, |
| 84 std::string* location) { | 110 std::string* location) { |
| 85 DCHECK(!storage_name.empty()); | 111 DCHECK(!storage_name.empty()); |
| 86 MediaTransferProtocolManager* mtp_manager = | 112 MediaTransferProtocolManager* mtp_manager = |
| 87 MediaTransferProtocolManager::GetInstance(); | 113 MediaTransferProtocolManager::GetInstance(); |
| 88 DCHECK(mtp_manager); | 114 DCHECK(mtp_manager); |
| 89 const MtpStorageInfo* storage_info = | 115 const MtpStorageInfo* storage_info = |
| 90 mtp_manager->GetStorageInfo(storage_name); | 116 mtp_manager->GetStorageInfo(storage_name); |
| 91 | 117 |
| 92 if (!storage_info) | 118 if (!storage_info) |
| 93 return; | 119 return; |
| 94 | 120 |
| 95 if (id) | 121 if (id) |
| 96 *id = GetDeviceIdFromStorageInfo(*storage_info); | 122 *id = GetDeviceIdFromStorageInfo(*storage_info); |
| 97 | 123 |
| 98 if (label) | 124 if (label) |
| 99 *label = GetDeviceLabelFromStorageInfo(*storage_info); | 125 *label = GetDeviceLabelFromStorageInfo(*storage_info); |
| 100 | 126 |
| 101 // Construct a dummy device path using the storage name. This is only used | 127 |
| 102 // for registering device media file system. | |
| 103 // E.g.: /usb:2,2:12345 | |
| 104 if (location) | 128 if (location) |
| 105 *location = kRootPath + storage_name; | 129 *location = GetDeviceLocationFromStorageName(storage_name); |
| 106 } | 130 } |
| 107 | 131 |
| 108 } // namespace | 132 } // namespace |
| 109 | 133 |
| 110 MediaTransferProtocolDeviceObserverCros:: | 134 MediaTransferProtocolDeviceObserverCros:: |
| 111 MediaTransferProtocolDeviceObserverCros() | 135 MediaTransferProtocolDeviceObserverCros() |
| 112 : get_storage_info_func_(&GetStorageInfo) { | 136 : get_storage_info_func_(&GetStorageInfo) { |
| 137 DCHECK(!g_mtp_device_observer); | |
| 138 g_mtp_device_observer = this; | |
| 139 | |
| 113 MediaTransferProtocolManager* mtp_manager = | 140 MediaTransferProtocolManager* mtp_manager = |
| 114 MediaTransferProtocolManager::GetInstance(); | 141 MediaTransferProtocolManager::GetInstance(); |
| 115 if (mtp_manager) | 142 if (mtp_manager) |
| 116 mtp_manager->AddObserver(this); | 143 mtp_manager->AddObserver(this); |
| 117 EnumerateStorages(); | 144 EnumerateStorages(); |
| 118 } | 145 } |
| 119 | 146 |
| 120 // This constructor is only used by unit tests. | 147 // This constructor is only used by unit tests. |
| 121 MediaTransferProtocolDeviceObserverCros:: | 148 MediaTransferProtocolDeviceObserverCros:: |
| 122 MediaTransferProtocolDeviceObserverCros( | 149 MediaTransferProtocolDeviceObserverCros( |
| 123 GetStorageInfoFunc get_storage_info_func) | 150 GetStorageInfoFunc get_storage_info_func) |
| 124 : get_storage_info_func_(get_storage_info_func) { | 151 : get_storage_info_func_(get_storage_info_func) { |
| 125 // In unit tests, we don't have a media transfer protocol manager. | 152 // In unit tests, we don't have a media transfer protocol manager. |
| 126 DCHECK(!MediaTransferProtocolManager::GetInstance()); | 153 DCHECK(!MediaTransferProtocolManager::GetInstance()); |
| 154 DCHECK(!g_mtp_device_observer); | |
| 155 g_mtp_device_observer = this; | |
| 127 } | 156 } |
| 128 | 157 |
| 129 MediaTransferProtocolDeviceObserverCros:: | 158 MediaTransferProtocolDeviceObserverCros:: |
| 130 ~MediaTransferProtocolDeviceObserverCros() { | 159 ~MediaTransferProtocolDeviceObserverCros() { |
| 160 DCHECK_EQ(this, g_mtp_device_observer); | |
| 161 g_mtp_device_observer = NULL; | |
| 162 | |
| 131 MediaTransferProtocolManager* mtp_manager = | 163 MediaTransferProtocolManager* mtp_manager = |
| 132 MediaTransferProtocolManager::GetInstance(); | 164 MediaTransferProtocolManager::GetInstance(); |
| 133 if (mtp_manager) | 165 if (mtp_manager) |
| 134 mtp_manager->RemoveObserver(this); | 166 mtp_manager->RemoveObserver(this); |
| 135 } | 167 } |
| 136 | 168 |
| 169 // static | |
| 170 MediaTransferProtocolDeviceObserverCros* | |
| 171 MediaTransferProtocolDeviceObserverCros::GetInstance() { | |
| 172 DCHECK(g_mtp_device_observer != NULL); | |
| 173 return g_mtp_device_observer; | |
| 174 } | |
| 175 | |
| 176 bool MediaTransferProtocolDeviceObserverCros::GetStorageInfoForPath( | |
| 177 const FilePath& path, | |
| 178 SystemMonitor::RemovableStorageInfo* storage_info) const { | |
| 179 if (!path.IsAbsolute()) | |
| 180 return false; | |
| 181 | |
| 182 std::vector<FilePath::StringType> path_components; | |
| 183 path.GetComponents(&path_components); | |
| 184 if (path_components.size() < 2) | |
| 185 return false; | |
| 186 | |
| 187 // First and second component of the path specifies the device location. | |
| 188 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the | |
| 189 // device location. | |
| 190 StorageLocationToInfoMap::const_iterator info_it = | |
| 191 storage_map_.find(path_components[0] + path_components[1]); | |
|
Lei Zhang
2012/09/18 00:33:20
Use GetDeviceLocationFromStorageName(path_componen
kmadhusu
2012/09/18 00:50:05
Done.
| |
| 192 if (info_it == storage_map_.end()) | |
| 193 return false; | |
| 194 | |
| 195 if (storage_info) | |
| 196 *storage_info = info_it->second; | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 137 // MediaTransferProtocolManager::Observer override. | 200 // MediaTransferProtocolManager::Observer override. |
| 138 void MediaTransferProtocolDeviceObserverCros::StorageChanged( | 201 void MediaTransferProtocolDeviceObserverCros::StorageChanged( |
| 139 bool is_attached, | 202 bool is_attached, |
| 140 const std::string& storage_name) { | 203 const std::string& storage_name) { |
| 141 DCHECK(!storage_name.empty()); | 204 DCHECK(!storage_name.empty()); |
| 142 | 205 |
| 143 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | 206 SystemMonitor* system_monitor = SystemMonitor::Get(); |
| 144 DCHECK(system_monitor); | 207 DCHECK(system_monitor); |
| 145 | 208 |
| 146 // New storage is attached. | 209 // New storage is attached. |
| 147 if (is_attached) { | 210 if (is_attached) { |
| 148 std::string device_id; | 211 std::string device_id; |
| 149 string16 device_name; | 212 string16 device_name; |
| 150 std::string location; | 213 std::string location; |
| 151 get_storage_info_func_(storage_name, &device_id, &device_name, &location); | 214 get_storage_info_func_(storage_name, &device_id, &device_name, &location); |
| 152 | 215 |
| 153 // Keep track of device id and device name to see how often we receive | 216 // Keep track of device id and device name to see how often we receive |
| 154 // empty values. | 217 // empty values. |
| 155 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable", | 218 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable", |
| 156 !device_id.empty()); | 219 !device_id.empty()); |
| 157 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable", | 220 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable", |
| 158 !device_name.empty()); | 221 !device_name.empty()); |
| 159 | 222 |
| 160 if (device_id.empty() || device_name.empty()) | 223 if (device_id.empty() || device_name.empty()) |
| 161 return; | 224 return; |
| 162 | 225 |
| 163 DCHECK(!ContainsKey(storage_map_, storage_name)); | 226 DCHECK(!ContainsKey(storage_map_, location)); |
| 164 storage_map_[storage_name] = device_id; | 227 |
| 228 SystemMonitor::RemovableStorageInfo storage_info(device_id, device_name, | |
| 229 location); | |
| 230 storage_map_[location] = storage_info; | |
| 165 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, | 231 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, |
| 166 location); | 232 location); |
| 167 } else { | 233 } else { |
| 168 // Existing storage is detached. | 234 // Existing storage is detached. |
| 169 StorageNameToIdMap::iterator it = storage_map_.find(storage_name); | 235 StorageLocationToInfoMap::iterator it = |
| 236 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); | |
| 170 if (it == storage_map_.end()) | 237 if (it == storage_map_.end()) |
| 171 return; | 238 return; |
| 172 system_monitor->ProcessRemovableStorageDetached(it->second); | 239 system_monitor->ProcessRemovableStorageDetached(it->second.device_id); |
| 173 storage_map_.erase(it); | 240 storage_map_.erase(it); |
| 174 } | 241 } |
| 175 } | 242 } |
| 176 | 243 |
| 177 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() { | 244 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() { |
| 178 typedef std::vector<std::string> StorageList; | 245 typedef std::vector<std::string> StorageList; |
| 179 MediaTransferProtocolManager* mtp_manager = | 246 MediaTransferProtocolManager* mtp_manager = |
| 180 MediaTransferProtocolManager::GetInstance(); | 247 MediaTransferProtocolManager::GetInstance(); |
| 181 DCHECK(mtp_manager); | 248 DCHECK(mtp_manager); |
| 182 StorageList storages = mtp_manager->GetStorages(); | 249 StorageList storages = mtp_manager->GetStorages(); |
| 183 for (StorageList::const_iterator storage_iter = storages.begin(); | 250 for (StorageList::const_iterator storage_iter = storages.begin(); |
| 184 storage_iter != storages.end(); ++storage_iter) { | 251 storage_iter != storages.end(); ++storage_iter) { |
| 185 StorageChanged(true, *storage_iter); | 252 StorageChanged(true, *storage_iter); |
| 186 } | 253 } |
| 187 } | 254 } |
| 188 | 255 |
| 189 } // namespace mtp | 256 } // namespace mtp |
| 190 } // namespace chromeos | 257 } // namespace chromeos |
| OLD | NEW |