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

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

Issue 10876093: Improve the device type constants. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years, 3 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
OLDNEW
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
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 REMOVABLE_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 REMOVABLE_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 = prefix_length != std::string::npos ? 105 std::string prefix = prefix_length != std::string::npos ?
106 device_id.substr(0, prefix_length + 1) : ""; 106 device_id.substr(0, prefix_length + 1) : "";
107 107
108 Type found_type; 108 Type found_type;
109 if (prefix == kUsbMassStorageWithDCIMPrefix) { 109 if (prefix == kRemovableMassStorageWithDCIMPrefix) {
110 found_type = USB_MASS_STORAGE_WITH_DCIM; 110 found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM;
111 } else if (prefix == kUsbMassStorageNoDCIMPrefix) { 111 } else if (prefix == kRemovableMassStorageNoDCIMPrefix) {
112 found_type = USB_MASS_STORAGE_NO_DCIM; 112 found_type = REMOVABLE_MASS_STORAGE_NO_DCIM;
113 } else if (prefix == kOtherMassStoragePrefix) { 113 } else if (prefix == kFixedMassStoragePrefix) {
114 found_type = OTHER_MASS_STORAGE; 114 found_type = FIXED_MASS_STORAGE;
115 } else if (prefix == kUsbMtpPrefix) { 115 } else if (prefix == kMtpPtpPrefix) {
116 found_type = USB_MTP; 116 found_type = MTP_OR_PTP;
117 } else { 117 } else {
118 NOTREACHED(); 118 NOTREACHED();
119 return false; 119 return false;
120 } 120 }
121 if (type) 121 if (type)
122 *type = found_type; 122 *type = found_type;
123 123
124 if (unique_id) 124 if (unique_id)
125 *unique_id = device_id.substr(prefix_length + 1); 125 *unique_id = device_id.substr(prefix_length + 1);
126 return true; 126 return true;
127 } 127 }
128 128
129 // static 129 // static
130 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { 130 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) {
131 Type type; 131 Type type;
132 return CrackDeviceId(device_id, &type, NULL) && 132 return CrackDeviceId(device_id, &type, NULL) &&
133 (type == USB_MASS_STORAGE_WITH_DCIM || type == USB_MTP); 133 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP);
134 } 134 }
135 135
136 // static 136 // static
137 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { 137 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) {
138 Type type; 138 Type type;
139 return CrackDeviceId(device_id, &type, NULL) && type != OTHER_MASS_STORAGE; 139 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE;
140 } 140 }
141 141
142 // static 142 // static
143 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, 143 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id,
144 const BoolCallback& callback) { 144 const BoolCallback& callback) {
145 Type type; 145 Type type;
146 std::string unique_id; 146 std::string unique_id;
147 if (!CrackDeviceId(device_id, &type, &unique_id)) { 147 if (!CrackDeviceId(device_id, &type, &unique_id)) {
148 callback.Run(false); 148 callback.Run(false);
149 return; 149 return;
150 } 150 }
151 151
152 switch (type) { 152 switch (type) {
153 case USB_MTP: // Fall through 153 case MTP_OR_PTP: // Fall through
154 case USB_MASS_STORAGE_WITH_DCIM: 154 case REMOVABLE_MASS_STORAGE_WITH_DCIM:
155 // We should be able to find media devices in SystemMonitor. 155 // We should be able to find media devices in SystemMonitor.
156 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); 156 callback.Run(!FindRemovableStorageLocationById(device_id).empty());
157 break; 157 break;
158 case USB_MASS_STORAGE_NO_DCIM: 158 case REMOVABLE_MASS_STORAGE_NO_DCIM:
159 FindUSBDeviceById(unique_id, 159 FindUSBDeviceById(unique_id,
160 base::Bind(&EmptyPathIsFalseCallback, callback)); 160 base::Bind(&EmptyPathIsFalseCallback, callback));
161 break; 161 break;
162 case OTHER_MASS_STORAGE: 162 case FIXED_MASS_STORAGE:
163 // For this type, the unique_id is the path. 163 // For this type, the unique_id is the path.
164 BrowserThread::PostTask( 164 BrowserThread::PostTask(
165 BrowserThread::FILE, FROM_HERE, 165 BrowserThread::FILE, FROM_HERE,
166 base::Bind(&ValidatePathOnFileThread, 166 base::Bind(&ValidatePathOnFileThread,
167 FilePath::FromUTF8Unsafe(unique_id), 167 FilePath::FromUTF8Unsafe(unique_id),
168 base::Bind(&EmptyPathIsFalseCallback, callback))); 168 base::Bind(&EmptyPathIsFalseCallback, callback)));
169 break; 169 break;
170 } 170 }
171 NOTREACHED(); 171 NOTREACHED();
172 callback.Run(false); 172 callback.Run(false);
173 } 173 }
174 174
175 // static 175 // static
176 void MediaStorageUtil::GetDeviceInfoFromPath( 176 void MediaStorageUtil::GetDeviceInfoFromPath(
177 const FilePath& path, const DeviceInfoCallback& callback) { 177 const FilePath& path, const DeviceInfoCallback& callback) {
178 // TODO(vandebo) This needs to be implemented per platform. Below is no 178 // TODO(vandebo) This needs to be implemented per platform. Below is no
179 // worse than what the code already does. 179 // worse than what the code already does.
180 // * Find mount point parent (determines relative file path) 180 // * Find mount point parent (determines relative file path)
181 // * Search System monitor, just in case. 181 // * Search System monitor, just in case.
182 // * If it's a USB device, generate device id, else use device root path as id 182 // * If it's a USB device, generate device id, else use device root path as id
183 std::string device_id = 183 std::string device_id =
184 MakeDeviceId(OTHER_MASS_STORAGE, path.AsUTF8Unsafe()); 184 MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe());
185 FilePath relative_path = MakePathRelative(path); 185 FilePath relative_path = MakePathRelative(path);
186 string16 display_name = path.BaseName().LossyDisplayName(); 186 string16 display_name = path.BaseName().LossyDisplayName();
187 187
188 callback.Run(device_id, relative_path, display_name); 188 callback.Run(device_id, relative_path, display_name);
189 return; 189 return;
190 } 190 }
191 191
192 // static 192 // static
193 void MediaStorageUtil::FindDevicePathById(const std::string& device_id, 193 void MediaStorageUtil::FindDevicePathById(const std::string& device_id,
194 const FilePathCallback& callback) { 194 const FilePathCallback& callback) {
195 Type type; 195 Type type;
196 std::string unique_id; 196 std::string unique_id;
197 if (!CrackDeviceId(device_id, &type, &unique_id)) 197 if (!CrackDeviceId(device_id, &type, &unique_id))
198 callback.Run(FilePath()); 198 callback.Run(FilePath());
199 199
200 switch (type) { 200 switch (type) {
201 case USB_MTP: 201 case MTP_OR_PTP:
202 callback.Run(FilePath()); 202 callback.Run(FilePath());
203 break; 203 break;
204 case USB_MASS_STORAGE_NO_DCIM: 204 case REMOVABLE_MASS_STORAGE_NO_DCIM:
205 FindUSBDeviceById(unique_id, callback); 205 FindUSBDeviceById(unique_id, callback);
206 break; 206 break;
207 case OTHER_MASS_STORAGE: 207 case FIXED_MASS_STORAGE:
208 // For this type, the unique_id is the path. 208 // For this type, the unique_id is the path.
209 BrowserThread::PostTask( 209 BrowserThread::PostTask(
210 BrowserThread::FILE, FROM_HERE, 210 BrowserThread::FILE, FROM_HERE,
211 base::Bind(&ValidatePathOnFileThread, 211 base::Bind(&ValidatePathOnFileThread,
212 FilePath::FromUTF8Unsafe(unique_id), callback)); 212 FilePath::FromUTF8Unsafe(unique_id), callback));
213 break; 213 break;
214 case USB_MASS_STORAGE_WITH_DCIM: 214 case REMOVABLE_MASS_STORAGE_WITH_DCIM:
215 callback.Run(FilePath(FindRemovableStorageLocationById(device_id))); 215 callback.Run(FilePath(FindRemovableStorageLocationById(device_id)));
216 break; 216 break;
217 } 217 }
218 NOTREACHED(); 218 NOTREACHED();
219 callback.Run(FilePath()); 219 callback.Run(FilePath());
220 } 220 }
221 221
222 MediaStorageUtil::MediaStorageUtil() {} 222 MediaStorageUtil::MediaStorageUtil() {}
223 223
224 // static 224 // static
225 void MediaStorageUtil::FindUSBDeviceById(const std::string& unique_id, 225 void MediaStorageUtil::FindUSBDeviceById(const std::string& unique_id,
226 const FilePathCallback& callback) { 226 const FilePathCallback& callback) {
227 // TODO(vandebo) This needs to be implemented per platform. 227 // TODO(vandebo) This needs to be implemented per platform.
228 // Type is USB_MASS_STORAGE_NO_DCIM, so it's a device possibly mounted 228 // Type is REMOVABLE_MASS_STORAGE_NO_DCIM, so it's a device possibly mounted
229 // somewhere... 229 // somewhere...
230 NOTREACHED(); 230 NOTREACHED();
231 callback.Run(FilePath()); 231 callback.Run(FilePath());
232 } 232 }
233 233
234 } // namespace chrome 234 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/media_gallery/media_storage_util.h ('k') | chrome/browser/media_gallery/media_storage_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698