| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // chrome::MediaStorageUtil implementation. | |
| 6 | |
| 7 #include "chrome/browser/media_gallery/media_storage_util.h" | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/system_monitor/system_monitor.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 using base::SystemMonitor; | |
| 18 using content::BrowserThread; | |
| 19 | |
| 20 namespace chrome { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo; | |
| 25 | |
| 26 // Prefix constants for different device id spaces. | |
| 27 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:"; | |
| 28 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:"; | |
| 29 const char kFixedMassStoragePrefix[] = "path:"; | |
| 30 const char kMtpPtpPrefix[] = "mtp:"; | |
| 31 | |
| 32 void EmptyPathIsFalseCallback(const MediaStorageUtil::BoolCallback& callback, | |
| 33 FilePath path) { | |
| 34 callback.Run(!path.empty()); | |
| 35 } | |
| 36 | |
| 37 void ValidatePathOnFileThread( | |
| 38 const FilePath& path, const MediaStorageUtil::FilePathCallback& callback) { | |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 40 FilePath result; | |
| 41 if (file_util::PathExists(path)) | |
| 42 result = path; | |
| 43 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 44 base::Bind(callback, path)); | |
| 45 } | |
| 46 | |
| 47 FilePath::StringType FindRemovableStorageLocationById( | |
| 48 const std::string& device_id) { | |
| 49 RemovableStorageInfo media_devices = | |
| 50 SystemMonitor::Get()->GetAttachedRemovableStorage(); | |
| 51 for (RemovableStorageInfo::const_iterator it = media_devices.begin(); | |
| 52 it != media_devices.end(); | |
| 53 ++it) { | |
| 54 if (it->device_id == device_id) | |
| 55 return it->location; | |
| 56 } | |
| 57 return FilePath::StringType(); | |
| 58 } | |
| 59 | |
| 60 // TODO(vandebo) use FilePath::AppendRelativePath instead | |
| 61 // Make |path| a relative path, i.e. strip the drive letter and leading /. | |
| 62 FilePath MakePathRelative(const FilePath& path) { | |
| 63 if (!path.IsAbsolute()) | |
| 64 return path; | |
| 65 | |
| 66 FilePath relative; | |
| 67 std::vector<FilePath::StringType> components; | |
| 68 path.GetComponents(&components); | |
| 69 | |
| 70 // On Windows, the first component may be the drive letter with the second | |
| 71 // being \\. | |
| 72 int start = 1; | |
| 73 if (components[1].size() == 1 && FilePath::IsSeparator(components[1][0])) | |
| 74 start = 2; | |
| 75 | |
| 76 for (size_t i = start; i < components.size(); i++) | |
| 77 relative = relative.Append(components[i]); | |
| 78 return relative; | |
| 79 } | |
| 80 | |
| 81 } // namespace | |
| 82 | |
| 83 // static | |
| 84 std::string MediaStorageUtil::MakeDeviceId(Type type, | |
| 85 const std::string& unique_id) { | |
| 86 DCHECK(!unique_id.empty()); | |
| 87 switch (type) { | |
| 88 case REMOVABLE_MASS_STORAGE_WITH_DCIM: | |
| 89 return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id; | |
| 90 case REMOVABLE_MASS_STORAGE_NO_DCIM: | |
| 91 return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id; | |
| 92 case FIXED_MASS_STORAGE: | |
| 93 return std::string(kFixedMassStoragePrefix) + unique_id; | |
| 94 case MTP_OR_PTP: | |
| 95 return std::string(kMtpPtpPrefix) + unique_id; | |
| 96 } | |
| 97 NOTREACHED(); | |
| 98 return std::string(); | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id, | |
| 103 Type* type, std::string* unique_id) { | |
| 104 size_t prefix_length = device_id.find_first_of(':'); | |
| 105 std::string prefix = prefix_length != std::string::npos ? | |
| 106 device_id.substr(0, prefix_length + 1) : ""; | |
| 107 | |
| 108 Type found_type; | |
| 109 if (prefix == kRemovableMassStorageWithDCIMPrefix) { | |
| 110 found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM; | |
| 111 } else if (prefix == kRemovableMassStorageNoDCIMPrefix) { | |
| 112 found_type = REMOVABLE_MASS_STORAGE_NO_DCIM; | |
| 113 } else if (prefix == kFixedMassStoragePrefix) { | |
| 114 found_type = FIXED_MASS_STORAGE; | |
| 115 } else if (prefix == kMtpPtpPrefix) { | |
| 116 found_type = MTP_OR_PTP; | |
| 117 } else { | |
| 118 NOTREACHED(); | |
| 119 return false; | |
| 120 } | |
| 121 if (type) | |
| 122 *type = found_type; | |
| 123 | |
| 124 if (unique_id) | |
| 125 *unique_id = device_id.substr(prefix_length + 1); | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 // static | |
| 130 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { | |
| 131 Type type; | |
| 132 return CrackDeviceId(device_id, &type, NULL) && | |
| 133 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP); | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { | |
| 138 Type type; | |
| 139 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE; | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, | |
| 144 const BoolCallback& callback) { | |
| 145 Type type; | |
| 146 std::string unique_id; | |
| 147 if (!CrackDeviceId(device_id, &type, &unique_id)) { | |
| 148 callback.Run(false); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 switch (type) { | |
| 153 case MTP_OR_PTP: // Fall through. | |
| 154 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through. | |
| 155 case REMOVABLE_MASS_STORAGE_NO_DCIM: | |
| 156 // We should be able to find removable storage in SystemMonitor. | |
| 157 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); | |
| 158 break; | |
| 159 case FIXED_MASS_STORAGE: | |
| 160 // For this type, the unique_id is the path. | |
| 161 BrowserThread::PostTask( | |
| 162 BrowserThread::FILE, FROM_HERE, | |
| 163 base::Bind(&ValidatePathOnFileThread, | |
| 164 FilePath::FromUTF8Unsafe(unique_id), | |
| 165 base::Bind(&EmptyPathIsFalseCallback, callback))); | |
| 166 break; | |
| 167 } | |
| 168 NOTREACHED(); | |
| 169 callback.Run(false); | |
| 170 } | |
| 171 | |
| 172 #if !defined(OS_LINUX) || defined(OS_CHROMEOS) | |
| 173 // static | |
| 174 void MediaStorageUtil::GetDeviceInfoFromPath( | |
| 175 const FilePath& path, const DeviceInfoCallback& callback) { | |
| 176 // TODO(vandebo) This needs to be implemented per platform. Below is no | |
| 177 // worse than what the code already does. | |
| 178 // * Find mount point parent (determines relative file path) | |
| 179 // * Search System monitor, just in case. | |
| 180 // * If it's a removable device, generate device id, else use device root | |
| 181 // path as id | |
| 182 std::string device_id = | |
| 183 MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | |
| 184 FilePath relative_path = MakePathRelative(path); | |
| 185 string16 display_name = path.BaseName().LossyDisplayName(); | |
| 186 | |
| 187 callback.Run(device_id, relative_path, display_name); | |
| 188 } | |
| 189 #endif | |
| 190 | |
| 191 // static | |
| 192 void MediaStorageUtil::FindDevicePathById(const std::string& device_id, | |
| 193 const FilePathCallback& callback) { | |
| 194 Type type; | |
| 195 std::string unique_id; | |
| 196 if (!CrackDeviceId(device_id, &type, &unique_id)) | |
| 197 callback.Run(FilePath()); | |
| 198 | |
| 199 switch (type) { | |
| 200 case MTP_OR_PTP: | |
| 201 // TODO(kmadhusu) We may want to return the MTP device location here. | |
| 202 callback.Run(FilePath()); | |
| 203 break; | |
| 204 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through. | |
| 205 case REMOVABLE_MASS_STORAGE_NO_DCIM: | |
| 206 callback.Run(FilePath(FindRemovableStorageLocationById(device_id))); | |
| 207 break; | |
| 208 case FIXED_MASS_STORAGE: | |
| 209 // For this type, the unique_id is the path. | |
| 210 BrowserThread::PostTask( | |
| 211 BrowserThread::FILE, FROM_HERE, | |
| 212 base::Bind(&ValidatePathOnFileThread, | |
| 213 FilePath::FromUTF8Unsafe(unique_id), callback)); | |
| 214 break; | |
| 215 } | |
| 216 NOTREACHED(); | |
| 217 callback.Run(FilePath()); | |
| 218 } | |
| 219 | |
| 220 MediaStorageUtil::MediaStorageUtil() {} | |
| 221 | |
| 222 } // namespace chrome | |
| OLD | NEW |