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

Side by Side Diff: chrome/browser/storage_monitor/storage_monitor_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: Add larger constructor, put linux data structures back 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::StorageMonitorCros implementation. 5 // chromeos::StorageMonitorCros implementation.
6 6
7 #include "chrome/browser/storage_monitor/storage_monitor_chromeos.h" 7 #include "chrome/browser/storage_monitor/storage_monitor_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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 BrowserThread::PostTask( 163 BrowserThread::PostTask(
148 BrowserThread::FILE, FROM_HERE, 164 BrowserThread::FILE, FROM_HERE,
149 base::Bind(&StorageMonitorCros::CheckMountedPathOnFileThread, this, 165 base::Bind(&StorageMonitorCros::CheckMountedPathOnFileThread, this,
150 mount_info)); 166 mount_info));
151 break; 167 break;
152 } 168 }
153 case disks::DiskMountManager::UNMOUNTING: { 169 case disks::DiskMountManager::UNMOUNTING: {
154 MountMap::iterator it = mount_map_.find(mount_info.mount_path); 170 MountMap::iterator it = mount_map_.find(mount_info.mount_path);
155 if (it == mount_map_.end()) 171 if (it == mount_map_.end())
156 return; 172 return;
157 receiver()->ProcessDetach(it->second.storage_info.device_id); 173 receiver()->ProcessDetach(it->second.device_id);
158 mount_map_.erase(it); 174 mount_map_.erase(it);
159 break; 175 break;
160 } 176 }
161 } 177 }
162 } 178 }
163 179
164 void StorageMonitorCros::OnFormatEvent( 180 void StorageMonitorCros::OnFormatEvent(
165 disks::DiskMountManager::FormatEvent event, 181 disks::DiskMountManager::FormatEvent event,
166 FormatError error_code, 182 FormatError error_code,
167 const std::string& device_path) { 183 const std::string& device_path) {
168 } 184 }
169 185
170 bool StorageMonitorCros::GetStorageInfoForPath( 186 bool StorageMonitorCros::GetStorageInfoForPath(
171 const base::FilePath& path, 187 const base::FilePath& path,
172 chrome::StorageInfo* device_info) const { 188 chrome::StorageInfo* device_info) const {
173 if (!path.IsAbsolute()) 189 if (!path.IsAbsolute())
174 return false; 190 return false;
175 191
176 base::FilePath current = path; 192 base::FilePath current = path;
177 while (!ContainsKey(mount_map_, current.value()) && 193 while (!ContainsKey(mount_map_, current.value()) &&
178 current != current.DirName()) { 194 current != current.DirName()) {
179 current = current.DirName(); 195 current = current.DirName();
180 } 196 }
181 197
182 MountMap::const_iterator info_it = mount_map_.find(current.value()); 198 MountMap::const_iterator info_it = mount_map_.find(current.value());
183 if (info_it == mount_map_.end()) 199 if (info_it == mount_map_.end())
184 return false; 200 return false;
185 201
186 if (device_info) 202 if (device_info)
187 *device_info = info_it->second.storage_info; 203 *device_info = info_it->second;
188 return true; 204 return true;
189 } 205 }
190 206
191 uint64 StorageMonitorCros::GetStorageSize( 207 uint64 StorageMonitorCros::GetStorageSize(
192 const std::string& device_location) const { 208 const std::string& device_location) const {
193 MountMap::const_iterator info_it = mount_map_.find(device_location); 209 MountMap::const_iterator info_it = mount_map_.find(device_location);
194 return (info_it != mount_map_.end()) ? 210 return (info_it != mount_map_.end()) ?
195 info_it->second.storage_size_in_bytes : 0; 211 info_it->second.total_size_in_bytes : 0;
196 } 212 }
197 213
198 void StorageMonitorCros::CheckMountedPathOnFileThread( 214 void StorageMonitorCros::CheckMountedPathOnFileThread(
199 const disks::DiskMountManager::MountPointInfo& mount_info) { 215 const disks::DiskMountManager::MountPointInfo& mount_info) {
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
201 217
202 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); 218 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path);
203 219
204 BrowserThread::PostTask( 220 BrowserThread::PostTask(
205 BrowserThread::UI, FROM_HERE, 221 BrowserThread::UI, FROM_HERE,
206 base::Bind(&StorageMonitorCros::AddMountedPathOnUIThread, this, 222 base::Bind(&StorageMonitorCros::AddMountedPathOnUIThread, this,
207 mount_info, has_dcim)); 223 mount_info, has_dcim));
208 } 224 }
209 225
210 void StorageMonitorCros::AddMountedPathOnUIThread( 226 void StorageMonitorCros::AddMountedPathOnUIThread(
211 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { 227 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213 229
214 if (ContainsKey(mount_map_, mount_info.mount_path)) { 230 if (ContainsKey(mount_map_, mount_info.mount_path)) {
215 // CheckExistingMountPointsOnUIThread() added the mount point information 231 // CheckExistingMountPointsOnUIThread() added the mount point information
216 // in the map before the device attached handler is called. Therefore, an 232 // in the map before the device attached handler is called. Therefore, an
217 // entry for the device already exists in the map. 233 // entry for the device already exists in the map.
218 return; 234 return;
219 } 235 }
220 236
221 // Get the media device uuid and label if exists. 237 // Get the media device uuid and label if exists.
222 std::string unique_id; 238 std::string unique_id;
223 string16 device_label; 239 string16 device_label;
240 string16 storage_label;
241 string16 vendor_name;
242 string16 model_name;
224 uint64 storage_size_in_bytes; 243 uint64 storage_size_in_bytes;
225 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label, 244 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label,
226 &storage_size_in_bytes)) 245 &storage_size_in_bytes, &storage_label,
246 &vendor_name, &model_name))
227 return; 247 return;
228 248
229 // Keep track of device uuid and label, to see how often we receive empty 249 // Keep track of device uuid and label, to see how often we receive empty
230 // values. 250 // values.
231 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, 251 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id,
232 device_label); 252 device_label);
233 if (unique_id.empty() || device_label.empty()) 253 if (unique_id.empty() || device_label.empty())
234 return; 254 return;
235 255
236 chrome::MediaStorageUtil::Type type = has_dcim ? 256 chrome::MediaStorageUtil::Type type = has_dcim ?
237 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : 257 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM :
238 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; 258 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
239 259
240 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, 260 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type,
241 unique_id); 261 unique_id);
242 StorageObjectInfo object_info = { 262 chrome::StorageInfo object_info(
243 chrome::StorageInfo(device_id, device_label, mount_info.mount_path),
244 storage_size_in_bytes
245 };
246 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info));
247 receiver()->ProcessAttach(chrome::StorageInfo(
248 device_id, 263 device_id,
249 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label), 264 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label),
250 mount_info.mount_path)); 265 mount_info.mount_path,
266 storage_label,
267 vendor_name,
268 model_name,
269 storage_size_in_bytes);
270
271 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info));
272
273 receiver()->ProcessAttach(object_info);
251 } 274 }
252 275
253 } // namespace chromeos 276 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698