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
|
// 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.
// chromeos::RemovableDeviceNotificationsCros implementation.
#include "chrome/browser/system_monitor/removable_device_notifications_chromeos.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/string_number_conversions.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/system_monitor/media_device_notifications_utils.h"
#include "chrome/browser/system_monitor/media_storage_util.h"
#include "chrome/browser/system_monitor/removable_device_constants.h"
#include "content/public/browser/browser_thread.h"
namespace chromeos {
using base::SystemMonitor;
namespace {
// Construct a device name using label or manufacturer (vendor and product) name
// details.
string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) {
std::string device_name = disk.device_label();
if (device_name.empty()) {
device_name = disk.vendor_name();
const std::string& product_name = disk.product_name();
if (!product_name.empty()) {
if (!device_name.empty())
device_name += chrome::kSpaceDelim;
device_name += product_name;
}
}
return UTF8ToUTF16(device_name);
}
// Construct a device id using uuid or manufacturer (vendor and product) id
// details.
std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) {
std::string uuid = disk.fs_uuid();
if (!uuid.empty())
return chrome::kFSUniqueIdPrefix + uuid;
// If one of the vendor or product information is missing, its value in the
// string is empty.
// Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo
// TODO(kmadhusu) Extract serial information for the disks and append it to
// the device unique id.
const std::string& vendor = disk.vendor_id();
const std::string& product = disk.product_id();
if (vendor.empty() && product.empty())
return std::string();
return base::StringPrintf("%s%s%s%s%s",
chrome::kVendorModelSerialPrefix,
vendor.c_str(), chrome::kNonSpaceDelim,
product.c_str(), chrome::kNonSpaceDelim);
}
static RemovableDeviceNotificationsCros*
g_removable_device_notifications_chromeos = NULL;
// Returns true if the requested device is valid, else false. On success, fills
// in |unique_id| and |device_label|
bool GetDeviceInfo(const std::string& source_path, std::string* unique_id,
string16* device_label) {
// Get the media device uuid and label if exists.
const disks::DiskMountManager::Disk* disk =
disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path);
if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN)
return false;
if (unique_id)
*unique_id = MakeDeviceUniqueId(*disk);
if (device_label)
*device_label = GetDeviceName(*disk);
return true;
}
} // namespace
using chrome::MediaStorageUtil;
using content::BrowserThread;
RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() {
DCHECK(disks::DiskMountManager::GetInstance());
DCHECK(!g_removable_device_notifications_chromeos);
g_removable_device_notifications_chromeos = this;
disks::DiskMountManager::GetInstance()->AddObserver(this);
CheckExistingMountPointsOnUIThread();
}
RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() {
DCHECK_EQ(this, g_removable_device_notifications_chromeos);
g_removable_device_notifications_chromeos = NULL;
disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance();
if (manager) {
manager->RemoveObserver(this);
}
}
// static
RemovableDeviceNotificationsCros*
RemovableDeviceNotificationsCros::GetInstance() {
return g_removable_device_notifications_chromeos;
}
void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const disks::DiskMountManager::MountPointMap& mount_point_map =
disks::DiskMountManager::GetInstance()->mount_points();
for (disks::DiskMountManager::MountPointMap::const_iterator it =
mount_point_map.begin(); it != mount_point_map.end(); ++it) {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(
&RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread,
this, it->second));
}
}
void RemovableDeviceNotificationsCros::DiskChanged(
disks::DiskMountManagerEventType event,
const disks::DiskMountManager::Disk* disk) {
}
void RemovableDeviceNotificationsCros::DeviceChanged(
disks::DiskMountManagerEventType event,
const std::string& device_path) {
}
void RemovableDeviceNotificationsCros::MountCompleted(
disks::DiskMountManager::MountEvent event_type,
MountError error_code,
const disks::DiskMountManager::MountPointInfo& mount_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Ignore mount points that are not devices.
if (mount_info.mount_type != MOUNT_TYPE_DEVICE)
return;
// Ignore errors.
if (error_code != MOUNT_ERROR_NONE)
return;
if (mount_info.mount_condition != disks::MOUNT_CONDITION_NONE)
return;
switch (event_type) {
case disks::DiskMountManager::MOUNTING: {
if (ContainsKey(mount_map_, mount_info.mount_path)) {
NOTREACHED();
return;
}
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(
&RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread,
this, mount_info));
break;
}
case disks::DiskMountManager::UNMOUNTING: {
MountMap::iterator it = mount_map_.find(mount_info.mount_path);
if (it == mount_map_.end())
return;
SystemMonitor::Get()->ProcessRemovableStorageDetached(
it->second.device_id);
mount_map_.erase(it);
break;
}
}
}
bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath(
const FilePath& path,
SystemMonitor::RemovableStorageInfo* device_info) const {
if (!path.IsAbsolute())
return false;
FilePath current = path;
while (!ContainsKey(mount_map_, current.value()) &&
current != current.DirName()) {
current = current.DirName();
}
MountMap::const_iterator info_it = mount_map_.find(current.value());
if (info_it == mount_map_.end())
return false;
if (device_info)
*device_info = info_it->second;
return true;
}
void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread(
const disks::DiskMountManager::MountPointInfo& mount_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread,
this, mount_info, has_dcim));
}
void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread(
const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (ContainsKey(mount_map_, mount_info.mount_path)) {
// CheckExistingMountPointsOnUIThread() added the mount point information
// in the map before the device attached handler is called. Therefore, an
// entry for the device already exists in the map.
return;
}
// Get the media device uuid and label if exists.
std::string unique_id;
string16 device_label;
if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label))
return;
// Keep track of device uuid and label, to see how often we receive empty
// values.
MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_label);
if (unique_id.empty() || device_label.empty())
return;
MediaStorageUtil::Type type = has_dcim ?
MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM :
MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type,
unique_id);
SystemMonitor::RemovableStorageInfo info(device_id, device_label,
mount_info.mount_path);
mount_map_.insert(std::make_pair(mount_info.mount_path, info));
SystemMonitor::Get()->ProcessRemovableStorageAttached(
device_id,
device_label,
mount_info.mount_path);
}
} // namespace chrome
|