1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// chrome::MediaStorageUtil implementation.
#include "chrome/browser/system_monitor/media_storage_util.h"
#include <vector>
#include "base/callback.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/system_monitor/system_monitor.h"
#include "content/public/browser/browser_thread.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.h"
#include "chrome/browser/system_monitor/removable_device_notifications_chromeos.h"
#elif defined(OS_LINUX)
#include "chrome/browser/system_monitor/removable_device_notifications_linux.h"
#elif defined(OS_MACOSX)
#include "chrome/browser/system_monitor/removable_device_notifications_mac.h"
#elif defined(OS_WIN)
#include "chrome/browser/system_monitor/removable_device_notifications_window_win.h"
#endif
using base::SystemMonitor;
using content::BrowserThread;
namespace chrome {
namespace {
typedef std::vector<SystemMonitor::RemovableStorageInfo> RemovableStorageInfo;
// MediaDeviceNotification.DeviceInfo histogram values.
enum DeviceInfoHistogramBuckets {
MASS_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE,
MASS_STORAGE_DEVICE_UUID_MISSING,
MASS_STORAGE_DEVICE_NAME_MISSING,
MASS_STORAGE_DEVICE_NAME_AND_UUID_MISSING,
MTP_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE,
MTP_STORAGE_DEVICE_UUID_MISSING,
MTP_STORAGE_DEVICE_NAME_MISSING,
MTP_STORAGE_DEVICE_NAME_AND_UUID_MISSING,
DEVICE_INFO_BUCKET_BOUNDARY
};
// Prefix constants for different device id spaces.
const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:";
const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:";
const char kFixedMassStoragePrefix[] = "path:";
const char kMtpPtpPrefix[] = "mtp:";
static bool (*g_test_get_device_info_from_path_function)(
const FilePath& path, std::string* device_id, string16* device_name,
FilePath* relative_path) = NULL;
void ValidatePathOnFileThread(
const FilePath& path, const MediaStorageUtil::BoolCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, file_util::PathExists(path)));
}
FilePath::StringType FindRemovableStorageLocationById(
const std::string& device_id) {
RemovableStorageInfo media_devices =
SystemMonitor::Get()->GetAttachedRemovableStorage();
for (RemovableStorageInfo::const_iterator it = media_devices.begin();
it != media_devices.end();
++it) {
if (it->device_id == device_id)
return it->location;
}
return FilePath::StringType();
}
void FilterAttachedDevicesOnFileThread(MediaStorageUtil::DeviceIdSet* devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
MediaStorageUtil::DeviceIdSet missing_devices;
for (MediaStorageUtil::DeviceIdSet::const_iterator it = devices->begin();
it != devices->end();
++it) {
MediaStorageUtil::Type type;
std::string unique_id;
if (!MediaStorageUtil::CrackDeviceId(*it, &type, &unique_id)) {
missing_devices.insert(*it);
continue;
}
if (type == MediaStorageUtil::FIXED_MASS_STORAGE) {
if (!file_util::PathExists(FilePath::FromUTF8Unsafe(unique_id)))
missing_devices.insert(*it);
continue;
}
DCHECK(type == MediaStorageUtil::MTP_OR_PTP ||
type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM);
if (FindRemovableStorageLocationById(*it).empty())
missing_devices.insert(*it);
}
for (MediaStorageUtil::DeviceIdSet::const_iterator it =
missing_devices.begin();
it != missing_devices.end();
++it) {
devices->erase(*it);
}
}
} // namespace
// static
std::string MediaStorageUtil::MakeDeviceId(Type type,
const std::string& unique_id) {
DCHECK(!unique_id.empty());
switch (type) {
case REMOVABLE_MASS_STORAGE_WITH_DCIM:
return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id;
case REMOVABLE_MASS_STORAGE_NO_DCIM:
return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id;
case FIXED_MASS_STORAGE:
return std::string(kFixedMassStoragePrefix) + unique_id;
case MTP_OR_PTP:
return std::string(kMtpPtpPrefix) + unique_id;
}
NOTREACHED();
return std::string();
}
// static
bool MediaStorageUtil::CrackDeviceId(const std::string& device_id,
Type* type, std::string* unique_id) {
size_t prefix_length = device_id.find_first_of(':');
std::string prefix = prefix_length != std::string::npos ?
device_id.substr(0, prefix_length + 1) : "";
Type found_type;
if (prefix == kRemovableMassStorageWithDCIMPrefix) {
found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM;
} else if (prefix == kRemovableMassStorageNoDCIMPrefix) {
found_type = REMOVABLE_MASS_STORAGE_NO_DCIM;
} else if (prefix == kFixedMassStoragePrefix) {
found_type = FIXED_MASS_STORAGE;
} else if (prefix == kMtpPtpPrefix) {
found_type = MTP_OR_PTP;
} else {
NOTREACHED();
return false;
}
if (type)
*type = found_type;
if (unique_id)
*unique_id = device_id.substr(prefix_length + 1);
return true;
}
// static
bool MediaStorageUtil::IsMediaDevice(const std::string& device_id) {
Type type;
return CrackDeviceId(device_id, &type, NULL) &&
(type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP);
}
// static
bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) {
Type type;
return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE;
}
// static
bool MediaStorageUtil::IsMassStorageDevice(const std::string& device_id) {
Type type;
return CrackDeviceId(device_id, &type, NULL) && type != MTP_OR_PTP;
}
// static
void MediaStorageUtil::IsDeviceAttached(const std::string& device_id,
const BoolCallback& callback) {
Type type;
std::string unique_id;
if (!CrackDeviceId(device_id, &type, &unique_id)) {
callback.Run(false);
return;
}
if (type == FIXED_MASS_STORAGE) {
// For this type, the unique_id is the path.
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&ValidatePathOnFileThread,
FilePath::FromUTF8Unsafe(unique_id),
callback));
} else {
DCHECK(type == MTP_OR_PTP ||
type == REMOVABLE_MASS_STORAGE_WITH_DCIM ||
type == REMOVABLE_MASS_STORAGE_NO_DCIM);
// We should be able to find removable storage in SystemMonitor.
callback.Run(!FindRemovableStorageLocationById(device_id).empty());
}
}
// static
void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
const base::Closure& done) {
if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
FilterAttachedDevicesOnFileThread(devices);
done.Run();
return;
}
BrowserThread::PostTaskAndReply(BrowserThread::FILE,
FROM_HERE,
base::Bind(&FilterAttachedDevicesOnFileThread,
devices),
done);
}
bool MediaStorageUtil::GetDeviceInfoFromPath(const FilePath& path,
std::string* device_id,
string16* device_name,
FilePath* relative_path) {
if (!path.IsAbsolute())
return false;
if (g_test_get_device_info_from_path_function) {
return g_test_get_device_info_from_path_function(path, device_id,
device_name,
relative_path);
}
bool found_device = false;
base::SystemMonitor::RemovableStorageInfo device_info;
#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_WIN)
RemovableDeviceNotifications* notifier =
RemovableDeviceNotifications::GetInstance();
found_device = notifier->GetDeviceInfoForPath(path, &device_info);
#endif
#if defined(OS_CHROMEOS)
if (!found_device) {
chromeos::mtp::MediaTransferProtocolDeviceObserverCros* mtp_manager =
chromeos::mtp::MediaTransferProtocolDeviceObserverCros::GetInstance();
found_device = mtp_manager->GetStorageInfoForPath(path, &device_info);
}
#endif
if (found_device && IsRemovableDevice(device_info.device_id)) {
if (device_id)
*device_id = device_info.device_id;
if (device_name)
*device_name = device_info.name;
if (relative_path) {
*relative_path = FilePath();
FilePath mount_point(device_info.location);
mount_point.AppendRelativePath(path, relative_path);
}
return true;
}
// On Posix systems, there's one root so any absolute path could be valid.
#if !defined(OS_POSIX)
if (!found_device)
return false;
#endif
if (device_id)
*device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe());
if (device_name)
*device_name = path.BaseName().LossyDisplayName();
if (relative_path)
*relative_path = FilePath();
return true;
}
// static
FilePath MediaStorageUtil::FindDevicePathById(const std::string& device_id) {
Type type;
std::string unique_id;
if (!CrackDeviceId(device_id, &type, &unique_id))
return FilePath();
if (type == FIXED_MASS_STORAGE) {
// For this type, the unique_id is the path.
return FilePath::FromUTF8Unsafe(unique_id);
}
DCHECK(type == MTP_OR_PTP ||
type == REMOVABLE_MASS_STORAGE_WITH_DCIM ||
type == REMOVABLE_MASS_STORAGE_NO_DCIM);
return FilePath(FindRemovableStorageLocationById(device_id));
}
// static
void MediaStorageUtil::RecordDeviceInfoHistogram(bool mass_storage,
const std::string& device_uuid,
const string16& device_name) {
unsigned int event_number = 0;
if (!mass_storage)
event_number = 4;
if (device_name.empty())
event_number += 2;
if (device_uuid.empty())
event_number += 1;
enum DeviceInfoHistogramBuckets event =
static_cast<enum DeviceInfoHistogramBuckets>(event_number);
if (event >= DEVICE_INFO_BUCKET_BOUNDARY) {
NOTREACHED();
return;
}
UMA_HISTOGRAM_ENUMERATION("MediaDeviceNotifications.DeviceInfo", event,
DEVICE_INFO_BUCKET_BOUNDARY);
}
// static
void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting(
GetDeviceInfoFromPathFunction function) {
g_test_get_device_info_from_path_function = function;
}
MediaStorageUtil::MediaStorageUtil() {}
} // namespace chrome
|