| 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/chromeos/mtp/media_transfer_protocol_manager.h" | 5 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 #include "chromeos/dbus/mtp_file_entry.pb.h" |
| 18 #include "chromeos/dbus/mtp_storage_info.pb.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 | 20 |
| 19 using content::BrowserThread; | 21 using content::BrowserThread; |
| 20 | 22 |
| 21 namespace chromeos { | 23 namespace chromeos { |
| 22 namespace mtp { | 24 namespace mtp { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; | 28 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 std::vector<std::string> storages; | 63 std::vector<std::string> storages; |
| 62 for (StorageInfoMap::const_iterator it = storage_info_map_.begin(); | 64 for (StorageInfoMap::const_iterator it = storage_info_map_.begin(); |
| 63 it != storage_info_map_.end(); | 65 it != storage_info_map_.end(); |
| 64 ++it) { | 66 ++it) { |
| 65 storages.push_back(it->first); | 67 storages.push_back(it->first); |
| 66 } | 68 } |
| 67 return storages; | 69 return storages; |
| 68 } | 70 } |
| 69 | 71 |
| 70 // MediaTransferProtocolManager override. | 72 // MediaTransferProtocolManager override. |
| 71 virtual const StorageInfo* GetStorageInfo( | 73 virtual const MtpStorageInfo* GetStorageInfo( |
| 72 const std::string& storage_name) const OVERRIDE { | 74 const std::string& storage_name) const OVERRIDE { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name); | 76 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name); |
| 75 if (it == storage_info_map_.end()) | 77 if (it == storage_info_map_.end()) |
| 76 return NULL; | 78 return NULL; |
| 77 return &it->second; | 79 return &it->second; |
| 78 } | 80 } |
| 79 | 81 |
| 80 // MediaTransferProtocolManager override. | 82 // MediaTransferProtocolManager override. |
| 81 virtual void OpenStorage(const std::string& storage_name, | 83 virtual void OpenStorage(const std::string& storage_name, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 weak_ptr_factory_.GetWeakPtr())); | 115 weak_ptr_factory_.GetWeakPtr())); |
| 114 } | 116 } |
| 115 | 117 |
| 116 // MediaTransferProtocolManager override. | 118 // MediaTransferProtocolManager override. |
| 117 virtual void ReadDirectoryByPath( | 119 virtual void ReadDirectoryByPath( |
| 118 const std::string& storage_handle, | 120 const std::string& storage_handle, |
| 119 const std::string& path, | 121 const std::string& path, |
| 120 const ReadDirectoryCallback& callback) OVERRIDE { | 122 const ReadDirectoryCallback& callback) OVERRIDE { |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 122 if (!ContainsKey(handles_, storage_handle)) { | 124 if (!ContainsKey(handles_, storage_handle)) { |
| 123 callback.Run(std::vector<FileEntry>(), true); | 125 callback.Run(std::vector<MtpFileEntry>(), true); |
| 124 return; | 126 return; |
| 125 } | 127 } |
| 126 read_directory_callbacks_.push(callback); | 128 read_directory_callbacks_.push(callback); |
| 127 mtp_client_->ReadDirectoryByPath( | 129 mtp_client_->ReadDirectoryByPath( |
| 128 storage_handle, | 130 storage_handle, |
| 129 path, | 131 path, |
| 130 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory, | 132 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory, |
| 131 weak_ptr_factory_.GetWeakPtr()), | 133 weak_ptr_factory_.GetWeakPtr()), |
| 132 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, | 134 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, |
| 133 weak_ptr_factory_.GetWeakPtr())); | 135 weak_ptr_factory_.GetWeakPtr())); |
| 134 } | 136 } |
| 135 | 137 |
| 136 // MediaTransferProtocolManager override. | 138 // MediaTransferProtocolManager override. |
| 137 virtual void ReadDirectoryById( | 139 virtual void ReadDirectoryById( |
| 138 const std::string& storage_handle, | 140 const std::string& storage_handle, |
| 139 uint32 file_id, | 141 uint32 file_id, |
| 140 const ReadDirectoryCallback& callback) OVERRIDE { | 142 const ReadDirectoryCallback& callback) OVERRIDE { |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 142 if (!ContainsKey(handles_, storage_handle)) { | 144 if (!ContainsKey(handles_, storage_handle)) { |
| 143 callback.Run(std::vector<FileEntry>(), true); | 145 callback.Run(std::vector<MtpFileEntry>(), true); |
| 144 return; | 146 return; |
| 145 } | 147 } |
| 146 read_directory_callbacks_.push(callback); | 148 read_directory_callbacks_.push(callback); |
| 147 mtp_client_->ReadDirectoryById( | 149 mtp_client_->ReadDirectoryById( |
| 148 storage_handle, | 150 storage_handle, |
| 149 file_id, | 151 file_id, |
| 150 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory, | 152 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectory, |
| 151 weak_ptr_factory_.GetWeakPtr()), | 153 weak_ptr_factory_.GetWeakPtr()), |
| 152 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, | 154 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, |
| 153 weak_ptr_factory_.GetWeakPtr())); | 155 weak_ptr_factory_.GetWeakPtr())); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 weak_ptr_factory_.GetWeakPtr()), | 191 weak_ptr_factory_.GetWeakPtr()), |
| 190 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError, | 192 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError, |
| 191 weak_ptr_factory_.GetWeakPtr())); | 193 weak_ptr_factory_.GetWeakPtr())); |
| 192 } | 194 } |
| 193 | 195 |
| 194 virtual void GetFileInfoByPath(const std::string& storage_handle, | 196 virtual void GetFileInfoByPath(const std::string& storage_handle, |
| 195 const std::string& path, | 197 const std::string& path, |
| 196 const GetFileInfoCallback& callback) OVERRIDE { | 198 const GetFileInfoCallback& callback) OVERRIDE { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 198 if (!ContainsKey(handles_, storage_handle)) { | 200 if (!ContainsKey(handles_, storage_handle)) { |
| 199 callback.Run(FileEntry(), true); | 201 callback.Run(MtpFileEntry(), true); |
| 200 return; | 202 return; |
| 201 } | 203 } |
| 202 get_file_info_callbacks_.push(callback); | 204 get_file_info_callbacks_.push(callback); |
| 203 mtp_client_->GetFileInfoByPath( | 205 mtp_client_->GetFileInfoByPath( |
| 204 storage_handle, | 206 storage_handle, |
| 205 path, | 207 path, |
| 206 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, | 208 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, |
| 207 weak_ptr_factory_.GetWeakPtr()), | 209 weak_ptr_factory_.GetWeakPtr()), |
| 208 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, | 210 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, |
| 209 weak_ptr_factory_.GetWeakPtr())); | 211 weak_ptr_factory_.GetWeakPtr())); |
| 210 } | 212 } |
| 211 | 213 |
| 212 virtual void GetFileInfoById(const std::string& storage_handle, | 214 virtual void GetFileInfoById(const std::string& storage_handle, |
| 213 uint32 file_id, | 215 uint32 file_id, |
| 214 const GetFileInfoCallback& callback) OVERRIDE { | 216 const GetFileInfoCallback& callback) OVERRIDE { |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 216 if (!ContainsKey(handles_, storage_handle)) { | 218 if (!ContainsKey(handles_, storage_handle)) { |
| 217 callback.Run(FileEntry(), true); | 219 callback.Run(MtpFileEntry(), true); |
| 218 return; | 220 return; |
| 219 } | 221 } |
| 220 get_file_info_callbacks_.push(callback); | 222 get_file_info_callbacks_.push(callback); |
| 221 mtp_client_->GetFileInfoById( | 223 mtp_client_->GetFileInfoById( |
| 222 storage_handle, | 224 storage_handle, |
| 223 file_id, | 225 file_id, |
| 224 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, | 226 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, |
| 225 weak_ptr_factory_.GetWeakPtr()), | 227 weak_ptr_factory_.GetWeakPtr()), |
| 226 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, | 228 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, |
| 227 weak_ptr_factory_.GetWeakPtr())); | 229 weak_ptr_factory_.GetWeakPtr())); |
| 228 } | 230 } |
| 229 | 231 |
| 230 private: | 232 private: |
| 231 // Map of storage names to storage info. | 233 // Map of storage names to storage info. |
| 232 typedef std::map<std::string, StorageInfo> StorageInfoMap; | 234 typedef std::map<std::string, MtpStorageInfo> StorageInfoMap; |
| 233 // Callback queues - DBus communication is in-order, thus callbacks are | 235 // Callback queues - DBus communication is in-order, thus callbacks are |
| 234 // received in the same order as the requests. | 236 // received in the same order as the requests. |
| 235 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue; | 237 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue; |
| 236 // (callback, handle) | 238 // (callback, handle) |
| 237 typedef std::queue<std::pair<CloseStorageCallback, std::string> | 239 typedef std::queue<std::pair<CloseStorageCallback, std::string> |
| 238 > CloseStorageCallbackQueue; | 240 > CloseStorageCallbackQueue; |
| 239 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue; | 241 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue; |
| 240 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue; | 242 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue; |
| 241 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; | 243 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; |
| 242 | 244 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 267 void OnEnumerateStorage(const std::vector<std::string>& storage_names) { | 269 void OnEnumerateStorage(const std::vector<std::string>& storage_names) { |
| 268 for (size_t i = 0; i < storage_names.size(); ++i) { | 270 for (size_t i = 0; i < storage_names.size(); ++i) { |
| 269 mtp_client_->GetStorageInfo( | 271 mtp_client_->GetStorageInfo( |
| 270 storage_names[i], | 272 storage_names[i], |
| 271 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo, | 273 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo, |
| 272 weak_ptr_factory_.GetWeakPtr()), | 274 weak_ptr_factory_.GetWeakPtr()), |
| 273 base::Bind(&base::DoNothing)); | 275 base::Bind(&base::DoNothing)); |
| 274 } | 276 } |
| 275 } | 277 } |
| 276 | 278 |
| 277 void OnGetStorageInfo(const StorageInfo& storage_info) { | 279 void OnGetStorageInfo(const MtpStorageInfo& storage_info) { |
| 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 279 const std::string& storage_name = storage_info.storage_name(); | 281 const std::string& storage_name = storage_info.storage_name(); |
| 280 if (ContainsKey(storage_info_map_, storage_name)) { | 282 if (ContainsKey(storage_info_map_, storage_name)) { |
| 281 // This should not happen, since MediaTransferProtocolManagerImpl should | 283 // This should not happen, since MediaTransferProtocolManagerImpl should |
| 282 // only call EnumerateStorage() once, which populates |storage_info_map_| | 284 // only call EnumerateStorage() once, which populates |storage_info_map_| |
| 283 // with the already-attached devices. | 285 // with the already-attached devices. |
| 284 // After that, all incoming signals are either for new storage | 286 // After that, all incoming signals are either for new storage |
| 285 // attachments, which should not be in |storage_info_map_|, or for | 287 // attachments, which should not be in |storage_info_map_|, or for |
| 286 // storage detachements, which do not add to |storage_info_map_|. | 288 // storage detachements, which do not add to |storage_info_map_|. |
| 287 NOTREACHED(); | 289 NOTREACHED(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 close_storage_callbacks_.front().first.Run(true); | 323 close_storage_callbacks_.front().first.Run(true); |
| 322 } | 324 } |
| 323 close_storage_callbacks_.pop(); | 325 close_storage_callbacks_.pop(); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void OnCloseStorageError() { | 328 void OnCloseStorageError() { |
| 327 close_storage_callbacks_.front().first.Run(true); | 329 close_storage_callbacks_.front().first.Run(true); |
| 328 close_storage_callbacks_.pop(); | 330 close_storage_callbacks_.pop(); |
| 329 } | 331 } |
| 330 | 332 |
| 331 void OnReadDirectory(const std::vector<FileEntry>& file_entries) { | 333 void OnReadDirectory(const std::vector<MtpFileEntry>& file_entries) { |
| 332 read_directory_callbacks_.front().Run(file_entries, false); | 334 read_directory_callbacks_.front().Run(file_entries, false); |
| 333 read_directory_callbacks_.pop(); | 335 read_directory_callbacks_.pop(); |
| 334 } | 336 } |
| 335 | 337 |
| 336 void OnReadDirectoryError() { | 338 void OnReadDirectoryError() { |
| 337 read_directory_callbacks_.front().Run(std::vector<FileEntry>(), true); | 339 read_directory_callbacks_.front().Run(std::vector<MtpFileEntry>(), true); |
| 338 read_directory_callbacks_.pop(); | 340 read_directory_callbacks_.pop(); |
| 339 } | 341 } |
| 340 | 342 |
| 341 void OnReadFile(const std::string& data) { | 343 void OnReadFile(const std::string& data) { |
| 342 read_file_callbacks_.front().Run(data, false); | 344 read_file_callbacks_.front().Run(data, false); |
| 343 read_file_callbacks_.pop(); | 345 read_file_callbacks_.pop(); |
| 344 } | 346 } |
| 345 | 347 |
| 346 void OnReadFileError() { | 348 void OnReadFileError() { |
| 347 read_file_callbacks_.front().Run(std::string(), true); | 349 read_file_callbacks_.front().Run(std::string(), true); |
| 348 read_file_callbacks_.pop(); | 350 read_file_callbacks_.pop(); |
| 349 } | 351 } |
| 350 | 352 |
| 351 void OnGetFileInfo(const FileEntry& entry) { | 353 void OnGetFileInfo(const MtpFileEntry& entry) { |
| 352 get_file_info_callbacks_.front().Run(entry, false); | 354 get_file_info_callbacks_.front().Run(entry, false); |
| 353 get_file_info_callbacks_.pop(); | 355 get_file_info_callbacks_.pop(); |
| 354 } | 356 } |
| 355 | 357 |
| 356 void OnGetFileInfoError() { | 358 void OnGetFileInfoError() { |
| 357 get_file_info_callbacks_.front().Run(FileEntry(), true); | 359 get_file_info_callbacks_.front().Run(MtpFileEntry(), true); |
| 358 get_file_info_callbacks_.pop(); | 360 get_file_info_callbacks_.pop(); |
| 359 } | 361 } |
| 360 | 362 |
| 361 // Mtpd DBus client. | 363 // Mtpd DBus client. |
| 362 MediaTransferProtocolDaemonClient* mtp_client_; | 364 MediaTransferProtocolDaemonClient* mtp_client_; |
| 363 | 365 |
| 364 // Device attachment / detachment observers. | 366 // Device attachment / detachment observers. |
| 365 ObserverList<Observer> observers_; | 367 ObserverList<Observer> observers_; |
| 366 | 368 |
| 367 base::WeakPtrFactory<MediaTransferProtocolManagerImpl> weak_ptr_factory_; | 369 base::WeakPtrFactory<MediaTransferProtocolManagerImpl> weak_ptr_factory_; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; | 410 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; |
| 409 } | 411 } |
| 410 | 412 |
| 411 // static | 413 // static |
| 412 MediaTransferProtocolManager* MediaTransferProtocolManager::GetInstance() { | 414 MediaTransferProtocolManager* MediaTransferProtocolManager::GetInstance() { |
| 413 return g_media_transfer_protocol_manager; | 415 return g_media_transfer_protocol_manager; |
| 414 } | 416 } |
| 415 | 417 |
| 416 } // namespace mtp | 418 } // namespace mtp |
| 417 } // namespace chromeos | 419 } // namespace chromeos |
| OLD | NEW |