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

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

Issue 12147002: Add a receiver interface to RemovableStorageNotifications. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merging Created 7 years, 10 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
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_ linux.h" 5 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_ linux.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/string_split.h" 10 #include "base/string_split.h"
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 device::MediaTransferProtocolManager* mtp_manager = 129 device::MediaTransferProtocolManager* mtp_manager =
130 device::MediaTransferProtocolManager::GetInstance(); 130 device::MediaTransferProtocolManager::GetInstance();
131 mtp_manager->AddObserver(this); 131 mtp_manager->AddObserver(this);
132 EnumerateStorages(); 132 EnumerateStorages();
133 } 133 }
134 134
135 // This constructor is only used by unit tests. 135 // This constructor is only used by unit tests.
136 MediaTransferProtocolDeviceObserverLinux:: 136 MediaTransferProtocolDeviceObserverLinux::
137 MediaTransferProtocolDeviceObserverLinux( 137 MediaTransferProtocolDeviceObserverLinux(
138 GetStorageInfoFunc get_storage_info_func) 138 GetStorageInfoFunc get_storage_info_func)
139 : get_storage_info_func_(get_storage_info_func) { 139 : get_storage_info_func_(get_storage_info_func),
140 notifications_(NULL) {
140 // In unit tests, we don't have a media transfer protocol manager. 141 // In unit tests, we don't have a media transfer protocol manager.
141 DCHECK(!device::MediaTransferProtocolManager::GetInstance()); 142 DCHECK(!device::MediaTransferProtocolManager::GetInstance());
142 DCHECK(!g_mtp_device_observer); 143 DCHECK(!g_mtp_device_observer);
143 g_mtp_device_observer = this; 144 g_mtp_device_observer = this;
144 } 145 }
145 146
146 MediaTransferProtocolDeviceObserverLinux:: 147 MediaTransferProtocolDeviceObserverLinux::
147 ~MediaTransferProtocolDeviceObserverLinux() { 148 ~MediaTransferProtocolDeviceObserverLinux() {
148 DCHECK_EQ(this, g_mtp_device_observer); 149 DCHECK_EQ(this, g_mtp_device_observer);
149 g_mtp_device_observer = NULL; 150 g_mtp_device_observer = NULL;
(...skipping 28 matching lines...) Expand all
178 StorageLocationToInfoMap::const_iterator info_it = 179 StorageLocationToInfoMap::const_iterator info_it =
179 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); 180 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1]));
180 if (info_it == storage_map_.end()) 181 if (info_it == storage_map_.end())
181 return false; 182 return false;
182 183
183 if (storage_info) 184 if (storage_info)
184 *storage_info = info_it->second; 185 *storage_info = info_it->second;
185 return true; 186 return true;
186 } 187 }
187 188
189 void MediaTransferProtocolDeviceObserverLinux::SetNotifications(
190 RemovableStorageNotifications::Receiver* notifications) {
191 notifications_ = notifications;
192 }
193
188 // device::MediaTransferProtocolManager::Observer override. 194 // device::MediaTransferProtocolManager::Observer override.
189 void MediaTransferProtocolDeviceObserverLinux::StorageChanged( 195 void MediaTransferProtocolDeviceObserverLinux::StorageChanged(
190 bool is_attached, 196 bool is_attached,
191 const std::string& storage_name) { 197 const std::string& storage_name) {
192 DCHECK(!storage_name.empty()); 198 DCHECK(!storage_name.empty());
193 199
194 RemovableStorageNotifications* notifications =
195 RemovableStorageNotifications::GetInstance();
196 DCHECK(notifications);
197
198 // New storage is attached. 200 // New storage is attached.
199 if (is_attached) { 201 if (is_attached) {
200 std::string device_id; 202 std::string device_id;
201 string16 device_name; 203 string16 device_name;
202 std::string location; 204 std::string location;
203 get_storage_info_func_(storage_name, &device_id, &device_name, &location); 205 get_storage_info_func_(storage_name, &device_id, &device_name, &location);
204 206
205 // Keep track of device id and device name to see how often we receive 207 // Keep track of device id and device name to see how often we receive
206 // empty values. 208 // empty values.
207 MediaStorageUtil::RecordDeviceInfoHistogram(false, device_id, device_name); 209 MediaStorageUtil::RecordDeviceInfoHistogram(false, device_id, device_name);
208 if (device_id.empty() || device_name.empty()) 210 if (device_id.empty() || device_name.empty())
209 return; 211 return;
210 212
211 DCHECK(!ContainsKey(storage_map_, location)); 213 DCHECK(!ContainsKey(storage_map_, location));
212 214
213 RemovableStorageNotifications::StorageInfo storage_info( 215 RemovableStorageNotifications::StorageInfo storage_info(
214 device_id, device_name, location); 216 device_id, device_name, location);
215 storage_map_[location] = storage_info; 217 storage_map_[location] = storage_info;
216 notifications->ProcessAttach(device_id, device_name, location); 218 notifications_->ProcessAttach(device_id, device_name, location);
217 } else { 219 } else {
218 // Existing storage is detached. 220 // Existing storage is detached.
219 StorageLocationToInfoMap::iterator it = 221 StorageLocationToInfoMap::iterator it =
220 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); 222 storage_map_.find(GetDeviceLocationFromStorageName(storage_name));
221 if (it == storage_map_.end()) 223 if (it == storage_map_.end())
222 return; 224 return;
223 notifications->ProcessDetach(it->second.device_id); 225 notifications_->ProcessDetach(it->second.device_id);
224 storage_map_.erase(it); 226 storage_map_.erase(it);
225 } 227 }
226 } 228 }
227 229
228 void MediaTransferProtocolDeviceObserverLinux::EnumerateStorages() { 230 void MediaTransferProtocolDeviceObserverLinux::EnumerateStorages() {
229 typedef std::vector<std::string> StorageList; 231 typedef std::vector<std::string> StorageList;
230 device::MediaTransferProtocolManager* mtp_manager = 232 device::MediaTransferProtocolManager* mtp_manager =
231 device::MediaTransferProtocolManager::GetInstance(); 233 device::MediaTransferProtocolManager::GetInstance();
232 StorageList storages = mtp_manager->GetStorages(); 234 StorageList storages = mtp_manager->GetStorages();
233 for (StorageList::const_iterator storage_iter = storages.begin(); 235 for (StorageList::const_iterator storage_iter = storages.begin();
234 storage_iter != storages.end(); ++storage_iter) { 236 storage_iter != storages.end(); ++storage_iter) {
235 StorageChanged(true, *storage_iter); 237 StorageChanged(true, *storage_iter);
236 } 238 }
237 } 239 }
238 240
239 } // namespace chrome 241 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698