Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(627)

Side by Side Diff: chrome/browser/media_gallery/media_storage_util.cc

Issue 10829384: SystemMonitor: Pull device type into the device id (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comment Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/media_gallery/media_storage_util.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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::MediaDeviceInfo> MediaDevicesInfo;
25
26 // Prefix constants for different device id spaces.
27 const char kUsbMassStorageWithDCIMPrefix[] = "dcim:";
28 const char kUsbMassStorageNoDCIMPrefix[] = "usb:";
29 const char kOtherMassStoragePrefix[] = "path:";
30 const char kUsbMtpPrefix[] = "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 FindMediaDeviceLocationById(const std::string& device_id) {
48 MediaDevicesInfo media_devices =
49 SystemMonitor::Get()->GetAttachedMediaDevices();
50 for (MediaDevicesInfo::const_iterator it = media_devices.begin();
51 it != media_devices.end();
52 ++it) {
53 if (it->unique_id == device_id)
54 return it->location;
55 }
56 return FilePath::StringType();
57 }
58
59 // TODO(vandebo) use FilePath::AppendRelativePath instead
60 // Make |path| a relative path, i.e. strip the drive letter and leading /.
61 FilePath MakePathRelative(const FilePath& path) {
62 if (!path.IsAbsolute())
63 return path;
64
65 FilePath relative;
66 std::vector<FilePath::StringType> components;
67 path.GetComponents(&components);
68
69 // On Windows, the first component may be the drive letter with the second
70 // being \\.
71 int start = 1;
72 if (components[1].size() == 1 && FilePath::IsSeparator(components[1][0]))
73 start = 2;
74
75 for (size_t i = start; i < components.size(); i++)
76 relative = relative.Append(components[i]);
77 return relative;
78 }
79
80 } // namespace
81
82 // static
83 std::string MediaStorageUtil::MakeDeviceId(Type type,
84 const std::string& unique_id) {
85 DCHECK(!unique_id.empty());
86 switch (type) {
87 case USB_MASS_STORAGE_WITH_DCIM:
88 return std::string(kUsbMassStorageWithDCIMPrefix) + unique_id;
89 case USB_MASS_STORAGE_NO_DCIM:
90 return std::string(kUsbMassStorageNoDCIMPrefix) + unique_id;
91 case OTHER_MASS_STORAGE:
92 return std::string(kOtherMassStoragePrefix) + unique_id;
93 case USB_MTP:
94 return std::string(kUsbMtpPrefix) + unique_id;
95 }
96 NOTREACHED();
97 return std::string();
98 }
99
100 // static
101 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id,
102 Type* type, std::string* unique_id) {
103 size_t prefix_length = device_id.find_first_of(':');
104 std::string prefix = device_id.substr(0, prefix_length);
105
106 Type found_type;
107 if (prefix == kUsbMassStorageWithDCIMPrefix) {
108 found_type = USB_MASS_STORAGE_WITH_DCIM;
109 } else if (prefix == kUsbMassStorageNoDCIMPrefix) {
110 found_type = USB_MASS_STORAGE_NO_DCIM;
111 } else if (prefix == kOtherMassStoragePrefix) {
112 found_type = OTHER_MASS_STORAGE;
113 } else if (prefix == kUsbMtpPrefix) {
114 found_type = USB_MTP;
115 } else {
116 NOTREACHED();
117 return false;
118 }
119 if (type)
120 *type = found_type;
121
122 if (unique_id)
123 *unique_id = device_id.substr(prefix_length + 1);
124 return true;
125 }
126
127 // static
128 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) {
129 Type type;
130 return CrackDeviceId(device_id, &type, NULL) &&
131 (type == USB_MASS_STORAGE_WITH_DCIM || type == USB_MTP);
132 }
133
134 // static
135 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) {
136 Type type;
137 return CrackDeviceId(device_id, &type, NULL) && type != OTHER_MASS_STORAGE;
138 }
139
140 // static
141 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id,
142 const BoolCallback& callback) {
143 Type type;
144 std::string unique_id;
145 if (!CrackDeviceId(device_id, &type, &unique_id)) {
146 callback.Run(false);
147 return;
148 }
149
150 switch (type) {
151 case USB_MTP: // Fall through
152 case USB_MASS_STORAGE_WITH_DCIM:
153 // We should be able to find media devices in SystemMonitor.
154 callback.Run(!FindMediaDeviceLocationById(device_id).empty());
155 break;
156 case USB_MASS_STORAGE_NO_DCIM:
157 FindUSBDeviceById(unique_id,
158 base::Bind(&EmptyPathIsFalseCallback, callback));
159 break;
160 case OTHER_MASS_STORAGE:
161 // For this type, the unique_id is the path.
162 BrowserThread::PostTask(
163 BrowserThread::FILE, FROM_HERE,
164 base::Bind(&ValidatePathOnFileThread,
165 FilePath::FromUTF8Unsafe(unique_id),
166 base::Bind(&EmptyPathIsFalseCallback, callback)));
167 break;
168 }
169 NOTREACHED();
170 callback.Run(false);
171 }
172
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 USB device, generate device id, else use device root path as id
181 std::string device_id =
182 MakeDeviceId(OTHER_MASS_STORAGE, path.AsUTF8Unsafe());
183 FilePath relative_path = MakePathRelative(path);
184 string16 display_name = path.BaseName().LossyDisplayName();
185
186 callback.Run(device_id, relative_path, display_name);
187 return;
188 }
189
190 // static
191 void MediaStorageUtil::FindDevicePathById(const std::string& device_id,
192 const FilePathCallback& callback) {
193 Type type;
194 std::string unique_id;
195 if (!CrackDeviceId(device_id, &type, &unique_id))
196 callback.Run(FilePath());
197
198 switch (type) {
199 case USB_MTP:
200 callback.Run(FilePath());
201 break;
202 case USB_MASS_STORAGE_NO_DCIM:
203 FindUSBDeviceById(unique_id, callback);
204 break;
205 case OTHER_MASS_STORAGE:
206 // For this type, the unique_id is the path.
207 BrowserThread::PostTask(
208 BrowserThread::FILE, FROM_HERE,
209 base::Bind(&ValidatePathOnFileThread,
210 FilePath::FromUTF8Unsafe(unique_id), callback));
211 break;
212 case USB_MASS_STORAGE_WITH_DCIM:
213 callback.Run(FilePath(FindMediaDeviceLocationById(device_id)));
214 break;
215 }
216 NOTREACHED();
217 callback.Run(FilePath());
218 }
219
220 MediaStorageUtil::MediaStorageUtil() {}
221
222 // static
223 void MediaStorageUtil::FindUSBDeviceById(const std::string& unique_id,
224 const FilePathCallback& callback) {
225 // TODO(vandebo) This needs to be implemented per platform.
226 // Type is USB_MASS_STORAGE_NO_DCIM, so it's a device possibly mounted
227 // somewhere...
228 NOTREACHED();
229 callback.Run(FilePath());
230 }
231
232 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/media_gallery/media_storage_util.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698