OLD | NEW |
| (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 // chromeos::RemovableDeviceNotificationsCros implementation. | |
6 | |
7 #include "chrome/browser/storage_monitor/removable_device_notifications_chromeos
.h" | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/string_util.h" | |
13 #include "base/strings/string_number_conversions.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "chrome/browser/storage_monitor/media_device_notifications_utils.h" | |
16 #include "chrome/browser/storage_monitor/media_storage_util.h" | |
17 #include "chrome/browser/storage_monitor/removable_device_constants.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 | |
20 namespace chromeos { | |
21 | |
22 namespace { | |
23 | |
24 // Constructs a device name using label or manufacturer (vendor and product) | |
25 // name details. | |
26 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { | |
27 if (disk.device_type() == DEVICE_TYPE_SD) { | |
28 // Mount path of an SD card will be one of the following: | |
29 // (1) /media/removable/<volume_label> | |
30 // (2) /media/removable/SD Card | |
31 // If the volume label is available, mount path will be (1) else (2). | |
32 base::FilePath mount_point(disk.mount_path()); | |
33 const string16 display_name(mount_point.BaseName().LossyDisplayName()); | |
34 if (!display_name.empty()) | |
35 return display_name; | |
36 } | |
37 | |
38 const std::string& device_label = disk.device_label(); | |
39 if (!device_label.empty() && IsStringUTF8(device_label)) | |
40 return UTF8ToUTF16(device_label); | |
41 return chrome::GetFullProductName(disk.vendor_name(), disk.product_name()); | |
42 } | |
43 | |
44 // Constructs a device id using uuid or manufacturer (vendor and product) id | |
45 // details. | |
46 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { | |
47 std::string uuid = disk.fs_uuid(); | |
48 if (!uuid.empty()) | |
49 return chrome::kFSUniqueIdPrefix + uuid; | |
50 | |
51 // If one of the vendor or product information is missing, its value in the | |
52 // string is empty. | |
53 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo | |
54 // TODO(kmadhusu) Extract serial information for the disks and append it to | |
55 // the device unique id. | |
56 const std::string& vendor = disk.vendor_id(); | |
57 const std::string& product = disk.product_id(); | |
58 if (vendor.empty() && product.empty()) | |
59 return std::string(); | |
60 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; | |
61 } | |
62 | |
63 // Returns true if the requested device is valid, else false. On success, fills | |
64 // in |unique_id|, |device_label| and |storage_size_in_bytes|. | |
65 bool GetDeviceInfo(const std::string& source_path, | |
66 std::string* unique_id, | |
67 string16* device_label, | |
68 uint64* storage_size_in_bytes) { | |
69 const disks::DiskMountManager::Disk* disk = | |
70 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); | |
71 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) | |
72 return false; | |
73 | |
74 if (unique_id) | |
75 *unique_id = MakeDeviceUniqueId(*disk); | |
76 | |
77 if (device_label) | |
78 *device_label = GetDeviceName(*disk); | |
79 | |
80 if (storage_size_in_bytes) | |
81 *storage_size_in_bytes = disk->total_size_in_bytes(); | |
82 return true; | |
83 } | |
84 | |
85 } // namespace | |
86 | |
87 using content::BrowserThread; | |
88 | |
89 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { | |
90 DCHECK(disks::DiskMountManager::GetInstance()); | |
91 disks::DiskMountManager::GetInstance()->AddObserver(this); | |
92 CheckExistingMountPointsOnUIThread(); | |
93 } | |
94 | |
95 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() { | |
96 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); | |
97 if (manager) { | |
98 manager->RemoveObserver(this); | |
99 } | |
100 } | |
101 | |
102 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() { | |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
104 const disks::DiskMountManager::MountPointMap& mount_point_map = | |
105 disks::DiskMountManager::GetInstance()->mount_points(); | |
106 for (disks::DiskMountManager::MountPointMap::const_iterator it = | |
107 mount_point_map.begin(); it != mount_point_map.end(); ++it) { | |
108 BrowserThread::PostTask( | |
109 BrowserThread::FILE, FROM_HERE, | |
110 base::Bind( | |
111 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | |
112 this, it->second)); | |
113 } | |
114 } | |
115 | |
116 void RemovableDeviceNotificationsCros::OnDiskEvent( | |
117 disks::DiskMountManager::DiskEvent event, | |
118 const disks::DiskMountManager::Disk* disk) { | |
119 } | |
120 | |
121 void RemovableDeviceNotificationsCros::OnDeviceEvent( | |
122 disks::DiskMountManager::DeviceEvent event, | |
123 const std::string& device_path) { | |
124 } | |
125 | |
126 void RemovableDeviceNotificationsCros::OnMountEvent( | |
127 disks::DiskMountManager::MountEvent event, | |
128 MountError error_code, | |
129 const disks::DiskMountManager::MountPointInfo& mount_info) { | |
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
131 | |
132 // Ignore mount points that are not devices. | |
133 if (mount_info.mount_type != MOUNT_TYPE_DEVICE) | |
134 return; | |
135 // Ignore errors. | |
136 if (error_code != MOUNT_ERROR_NONE) | |
137 return; | |
138 if (mount_info.mount_condition != disks::MOUNT_CONDITION_NONE) | |
139 return; | |
140 | |
141 switch (event) { | |
142 case disks::DiskMountManager::MOUNTING: { | |
143 if (ContainsKey(mount_map_, mount_info.mount_path)) { | |
144 NOTREACHED(); | |
145 return; | |
146 } | |
147 | |
148 BrowserThread::PostTask( | |
149 BrowserThread::FILE, FROM_HERE, | |
150 base::Bind( | |
151 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | |
152 this, mount_info)); | |
153 break; | |
154 } | |
155 case disks::DiskMountManager::UNMOUNTING: { | |
156 MountMap::iterator it = mount_map_.find(mount_info.mount_path); | |
157 if (it == mount_map_.end()) | |
158 return; | |
159 receiver()->ProcessDetach(it->second.storage_info.device_id); | |
160 mount_map_.erase(it); | |
161 break; | |
162 } | |
163 } | |
164 } | |
165 | |
166 void RemovableDeviceNotificationsCros::OnFormatEvent( | |
167 disks::DiskMountManager::FormatEvent event, | |
168 FormatError error_code, | |
169 const std::string& device_path) { | |
170 } | |
171 | |
172 bool RemovableDeviceNotificationsCros::GetStorageInfoForPath( | |
173 const base::FilePath& path, | |
174 StorageInfo* device_info) const { | |
175 if (!path.IsAbsolute()) | |
176 return false; | |
177 | |
178 base::FilePath current = path; | |
179 while (!ContainsKey(mount_map_, current.value()) && | |
180 current != current.DirName()) { | |
181 current = current.DirName(); | |
182 } | |
183 | |
184 MountMap::const_iterator info_it = mount_map_.find(current.value()); | |
185 if (info_it == mount_map_.end()) | |
186 return false; | |
187 | |
188 if (device_info) | |
189 *device_info = info_it->second.storage_info; | |
190 return true; | |
191 } | |
192 | |
193 uint64 RemovableDeviceNotificationsCros::GetStorageSize( | |
194 const std::string& device_location) const { | |
195 MountMap::const_iterator info_it = mount_map_.find(device_location); | |
196 return (info_it != mount_map_.end()) ? | |
197 info_it->second.storage_size_in_bytes : 0; | |
198 } | |
199 | |
200 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( | |
201 const disks::DiskMountManager::MountPointInfo& mount_info) { | |
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
203 | |
204 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); | |
205 | |
206 BrowserThread::PostTask( | |
207 BrowserThread::UI, FROM_HERE, | |
208 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, | |
209 this, mount_info, has_dcim)); | |
210 } | |
211 | |
212 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( | |
213 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { | |
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
215 | |
216 if (ContainsKey(mount_map_, mount_info.mount_path)) { | |
217 // CheckExistingMountPointsOnUIThread() added the mount point information | |
218 // in the map before the device attached handler is called. Therefore, an | |
219 // entry for the device already exists in the map. | |
220 return; | |
221 } | |
222 | |
223 // Get the media device uuid and label if exists. | |
224 std::string unique_id; | |
225 string16 device_label; | |
226 uint64 storage_size_in_bytes; | |
227 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label, | |
228 &storage_size_in_bytes)) | |
229 return; | |
230 | |
231 // Keep track of device uuid and label, to see how often we receive empty | |
232 // values. | |
233 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, | |
234 device_label); | |
235 if (unique_id.empty() || device_label.empty()) | |
236 return; | |
237 | |
238 chrome::MediaStorageUtil::Type type = has_dcim ? | |
239 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : | |
240 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | |
241 | |
242 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, | |
243 unique_id); | |
244 StorageObjectInfo object_info = { | |
245 StorageInfo(device_id, device_label, mount_info.mount_path), | |
246 storage_size_in_bytes | |
247 }; | |
248 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info)); | |
249 receiver()->ProcessAttach(StorageInfo( | |
250 device_id, | |
251 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label), | |
252 mount_info.mount_path)); | |
253 } | |
254 | |
255 } // namespace chromeos | |
OLD | NEW |