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 // chrome::MediaStorageUtil implementation. | 5 // chrome::MediaStorageUtil implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/system_monitor/media_storage_util.h" | 7 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/system_monitor/system_monitor.h" | 14 #include "base/system_monitor/system_monitor.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 | 16 |
| 17 #if defined(OS_CHROMEOS) | |
| 18 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ chromeos.h" | |
| 19 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos. h" | |
| 20 #elif defined(OS_LINUX) | |
| 21 #include "chrome/browser/system_monitor/removable_device_notifications_linux.h" | |
| 22 #elif defined(OS_MACOSX) | |
| 23 #include "chrome/browser/system_monitor/removable_device_notifications_mac.h" | |
| 24 #elif defined(OS_WIN) | |
| 25 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h" | |
| 26 #endif | |
| 27 | |
| 17 using base::SystemMonitor; | 28 using base::SystemMonitor; |
| 18 using content::BrowserThread; | 29 using content::BrowserThread; |
| 19 | 30 |
| 20 namespace chrome { | 31 namespace chrome { |
| 21 | 32 |
| 22 namespace { | 33 namespace { |
| 23 | 34 |
| 24 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo; | 35 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo; |
| 25 | 36 |
| 26 // Prefix constants for different device id spaces. | 37 // Prefix constants for different device id spaces. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 // We should be able to find removable storage in SystemMonitor. | 154 // We should be able to find removable storage in SystemMonitor. |
| 144 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); | 155 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); |
| 145 } | 156 } |
| 146 } | 157 } |
| 147 | 158 |
| 148 // static | 159 // static |
| 149 bool MediaStorageUtil::GetDeviceInfoFromPath(const FilePath& path, | 160 bool MediaStorageUtil::GetDeviceInfoFromPath(const FilePath& path, |
| 150 std::string* device_id, | 161 std::string* device_id, |
| 151 string16* device_name, | 162 string16* device_name, |
| 152 FilePath* relative_path) { | 163 FilePath* relative_path) { |
| 164 if (!path.IsAbsolute()) | |
| 165 return false; | |
| 166 | |
| 153 if (g_test_get_device_info_from_path_function) { | 167 if (g_test_get_device_info_from_path_function) { |
| 154 return g_test_get_device_info_from_path_function(path, device_id, | 168 return g_test_get_device_info_from_path_function(path, device_id, |
| 155 device_name, | 169 device_name, |
| 156 relative_path); | 170 relative_path); |
| 157 } else { | |
| 158 return GetDeviceInfoFromPathImpl(path, device_id, device_name, | |
| 159 relative_path); | |
| 160 } | 171 } |
| 172 | |
| 173 bool found_device = false; | |
| 174 base::SystemMonitor::RemovableStorageInfo device_info; | |
| 175 #if (defined(OS_LINUX) || defined(OS_MACOSX)) && !defined(OS_CHROMEOS) | |
| 176 RemovableDeviceNotifications* notifier = | |
| 177 RemovableDeviceNotifications::GetInstance(); | |
| 178 found_device = notifier->GetDeviceInfoForPath(path, &device_info); | |
| 179 #endif | |
| 180 | |
| 181 #if 0 && defined(OS_CHROMEOS) | |
|
Lei Zhang
2012/09/17 00:25:47
Whose job is it to remove the #if 0 bit and use mt
vandebo (ex-Chrome)
2012/09/17 00:32:58
http://codereview.chromium.org/10908277/ will take
| |
| 182 MediaTransferProtocolDeviceObserver* mtp_manager = | |
| 183 MediaTransferProtocolDeviceObserver::GetInstance(); | |
| 184 found_device = mtp_manager->GetStorageInfoForPath(path, &device_info); | |
| 185 #endif | |
| 186 | |
| 187 if (found_device && IsRemovableDevice(device_info.device_id)) { | |
| 188 if (device_id) | |
| 189 *device_id = device_info.device_id; | |
| 190 if (device_name) | |
| 191 *device_name = device_info.name; | |
| 192 if (relative_path) { | |
| 193 *relative_path = FilePath(); | |
| 194 FilePath mount_point(device_info.location); | |
| 195 mount_point.AppendRelativePath(path, relative_path); | |
| 196 } | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 #if !defined(POSIX) | |
|
Lei Zhang
2012/09/17 00:25:47
OS_POSIX?
vandebo (ex-Chrome)
2012/09/17 00:32:58
Done.
| |
| 201 if (!found_device) | |
| 202 return false; | |
| 203 #endif | |
| 204 | |
| 205 // On Posix systems, there's one root so any absolute path could be valid. | |
| 206 if (device_id) | |
| 207 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | |
| 208 if (device_name) | |
| 209 *device_name = path.BaseName().LossyDisplayName(); | |
| 210 if (relative_path) | |
| 211 *relative_path = FilePath(); | |
| 212 return true; | |
| 161 } | 213 } |
| 162 | 214 |
| 163 // static | 215 // static |
| 164 FilePath MediaStorageUtil::FindDevicePathById(const std::string& device_id) { | 216 FilePath MediaStorageUtil::FindDevicePathById(const std::string& device_id) { |
| 165 Type type; | 217 Type type; |
| 166 std::string unique_id; | 218 std::string unique_id; |
| 167 if (!CrackDeviceId(device_id, &type, &unique_id)) | 219 if (!CrackDeviceId(device_id, &type, &unique_id)) |
| 168 return FilePath(); | 220 return FilePath(); |
| 169 | 221 |
| 170 if (type == FIXED_MASS_STORAGE) { | 222 if (type == FIXED_MASS_STORAGE) { |
| 171 // For this type, the unique_id is the path. | 223 // For this type, the unique_id is the path. |
| 172 return FilePath::FromUTF8Unsafe(unique_id); | 224 return FilePath::FromUTF8Unsafe(unique_id); |
| 173 } | 225 } |
| 174 | 226 |
| 175 DCHECK(type == MTP_OR_PTP || | 227 DCHECK(type == MTP_OR_PTP || |
| 176 type == REMOVABLE_MASS_STORAGE_WITH_DCIM || | 228 type == REMOVABLE_MASS_STORAGE_WITH_DCIM || |
| 177 type == REMOVABLE_MASS_STORAGE_NO_DCIM); | 229 type == REMOVABLE_MASS_STORAGE_NO_DCIM); |
| 178 return FilePath(FindRemovableStorageLocationById(device_id)); | 230 return FilePath(FindRemovableStorageLocationById(device_id)); |
| 179 } | 231 } |
| 180 | 232 |
| 181 // static | 233 // static |
| 182 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting( | 234 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting( |
| 183 GetDeviceInfoFromPathFunction function) { | 235 GetDeviceInfoFromPathFunction function) { |
| 184 g_test_get_device_info_from_path_function = function; | 236 g_test_get_device_info_from_path_function = function; |
| 185 } | 237 } |
| 186 | 238 |
| 187 MediaStorageUtil::MediaStorageUtil() {} | 239 MediaStorageUtil::MediaStorageUtil() {} |
| 188 | 240 |
| 189 #if !defined(OS_LINUX) || defined(OS_CHROMEOS) | |
| 190 // static | |
| 191 bool MediaStorageUtil::GetDeviceInfoFromPathImpl(const FilePath& path, | |
| 192 std::string* device_id, | |
| 193 string16* device_name, | |
| 194 FilePath* relative_path) { | |
| 195 // TODO(vandebo) This needs to be implemented per platform. Below is no | |
| 196 // worse than what the code already does. | |
| 197 // * Find mount point parent (determines relative file path) | |
| 198 // * Search System monitor, just in case. | |
| 199 // * If it's a removable device, generate device id, else use device root | |
| 200 // path as id | |
| 201 if (device_id) | |
| 202 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | |
| 203 if (device_name) | |
| 204 *device_name = path.BaseName().LossyDisplayName(); | |
| 205 if (relative_path) | |
| 206 *relative_path = FilePath(); | |
| 207 return true; | |
| 208 } | |
| 209 #endif | |
| 210 | |
| 211 } // namespace chrome | 241 } // namespace chrome |
| OLD | NEW |