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

Side by Side Diff: chrome/browser/system_monitor/media_transfer_protocol_device_observer_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
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 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ chromeos.h" 5 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ chromeos.h"
6 6
7 #include "base/file_path.h"
7 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
8 #include "base/stl_util.h" 9 #include "base/stl_util.h"
9 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
10 #include "base/string_split.h" 11 #include "base/string_split.h"
11 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
12 #include "base/system_monitor/system_monitor.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h" 14 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h"
15 #include "chrome/browser/system_monitor/removable_device_constants.h" 15 #include "chrome/browser/system_monitor/removable_device_constants.h"
16 #include "chrome/browser/system_monitor/media_storage_util.h" 16 #include "chrome/browser/system_monitor/media_storage_util.h"
17 #include "chromeos/dbus/mtp_storage_info.pb.h" 17 #include "chromeos/dbus/mtp_storage_info.pb.h"
18 18
19 namespace chromeos { 19 namespace chromeos {
20 namespace mtp { 20 namespace mtp {
21 21
22 using base::SystemMonitor;
22 using chrome::MediaStorageUtil; 23 using chrome::MediaStorageUtil;
23 24
24 namespace { 25 namespace {
25 26
27 static MediaTransferProtocolDeviceObserverCros* g_mtp_device_observer = NULL;
28
26 // Device root path constant. 29 // Device root path constant.
27 const char kRootPath[] = "/"; 30 const char kRootPath[] = "/";
28 31
32 // Constructs and returns the location of the device using the |storage_name|.
33 std::string GetDeviceLocationFromStorageName(const std::string& storage_name) {
34 // Construct a dummy device path using the storage name. This is only used
35 // for registering device media file system.
36 // E.g.: /usb:2,2:12345
37 DCHECK(!storage_name.empty());
38 return kRootPath + storage_name;
39 }
40
29 // Returns the storage identifier of the device from the given |storage_name|. 41 // Returns the storage identifier of the device from the given |storage_name|.
30 // E.g. If the |storage_name| is "usb:2,2:65537", the storage identifier is 42 // E.g. If the |storage_name| is "usb:2,2:65537", the storage identifier is
31 // "65537". 43 // "65537".
32 std::string GetStorageIdFromStorageName(const std::string& storage_name) { 44 std::string GetStorageIdFromStorageName(const std::string& storage_name) {
33 std::vector<std::string> name_parts; 45 std::vector<std::string> name_parts;
34 base::SplitString(storage_name, ':', &name_parts); 46 base::SplitString(storage_name, ':', &name_parts);
35 return name_parts.size() == 3 ? name_parts[2] : std::string(); 47 return name_parts.size() == 3 ? name_parts[2] : std::string();
36 } 48 }
37 49
38 // Returns a unique device id from the given |storage_info|. 50 // Returns a unique device id from the given |storage_info|.
(...skipping 27 matching lines...) Expand all
66 std::string device_label; 78 std::string device_label;
67 const std::string& vendor_name = storage_info.vendor(); 79 const std::string& vendor_name = storage_info.vendor();
68 device_label = vendor_name; 80 device_label = vendor_name;
69 81
70 const std::string& product_name = storage_info.product(); 82 const std::string& product_name = storage_info.product();
71 if (!product_name.empty()) { 83 if (!product_name.empty()) {
72 if (!device_label.empty()) 84 if (!device_label.empty())
73 device_label += chrome::kSpaceDelim; 85 device_label += chrome::kSpaceDelim;
74 device_label += product_name; 86 device_label += product_name;
75 } 87 }
88
89 // Add the data store id to the device label.
90 if (!device_label.empty()) {
91 const std::string volume_id = storage_info.volume_identifier();
92 if (volume_id.empty())
93 volume_id = GetStorageIdFromStorageName(storage_info.storage_name());
94 if (!volume_id.empty())
95 device_label += chrome::kNonSpaceDelim + volume_id;
96 }
76 return UTF8ToUTF16(device_label); 97 return UTF8ToUTF16(device_label);
77 } 98 }
78 99
79 // Helper function to get the device storage details such as device id, label 100 // Helper function to get the device storage details such as device id, label
80 // and location. On success and fills in |id|, |label| and |location|. 101 // and location. On success and fills in |id|, |label| and |location|.
81 void GetStorageInfo(const std::string& storage_name, 102 void GetStorageInfo(const std::string& storage_name,
82 std::string* id, 103 std::string* id,
83 string16* label, 104 string16* label,
84 std::string* location) { 105 std::string* location) {
85 DCHECK(!storage_name.empty()); 106 DCHECK(!storage_name.empty());
86 MediaTransferProtocolManager* mtp_manager = 107 MediaTransferProtocolManager* mtp_manager =
87 MediaTransferProtocolManager::GetInstance(); 108 MediaTransferProtocolManager::GetInstance();
88 DCHECK(mtp_manager); 109 DCHECK(mtp_manager);
89 const MtpStorageInfo* storage_info = 110 const MtpStorageInfo* storage_info =
90 mtp_manager->GetStorageInfo(storage_name); 111 mtp_manager->GetStorageInfo(storage_name);
91 112
92 if (!storage_info) 113 if (!storage_info)
93 return; 114 return;
94 115
95 if (id) 116 if (id)
96 *id = GetDeviceIdFromStorageInfo(*storage_info); 117 *id = GetDeviceIdFromStorageInfo(*storage_info);
97 118
98 if (label) 119 if (label)
99 *label = GetDeviceLabelFromStorageInfo(*storage_info); 120 *label = GetDeviceLabelFromStorageInfo(*storage_info);
100 121
101 // Construct a dummy device path using the storage name. This is only used 122
102 // for registering device media file system.
103 // E.g.: /usb:2,2:12345
104 if (location) 123 if (location)
105 *location = kRootPath + storage_name; 124 *location = GetDeviceLocationFromStorageName(storage_name);
106 } 125 }
107 126
108 } // namespace 127 } // namespace
109 128
110 MediaTransferProtocolDeviceObserverCros:: 129 MediaTransferProtocolDeviceObserverCros::
111 MediaTransferProtocolDeviceObserverCros() 130 MediaTransferProtocolDeviceObserverCros()
112 : get_storage_info_func_(&GetStorageInfo) { 131 : get_storage_info_func_(&GetStorageInfo) {
132 DCHECK(!g_mtp_device_observer);
133 g_mtp_device_observer = this;
134
113 MediaTransferProtocolManager* mtp_manager = 135 MediaTransferProtocolManager* mtp_manager =
114 MediaTransferProtocolManager::GetInstance(); 136 MediaTransferProtocolManager::GetInstance();
115 if (mtp_manager) 137 if (mtp_manager)
116 mtp_manager->AddObserver(this); 138 mtp_manager->AddObserver(this);
117 EnumerateStorages(); 139 EnumerateStorages();
118 } 140 }
119 141
120 // This constructor is only used by unit tests. 142 // This constructor is only used by unit tests.
121 MediaTransferProtocolDeviceObserverCros:: 143 MediaTransferProtocolDeviceObserverCros::
122 MediaTransferProtocolDeviceObserverCros( 144 MediaTransferProtocolDeviceObserverCros(
123 GetStorageInfoFunc get_storage_info_func) 145 GetStorageInfoFunc get_storage_info_func)
124 : get_storage_info_func_(get_storage_info_func) { 146 : get_storage_info_func_(get_storage_info_func) {
125 // In unit tests, we don't have a media transfer protocol manager. 147 // In unit tests, we don't have a media transfer protocol manager.
126 DCHECK(!MediaTransferProtocolManager::GetInstance()); 148 DCHECK(!MediaTransferProtocolManager::GetInstance());
149 DCHECK(!g_mtp_device_observer);
150 g_mtp_device_observer = this;
127 } 151 }
128 152
129 MediaTransferProtocolDeviceObserverCros:: 153 MediaTransferProtocolDeviceObserverCros::
130 ~MediaTransferProtocolDeviceObserverCros() { 154 ~MediaTransferProtocolDeviceObserverCros() {
155 DCHECK_EQ(this, g_mtp_device_observer);
156 g_mtp_device_observer = NULL;
157
131 MediaTransferProtocolManager* mtp_manager = 158 MediaTransferProtocolManager* mtp_manager =
132 MediaTransferProtocolManager::GetInstance(); 159 MediaTransferProtocolManager::GetInstance();
133 if (mtp_manager) 160 if (mtp_manager)
134 mtp_manager->RemoveObserver(this); 161 mtp_manager->RemoveObserver(this);
135 } 162 }
136 163
164 // static
165 MediaTransferProtocolDeviceObserverCros*
166 MediaTransferProtocolDeviceObserverCros::GetInstance() {
167 DCHECK(g_mtp_device_observer != NULL);
168 return g_mtp_device_observer;
169 }
170
171 bool MediaTransferProtocolDeviceObserverCros::GetStorageInfoForPath(
172 const FilePath& path,
173 SystemMonitor::RemovableStorageInfo* storage_info) const {
174 if (!path.IsAbsolute())
175 return false;
176
177 std::vector<FilePath::StringType> path_components;
178 path.GetComponents(&path_components);
179 if (path_components.size() < 2)
180 return false;
181
182 // First and second component of the path specifies the device location.
183 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the
184 // device location.
185 StorageLocationToInfoMap::const_iterator info_it =
186 storage_map_.find(path_components[0] + path_components[1]);
187 if (info_it == storage_map_.end())
188 return false;
189
190 if (storage_info)
191 *storage_info = info_it->second;
192 return true;
193 }
194
137 // MediaTransferProtocolManager::Observer override. 195 // MediaTransferProtocolManager::Observer override.
138 void MediaTransferProtocolDeviceObserverCros::StorageChanged( 196 void MediaTransferProtocolDeviceObserverCros::StorageChanged(
139 bool is_attached, 197 bool is_attached,
140 const std::string& storage_name) { 198 const std::string& storage_name) {
141 DCHECK(!storage_name.empty()); 199 DCHECK(!storage_name.empty());
142 200
143 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); 201 SystemMonitor* system_monitor = SystemMonitor::Get();
144 DCHECK(system_monitor); 202 DCHECK(system_monitor);
145 203
146 // New storage is attached. 204 // New storage is attached.
147 if (is_attached) { 205 if (is_attached) {
148 std::string device_id; 206 std::string device_id;
149 string16 device_name; 207 string16 device_name;
150 std::string location; 208 std::string location;
151 get_storage_info_func_(storage_name, &device_id, &device_name, &location); 209 get_storage_info_func_(storage_name, &device_id, &device_name, &location);
152 210
153 // Keep track of device id and device name to see how often we receive 211 // Keep track of device id and device name to see how often we receive
154 // empty values. 212 // empty values.
155 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable", 213 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable",
156 !device_id.empty()); 214 !device_id.empty());
157 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable", 215 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable",
158 !device_name.empty()); 216 !device_name.empty());
159 217
160 if (device_id.empty() || device_name.empty()) 218 if (device_id.empty() || device_name.empty())
161 return; 219 return;
162 220
163 DCHECK(!ContainsKey(storage_map_, storage_name)); 221 DCHECK(!ContainsKey(storage_map_, location));
164 storage_map_[storage_name] = device_id; 222
223 SystemMonitor::RemovableStorageInfo storage_info(device_id, device_name,
224 location);
225 storage_map_[location] = storage_info;
165 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, 226 system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
166 location); 227 location);
167 } else { 228 } else {
168 // Existing storage is detached. 229 // Existing storage is detached.
169 StorageNameToIdMap::iterator it = storage_map_.find(storage_name); 230 StorageLocationToInfoMap::iterator it =
231 storage_map_.find(GetDeviceLocationFromStorageName(storage_name));
170 if (it == storage_map_.end()) 232 if (it == storage_map_.end())
171 return; 233 return;
172 system_monitor->ProcessRemovableStorageDetached(it->second); 234 system_monitor->ProcessRemovableStorageDetached(it->second.device_id);
173 storage_map_.erase(it); 235 storage_map_.erase(it);
174 } 236 }
175 } 237 }
176 238
177 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() { 239 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() {
178 typedef std::vector<std::string> StorageList; 240 typedef std::vector<std::string> StorageList;
179 MediaTransferProtocolManager* mtp_manager = 241 MediaTransferProtocolManager* mtp_manager =
180 MediaTransferProtocolManager::GetInstance(); 242 MediaTransferProtocolManager::GetInstance();
181 DCHECK(mtp_manager); 243 DCHECK(mtp_manager);
182 StorageList storages = mtp_manager->GetStorages(); 244 StorageList storages = mtp_manager->GetStorages();
183 for (StorageList::const_iterator storage_iter = storages.begin(); 245 for (StorageList::const_iterator storage_iter = storages.begin();
184 storage_iter != storages.end(); ++storage_iter) { 246 storage_iter != storages.end(); ++storage_iter) {
185 StorageChanged(true, *storage_iter); 247 StorageChanged(true, *storage_iter);
186 } 248 }
187 } 249 }
188 250
189 } // namespace mtp 251 } // namespace mtp
190 } // namespace chromeos 252 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698