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/media_gallery/media_storage_util.h" | 7 #include "chrome/browser/media_gallery/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 using base::SystemMonitor; | 17 using base::SystemMonitor; |
18 using content::BrowserThread; | 18 using content::BrowserThread; |
19 | 19 |
20 namespace chrome { | 20 namespace chrome { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo; | 24 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo; |
25 | 25 |
26 // Prefix constants for different device id spaces. | 26 // Prefix constants for different device id spaces. |
27 const char kUsbMassStorageWithDCIMPrefix[] = "dcim:"; | 27 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:"; |
28 const char kUsbMassStorageNoDCIMPrefix[] = "usb:"; | 28 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:"; |
29 const char kOtherMassStoragePrefix[] = "path:"; | 29 const char kFixedMassStoragePrefix[] = "path:"; |
30 const char kUsbMtpPrefix[] = "mtp:"; | 30 const char kMtpPtpPrefix[] = "mtp:"; |
31 | 31 |
32 void EmptyPathIsFalseCallback(const MediaStorageUtil::BoolCallback& callback, | 32 void EmptyPathIsFalseCallback(const MediaStorageUtil::BoolCallback& callback, |
33 FilePath path) { | 33 FilePath path) { |
34 callback.Run(!path.empty()); | 34 callback.Run(!path.empty()); |
35 } | 35 } |
36 | 36 |
37 void ValidatePathOnFileThread( | 37 void ValidatePathOnFileThread( |
38 const FilePath& path, const MediaStorageUtil::FilePathCallback& callback) { | 38 const FilePath& path, const MediaStorageUtil::FilePathCallback& callback) { |
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
40 FilePath result; | 40 FilePath result; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 return relative; | 78 return relative; |
79 } | 79 } |
80 | 80 |
81 } // namespace | 81 } // namespace |
82 | 82 |
83 // static | 83 // static |
84 std::string MediaStorageUtil::MakeDeviceId(Type type, | 84 std::string MediaStorageUtil::MakeDeviceId(Type type, |
85 const std::string& unique_id) { | 85 const std::string& unique_id) { |
86 DCHECK(!unique_id.empty()); | 86 DCHECK(!unique_id.empty()); |
87 switch (type) { | 87 switch (type) { |
88 case USB_MASS_STORAGE_WITH_DCIM: | 88 case REMOVALBE_MASS_STORAGE_WITH_DCIM: |
89 return std::string(kUsbMassStorageWithDCIMPrefix) + unique_id; | 89 return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id; |
90 case USB_MASS_STORAGE_NO_DCIM: | 90 case REMOVALBE_MASS_STORAGE_NO_DCIM: |
91 return std::string(kUsbMassStorageNoDCIMPrefix) + unique_id; | 91 return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id; |
92 case OTHER_MASS_STORAGE: | 92 case FIXED_MASS_STORAGE: |
93 return std::string(kOtherMassStoragePrefix) + unique_id; | 93 return std::string(kFixedMassStoragePrefix) + unique_id; |
94 case USB_MTP: | 94 case MTP_OR_PTP: |
95 return std::string(kUsbMtpPrefix) + unique_id; | 95 return std::string(kMtpPtpPrefix) + unique_id; |
96 } | 96 } |
97 NOTREACHED(); | 97 NOTREACHED(); |
98 return std::string(); | 98 return std::string(); |
99 } | 99 } |
100 | 100 |
101 // static | 101 // static |
102 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id, | 102 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id, |
103 Type* type, std::string* unique_id) { | 103 Type* type, std::string* unique_id) { |
104 size_t prefix_length = device_id.find_first_of(':'); | 104 size_t prefix_length = device_id.find_first_of(':'); |
105 std::string prefix = device_id.substr(0, prefix_length); | 105 std::string prefix = device_id.substr(0, prefix_length); |
106 | 106 |
107 Type found_type; | 107 Type found_type; |
108 if (prefix == kUsbMassStorageWithDCIMPrefix) { | 108 if (prefix == kRemovableMassStorageWithDCIMPrefix) { |
109 found_type = USB_MASS_STORAGE_WITH_DCIM; | 109 found_type = REMOVALBE_MASS_STORAGE_WITH_DCIM; |
110 } else if (prefix == kUsbMassStorageNoDCIMPrefix) { | 110 } else if (prefix == kRemovableMassStorageNoDCIMPrefix) { |
111 found_type = USB_MASS_STORAGE_NO_DCIM; | 111 found_type = REMOVALBE_MASS_STORAGE_NO_DCIM; |
112 } else if (prefix == kOtherMassStoragePrefix) { | 112 } else if (prefix == kFixedMassStoragePrefix) { |
113 found_type = OTHER_MASS_STORAGE; | 113 found_type = FIXED_MASS_STORAGE; |
114 } else if (prefix == kUsbMtpPrefix) { | 114 } else if (prefix == kMtpPtpPrefix) { |
115 found_type = USB_MTP; | 115 found_type = MTP_OR_PTP; |
116 } else { | 116 } else { |
117 NOTREACHED(); | 117 NOTREACHED(); |
118 return false; | 118 return false; |
119 } | 119 } |
120 if (type) | 120 if (type) |
121 *type = found_type; | 121 *type = found_type; |
122 | 122 |
123 if (unique_id) | 123 if (unique_id) |
124 *unique_id = device_id.substr(prefix_length + 1); | 124 *unique_id = device_id.substr(prefix_length + 1); |
125 return true; | 125 return true; |
126 } | 126 } |
127 | 127 |
128 // static | 128 // static |
129 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { | 129 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { |
130 Type type; | 130 Type type; |
131 return CrackDeviceId(device_id, &type, NULL) && | 131 return CrackDeviceId(device_id, &type, NULL) && |
132 (type == USB_MASS_STORAGE_WITH_DCIM || type == USB_MTP); | 132 (type == REMOVALBE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP); |
133 } | 133 } |
134 | 134 |
135 // static | 135 // static |
136 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { | 136 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { |
137 Type type; | 137 Type type; |
138 return CrackDeviceId(device_id, &type, NULL) && type != OTHER_MASS_STORAGE; | 138 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE; |
139 } | 139 } |
140 | 140 |
141 // static | 141 // static |
142 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, | 142 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, |
143 const BoolCallback& callback) { | 143 const BoolCallback& callback) { |
144 Type type; | 144 Type type; |
145 std::string unique_id; | 145 std::string unique_id; |
146 if (!CrackDeviceId(device_id, &type, &unique_id)) { | 146 if (!CrackDeviceId(device_id, &type, &unique_id)) { |
147 callback.Run(false); | 147 callback.Run(false); |
148 return; | 148 return; |
149 } | 149 } |
150 | 150 |
151 switch (type) { | 151 switch (type) { |
152 case USB_MTP: // Fall through | 152 case MTP_OR_PTP: // Fall through |
153 case USB_MASS_STORAGE_WITH_DCIM: | 153 case REMOVALBE_MASS_STORAGE_WITH_DCIM: |
154 // We should be able to find media devices in SystemMonitor. | 154 // We should be able to find media devices in SystemMonitor. |
155 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); | 155 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); |
156 break; | 156 break; |
157 case USB_MASS_STORAGE_NO_DCIM: | 157 case REMOVALBE_MASS_STORAGE_NO_DCIM: |
158 FindUSBDeviceById(unique_id, | 158 FindUSBDeviceById(unique_id, |
159 base::Bind(&EmptyPathIsFalseCallback, callback)); | 159 base::Bind(&EmptyPathIsFalseCallback, callback)); |
160 break; | 160 break; |
161 case OTHER_MASS_STORAGE: | 161 case FIXED_MASS_STORAGE: |
162 // For this type, the unique_id is the path. | 162 // For this type, the unique_id is the path. |
163 BrowserThread::PostTask( | 163 BrowserThread::PostTask( |
164 BrowserThread::FILE, FROM_HERE, | 164 BrowserThread::FILE, FROM_HERE, |
165 base::Bind(&ValidatePathOnFileThread, | 165 base::Bind(&ValidatePathOnFileThread, |
166 FilePath::FromUTF8Unsafe(unique_id), | 166 FilePath::FromUTF8Unsafe(unique_id), |
167 base::Bind(&EmptyPathIsFalseCallback, callback))); | 167 base::Bind(&EmptyPathIsFalseCallback, callback))); |
168 break; | 168 break; |
169 } | 169 } |
170 NOTREACHED(); | 170 NOTREACHED(); |
171 callback.Run(false); | 171 callback.Run(false); |
172 } | 172 } |
173 | 173 |
174 // static | 174 // static |
175 void MediaStorageUtil::GetDeviceInfoFromPath( | 175 void MediaStorageUtil::GetDeviceInfoFromPath( |
176 const FilePath& path, const DeviceInfoCallback& callback) { | 176 const FilePath& path, const DeviceInfoCallback& callback) { |
177 // TODO(vandebo) This needs to be implemented per platform. Below is no | 177 // TODO(vandebo) This needs to be implemented per platform. Below is no |
178 // worse than what the code already does. | 178 // worse than what the code already does. |
179 // * Find mount point parent (determines relative file path) | 179 // * Find mount point parent (determines relative file path) |
180 // * Search System monitor, just in case. | 180 // * Search System monitor, just in case. |
181 // * If it's a USB device, generate device id, else use device root path as id | 181 // * If it's a USB device, generate device id, else use device root path as id |
182 std::string device_id = | 182 std::string device_id = |
183 MakeDeviceId(OTHER_MASS_STORAGE, path.AsUTF8Unsafe()); | 183 MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); |
184 FilePath relative_path = MakePathRelative(path); | 184 FilePath relative_path = MakePathRelative(path); |
185 string16 display_name = path.BaseName().LossyDisplayName(); | 185 string16 display_name = path.BaseName().LossyDisplayName(); |
186 | 186 |
187 callback.Run(device_id, relative_path, display_name); | 187 callback.Run(device_id, relative_path, display_name); |
188 return; | 188 return; |
189 } | 189 } |
190 | 190 |
191 // static | 191 // static |
192 void MediaStorageUtil::FindDevicePathById(const std::string& device_id, | 192 void MediaStorageUtil::FindDevicePathById(const std::string& device_id, |
193 const FilePathCallback& callback) { | 193 const FilePathCallback& callback) { |
194 Type type; | 194 Type type; |
195 std::string unique_id; | 195 std::string unique_id; |
196 if (!CrackDeviceId(device_id, &type, &unique_id)) | 196 if (!CrackDeviceId(device_id, &type, &unique_id)) |
197 callback.Run(FilePath()); | 197 callback.Run(FilePath()); |
198 | 198 |
199 switch (type) { | 199 switch (type) { |
200 case USB_MTP: | 200 case MTP_OR_PTP: |
201 callback.Run(FilePath()); | 201 callback.Run(FilePath()); |
202 break; | 202 break; |
203 case USB_MASS_STORAGE_NO_DCIM: | 203 case REMOVALBE_MASS_STORAGE_NO_DCIM: |
204 FindUSBDeviceById(unique_id, callback); | 204 FindUSBDeviceById(unique_id, callback); |
205 break; | 205 break; |
206 case OTHER_MASS_STORAGE: | 206 case FIXED_MASS_STORAGE: |
207 // For this type, the unique_id is the path. | 207 // For this type, the unique_id is the path. |
208 BrowserThread::PostTask( | 208 BrowserThread::PostTask( |
209 BrowserThread::FILE, FROM_HERE, | 209 BrowserThread::FILE, FROM_HERE, |
210 base::Bind(&ValidatePathOnFileThread, | 210 base::Bind(&ValidatePathOnFileThread, |
211 FilePath::FromUTF8Unsafe(unique_id), callback)); | 211 FilePath::FromUTF8Unsafe(unique_id), callback)); |
212 break; | 212 break; |
213 case USB_MASS_STORAGE_WITH_DCIM: | 213 case REMOVALBE_MASS_STORAGE_WITH_DCIM: |
214 callback.Run(FilePath(FindRemovableStorageLocationById(device_id))); | 214 callback.Run(FilePath(FindRemovableStorageLocationById(device_id))); |
215 break; | 215 break; |
216 } | 216 } |
217 NOTREACHED(); | 217 NOTREACHED(); |
218 callback.Run(FilePath()); | 218 callback.Run(FilePath()); |
219 } | 219 } |
220 | 220 |
221 MediaStorageUtil::MediaStorageUtil() {} | 221 MediaStorageUtil::MediaStorageUtil() {} |
222 | 222 |
223 // static | 223 // static |
224 void MediaStorageUtil::FindUSBDeviceById(const std::string& unique_id, | 224 void MediaStorageUtil::FindUSBDeviceById(const std::string& unique_id, |
225 const FilePathCallback& callback) { | 225 const FilePathCallback& callback) { |
226 // TODO(vandebo) This needs to be implemented per platform. | 226 // TODO(vandebo) This needs to be implemented per platform. |
227 // Type is USB_MASS_STORAGE_NO_DCIM, so it's a device possibly mounted | 227 // Type is REMOVALBE_MASS_STORAGE_NO_DCIM, so it's a device possibly mounted |
228 // somewhere... | 228 // somewhere... |
229 NOTREACHED(); | 229 NOTREACHED(); |
230 callback.Run(FilePath()); | 230 callback.Run(FilePath()); |
231 } | 231 } |
232 | 232 |
233 } // namespace chrome | 233 } // namespace chrome |
OLD | NEW |