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

Side by Side Diff: chrome/browser/storage_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: Reset header file Created 7 years, 9 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/storage_monitor/removable_device_notifications_chromeos .h" 7 #include "chrome/browser/storage_monitor/removable_device_notifications_chromeos .h"
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/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_util.h" 12 #include "base/string_util.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/storage_monitor/media_device_notifications_utils.h" 15 #include "chrome/browser/storage_monitor/media_device_notifications_utils.h"
16 #include "chrome/browser/storage_monitor/media_storage_util.h" 16 #include "chrome/browser/storage_monitor/media_storage_util.h"
17 #include "chrome/browser/storage_monitor/removable_device_constants.h" 17 #include "chrome/browser/storage_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,
94 vendor_name, model_name);
79 95
80 if (storage_size_in_bytes) 96 if (storage_size_in_bytes)
81 *storage_size_in_bytes = disk->total_size_in_bytes(); 97 *storage_size_in_bytes = disk->total_size_in_bytes();
82 return true; 98 return true;
83 } 99 }
84 100
85 } // namespace 101 } // namespace
86 102
87 using content::BrowserThread; 103 using content::BrowserThread;
88 104
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 BrowserThread::FILE, FROM_HERE, 165 BrowserThread::FILE, FROM_HERE,
150 base::Bind( 166 base::Bind(
151 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, 167 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread,
152 this, mount_info)); 168 this, mount_info));
153 break; 169 break;
154 } 170 }
155 case disks::DiskMountManager::UNMOUNTING: { 171 case disks::DiskMountManager::UNMOUNTING: {
156 MountMap::iterator it = mount_map_.find(mount_info.mount_path); 172 MountMap::iterator it = mount_map_.find(mount_info.mount_path);
157 if (it == mount_map_.end()) 173 if (it == mount_map_.end())
158 return; 174 return;
159 receiver()->ProcessDetach(it->second.storage_info.device_id); 175 receiver()->ProcessDetach(it->second.device_id);
160 mount_map_.erase(it); 176 mount_map_.erase(it);
161 break; 177 break;
162 } 178 }
163 } 179 }
164 } 180 }
165 181
166 void RemovableDeviceNotificationsCros::OnFormatEvent( 182 void RemovableDeviceNotificationsCros::OnFormatEvent(
167 disks::DiskMountManager::FormatEvent event, 183 disks::DiskMountManager::FormatEvent event,
168 FormatError error_code, 184 FormatError error_code,
169 const std::string& device_path) { 185 const std::string& device_path) {
170 } 186 }
171 187
172 bool RemovableDeviceNotificationsCros::GetStorageInfoForPath( 188 bool RemovableDeviceNotificationsCros::GetStorageInfoForPath(
173 const base::FilePath& path, 189 const base::FilePath& path,
174 StorageInfo* device_info) const { 190 StorageInfo* device_info) const {
175 if (!path.IsAbsolute()) 191 if (!path.IsAbsolute())
176 return false; 192 return false;
177 193
178 base::FilePath current = path; 194 base::FilePath current = path;
179 while (!ContainsKey(mount_map_, current.value()) && 195 while (!ContainsKey(mount_map_, current.value()) &&
180 current != current.DirName()) { 196 current != current.DirName()) {
181 current = current.DirName(); 197 current = current.DirName();
182 } 198 }
183 199
184 MountMap::const_iterator info_it = mount_map_.find(current.value()); 200 MountMap::const_iterator info_it = mount_map_.find(current.value());
185 if (info_it == mount_map_.end()) 201 if (info_it == mount_map_.end())
186 return false; 202 return false;
187 203
188 if (device_info) 204 if (device_info)
189 *device_info = info_it->second.storage_info; 205 *device_info = info_it->second;
190 return true; 206 return true;
191 } 207 }
192 208
193 uint64 RemovableDeviceNotificationsCros::GetStorageSize( 209 uint64 RemovableDeviceNotificationsCros::GetStorageSize(
194 const std::string& device_location) const { 210 const std::string& device_location) const {
195 MountMap::const_iterator info_it = mount_map_.find(device_location); 211 MountMap::const_iterator info_it = mount_map_.find(device_location);
196 return (info_it != mount_map_.end()) ? 212 return (info_it != mount_map_.end()) ?
197 info_it->second.storage_size_in_bytes : 0; 213 info_it->second.total_size_in_bytes : 0;
198 } 214 }
199 215
200 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( 216 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread(
201 const disks::DiskMountManager::MountPointInfo& mount_info) { 217 const disks::DiskMountManager::MountPointInfo& mount_info) {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
203 219
204 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); 220 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path);
205 221
206 BrowserThread::PostTask( 222 BrowserThread::PostTask(
207 BrowserThread::UI, FROM_HERE, 223 BrowserThread::UI, FROM_HERE,
208 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, 224 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread,
209 this, mount_info, has_dcim)); 225 this, mount_info, has_dcim));
210 } 226 }
211 227
212 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( 228 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread(
213 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { 229 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 231
216 if (ContainsKey(mount_map_, mount_info.mount_path)) { 232 if (ContainsKey(mount_map_, mount_info.mount_path)) {
217 // CheckExistingMountPointsOnUIThread() added the mount point information 233 // CheckExistingMountPointsOnUIThread() added the mount point information
218 // in the map before the device attached handler is called. Therefore, an 234 // in the map before the device attached handler is called. Therefore, an
219 // entry for the device already exists in the map. 235 // entry for the device already exists in the map.
220 return; 236 return;
221 } 237 }
222 238
223 // Get the media device uuid and label if exists. 239 // Get the media device uuid and label if exists.
224 std::string unique_id; 240 std::string unique_id;
225 string16 device_label; 241 string16 device_label;
242 string16 storage_label;
243 string16 vendor_name;
244 string16 model_name;
226 uint64 storage_size_in_bytes; 245 uint64 storage_size_in_bytes;
227 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label, 246 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label,
228 &storage_size_in_bytes)) 247 &storage_size_in_bytes, &storage_label,
248 &vendor_name, &model_name))
229 return; 249 return;
230 250
231 // Keep track of device uuid and label, to see how often we receive empty 251 // Keep track of device uuid and label, to see how often we receive empty
232 // values. 252 // values.
233 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, 253 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id,
234 device_label); 254 device_label);
235 if (unique_id.empty() || device_label.empty()) 255 if (unique_id.empty() || device_label.empty())
236 return; 256 return;
237 257
238 chrome::MediaStorageUtil::Type type = has_dcim ? 258 chrome::MediaStorageUtil::Type type = has_dcim ?
239 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : 259 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM :
240 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; 260 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
241 261
242 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, 262 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type,
243 unique_id); 263 unique_id);
244 StorageObjectInfo object_info = { 264 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(StorageInfo(
250 device_id, 265 device_id,
251 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label), 266 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label),
252 mount_info.mount_path)); 267 mount_info.mount_path);
268 object_info.total_size_in_bytes = storage_size_in_bytes;
vandebo (ex-Chrome) 2013/03/01 22:21:48 This is weird.... The constructor is just a helpe
Greg Billock 2013/03/05 19:20:55 Yes, we should have a constructor like that. I'm g
vandebo (ex-Chrome) 2013/03/06 01:34:54 If you like. But if so, add a TODO here so that w
Greg Billock 2013/03/06 23:24:25 Refactor is in, just updated constructor.
269 object_info.storage_label = storage_label;
270 object_info.vendor_name = vendor_name;
271 object_info.model_name = model_name;
272
273 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info));
274
275 receiver()->ProcessAttach(object_info);
253 } 276 }
254 277
255 } // namespace chromeos 278 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698