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

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

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

Powered by Google App Engine
This is Rietveld 408576698