| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/storage_monitor/media_transfer_protocol_device_observer_lin
ux.h" | 5 #include "components/storage_monitor/media_transfer_protocol_device_observer_chr
omeos.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/storage_monitor/media_storage_util.h" | 14 #include "components/storage_monitor/media_storage_util.h" |
| 15 #include "components/storage_monitor/removable_device_constants.h" | 15 #include "components/storage_monitor/removable_device_constants.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 112 |
| 113 *id = GetDeviceIdFromStorageInfo(*storage_info); | 113 *id = GetDeviceIdFromStorageInfo(*storage_info); |
| 114 *label = GetDeviceLabelFromStorageInfo(*storage_info); | 114 *label = GetDeviceLabelFromStorageInfo(*storage_info); |
| 115 *location = GetDeviceLocationFromStorageName(storage_name); | 115 *location = GetDeviceLocationFromStorageName(storage_name); |
| 116 *vendor_name = base::UTF8ToUTF16(storage_info->vendor()); | 116 *vendor_name = base::UTF8ToUTF16(storage_info->vendor()); |
| 117 *product_name = base::UTF8ToUTF16(storage_info->product()); | 117 *product_name = base::UTF8ToUTF16(storage_info->product()); |
| 118 } | 118 } |
| 119 | 119 |
| 120 } // namespace | 120 } // namespace |
| 121 | 121 |
| 122 MediaTransferProtocolDeviceObserverLinux:: | 122 MediaTransferProtocolDeviceObserverChromeOS:: |
| 123 MediaTransferProtocolDeviceObserverLinux( | 123 MediaTransferProtocolDeviceObserverChromeOS( |
| 124 StorageMonitor::Receiver* receiver, | 124 StorageMonitor::Receiver* receiver, |
| 125 device::MediaTransferProtocolManager* mtp_manager) | 125 device::MediaTransferProtocolManager* mtp_manager) |
| 126 : mtp_manager_(mtp_manager), | 126 : mtp_manager_(mtp_manager), |
| 127 get_storage_info_func_(&GetStorageInfo), | 127 get_storage_info_func_(&GetStorageInfo), |
| 128 notifications_(receiver) { | 128 notifications_(receiver) { |
| 129 mtp_manager_->AddObserver(this); | 129 mtp_manager_->AddObserver(this); |
| 130 EnumerateStorages(); | 130 EnumerateStorages(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 // This constructor is only used by unit tests. | 133 // This constructor is only used by unit tests. |
| 134 MediaTransferProtocolDeviceObserverLinux:: | 134 MediaTransferProtocolDeviceObserverChromeOS:: |
| 135 MediaTransferProtocolDeviceObserverLinux( | 135 MediaTransferProtocolDeviceObserverChromeOS( |
| 136 StorageMonitor::Receiver* receiver, | 136 StorageMonitor::Receiver* receiver, |
| 137 device::MediaTransferProtocolManager* mtp_manager, | 137 device::MediaTransferProtocolManager* mtp_manager, |
| 138 GetStorageInfoFunc get_storage_info_func) | 138 GetStorageInfoFunc get_storage_info_func) |
| 139 : mtp_manager_(mtp_manager), | 139 : mtp_manager_(mtp_manager), |
| 140 get_storage_info_func_(get_storage_info_func), | 140 get_storage_info_func_(get_storage_info_func), |
| 141 notifications_(receiver) { | 141 notifications_(receiver) {} |
| 142 } | |
| 143 | 142 |
| 144 MediaTransferProtocolDeviceObserverLinux:: | 143 MediaTransferProtocolDeviceObserverChromeOS:: |
| 145 ~MediaTransferProtocolDeviceObserverLinux() { | 144 ~MediaTransferProtocolDeviceObserverChromeOS() { |
| 146 mtp_manager_->RemoveObserver(this); | 145 mtp_manager_->RemoveObserver(this); |
| 147 } | 146 } |
| 148 | 147 |
| 149 bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath( | 148 bool MediaTransferProtocolDeviceObserverChromeOS::GetStorageInfoForPath( |
| 150 const base::FilePath& path, | 149 const base::FilePath& path, |
| 151 StorageInfo* storage_info) const { | 150 StorageInfo* storage_info) const { |
| 152 DCHECK(storage_info); | 151 DCHECK(storage_info); |
| 153 | 152 |
| 154 if (!path.IsAbsolute()) | 153 if (!path.IsAbsolute()) |
| 155 return false; | 154 return false; |
| 156 | 155 |
| 157 std::vector<base::FilePath::StringType> path_components; | 156 std::vector<base::FilePath::StringType> path_components; |
| 158 path.GetComponents(&path_components); | 157 path.GetComponents(&path_components); |
| 159 if (path_components.size() < 2) | 158 if (path_components.size() < 2) |
| 160 return false; | 159 return false; |
| 161 | 160 |
| 162 // First and second component of the path specifies the device location. | 161 // First and second component of the path specifies the device location. |
| 163 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the | 162 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the |
| 164 // device location. | 163 // device location. |
| 165 StorageLocationToInfoMap::const_iterator info_it = | 164 StorageLocationToInfoMap::const_iterator info_it = |
| 166 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); | 165 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); |
| 167 if (info_it == storage_map_.end()) | 166 if (info_it == storage_map_.end()) |
| 168 return false; | 167 return false; |
| 169 | 168 |
| 170 *storage_info = info_it->second; | 169 *storage_info = info_it->second; |
| 171 return true; | 170 return true; |
| 172 } | 171 } |
| 173 | 172 |
| 174 void MediaTransferProtocolDeviceObserverLinux::EjectDevice( | 173 void MediaTransferProtocolDeviceObserverChromeOS::EjectDevice( |
| 175 const std::string& device_id, | 174 const std::string& device_id, |
| 176 base::Callback<void(StorageMonitor::EjectStatus)> callback) { | 175 base::Callback<void(StorageMonitor::EjectStatus)> callback) { |
| 177 std::string location; | 176 std::string location; |
| 178 if (!GetLocationForDeviceId(device_id, &location)) { | 177 if (!GetLocationForDeviceId(device_id, &location)) { |
| 179 callback.Run(StorageMonitor::EJECT_NO_SUCH_DEVICE); | 178 callback.Run(StorageMonitor::EJECT_NO_SUCH_DEVICE); |
| 180 return; | 179 return; |
| 181 } | 180 } |
| 182 | 181 |
| 183 // TODO(thestig): Change this to tell the mtp manager to eject the device. | 182 // TODO(thestig): Change this to tell the mtp manager to eject the device. |
| 184 | 183 |
| 185 StorageChanged(false, location); | 184 StorageChanged(false, location); |
| 186 callback.Run(StorageMonitor::EJECT_OK); | 185 callback.Run(StorageMonitor::EJECT_OK); |
| 187 } | 186 } |
| 188 | 187 |
| 189 // device::MediaTransferProtocolManager::Observer override. | 188 // device::MediaTransferProtocolManager::Observer override. |
| 190 void MediaTransferProtocolDeviceObserverLinux::StorageChanged( | 189 void MediaTransferProtocolDeviceObserverChromeOS::StorageChanged( |
| 191 bool is_attached, | 190 bool is_attached, |
| 192 const std::string& storage_name) { | 191 const std::string& storage_name) { |
| 193 DCHECK(!storage_name.empty()); | 192 DCHECK(!storage_name.empty()); |
| 194 | 193 |
| 195 // New storage is attached. | 194 // New storage is attached. |
| 196 if (is_attached) { | 195 if (is_attached) { |
| 197 std::string device_id; | 196 std::string device_id; |
| 198 base::string16 storage_label; | 197 base::string16 storage_label; |
| 199 std::string location; | 198 std::string location; |
| 200 base::string16 vendor_name; | 199 base::string16 vendor_name; |
| 201 base::string16 product_name; | 200 base::string16 product_name; |
| 202 get_storage_info_func_(storage_name, mtp_manager_, | 201 get_storage_info_func_(storage_name, mtp_manager_, &device_id, |
| 203 &device_id, &storage_label, &location, | 202 &storage_label, &location, &vendor_name, |
| 204 &vendor_name, &product_name); | 203 &product_name); |
| 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, | 207 MediaStorageUtil::RecordDeviceInfoHistogram(false, device_id, |
| 209 storage_label); | 208 storage_label); |
| 210 if (device_id.empty() || storage_label.empty()) | 209 if (device_id.empty() || storage_label.empty()) |
| 211 return; | 210 return; |
| 212 | 211 |
| 213 DCHECK(!base::ContainsKey(storage_map_, location)); | 212 DCHECK(!base::ContainsKey(storage_map_, location)); |
| 214 | 213 |
| 215 StorageInfo storage_info(device_id, location, storage_label, | 214 StorageInfo storage_info(device_id, location, storage_label, vendor_name, |
| 216 vendor_name, product_name, 0); | 215 product_name, 0); |
| 217 storage_map_[location] = storage_info; | 216 storage_map_[location] = storage_info; |
| 218 notifications_->ProcessAttach(storage_info); | 217 notifications_->ProcessAttach(storage_info); |
| 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 notifications_->ProcessDetach(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 MediaTransferProtocolDeviceObserverChromeOS::EnumerateStorages() { |
| 231 typedef std::vector<std::string> StorageList; | 230 typedef std::vector<std::string> StorageList; |
| 232 StorageList storages = mtp_manager_->GetStorages(); | 231 StorageList storages = mtp_manager_->GetStorages(); |
| 233 for (StorageList::const_iterator storage_iter = storages.begin(); | 232 for (StorageList::const_iterator storage_iter = storages.begin(); |
| 234 storage_iter != storages.end(); ++storage_iter) { | 233 storage_iter != storages.end(); ++storage_iter) { |
| 235 StorageChanged(true, *storage_iter); | 234 StorageChanged(true, *storage_iter); |
| 236 } | 235 } |
| 237 } | 236 } |
| 238 | 237 |
| 239 bool MediaTransferProtocolDeviceObserverLinux::GetLocationForDeviceId( | 238 bool MediaTransferProtocolDeviceObserverChromeOS::GetLocationForDeviceId( |
| 240 const std::string& device_id, std::string* location) const { | 239 const std::string& device_id, |
| 240 std::string* location) const { |
| 241 for (StorageLocationToInfoMap::const_iterator it = storage_map_.begin(); | 241 for (StorageLocationToInfoMap::const_iterator it = storage_map_.begin(); |
| 242 it != storage_map_.end(); ++it) { | 242 it != storage_map_.end(); ++it) { |
| 243 if (it->second.device_id() == device_id) { | 243 if (it->second.device_id() == device_id) { |
| 244 *location = it->first; | 244 *location = it->first; |
| 245 return true; | 245 return true; |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 | 248 |
| 249 return false; | 249 return false; |
| 250 } | 250 } |
| 251 | 251 |
| 252 } // namespace storage_monitor | 252 } // namespace storage_monitor |
| OLD | NEW |