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

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

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

Powered by Google App Engine
This is Rietveld 408576698