| 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 static void (*g_test_get_device_info_from_path_function)( | |
| 33 const FilePath& path, std::string* device_id, string16* device_name, | |
| 34 FilePath* relative_path) = NULL; | |
| 35 | |
| 36 void EmptyPathIsFalseCallback(const MediaStorageUtil::BoolCallback& callback, | |
| 37 FilePath path) { | |
| 38 callback.Run(!path.empty()); | |
| 39 } | |
| 40 | |
| 41 void ValidatePathOnFileThread( | |
| 42 const FilePath& path, const MediaStorageUtil::FilePathCallback& callback) { | |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 44 FilePath result; | |
| 45 if (file_util::PathExists(path)) | |
| 46 result = path; | |
| 47 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 48 base::Bind(callback, path)); | |
| 49 } | |
| 50 | |
| 51 FilePath::StringType FindRemovableStorageLocationById( | |
| 52 const std::string& device_id) { | |
| 53 RemovableStorageInfo media_devices = | |
| 54 SystemMonitor::Get()->GetAttachedRemovableStorage(); | |
| 55 for (RemovableStorageInfo::const_iterator it = media_devices.begin(); | |
| 56 it != media_devices.end(); | |
| 57 ++it) { | |
| 58 if (it->device_id == device_id) | |
| 59 return it->location; | |
| 60 } | |
| 61 return FilePath::StringType(); | |
| 62 } | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 // static | |
| 67 std::string MediaStorageUtil::MakeDeviceId(Type type, | |
| 68 const std::string& unique_id) { | |
| 69 DCHECK(!unique_id.empty()); | |
| 70 switch (type) { | |
| 71 case REMOVABLE_MASS_STORAGE_WITH_DCIM: | |
| 72 return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id; | |
| 73 case REMOVABLE_MASS_STORAGE_NO_DCIM: | |
| 74 return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id; | |
| 75 case FIXED_MASS_STORAGE: | |
| 76 return std::string(kFixedMassStoragePrefix) + unique_id; | |
| 77 case MTP_OR_PTP: | |
| 78 return std::string(kMtpPtpPrefix) + unique_id; | |
| 79 } | |
| 80 NOTREACHED(); | |
| 81 return std::string(); | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id, | |
| 86 Type* type, std::string* unique_id) { | |
| 87 size_t prefix_length = device_id.find_first_of(':'); | |
| 88 std::string prefix = prefix_length != std::string::npos ? | |
| 89 device_id.substr(0, prefix_length + 1) : ""; | |
| 90 | |
| 91 Type found_type; | |
| 92 if (prefix == kRemovableMassStorageWithDCIMPrefix) { | |
| 93 found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM; | |
| 94 } else if (prefix == kRemovableMassStorageNoDCIMPrefix) { | |
| 95 found_type = REMOVABLE_MASS_STORAGE_NO_DCIM; | |
| 96 } else if (prefix == kFixedMassStoragePrefix) { | |
| 97 found_type = FIXED_MASS_STORAGE; | |
| 98 } else if (prefix == kMtpPtpPrefix) { | |
| 99 found_type = MTP_OR_PTP; | |
| 100 } else { | |
| 101 NOTREACHED(); | |
| 102 return false; | |
| 103 } | |
| 104 if (type) | |
| 105 *type = found_type; | |
| 106 | |
| 107 if (unique_id) | |
| 108 *unique_id = device_id.substr(prefix_length + 1); | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { | |
| 114 Type type; | |
| 115 return CrackDeviceId(device_id, &type, NULL) && | |
| 116 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP); | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { | |
| 121 Type type; | |
| 122 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE; | |
| 123 } | |
| 124 | |
| 125 // static | |
| 126 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, | |
| 127 const BoolCallback& callback) { | |
| 128 Type type; | |
| 129 std::string unique_id; | |
| 130 if (!CrackDeviceId(device_id, &type, &unique_id)) { | |
| 131 callback.Run(false); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 switch (type) { | |
| 136 case MTP_OR_PTP: // Fall through. | |
| 137 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through. | |
| 138 case REMOVABLE_MASS_STORAGE_NO_DCIM: | |
| 139 // We should be able to find removable storage in SystemMonitor. | |
| 140 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); | |
| 141 break; | |
| 142 case FIXED_MASS_STORAGE: | |
| 143 // For this type, the unique_id is the path. | |
| 144 BrowserThread::PostTask( | |
| 145 BrowserThread::FILE, FROM_HERE, | |
| 146 base::Bind(&ValidatePathOnFileThread, | |
| 147 FilePath::FromUTF8Unsafe(unique_id), | |
| 148 base::Bind(&EmptyPathIsFalseCallback, callback))); | |
| 149 break; | |
| 150 } | |
| 151 NOTREACHED(); | |
| 152 callback.Run(false); | |
| 153 } | |
| 154 | |
| 155 // static | |
| 156 void MediaStorageUtil::GetDeviceInfoFromPath(const FilePath& path, | |
| 157 std::string* device_id, | |
| 158 string16* device_name, | |
| 159 FilePath* relative_path) { | |
| 160 if (g_test_get_device_info_from_path_function) { | |
| 161 g_test_get_device_info_from_path_function(path, device_id, device_name, | |
| 162 relative_path); | |
| 163 } else { | |
| 164 GetDeviceInfoFromPathImpl(path, device_id, device_name, relative_path); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 // static | |
| 169 void MediaStorageUtil::FindDevicePathById(const std::string& device_id, | |
| 170 const FilePathCallback& callback) { | |
| 171 Type type; | |
| 172 std::string unique_id; | |
| 173 if (!CrackDeviceId(device_id, &type, &unique_id)) | |
| 174 callback.Run(FilePath()); | |
| 175 | |
| 176 switch (type) { | |
| 177 case MTP_OR_PTP: | |
| 178 // TODO(kmadhusu) We may want to return the MTP device location here. | |
| 179 callback.Run(FilePath()); | |
| 180 break; | |
| 181 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through. | |
| 182 case REMOVABLE_MASS_STORAGE_NO_DCIM: | |
| 183 callback.Run(FilePath(FindRemovableStorageLocationById(device_id))); | |
| 184 break; | |
| 185 case FIXED_MASS_STORAGE: | |
| 186 // For this type, the unique_id is the path. | |
| 187 BrowserThread::PostTask( | |
| 188 BrowserThread::FILE, FROM_HERE, | |
| 189 base::Bind(&ValidatePathOnFileThread, | |
| 190 FilePath::FromUTF8Unsafe(unique_id), callback)); | |
| 191 break; | |
| 192 } | |
| 193 NOTREACHED(); | |
| 194 callback.Run(FilePath()); | |
| 195 } | |
| 196 | |
| 197 // static | |
| 198 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting( | |
| 199 GetDeviceInfoFromPathFunction function) { | |
| 200 g_test_get_device_info_from_path_function = function; | |
| 201 } | |
| 202 | |
| 203 MediaStorageUtil::MediaStorageUtil() {} | |
| 204 | |
| 205 #if !defined(OS_LINUX) || defined(OS_CHROMEOS) | |
| 206 // static | |
| 207 void MediaStorageUtil::GetDeviceInfoFromPathImpl(const FilePath& path, | |
| 208 std::string* device_id, | |
| 209 string16* device_name, | |
| 210 FilePath* relative_path) { | |
| 211 // TODO(vandebo) This needs to be implemented per platform. Below is no | |
| 212 // worse than what the code already does. | |
| 213 // * Find mount point parent (determines relative file path) | |
| 214 // * Search System monitor, just in case. | |
| 215 // * If it's a removable device, generate device id, else use device root | |
| 216 // path as id | |
| 217 if (device_id) | |
| 218 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | |
| 219 if (device_name) | |
| 220 *device_name = path.BaseName().LossyDisplayName(); | |
| 221 if (relative_path) | |
| 222 *relative_path = FilePath(); | |
| 223 } | |
| 224 #endif | |
| 225 | |
| 226 } // namespace chrome | |
| OLD | NEW |