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. | |
kmadhusu
2012/08/17 18:53:35
nit: Comment is not required.
vandebo (ex-Chrome)
2012/08/17 22:55:51
In Lei's readability review the reviewer suggested
| |
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/lazy_instance.h" | |
kmadhusu
2012/08/17 18:53:35
lazy_instance is included both in .h and .cc file.
vandebo (ex-Chrome)
2012/08/17 22:55:51
And used in neither. Fixed.
| |
14 #include "base/logging.h" | |
15 #include "base/system_monitor/system_monitor.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 | |
18 namespace chrome { | |
19 | |
20 namespace { | |
21 | |
22 typedef std::vector<base::SystemMonitor::MediaDeviceInfo> MediaDevicesInfo; | |
23 | |
24 const char kUsbMassStorageWithDCIMPrefix[] = "dcim:"; | |
kmadhusu
2012/08/17 18:53:35
nit: // Media device unique id prefix constants.
vandebo (ex-Chrome)
2012/08/17 22:55:51
Done. (Added a more "why" comment than you sugges
| |
25 const char kUsbMassStorageNoDCIMPrefix[] = "usb:"; | |
26 const char kOtherMassStoragePrefix[] = "path:"; | |
27 const char kUsbMtpPrefix[] = "mtp:"; | |
28 | |
29 void EmptyPathIsFalseCallback(const base::Callback<void(bool)>& callback, | |
kmadhusu
2012/08/17 18:53:35
Document this helper function.
vandebo (ex-Chrome)
2012/08/17 22:55:51
I think the name and body are pretty self descript
| |
30 FilePath path) { | |
kmadhusu
2012/08/17 18:53:35
nit: "const FilePath&"
vandebo (ex-Chrome)
2012/08/17 22:55:51
This can't be const because it has to match the Fi
| |
31 callback.Run(!path.empty()); | |
32 } | |
33 | |
34 void ValidatePathOnFileThread(const FilePath& path, | |
35 const base::Callback<void(FilePath)>& callback) { | |
36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
kmadhusu
2012/08/17 18:53:35
nit: Add using content::BrowserThread;
vandebo (ex-Chrome)
2012/08/20 18:35:14
Done.
| |
37 FilePath result; | |
38 if (file_util::PathExists(path)) | |
39 result = path; | |
40 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
41 base::Bind(callback, path)); | |
42 } | |
43 | |
44 FilePath MakePathRelative(FilePath path) { | |
kmadhusu
2012/08/17 18:53:35
nit: GetRelativePath
nit: const FilePath&
nit: Doc
vandebo (ex-Chrome)
2012/08/17 22:55:51
Done.
| |
45 if (!path.IsAbsolute()) | |
46 return path; | |
47 | |
48 FilePath relative; | |
49 std::vector<FilePath::StringType> components; | |
50 path.GetComponents(&components); | |
51 | |
52 // On Windows, the first component may be the drive letter with the second | |
53 // being \\. | |
54 int start = 1; | |
55 if (components[1].size() == 1 && FilePath::IsSeparator(components[1][0])) | |
56 start = 2; | |
57 | |
58 for (size_t i = start; i < components.size(); i++) | |
59 relative = relative.Append(components[i]); | |
60 return relative; | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 // static | |
66 std::string MediaStorageUtil::MakeDeviceId(Type type, | |
kmadhusu
2012/08/17 18:53:35
nit: const Type&
nit: DCHECK(!unique_id.empty());
vandebo (ex-Chrome)
2012/08/17 22:55:51
I don't think we usually use const ref's for enums
| |
67 const std::string& unique_id) { | |
68 switch (type) { | |
69 case USB_MASS_STORAGE_WITH_DCIM: | |
70 return std::string(kUsbMassStorageWithDCIMPrefix) + unique_id; | |
71 case USB_MASS_STORAGE_NO_DCIM: | |
72 return std::string(kUsbMassStorageNoDCIMPrefix) + unique_id; | |
73 case OTHER_MASS_STORAGE: | |
74 return std::string(kOtherMassStoragePrefix) + unique_id; | |
75 case USB_MTP: | |
76 return std::string(kUsbMtpPrefix) + unique_id; | |
77 default: | |
78 NOTREACHED(); | |
79 return std::string(); | |
kmadhusu
2012/08/17 18:53:35
Can we return a bool to notify failure?
vandebo (ex-Chrome)
2012/08/17 22:55:51
Failure shouldn't happen, hence the NOTREACHED.
kmadhusu
2012/08/20 17:13:49
(repeating our offline discussion): There is a cha
vandebo (ex-Chrome)
2012/08/20 18:35:14
Our in person conversation was about corrupt prefe
| |
80 } | |
81 } | |
82 | |
83 // static | |
84 void MediaStorageUtil::CrackDeviceId(const std::string& device_id, | |
85 Type* type, std::string* unique_id) { | |
86 size_t prefix_length = device_id.find_first_of(':'); | |
87 std::string prefix = device_id.substr(0, prefix_length); | |
88 | |
89 if (type) { | |
90 if (prefix == kUsbMassStorageWithDCIMPrefix) { | |
91 *type = USB_MASS_STORAGE_WITH_DCIM; | |
92 } else if (prefix == kUsbMassStorageNoDCIMPrefix) { | |
93 *type = USB_MASS_STORAGE_NO_DCIM; | |
94 } else if (prefix == kOtherMassStoragePrefix) { | |
95 *type = OTHER_MASS_STORAGE; | |
96 } else if (prefix == kUsbMtpPrefix) { | |
97 *type = USB_MTP; | |
98 } else { | |
99 NOTREACHED(); | |
kmadhusu
2012/08/17 18:53:35
Can we return a bool to notify failure?
vandebo (ex-Chrome)
2012/08/17 22:55:51
Failure shouldn't happen, hence the NOTREACHED.
kmadhusu
2012/08/20 17:13:49
same here.
vandebo (ex-Chrome)
2012/08/20 18:35:14
Done.
| |
100 } | |
101 } | |
102 | |
103 if (unique_id) | |
104 *unique_id = device_id.substr(prefix_length + 1); | |
105 } | |
106 | |
107 // static | |
108 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { | |
109 Type type; | |
110 CrackDeviceId(device_id, &type, NULL); | |
111 return type == USB_MASS_STORAGE_WITH_DCIM || type == USB_MTP; | |
112 } | |
113 | |
114 // static | |
115 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { | |
116 Type type; | |
117 CrackDeviceId(device_id, &type, NULL); | |
118 return type != OTHER_MASS_STORAGE; | |
119 } | |
120 | |
121 // static | |
122 void MediaStorageUtil::IsDeviceAttached( | |
123 const std::string& device_id, const base::Callback<void(bool)>& callback) { | |
124 Type type; | |
125 std::string unique_id; | |
126 CrackDeviceId(device_id, &type, &unique_id); | |
127 | |
128 switch (type) { | |
129 case USB_MTP: // Fall through | |
130 case USB_MASS_STORAGE_WITH_DCIM: { | |
131 // We should be able to find media devices in SystemMonitor. | |
132 MediaDevicesInfo media_devices = | |
133 base::SystemMonitor::Get()->GetAttachedMediaDevices(); | |
kmadhusu
2012/08/17 18:53:35
Add a helper function in SystemMonitor to check wh
vandebo (ex-Chrome)
2012/08/17 22:55:51
What advantage would that have?
kmadhusu
2012/08/20 17:13:49
As we discussed, you can leave this as it is.
vandebo (ex-Chrome)
2012/08/20 18:35:14
As you suggested, I pulled the duplicated code int
| |
134 for (MediaDevicesInfo::const_iterator it = media_devices.begin(); | |
135 it != media_devices.end(); | |
136 ++it) { | |
137 if (it->unique_id == device_id) { | |
138 callback.Run(true); | |
139 return; | |
140 } | |
141 } | |
142 callback.Run(false); | |
143 break; | |
144 } | |
145 case USB_MASS_STORAGE_NO_DCIM: | |
146 FindUSBDeviceById(unique_id, | |
147 base::Bind(&EmptyPathIsFalseCallback, callback)); | |
kmadhusu
2012/08/17 18:53:35
nit: Can we rename "EmptyPathIsFalseCallback" ?
vandebo (ex-Chrome)
2012/08/17 22:55:51
Do you have a suggestion? I think it's a descript
| |
148 break; | |
149 case OTHER_MASS_STORAGE: | |
150 // For this type, the unique_id is the path. | |
151 content::BrowserThread::PostTask( | |
152 content::BrowserThread::FILE, FROM_HERE, | |
153 base::Bind(&ValidatePathOnFileThread, | |
154 FilePath::FromUTF8Unsafe(unique_id), | |
155 base::Bind(&EmptyPathIsFalseCallback, callback))); | |
156 break; | |
157 default: | |
158 NOTREACHED(); | |
159 } | |
160 } | |
161 | |
162 // static | |
163 void MediaStorageUtil::GetDeviceInfoFromPath( | |
164 const FilePath& input, | |
165 const base::Callback<void(std::string, FilePath, string16)>& callback) { | |
166 // TODO(vandebo) This needs to be implemented per platform. Below is no | |
167 // worse than what the code already does. | |
168 // * Find mount point parent (determines relative file path) | |
169 // * Search System monitor, just in case. | |
170 // * If it's a USB device, generate device id, else use device root path as id | |
171 std::string device_id = | |
172 MakeDeviceId(OTHER_MASS_STORAGE, input.AsUTF8Unsafe()); | |
173 FilePath relative_path = MakePathRelative(input); | |
174 string16 display_name = input.BaseName().LossyDisplayName(); | |
175 | |
176 callback.Run(device_id, relative_path, display_name); | |
177 return; | |
178 } | |
179 | |
180 // static | |
181 void MediaStorageUtil::FindDevicePathById( | |
182 const std::string& device_id, | |
183 const base::Callback<void(FilePath)>& callback) { | |
184 Type type; | |
185 std::string unique_id; | |
186 CrackDeviceId(device_id, &type, &unique_id); | |
187 | |
188 switch (type) { | |
189 case USB_MTP: | |
190 callback.Run(FilePath()); | |
191 break; | |
192 case USB_MASS_STORAGE_NO_DCIM: | |
193 FindUSBDeviceById(unique_id, callback); | |
194 break; | |
195 case OTHER_MASS_STORAGE: | |
196 // For this type, the unique_id is the path. | |
197 content::BrowserThread::PostTask( | |
198 content::BrowserThread::FILE, FROM_HERE, | |
199 base::Bind(&ValidatePathOnFileThread, | |
200 FilePath::FromUTF8Unsafe(unique_id), callback)); | |
201 break; | |
202 case USB_MASS_STORAGE_WITH_DCIM: { | |
203 FilePath result; | |
204 MediaDevicesInfo media_devices = | |
205 base::SystemMonitor::Get()->GetAttachedMediaDevices(); | |
kmadhusu
2012/08/17 18:53:35
nit: using base::SystemMonitor;
vandebo (ex-Chrome)
2012/08/20 18:35:14
Done.
| |
206 for (MediaDevicesInfo::const_iterator it = media_devices.begin(); | |
207 it != media_devices.end(); | |
208 ++it) { | |
209 if (it->unique_id == device_id) { | |
210 result = FilePath(it->location); | |
211 break; | |
212 } | |
213 } | |
214 callback.Run(result); | |
215 break; | |
216 } | |
217 default: | |
218 NOTREACHED(); | |
219 } | |
220 } | |
221 | |
222 MediaStorageUtil::MediaStorageUtil() {} | |
223 | |
224 // static | |
225 void MediaStorageUtil::FindUSBDeviceById( | |
226 const std::string unique_id, | |
227 const base::Callback<void(FilePath)>& callback) { | |
228 // TODO(vandebo) This needs to be implemented per platform. | |
229 // Type is USB_MASS_STORAGE_NO_DCIM, so it's a device possibly mounted | |
230 // somewhere... | |
231 NOTREACHED(); | |
232 callback.Run(FilePath()); | |
233 } | |
234 | |
235 } // namespace chrome | |
OLD | NEW |