OLD | NEW |
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" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/media_transfer_protocol/mtp_storage_info.pb.h" | |
13 #include "chrome/browser/system_monitor/media_storage_util.h" | 12 #include "chrome/browser/system_monitor/media_storage_util.h" |
14 #include "chrome/browser/system_monitor/removable_device_constants.h" | 13 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 14 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" |
15 | 15 |
16 namespace chrome { | 16 namespace chrome { |
17 | 17 |
18 using base::SystemMonitor; | 18 using base::SystemMonitor; |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 static MediaTransferProtocolDeviceObserverLinux* g_mtp_device_observer = NULL; | 22 static MediaTransferProtocolDeviceObserverLinux* g_mtp_device_observer = NULL; |
23 | 23 |
24 // Device root path constant. | 24 // Device root path constant. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 return UTF8ToUTF16(device_label); | 94 return UTF8ToUTF16(device_label); |
95 } | 95 } |
96 | 96 |
97 // Helper function to get the device storage details such as device id, label | 97 // Helper function to get the device storage details such as device id, label |
98 // and location. On success and fills in |id|, |label| and |location|. | 98 // and location. On success and fills in |id|, |label| and |location|. |
99 void GetStorageInfo(const std::string& storage_name, | 99 void GetStorageInfo(const std::string& storage_name, |
100 std::string* id, | 100 std::string* id, |
101 string16* label, | 101 string16* label, |
102 std::string* location) { | 102 std::string* location) { |
103 DCHECK(!storage_name.empty()); | 103 DCHECK(!storage_name.empty()); |
104 MediaTransferProtocolManager* mtp_manager = | 104 device::MediaTransferProtocolManager* mtp_manager = |
105 MediaTransferProtocolManager::GetInstance(); | 105 device::MediaTransferProtocolManager::GetInstance(); |
106 const MtpStorageInfo* storage_info = | 106 const MtpStorageInfo* storage_info = |
107 mtp_manager->GetStorageInfo(storage_name); | 107 mtp_manager->GetStorageInfo(storage_name); |
108 | 108 |
109 if (!storage_info) | 109 if (!storage_info) |
110 return; | 110 return; |
111 | 111 |
112 if (id) | 112 if (id) |
113 *id = GetDeviceIdFromStorageInfo(*storage_info); | 113 *id = GetDeviceIdFromStorageInfo(*storage_info); |
114 | 114 |
115 if (label) | 115 if (label) |
116 *label = GetDeviceLabelFromStorageInfo(*storage_info); | 116 *label = GetDeviceLabelFromStorageInfo(*storage_info); |
117 | 117 |
118 | 118 |
119 if (location) | 119 if (location) |
120 *location = GetDeviceLocationFromStorageName(storage_name); | 120 *location = GetDeviceLocationFromStorageName(storage_name); |
121 } | 121 } |
122 | 122 |
123 } // namespace | 123 } // namespace |
124 | 124 |
125 MediaTransferProtocolDeviceObserverLinux:: | 125 MediaTransferProtocolDeviceObserverLinux:: |
126 MediaTransferProtocolDeviceObserverLinux() | 126 MediaTransferProtocolDeviceObserverLinux() |
127 : get_storage_info_func_(&GetStorageInfo) { | 127 : get_storage_info_func_(&GetStorageInfo) { |
128 DCHECK(!g_mtp_device_observer); | 128 DCHECK(!g_mtp_device_observer); |
129 g_mtp_device_observer = this; | 129 g_mtp_device_observer = this; |
130 | 130 |
131 MediaTransferProtocolManager* mtp_manager = | 131 device::MediaTransferProtocolManager* mtp_manager = |
132 MediaTransferProtocolManager::GetInstance(); | 132 device::MediaTransferProtocolManager::GetInstance(); |
133 mtp_manager->AddObserver(this); | 133 mtp_manager->AddObserver(this); |
134 EnumerateStorages(); | 134 EnumerateStorages(); |
135 } | 135 } |
136 | 136 |
137 // This constructor is only used by unit tests. | 137 // This constructor is only used by unit tests. |
138 MediaTransferProtocolDeviceObserverLinux:: | 138 MediaTransferProtocolDeviceObserverLinux:: |
139 MediaTransferProtocolDeviceObserverLinux( | 139 MediaTransferProtocolDeviceObserverLinux( |
140 GetStorageInfoFunc get_storage_info_func) | 140 GetStorageInfoFunc get_storage_info_func) |
141 : get_storage_info_func_(get_storage_info_func) { | 141 : get_storage_info_func_(get_storage_info_func) { |
142 // In unit tests, we don't have a media transfer protocol manager. | 142 // In unit tests, we don't have a media transfer protocol manager. |
143 DCHECK(!MediaTransferProtocolManager::GetInstance()); | 143 DCHECK(!device::MediaTransferProtocolManager::GetInstance()); |
144 DCHECK(!g_mtp_device_observer); | 144 DCHECK(!g_mtp_device_observer); |
145 g_mtp_device_observer = this; | 145 g_mtp_device_observer = this; |
146 } | 146 } |
147 | 147 |
148 MediaTransferProtocolDeviceObserverLinux:: | 148 MediaTransferProtocolDeviceObserverLinux:: |
149 ~MediaTransferProtocolDeviceObserverLinux() { | 149 ~MediaTransferProtocolDeviceObserverLinux() { |
150 DCHECK_EQ(this, g_mtp_device_observer); | 150 DCHECK_EQ(this, g_mtp_device_observer); |
151 g_mtp_device_observer = NULL; | 151 g_mtp_device_observer = NULL; |
152 | 152 |
153 MediaTransferProtocolManager* mtp_manager = | 153 device::MediaTransferProtocolManager* mtp_manager = |
154 MediaTransferProtocolManager::GetInstance(); | 154 device::MediaTransferProtocolManager::GetInstance(); |
155 if (mtp_manager) | 155 if (mtp_manager) |
156 mtp_manager->RemoveObserver(this); | 156 mtp_manager->RemoveObserver(this); |
157 } | 157 } |
158 | 158 |
159 // static | 159 // static |
160 MediaTransferProtocolDeviceObserverLinux* | 160 MediaTransferProtocolDeviceObserverLinux* |
161 MediaTransferProtocolDeviceObserverLinux::GetInstance() { | 161 MediaTransferProtocolDeviceObserverLinux::GetInstance() { |
162 DCHECK(g_mtp_device_observer != NULL); | 162 DCHECK(g_mtp_device_observer != NULL); |
163 return g_mtp_device_observer; | 163 return g_mtp_device_observer; |
164 } | 164 } |
(...skipping 15 matching lines...) Expand all Loading... |
180 StorageLocationToInfoMap::const_iterator info_it = | 180 StorageLocationToInfoMap::const_iterator info_it = |
181 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); | 181 storage_map_.find(GetDeviceLocationFromStorageName(path_components[1])); |
182 if (info_it == storage_map_.end()) | 182 if (info_it == storage_map_.end()) |
183 return false; | 183 return false; |
184 | 184 |
185 if (storage_info) | 185 if (storage_info) |
186 *storage_info = info_it->second; | 186 *storage_info = info_it->second; |
187 return true; | 187 return true; |
188 } | 188 } |
189 | 189 |
190 // MediaTransferProtocolManager::Observer override. | 190 // device::MediaTransferProtocolManager::Observer override. |
191 void MediaTransferProtocolDeviceObserverLinux::StorageChanged( | 191 void MediaTransferProtocolDeviceObserverLinux::StorageChanged( |
192 bool is_attached, | 192 bool is_attached, |
193 const std::string& storage_name) { | 193 const std::string& storage_name) { |
194 DCHECK(!storage_name.empty()); | 194 DCHECK(!storage_name.empty()); |
195 | 195 |
196 SystemMonitor* system_monitor = SystemMonitor::Get(); | 196 SystemMonitor* system_monitor = SystemMonitor::Get(); |
197 DCHECK(system_monitor); | 197 DCHECK(system_monitor); |
198 | 198 |
199 // New storage is attached. | 199 // New storage is attached. |
200 if (is_attached) { | 200 if (is_attached) { |
(...skipping 21 matching lines...) Expand all Loading... |
222 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); | 222 storage_map_.find(GetDeviceLocationFromStorageName(storage_name)); |
223 if (it == storage_map_.end()) | 223 if (it == storage_map_.end()) |
224 return; | 224 return; |
225 system_monitor->ProcessRemovableStorageDetached(it->second.device_id); | 225 system_monitor->ProcessRemovableStorageDetached(it->second.device_id); |
226 storage_map_.erase(it); | 226 storage_map_.erase(it); |
227 } | 227 } |
228 } | 228 } |
229 | 229 |
230 void MediaTransferProtocolDeviceObserverLinux::EnumerateStorages() { | 230 void MediaTransferProtocolDeviceObserverLinux::EnumerateStorages() { |
231 typedef std::vector<std::string> StorageList; | 231 typedef std::vector<std::string> StorageList; |
232 MediaTransferProtocolManager* mtp_manager = | 232 device::MediaTransferProtocolManager* mtp_manager = |
233 MediaTransferProtocolManager::GetInstance(); | 233 device::MediaTransferProtocolManager::GetInstance(); |
234 StorageList storages = mtp_manager->GetStorages(); | 234 StorageList storages = mtp_manager->GetStorages(); |
235 for (StorageList::const_iterator storage_iter = storages.begin(); | 235 for (StorageList::const_iterator storage_iter = storages.begin(); |
236 storage_iter != storages.end(); ++storage_iter) { | 236 storage_iter != storages.end(); ++storage_iter) { |
237 StorageChanged(true, *storage_iter); | 237 StorageChanged(true, *storage_iter); |
238 } | 238 } |
239 } | 239 } |
240 | 240 |
241 } // namespace chrome | 241 } // namespace chrome |
OLD | NEW |