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

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

Issue 10918259: [Chrome OS]Implement MediaStorageUtil::GetDeviceInfoForPath function to support media gallery permi… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/system_monitor/removable_device_notifications_chromeos.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" 16 #include "chrome/browser/system_monitor/media_device_notifications_utils.h"
17 #include "chrome/browser/system_monitor/media_storage_util.h" 17 #include "chrome/browser/system_monitor/media_storage_util.h"
18 #include "chrome/browser/system_monitor/removable_device_constants.h" 18 #include "chrome/browser/system_monitor/removable_device_constants.h"
19 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
20 20
21 namespace chromeos { 21 namespace chromeos {
22 22
23 using base::SystemMonitor;
24
23 namespace { 25 namespace {
24 26
25 // Construct a device name using label or manufacturer (vendor and product) name 27 // Construct a device name using label or manufacturer (vendor and product) name
26 // details. 28 // details.
27 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { 29 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) {
28 std::string device_name = disk.device_label(); 30 std::string device_name = disk.device_label();
29 if (device_name.empty()) { 31 if (device_name.empty()) {
30 device_name = disk.vendor_name(); 32 device_name = disk.vendor_name();
31 const std::string& product_name = disk.product_name(); 33 const std::string& product_name = disk.product_name();
32 if (!product_name.empty()) { 34 if (!product_name.empty()) {
(...skipping 20 matching lines...) Expand all
53 const std::string& vendor = disk.vendor_id(); 55 const std::string& vendor = disk.vendor_id();
54 const std::string& product = disk.product_id(); 56 const std::string& product = disk.product_id();
55 if (vendor.empty() && product.empty()) 57 if (vendor.empty() && product.empty())
56 return std::string(); 58 return std::string();
57 return base::StringPrintf("%s%s%s%s%s", 59 return base::StringPrintf("%s%s%s%s%s",
58 chrome::kVendorModelSerialPrefix, 60 chrome::kVendorModelSerialPrefix,
59 vendor.c_str(), chrome::kNonSpaceDelim, 61 vendor.c_str(), chrome::kNonSpaceDelim,
60 product.c_str(), chrome::kNonSpaceDelim); 62 product.c_str(), chrome::kNonSpaceDelim);
61 } 63 }
62 64
65 static RemovableDeviceNotificationsCros*
66 g_removable_device_notifications_chromeos = NULL;
67
63 // Returns true if the requested device is valid, else false. On success, fills 68 // Returns true if the requested device is valid, else false. On success, fills
64 // in |unique_id| and |device_label| 69 // in |unique_id| and |device_label|
65 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, 70 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id,
66 string16* device_label) { 71 string16* device_label) {
67 // Get the media device uuid and label if exists. 72 // Get the media device uuid and label if exists.
68 const disks::DiskMountManager::Disk* disk = 73 const disks::DiskMountManager::Disk* disk =
69 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); 74 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path);
70 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) 75 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN)
71 return false; 76 return false;
72 77
73 if (unique_id) 78 if (unique_id)
74 *unique_id = MakeDeviceUniqueId(*disk); 79 *unique_id = MakeDeviceUniqueId(*disk);
75 80
76 if (device_label) 81 if (device_label)
77 *device_label = GetDeviceName(*disk); 82 *device_label = GetDeviceName(*disk);
78 return true; 83 return true;
79 } 84 }
80 85
81 } // namespace 86 } // namespace
82 87
83 using content::BrowserThread; 88 using content::BrowserThread;
84 89
85 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { 90 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() {
86 DCHECK(disks::DiskMountManager::GetInstance()); 91 DCHECK(disks::DiskMountManager::GetInstance());
92 DCHECK(!g_removable_device_notifications_chromeos);
93 g_removable_device_notifications_chromeos = this;
87 disks::DiskMountManager::GetInstance()->AddObserver(this); 94 disks::DiskMountManager::GetInstance()->AddObserver(this);
88 CheckExistingMountPointsOnUIThread(); 95 CheckExistingMountPointsOnUIThread();
89 } 96 }
90 97
91 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() { 98 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() {
99 DCHECK_EQ(this, g_removable_device_notifications_chromeos);
100 g_removable_device_notifications_chromeos = NULL;
92 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); 101 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance();
93 if (manager) { 102 if (manager) {
94 manager->RemoveObserver(this); 103 manager->RemoveObserver(this);
95 } 104 }
96 } 105 }
97 106
107 // static
108 RemovableDeviceNotificationsCros*
109 RemovableDeviceNotificationsCros::GetInstance() {
110 return g_removable_device_notifications_chromeos;
111 }
112
98 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() { 113 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
100 const disks::DiskMountManager::MountPointMap& mount_point_map = 115 const disks::DiskMountManager::MountPointMap& mount_point_map =
101 disks::DiskMountManager::GetInstance()->mount_points(); 116 disks::DiskMountManager::GetInstance()->mount_points();
102 for (disks::DiskMountManager::MountPointMap::const_iterator it = 117 for (disks::DiskMountManager::MountPointMap::const_iterator it =
103 mount_point_map.begin(); it != mount_point_map.end(); ++it) { 118 mount_point_map.begin(); it != mount_point_map.end(); ++it) {
104 BrowserThread::PostTask( 119 BrowserThread::PostTask(
105 BrowserThread::FILE, FROM_HERE, 120 BrowserThread::FILE, FROM_HERE,
106 base::Bind( 121 base::Bind(
107 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, 122 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 BrowserThread::FILE, FROM_HERE, 160 BrowserThread::FILE, FROM_HERE,
146 base::Bind( 161 base::Bind(
147 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, 162 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread,
148 this, mount_info)); 163 this, mount_info));
149 break; 164 break;
150 } 165 }
151 case disks::DiskMountManager::UNMOUNTING: { 166 case disks::DiskMountManager::UNMOUNTING: {
152 MountMap::iterator it = mount_map_.find(mount_info.mount_path); 167 MountMap::iterator it = mount_map_.find(mount_info.mount_path);
153 if (it == mount_map_.end()) 168 if (it == mount_map_.end())
154 return; 169 return;
155 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(it->second); 170 SystemMonitor::Get()->ProcessRemovableStorageDetached(
171 it->second.device_id);
156 mount_map_.erase(it); 172 mount_map_.erase(it);
157 break; 173 break;
158 } 174 }
159 } 175 }
160 } 176 }
161 177
178 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath(
179 const FilePath& path,
180 SystemMonitor::RemovableStorageInfo* device_info) const {
181 if (!path.IsAbsolute())
182 return false;
183
184 FilePath current = path;
185 while (!ContainsKey(mount_map_, current.value()) &&
186 current != current.DirName()) {
187 current = current.DirName();
188 }
189
190 MountMap::const_iterator info_it = mount_map_.find(current.value());
191 if (info_it == mount_map_.end())
192 return false;
193
194 if (device_info)
195 *device_info = info_it->second;
196 return true;
197 }
198
162 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( 199 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread(
163 const disks::DiskMountManager::MountPointInfo& mount_info) { 200 const disks::DiskMountManager::MountPointInfo& mount_info) {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
165 202
166 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); 203 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path);
167 204
168 BrowserThread::PostTask( 205 BrowserThread::PostTask(
169 BrowserThread::UI, FROM_HERE, 206 BrowserThread::UI, FROM_HERE,
170 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, 207 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread,
171 this, mount_info, has_dcim)); 208 this, mount_info, has_dcim));
172 } 209 }
173 210
174 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( 211 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread(
175 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { 212 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177 214
178 if (ContainsKey(mount_map_, mount_info.mount_path)) { 215 if (ContainsKey(mount_map_, mount_info.mount_path))
179 NOTREACHED();
kmadhusu 2012/09/17 21:47:55 CheckExistingMountPointsOnUIThread() added the mou
180 return; 216 return;
181 }
182 217
183 // Get the media device uuid and label if exists. 218 // Get the media device uuid and label if exists.
184 std::string unique_id; 219 std::string unique_id;
185 string16 device_label; 220 string16 device_label;
186 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label)) 221 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label))
187 return; 222 return;
188 223
189 // Keep track of device uuid, to see how often we receive empty uuid values. 224 // Keep track of device uuid, to see how often we receive empty uuid values.
190 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.DeviceUUIDAvailable", 225 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.DeviceUUIDAvailable",
191 !unique_id.empty()); 226 !unique_id.empty());
192 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.DeviceNameAvailable", 227 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.DeviceNameAvailable",
193 !device_label.empty()); 228 !device_label.empty());
194 if (unique_id.empty() || device_label.empty()) 229 if (unique_id.empty() || device_label.empty())
195 return; 230 return;
196 231
197 chrome::MediaStorageUtil::Type type = has_dcim ? 232 chrome::MediaStorageUtil::Type type = has_dcim ?
198 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : 233 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM :
199 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; 234 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
200 235
201 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, 236 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type,
202 unique_id); 237 unique_id);
203 mount_map_.insert(std::make_pair(mount_info.mount_path, device_id)); 238 SystemMonitor::RemovableStorageInfo info(device_id, device_label,
204 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( 239 mount_info.mount_path);
240 mount_map_.insert(std::make_pair(mount_info.mount_path, info));
241 SystemMonitor::Get()->ProcessRemovableStorageAttached(
205 device_id, 242 device_id,
206 device_label, 243 device_label,
207 mount_info.mount_path); 244 mount_info.mount_path);
208 } 245 }
209 246
210 } // namespace chrome 247 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/system_monitor/removable_device_notifications_chromeos.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698