| 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_
linux.h" | 5 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_
linux.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/system_monitor/media_storage_util.h" | 12 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 13 #include "chrome/browser/system_monitor/removable_device_constants.h" | 13 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 14 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" | 14 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" |
| 15 | 15 |
| 16 namespace chrome { | 16 namespace chrome { |
| 17 | 17 |
| 18 using base::SystemMonitor; | |
| 19 | |
| 20 namespace { | 18 namespace { |
| 21 | 19 |
| 22 static MediaTransferProtocolDeviceObserverLinux* g_mtp_device_observer = NULL; | 20 static MediaTransferProtocolDeviceObserverLinux* g_mtp_device_observer = NULL; |
| 23 | 21 |
| 24 // Device root path constant. | 22 // Device root path constant. |
| 25 const char kRootPath[] = "/"; | 23 const char kRootPath[] = "/"; |
| 26 | 24 |
| 27 // Constructs and returns the location of the device using the |storage_name|. | 25 // Constructs and returns the location of the device using the |storage_name|. |
| 28 std::string GetDeviceLocationFromStorageName(const std::string& storage_name) { | 26 std::string GetDeviceLocationFromStorageName(const std::string& storage_name) { |
| 29 // Construct a dummy device path using the storage name. This is only used | 27 // Construct a dummy device path using the storage name. This is only used |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 156 |
| 159 // static | 157 // static |
| 160 MediaTransferProtocolDeviceObserverLinux* | 158 MediaTransferProtocolDeviceObserverLinux* |
| 161 MediaTransferProtocolDeviceObserverLinux::GetInstance() { | 159 MediaTransferProtocolDeviceObserverLinux::GetInstance() { |
| 162 DCHECK(g_mtp_device_observer != NULL); | 160 DCHECK(g_mtp_device_observer != NULL); |
| 163 return g_mtp_device_observer; | 161 return g_mtp_device_observer; |
| 164 } | 162 } |
| 165 | 163 |
| 166 bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath( | 164 bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath( |
| 167 const FilePath& path, | 165 const FilePath& path, |
| 168 SystemMonitor::RemovableStorageInfo* storage_info) const { | 166 RemovableStorageNotifications::StorageInfo* storage_info) const { |
| 169 if (!path.IsAbsolute()) | 167 if (!path.IsAbsolute()) |
| 170 return false; | 168 return false; |
| 171 | 169 |
| 172 std::vector<FilePath::StringType> path_components; | 170 std::vector<FilePath::StringType> path_components; |
| 173 path.GetComponents(&path_components); | 171 path.GetComponents(&path_components); |
| 174 if (path_components.size() < 2) | 172 if (path_components.size() < 2) |
| 175 return false; | 173 return false; |
| 176 | 174 |
| 177 // First and second component of the path specifies the device location. | 175 // First and second component of the path specifies the device location. |
| 178 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the | 176 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the |
| 179 // device location. | 177 // device location. |
| 180 StorageLocationToInfoMap::const_iterator info_it = | 178 StorageLocationToInfoMap::const_iterator info_it = |
| 181 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); | 179 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); |
| 182 if (info_it == storage_map_.end()) | 180 if (info_it == storage_map_.end()) |
| 183 return false; | 181 return false; |
| 184 | 182 |
| 185 if (storage_info) | 183 if (storage_info) |
| 186 *storage_info = info_it->second; | 184 *storage_info = info_it->second; |
| 187 return true; | 185 return true; |
| 188 } | 186 } |
| 189 | 187 |
| 190 // device::MediaTransferProtocolManager::Observer override. | 188 // device::MediaTransferProtocolManager::Observer override. |
| 191 void MediaTransferProtocolDeviceObserverLinux::StorageChanged( | 189 void MediaTransferProtocolDeviceObserverLinux::StorageChanged( |
| 192 bool is_attached, | 190 bool is_attached, |
| 193 const std::string& storage_name) { | 191 const std::string& storage_name) { |
| 194 DCHECK(!storage_name.empty()); | 192 DCHECK(!storage_name.empty()); |
| 195 | 193 |
| 196 SystemMonitor* system_monitor = SystemMonitor::Get(); | 194 RemovableStorageNotifications* notifications = |
| 197 DCHECK(system_monitor); | 195 RemovableStorageNotifications::GetInstance(); |
| 196 DCHECK(notifications); |
| 198 | 197 |
| 199 // New storage is attached. | 198 // New storage is attached. |
| 200 if (is_attached) { | 199 if (is_attached) { |
| 201 std::string device_id; | 200 std::string device_id; |
| 202 string16 device_name; | 201 string16 device_name; |
| 203 std::string location; | 202 std::string location; |
| 204 get_storage_info_func_(storage_name, &device_id, &device_name, &location); | 203 get_storage_info_func_(storage_name, &device_id, &device_name, &location); |
| 205 | 204 |
| 206 // Keep track of device id and device name to see how often we receive | 205 // Keep track of device id and device name to see how often we receive |
| 207 // empty values. | 206 // empty values. |
| 208 MediaStorageUtil::RecordDeviceInfoHistogram(false, device_id, device_name); | 207 MediaStorageUtil::RecordDeviceInfoHistogram(false, device_id, device_name); |
| 209 if (device_id.empty() || device_name.empty()) | 208 if (device_id.empty() || device_name.empty()) |
| 210 return; | 209 return; |
| 211 | 210 |
| 212 DCHECK(!ContainsKey(storage_map_, location)); | 211 DCHECK(!ContainsKey(storage_map_, location)); |
| 213 | 212 |
| 214 SystemMonitor::RemovableStorageInfo storage_info(device_id, device_name, | 213 RemovableStorageNotifications::StorageInfo storage_info( |
| 215 location); | 214 device_id, device_name, location); |
| 216 storage_map_[location] = storage_info; | 215 storage_map_[location] = storage_info; |
| 217 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, | 216 notifications->ProcessAttach(device_id, device_name, |
| 218 location); | 217 location); |
| 219 } else { | 218 } else { |
| 220 // Existing storage is detached. | 219 // Existing storage is detached. |
| 221 StorageLocationToInfoMap::iterator it = | 220 StorageLocationToInfoMap::iterator it = |
| 222 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); | 221 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); |
| 223 if (it == storage_map_.end()) | 222 if (it == storage_map_.end()) |
| 224 return; | 223 return; |
| 225 system_monitor->ProcessRemovableStorageDetached(it->second.device_id); | 224 notifications->ProcessDetach(it->second.device_id); |
| 226 storage_map_.erase(it); | 225 storage_map_.erase(it); |
| 227 } | 226 } |
| 228 } | 227 } |
| 229 | 228 |
| 230 void MediaTransferProtocolDeviceObserverLinux::EnumerateStorages() { | 229 void MediaTransferProtocolDeviceObserverLinux::EnumerateStorages() { |
| 231 typedef std::vector<std::string> StorageList; | 230 typedef std::vector<std::string> StorageList; |
| 232 device::MediaTransferProtocolManager* mtp_manager = | 231 device::MediaTransferProtocolManager* mtp_manager = |
| 233 device::MediaTransferProtocolManager::GetInstance(); | 232 device::MediaTransferProtocolManager::GetInstance(); |
| 234 StorageList storages = mtp_manager->GetStorages(); | 233 StorageList storages = mtp_manager->GetStorages(); |
| 235 for (StorageList::const_iterator storage_iter = storages.begin(); | 234 for (StorageList::const_iterator storage_iter = storages.begin(); |
| 236 storage_iter != storages.end(); ++storage_iter) { | 235 storage_iter != storages.end(); ++storage_iter) { |
| 237 StorageChanged(true, *storage_iter); | 236 StorageChanged(true, *storage_iter); |
| 238 } | 237 } |
| 239 } | 238 } |
| 240 | 239 |
| 241 } // namespace chrome | 240 } // namespace chrome |
| OLD | NEW |