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

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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 103
92 if (!storage_info) 104 if (!storage_info)
93 return; 105 return;
94 106
95 if (id) 107 if (id)
96 *id = GetDeviceIdFromStorageInfo(*storage_info); 108 *id = GetDeviceIdFromStorageInfo(*storage_info);
97 109
98 if (label) 110 if (label)
99 *label = GetDeviceLabelFromStorageInfo(*storage_info); 111 *label = GetDeviceLabelFromStorageInfo(*storage_info);
100 112
101 // Construct a dummy device path using the storage name. This is only used 113
102 // for registering device media file system.
103 // E.g.: /usb:2,2:12345
104 if (location) 114 if (location)
105 *location = kRootPath + storage_name; 115 *location = GetDeviceLocationFromStorageName(storage_name);
106 } 116 }
107 117
108 } // namespace 118 } // namespace
109 119
110 MediaTransferProtocolDeviceObserverCros:: 120 MediaTransferProtocolDeviceObserverCros::
111 MediaTransferProtocolDeviceObserverCros() 121 MediaTransferProtocolDeviceObserverCros()
112 : get_storage_info_func_(&GetStorageInfo) { 122 : get_storage_info_func_(&GetStorageInfo) {
123 DCHECK(!g_mtp_device_observer);
124 g_mtp_device_observer = this;
125
113 MediaTransferProtocolManager* mtp_manager = 126 MediaTransferProtocolManager* mtp_manager =
114 MediaTransferProtocolManager::GetInstance(); 127 MediaTransferProtocolManager::GetInstance();
115 if (mtp_manager) 128 if (mtp_manager)
116 mtp_manager->AddObserver(this); 129 mtp_manager->AddObserver(this);
117 EnumerateStorages(); 130 EnumerateStorages();
118 } 131 }
119 132
120 // This constructor is only used by unit tests. 133 // This constructor is only used by unit tests.
121 MediaTransferProtocolDeviceObserverCros:: 134 MediaTransferProtocolDeviceObserverCros::
122 MediaTransferProtocolDeviceObserverCros( 135 MediaTransferProtocolDeviceObserverCros(
123 GetStorageInfoFunc get_storage_info_func) 136 GetStorageInfoFunc get_storage_info_func)
124 : get_storage_info_func_(get_storage_info_func) { 137 : get_storage_info_func_(get_storage_info_func) {
125 // In unit tests, we don't have a media transfer protocol manager. 138 // In unit tests, we don't have a media transfer protocol manager.
126 DCHECK(!MediaTransferProtocolManager::GetInstance()); 139 DCHECK(!MediaTransferProtocolManager::GetInstance());
140
vandebo (ex-Chrome) 2012/09/16 23:03:19 nit: extra line
kmadhusu 2012/09/17 21:47:55 Done.
141 DCHECK(!g_mtp_device_observer);
142 g_mtp_device_observer = this;
127 } 143 }
128 144
129 MediaTransferProtocolDeviceObserverCros:: 145 MediaTransferProtocolDeviceObserverCros::
130 ~MediaTransferProtocolDeviceObserverCros() { 146 ~MediaTransferProtocolDeviceObserverCros() {
147 if (g_mtp_device_observer) {
vandebo (ex-Chrome) 2012/09/16 23:03:19 No if - this is set in the constructor.
kmadhusu 2012/09/17 21:47:55 Done.
148 DCHECK_EQ(this, g_mtp_device_observer);
149 g_mtp_device_observer = NULL;
150 }
151
131 MediaTransferProtocolManager* mtp_manager = 152 MediaTransferProtocolManager* mtp_manager =
132 MediaTransferProtocolManager::GetInstance(); 153 MediaTransferProtocolManager::GetInstance();
133 if (mtp_manager) 154 if (mtp_manager)
134 mtp_manager->RemoveObserver(this); 155 mtp_manager->RemoveObserver(this);
135 } 156 }
136 157
158 // static
159 MediaTransferProtocolDeviceObserverCros*
160 MediaTransferProtocolDeviceObserverCros::GetInstance() {
161 DCHECK(g_mtp_device_observer != NULL);
162 return g_mtp_device_observer;
163 }
164
165 bool MediaTransferProtocolDeviceObserverCros::GetStorageInfoForPath(
166 const FilePath& path,
167 SystemMonitor::RemovableStorageInfo* storage_info) const {
168 if (!path.IsAbsolute())
169 return false;
170
171 std::vector<FilePath::StringType> path_components;
172 path.GetComponents(&path_components);
173 if (path_components.size() < 2)
174 return false;
175
176 // First and second component of the path specifies the device location.
177 // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the
178 // device location.
179 StorageLocationToInfoMap::const_iterator info_it =
180 storage_map_.find(path_components[0] + path_components[1]);
181 if (info_it == storage_map_.end())
182 return false;
183
184 if (storage_info)
185 *storage_info = info_it->second;
186 return true;
187 }
188
137 // MediaTransferProtocolManager::Observer override. 189 // MediaTransferProtocolManager::Observer override.
138 void MediaTransferProtocolDeviceObserverCros::StorageChanged( 190 void MediaTransferProtocolDeviceObserverCros::StorageChanged(
139 bool is_attached, 191 bool is_attached,
140 const std::string& storage_name) { 192 const std::string& storage_name) {
141 DCHECK(!storage_name.empty()); 193 DCHECK(!storage_name.empty());
142 194
143 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); 195 SystemMonitor* system_monitor = SystemMonitor::Get();
144 DCHECK(system_monitor); 196 DCHECK(system_monitor);
145 197
146 // New storage is attached. 198 // New storage is attached.
147 if (is_attached) { 199 if (is_attached) {
148 std::string device_id; 200 std::string device_id;
149 string16 device_name; 201 string16 device_name;
150 std::string location; 202 std::string location;
151 get_storage_info_func_(storage_name, &device_id, &device_name, &location); 203 get_storage_info_func_(storage_name, &device_id, &device_name, &location);
152 204
153 // Keep track of device id and device name to see how often we receive 205 // Keep track of device id and device name to see how often we receive
154 // empty values. 206 // empty values.
155 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable", 207 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable",
156 !device_id.empty()); 208 !device_id.empty());
157 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable", 209 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable",
158 !device_name.empty()); 210 !device_name.empty());
159 211
160 if (device_id.empty() || device_name.empty()) 212 if (device_id.empty() || device_name.empty())
161 return; 213 return;
162 214
163 DCHECK(!ContainsKey(storage_map_, storage_name)); 215 DCHECK(!ContainsKey(storage_map_, location));
164 storage_map_[storage_name] = device_id; 216
217 SystemMonitor::RemovableStorageInfo storage_info(device_id, device_name,
218 location);
219 storage_map_[location] = storage_info;
165 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, 220 system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
166 location); 221 location);
167 } else { 222 } else {
168 // Existing storage is detached. 223 // Existing storage is detached.
169 StorageNameToIdMap::iterator it = storage_map_.find(storage_name); 224 StorageLocationToInfoMap::iterator it =
225 storage_map_.find(GetDeviceLocationFromStorageName(storage_name));
170 if (it == storage_map_.end()) 226 if (it == storage_map_.end())
171 return; 227 return;
172 system_monitor->ProcessRemovableStorageDetached(it->second); 228 system_monitor->ProcessRemovableStorageDetached(it->second.device_id);
173 storage_map_.erase(it); 229 storage_map_.erase(it);
174 } 230 }
175 } 231 }
176 232
177 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() { 233 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() {
178 typedef std::vector<std::string> StorageList; 234 typedef std::vector<std::string> StorageList;
179 MediaTransferProtocolManager* mtp_manager = 235 MediaTransferProtocolManager* mtp_manager =
180 MediaTransferProtocolManager::GetInstance(); 236 MediaTransferProtocolManager::GetInstance();
181 DCHECK(mtp_manager); 237 DCHECK(mtp_manager);
182 StorageList storages = mtp_manager->GetStorages(); 238 StorageList storages = mtp_manager->GetStorages();
183 for (StorageList::const_iterator storage_iter = storages.begin(); 239 for (StorageList::const_iterator storage_iter = storages.begin();
184 storage_iter != storages.end(); ++storage_iter) { 240 storage_iter != storages.end(); ++storage_iter) {
185 StorageChanged(true, *storage_iter); 241 StorageChanged(true, *storage_iter);
186 } 242 }
187 } 243 }
188 244
189 } // namespace mtp 245 } // namespace mtp
190 } // namespace chromeos 246 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698