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

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

Issue 12211084: [Media Galleries] Populate volume metadata in ChromeOS/Linux (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 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
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 // chromeos::RemovableDeviceNotificationsCros implementation. 5 // chromeos::RemovableDeviceNotificationsCros implementation.
6 6
7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos. h" 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos. h"
8 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h"
16 #include "chrome/browser/system_monitor/media_storage_util.h" 16 #include "chrome/browser/system_monitor/media_storage_util.h"
17 #include "chrome/browser/system_monitor/removable_device_constants.h" 17 #include "chrome/browser/system_monitor/removable_device_constants.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 19
20 namespace chromeos { 20 namespace chromeos {
21 21
22 namespace { 22 namespace {
23 23
24 // Constructs a device name using label or manufacturer (vendor and product) 24 // Constructs a device name using label or manufacturer (vendor and product)
25 // name details. 25 // name details.
26 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { 26 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk,
27 string16* storage_label,
28 string16* vendor_name,
29 string16* model_name) {
27 if (disk.device_type() == DEVICE_TYPE_SD) { 30 if (disk.device_type() == DEVICE_TYPE_SD) {
28 // Mount path of an SD card will be one of the following: 31 // Mount path of an SD card will be one of the following:
29 // (1) /media/removable/<volume_label> 32 // (1) /media/removable/<volume_label>
30 // (2) /media/removable/SD Card 33 // (2) /media/removable/SD Card
31 // If the volume label is available, mount path will be (1) else (2). 34 // If the volume label is available, mount path will be (1) else (2).
32 base::FilePath mount_point(disk.mount_path()); 35 base::FilePath mount_point(disk.mount_path());
33 const string16 display_name(mount_point.BaseName().LossyDisplayName()); 36 const string16 display_name(mount_point.BaseName().LossyDisplayName());
34 if (!display_name.empty()) 37 if (!display_name.empty())
35 return display_name; 38 return display_name;
36 } 39 }
37 40
38 const std::string& device_label = disk.device_label(); 41 const std::string& device_label = disk.device_label();
42
43 if (storage_label)
44 *storage_label = UTF8ToUTF16(device_label);
45 if (vendor_name)
46 *vendor_name = UTF8ToUTF16(disk.vendor_name());
47 if (model_name)
48 *model_name = UTF8ToUTF16(disk.product_name());
49
39 if (!device_label.empty() && IsStringUTF8(device_label)) 50 if (!device_label.empty() && IsStringUTF8(device_label))
40 return UTF8ToUTF16(device_label); 51 return UTF8ToUTF16(device_label);
52
41 return chrome::GetFullProductName(disk.vendor_name(), disk.product_name()); 53 return chrome::GetFullProductName(disk.vendor_name(), disk.product_name());
42 } 54 }
43 55
44 // Constructs a device id using uuid or manufacturer (vendor and product) id 56 // Constructs a device id using uuid or manufacturer (vendor and product) id
45 // details. 57 // details.
46 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { 58 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) {
47 std::string uuid = disk.fs_uuid(); 59 std::string uuid = disk.fs_uuid();
48 if (!uuid.empty()) 60 if (!uuid.empty())
49 return chrome::kFSUniqueIdPrefix + uuid; 61 return chrome::kFSUniqueIdPrefix + uuid;
50 62
51 // If one of the vendor or product information is missing, its value in the 63 // If one of the vendor or product information is missing, its value in the
52 // string is empty. 64 // string is empty.
53 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo 65 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo
54 // TODO(kmadhusu) Extract serial information for the disks and append it to 66 // TODO(kmadhusu) Extract serial information for the disks and append it to
55 // the device unique id. 67 // the device unique id.
56 const std::string& vendor = disk.vendor_id(); 68 const std::string& vendor = disk.vendor_id();
57 const std::string& product = disk.product_id(); 69 const std::string& product = disk.product_id();
58 if (vendor.empty() && product.empty()) 70 if (vendor.empty() && product.empty())
59 return std::string(); 71 return std::string();
60 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; 72 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":";
61 } 73 }
62 74
63 // Returns true if the requested device is valid, else false. On success, fills 75 // Returns true if the requested device is valid, else false. On success, fills
64 // in |unique_id|, |device_label| and |storage_size_in_bytes|. 76 // in |unique_id|, |device_label| and |storage_size_in_bytes|.
65 bool GetDeviceInfo(const std::string& source_path, 77 bool GetDeviceInfo(const std::string& source_path,
66 std::string* unique_id, 78 std::string* unique_id,
67 string16* device_label, 79 string16* device_label,
68 uint64* storage_size_in_bytes) { 80 uint64* storage_size_in_bytes,
81 string16* storage_label,
82 string16* vendor_name,
83 string16* model_name) {
69 const disks::DiskMountManager::Disk* disk = 84 const disks::DiskMountManager::Disk* disk =
70 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); 85 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path);
71 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) 86 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN)
72 return false; 87 return false;
73 88
74 if (unique_id) 89 if (unique_id)
75 *unique_id = MakeDeviceUniqueId(*disk); 90 *unique_id = MakeDeviceUniqueId(*disk);
76 91
77 if (device_label) 92 if (device_label)
78 *device_label = GetDeviceName(*disk); 93 *device_label = GetDeviceName(*disk, storage_label, vendor_name, model_name) ;
79 94
80 if (storage_size_in_bytes) 95 if (storage_size_in_bytes)
81 *storage_size_in_bytes = disk->total_size_in_bytes(); 96 *storage_size_in_bytes = disk->total_size_in_bytes();
82 return true; 97 return true;
83 } 98 }
84 99
85 } // namespace 100 } // namespace
86 101
87 using content::BrowserThread; 102 using content::BrowserThread;
88 103
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 BrowserThread::FILE, FROM_HERE, 164 BrowserThread::FILE, FROM_HERE,
150 base::Bind( 165 base::Bind(
151 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, 166 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread,
152 this, mount_info)); 167 this, mount_info));
153 break; 168 break;
154 } 169 }
155 case disks::DiskMountManager::UNMOUNTING: { 170 case disks::DiskMountManager::UNMOUNTING: {
156 MountMap::iterator it = mount_map_.find(mount_info.mount_path); 171 MountMap::iterator it = mount_map_.find(mount_info.mount_path);
157 if (it == mount_map_.end()) 172 if (it == mount_map_.end())
158 return; 173 return;
159 receiver()->ProcessDetach(it->second.storage_info.device_id); 174 receiver()->ProcessDetach(it->second.device_id);
160 mount_map_.erase(it); 175 mount_map_.erase(it);
161 break; 176 break;
162 } 177 }
163 } 178 }
164 } 179 }
165 180
166 void RemovableDeviceNotificationsCros::OnFormatEvent( 181 void RemovableDeviceNotificationsCros::OnFormatEvent(
167 disks::DiskMountManager::FormatEvent event, 182 disks::DiskMountManager::FormatEvent event,
168 FormatError error_code, 183 FormatError error_code,
169 const std::string& device_path) { 184 const std::string& device_path) {
170 } 185 }
171 186
172 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( 187 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath(
173 const base::FilePath& path, 188 const base::FilePath& path,
174 StorageInfo* device_info) const { 189 StorageInfo* device_info) const {
175 if (!path.IsAbsolute()) 190 if (!path.IsAbsolute())
176 return false; 191 return false;
177 192
178 base::FilePath current = path; 193 base::FilePath current = path;
179 while (!ContainsKey(mount_map_, current.value()) && 194 while (!ContainsKey(mount_map_, current.value()) &&
180 current != current.DirName()) { 195 current != current.DirName()) {
181 current = current.DirName(); 196 current = current.DirName();
182 } 197 }
183 198
184 MountMap::const_iterator info_it = mount_map_.find(current.value()); 199 MountMap::const_iterator info_it = mount_map_.find(current.value());
185 if (info_it == mount_map_.end()) 200 if (info_it == mount_map_.end())
186 return false; 201 return false;
187 202
188 if (device_info) 203 if (device_info)
189 *device_info = info_it->second.storage_info; 204 *device_info = info_it->second;
190 return true; 205 return true;
191 } 206 }
192 207
193 uint64 RemovableDeviceNotificationsCros::GetStorageSize( 208 uint64 RemovableDeviceNotificationsCros::GetStorageSize(
194 const std::string& device_location) const { 209 const std::string& device_location) const {
195 MountMap::const_iterator info_it = mount_map_.find(device_location); 210 MountMap::const_iterator info_it = mount_map_.find(device_location);
196 return (info_it != mount_map_.end()) ? 211 return (info_it != mount_map_.end()) ?
197 info_it->second.storage_size_in_bytes : 0; 212 info_it->second.total_size_in_bytes : 0;
198 } 213 }
199 214
200 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( 215 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread(
201 const disks::DiskMountManager::MountPointInfo& mount_info) { 216 const disks::DiskMountManager::MountPointInfo& mount_info) {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
203 218
204 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); 219 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path);
205 220
206 BrowserThread::PostTask( 221 BrowserThread::PostTask(
207 BrowserThread::UI, FROM_HERE, 222 BrowserThread::UI, FROM_HERE,
208 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, 223 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread,
209 this, mount_info, has_dcim)); 224 this, mount_info, has_dcim));
210 } 225 }
211 226
212 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( 227 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread(
213 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { 228 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 230
216 if (ContainsKey(mount_map_, mount_info.mount_path)) { 231 if (ContainsKey(mount_map_, mount_info.mount_path)) {
217 // CheckExistingMountPointsOnUIThread() added the mount point information 232 // CheckExistingMountPointsOnUIThread() added the mount point information
218 // in the map before the device attached handler is called. Therefore, an 233 // in the map before the device attached handler is called. Therefore, an
219 // entry for the device already exists in the map. 234 // entry for the device already exists in the map.
220 return; 235 return;
221 } 236 }
222 237
223 // Get the media device uuid and label if exists. 238 // Get the media device uuid and label if exists.
224 std::string unique_id; 239 std::string unique_id;
225 string16 device_label; 240 string16 device_label;
241 string16 storage_label;
242 string16 vendor_name;
243 string16 model_name;
226 uint64 storage_size_in_bytes; 244 uint64 storage_size_in_bytes;
227 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label, 245 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label,
228 &storage_size_in_bytes)) 246 &storage_size_in_bytes, &storage_label,
247 &vendor_name, &model_name))
229 return; 248 return;
230 249
231 // Keep track of device uuid and label, to see how often we receive empty 250 // Keep track of device uuid and label, to see how often we receive empty
232 // values. 251 // values.
233 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, 252 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id,
234 device_label); 253 device_label);
235 if (unique_id.empty() || device_label.empty()) 254 if (unique_id.empty() || device_label.empty())
236 return; 255 return;
237 256
238 chrome::MediaStorageUtil::Type type = has_dcim ? 257 chrome::MediaStorageUtil::Type type = has_dcim ?
239 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : 258 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM :
240 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; 259 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
241 260
242 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, 261 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type,
243 unique_id); 262 unique_id);
244 StorageObjectInfo object_info = { 263 StorageInfo 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(
250 device_id, 264 device_id,
251 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label), 265 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label),
252 mount_info.mount_path); 266 mount_info.mount_path);
267 object_info.total_size_in_bytes = storage_size_in_bytes;
268 object_info.device_type = has_dcim ?
269 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM :
270 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
271 object_info.storage_label = storage_label;
272 object_info.vendor_name = vendor_name;
273 object_info.model_name = model_name;
274
275 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info));
276
277 receiver()->ProcessAttach(object_info);
253 } 278 }
254 279
255 } // namespace chromeos 280 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698