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

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

Issue 11490010: [Media Galleries] Introduce a new type for Mac Image Capture. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years 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/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
(...skipping 16 matching lines...) Expand all
27 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h" 27 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h"
28 #endif 28 #endif
29 29
30 #if defined(OS_LINUX) // Implies OS_CHROMEOS 30 #if defined(OS_LINUX) // Implies OS_CHROMEOS
31 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ linux.h" 31 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ linux.h"
32 #endif 32 #endif
33 33
34 using base::SystemMonitor; 34 using base::SystemMonitor;
35 using content::BrowserThread; 35 using content::BrowserThread;
36 36
37 const char kRootPath[] = "/";
38
37 namespace chrome { 39 namespace chrome {
38 40
39 namespace { 41 namespace {
40 42
41 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo; 43 typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo;
42 44
43 // MediaDeviceNotification.DeviceInfo histogram values. 45 // MediaDeviceNotification.DeviceInfo histogram values.
44 enum DeviceInfoHistogramBuckets { 46 enum DeviceInfoHistogramBuckets {
45 MASS_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE, 47 MASS_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE,
46 MASS_STORAGE_DEVICE_UUID_MISSING, 48 MASS_STORAGE_DEVICE_UUID_MISSING,
47 MASS_STORAGE_DEVICE_NAME_MISSING, 49 MASS_STORAGE_DEVICE_NAME_MISSING,
48 MASS_STORAGE_DEVICE_NAME_AND_UUID_MISSING, 50 MASS_STORAGE_DEVICE_NAME_AND_UUID_MISSING,
49 MTP_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE, 51 MTP_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE,
50 MTP_STORAGE_DEVICE_UUID_MISSING, 52 MTP_STORAGE_DEVICE_UUID_MISSING,
51 MTP_STORAGE_DEVICE_NAME_MISSING, 53 MTP_STORAGE_DEVICE_NAME_MISSING,
52 MTP_STORAGE_DEVICE_NAME_AND_UUID_MISSING, 54 MTP_STORAGE_DEVICE_NAME_AND_UUID_MISSING,
53 DEVICE_INFO_BUCKET_BOUNDARY 55 DEVICE_INFO_BUCKET_BOUNDARY
54 }; 56 };
55 57
56 // Prefix constants for different device id spaces. 58 // Prefix constants for different device id spaces.
57 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:"; 59 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:";
58 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:"; 60 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:";
59 const char kFixedMassStoragePrefix[] = "path:"; 61 const char kFixedMassStoragePrefix[] = "path:";
60 const char kMtpPtpPrefix[] = "mtp:"; 62 const char kMtpPtpPrefix[] = "mtp:";
63 const char kMacImageCapture[] = "ic:";
61 64
62 static bool (*g_test_get_device_info_from_path_function)( // NOLINT 65 static bool (*g_test_get_device_info_from_path_function)( // NOLINT
63 const FilePath& path, std::string* device_id, string16* device_name, 66 const FilePath& path, std::string* device_id, string16* device_name,
64 FilePath* relative_path) = NULL; 67 FilePath* relative_path) = NULL;
65 68
66 void ValidatePathOnFileThread( 69 void ValidatePathOnFileThread(
67 const FilePath& path, const MediaStorageUtil::BoolCallback& callback) { 70 const FilePath& path, const MediaStorageUtil::BoolCallback& callback) {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
69 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 72 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
70 base::Bind(callback, file_util::PathExists(path))); 73 base::Bind(callback, file_util::PathExists(path)));
71 } 74 }
72 75
76 bool IsMTPDeviceAttached(const std::string& id) {
Lei Zhang 2012/12/10 21:53:41 Isn't this function really "IsDeviceAttached()" ?
Lei Zhang 2012/12/10 21:53:41 Can this just call FindRemovableStorageLocationByI
Greg Billock 2012/12/10 22:39:41 I think that's what I was doing. I didn't want to
77 RemovableStorageInfo media_devices =
78 SystemMonitor::Get()->GetAttachedRemovableStorage();
79 for (RemovableStorageInfo::const_iterator it = media_devices.begin();
80 it != media_devices.end();
81 ++it) {
82 if (it->device_id == id)
83 return true;
84 }
85 return false;
86 }
87
73 FilePath::StringType FindRemovableStorageLocationById( 88 FilePath::StringType FindRemovableStorageLocationById(
74 const std::string& device_id) { 89 const std::string& device_id) {
75 RemovableStorageInfo media_devices = 90 RemovableStorageInfo media_devices =
76 SystemMonitor::Get()->GetAttachedRemovableStorage(); 91 SystemMonitor::Get()->GetAttachedRemovableStorage();
77 for (RemovableStorageInfo::const_iterator it = media_devices.begin(); 92 for (RemovableStorageInfo::const_iterator it = media_devices.begin();
78 it != media_devices.end(); 93 it != media_devices.end();
79 ++it) { 94 ++it) {
80 if (it->device_id == device_id) 95 if (it->device_id == device_id)
81 return it->location; 96 return it->location;
82 } 97 }
(...skipping 14 matching lines...) Expand all
97 continue; 112 continue;
98 } 113 }
99 114
100 if (type == MediaStorageUtil::FIXED_MASS_STORAGE) { 115 if (type == MediaStorageUtil::FIXED_MASS_STORAGE) {
101 if (!file_util::PathExists(FilePath::FromUTF8Unsafe(unique_id))) 116 if (!file_util::PathExists(FilePath::FromUTF8Unsafe(unique_id)))
102 missing_devices.insert(*it); 117 missing_devices.insert(*it);
103 continue; 118 continue;
104 } 119 }
105 120
106 DCHECK(type == MediaStorageUtil::MTP_OR_PTP || 121 DCHECK(type == MediaStorageUtil::MTP_OR_PTP ||
122 type == MediaStorageUtil::MAC_IMAGE_CAPTURE ||
107 type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM || 123 type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
108 type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); 124 type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM);
109 if (FindRemovableStorageLocationById(*it).empty()) 125 if (type == MediaStorageUtil::MAC_IMAGE_CAPTURE) {
126 if (!IsMTPDeviceAttached(*it))
127 missing_devices.insert(*it);
128 } else if (FindRemovableStorageLocationById(*it).empty()) {
110 missing_devices.insert(*it); 129 missing_devices.insert(*it);
130 }
111 } 131 }
112 132
113 for (MediaStorageUtil::DeviceIdSet::const_iterator it = 133 for (MediaStorageUtil::DeviceIdSet::const_iterator it =
114 missing_devices.begin(); 134 missing_devices.begin();
115 it != missing_devices.end(); 135 it != missing_devices.end();
116 ++it) { 136 ++it) {
117 devices->erase(*it); 137 devices->erase(*it);
118 } 138 }
119 } 139 }
120 140
121 } // namespace 141 } // namespace
122 142
143 // NOTE: instead of all this parsing, why not just pass the type itself?
sail 2012/12/10 19:38:48 this comment isn't clear, is this supposed to be a
Greg Billock 2012/12/10 19:47:37 Removing. This was a note-to-self. On 2012/12/10
123 // static 144 // static
124 std::string MediaStorageUtil::MakeDeviceId(Type type, 145 std::string MediaStorageUtil::MakeDeviceId(Type type,
125 const std::string& unique_id) { 146 const std::string& unique_id) {
126 DCHECK(!unique_id.empty()); 147 DCHECK(!unique_id.empty());
127 switch (type) { 148 switch (type) {
128 case REMOVABLE_MASS_STORAGE_WITH_DCIM: 149 case REMOVABLE_MASS_STORAGE_WITH_DCIM:
129 return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id; 150 return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id;
130 case REMOVABLE_MASS_STORAGE_NO_DCIM: 151 case REMOVABLE_MASS_STORAGE_NO_DCIM:
131 return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id; 152 return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id;
132 case FIXED_MASS_STORAGE: 153 case FIXED_MASS_STORAGE:
133 return std::string(kFixedMassStoragePrefix) + unique_id; 154 return std::string(kFixedMassStoragePrefix) + unique_id;
134 case MTP_OR_PTP: 155 case MTP_OR_PTP:
135 return std::string(kMtpPtpPrefix) + unique_id; 156 return std::string(kMtpPtpPrefix) + unique_id;
157 case MAC_IMAGE_CAPTURE:
158 return std::string(kMacImageCapture) + unique_id;
136 } 159 }
137 NOTREACHED(); 160 NOTREACHED();
138 return std::string(); 161 return std::string();
139 } 162 }
140 163
141 // static 164 // static
142 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id, 165 bool MediaStorageUtil::CrackDeviceId(const std::string& device_id,
143 Type* type, std::string* unique_id) { 166 Type* type, std::string* unique_id) {
144 size_t prefix_length = device_id.find_first_of(':'); 167 size_t prefix_length = device_id.find_first_of(':');
145 std::string prefix = prefix_length != std::string::npos ? 168 std::string prefix = prefix_length != std::string::npos ?
146 device_id.substr(0, prefix_length + 1) : ""; 169 device_id.substr(0, prefix_length + 1) : "";
147 170
148 Type found_type; 171 Type found_type;
149 if (prefix == kRemovableMassStorageWithDCIMPrefix) { 172 if (prefix == kRemovableMassStorageWithDCIMPrefix) {
150 found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM; 173 found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM;
151 } else if (prefix == kRemovableMassStorageNoDCIMPrefix) { 174 } else if (prefix == kRemovableMassStorageNoDCIMPrefix) {
152 found_type = REMOVABLE_MASS_STORAGE_NO_DCIM; 175 found_type = REMOVABLE_MASS_STORAGE_NO_DCIM;
153 } else if (prefix == kFixedMassStoragePrefix) { 176 } else if (prefix == kFixedMassStoragePrefix) {
154 found_type = FIXED_MASS_STORAGE; 177 found_type = FIXED_MASS_STORAGE;
155 } else if (prefix == kMtpPtpPrefix) { 178 } else if (prefix == kMtpPtpPrefix) {
156 found_type = MTP_OR_PTP; 179 found_type = MTP_OR_PTP;
180 } else if (prefix == kMacImageCapture) {
181 found_type = MAC_IMAGE_CAPTURE;
157 } else { 182 } else {
158 NOTREACHED(); 183 NOTREACHED();
159 return false; 184 return false;
160 } 185 }
161 if (type) 186 if (type)
162 *type = found_type; 187 *type = found_type;
163 188
164 if (unique_id) 189 if (unique_id)
165 *unique_id = device_id.substr(prefix_length + 1); 190 *unique_id = device_id.substr(prefix_length + 1);
166 return true; 191 return true;
167 } 192 }
168 193
169 // static 194 // static
170 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) { 195 bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) {
171 Type type; 196 Type type;
172 return CrackDeviceId(device_id, &type, NULL) && 197 return CrackDeviceId(device_id, &type, NULL) &&
173 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP); 198 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP ||
199 type == MAC_IMAGE_CAPTURE);
174 } 200 }
175 201
176 // static 202 // static
177 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { 203 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) {
178 Type type; 204 Type type;
179 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE; 205 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE;
180 } 206 }
181 207
182 // static 208 // static
183 bool MediaStorageUtil::IsMassStorageDevice(const std::string& device_id) { 209 bool MediaStorageUtil::IsMassStorageDevice(const std::string& device_id) {
184 Type type; 210 Type type;
185 return CrackDeviceId(device_id, &type, NULL) && type != MTP_OR_PTP; 211 return CrackDeviceId(device_id, &type, NULL) &&
212 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM ||
213 type == REMOVABLE_MASS_STORAGE_NO_DCIM ||
214 type == FIXED_MASS_STORAGE);
186 } 215 }
187 216
188 // static 217 // static
218 bool MediaStorageUtil::CanCreateFileSystem(const std::string& device_id,
219 const FilePath& path) {
220 Type type;
221 if (!CrackDeviceId(device_id, &type, NULL)) return false;
sail 2012/12/10 19:38:48 return on separate line
Greg Billock 2012/12/10 19:47:37 Done.
222
223 if (type == MAC_IMAGE_CAPTURE)
224 return true;
225
226 return path.IsAbsolute() && path.ReferencesParent();
Lei Zhang 2012/12/10 21:53:41 the second part is "not path.ReferencesParent()"
Greg Billock 2012/12/10 22:39:41 Yes, I'd noticed this up in my client. I don't rem
227 }
228
229 // static
189 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, 230 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id,
190 const BoolCallback& callback) { 231 const BoolCallback& callback) {
191 Type type; 232 Type type;
192 std::string unique_id; 233 std::string unique_id;
193 if (!CrackDeviceId(device_id, &type, &unique_id)) { 234 if (!CrackDeviceId(device_id, &type, &unique_id)) {
194 callback.Run(false); 235 callback.Run(false);
195 return; 236 return;
196 } 237 }
197 238
198 if (type == FIXED_MASS_STORAGE) { 239 if (type == FIXED_MASS_STORAGE) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 Type type; 344 Type type;
304 std::string unique_id; 345 std::string unique_id;
305 if (!CrackDeviceId(device_id, &type, &unique_id)) 346 if (!CrackDeviceId(device_id, &type, &unique_id))
306 return FilePath(); 347 return FilePath();
307 348
308 if (type == FIXED_MASS_STORAGE) { 349 if (type == FIXED_MASS_STORAGE) {
309 // For this type, the unique_id is the path. 350 // For this type, the unique_id is the path.
310 return FilePath::FromUTF8Unsafe(unique_id); 351 return FilePath::FromUTF8Unsafe(unique_id);
311 } 352 }
312 353
354 // This should return "/" because when the prefs returns an absolute path,
355 // it just smacks it on the end of whatever this method returns.
356 // For ImageCapture, we're not using the path as a path anyway, so return
357 // something to make the fake path arithmetic work out.
358 if (type == MAC_IMAGE_CAPTURE) {
359 return FilePath(kRootPath + device_id);
360 }
361
313 DCHECK(type == MTP_OR_PTP || 362 DCHECK(type == MTP_OR_PTP ||
314 type == REMOVABLE_MASS_STORAGE_WITH_DCIM || 363 type == REMOVABLE_MASS_STORAGE_WITH_DCIM ||
315 type == REMOVABLE_MASS_STORAGE_NO_DCIM); 364 type == REMOVABLE_MASS_STORAGE_NO_DCIM);
316 return FilePath(FindRemovableStorageLocationById(device_id)); 365 return FilePath(FindRemovableStorageLocationById(device_id));
317 } 366 }
318 367
319 // static 368 // static
320 void MediaStorageUtil::RecordDeviceInfoHistogram(bool mass_storage, 369 void MediaStorageUtil::RecordDeviceInfoHistogram(bool mass_storage,
321 const std::string& device_uuid, 370 const std::string& device_uuid,
322 const string16& device_name) { 371 const string16& device_name) {
(...skipping 18 matching lines...) Expand all
341 390
342 // static 391 // static
343 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting( 392 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting(
344 GetDeviceInfoFromPathFunction function) { 393 GetDeviceInfoFromPathFunction function) {
345 g_test_get_device_info_from_path_function = function; 394 g_test_get_device_info_from_path_function = function;
346 } 395 }
347 396
348 MediaStorageUtil::MediaStorageUtil() {} 397 MediaStorageUtil::MediaStorageUtil() {}
349 398
350 } // namespace chrome 399 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698