| 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 "device/media_transfer_protocol/media_transfer_protocol_manager.h" | 5 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 18 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 19 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 20 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "build/build_config.h" |
| 21 #include "dbus/bus.h" | 23 #include "dbus/bus.h" |
| 22 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" | 24 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" |
| 23 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" | 25 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" |
| 24 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" | 26 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" |
| 25 #include "third_party/cros_system_api/dbus/service_constants.h" | 27 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 26 | 28 |
| 27 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 28 #include "chromeos/dbus/dbus_thread_manager.h" | 30 #include "chromeos/dbus/dbus_thread_manager.h" |
| 29 #endif | 31 #endif |
| 30 | 32 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 close_storage_callbacks_.push(std::make_pair(callback, storage_handle)); | 176 close_storage_callbacks_.push(std::make_pair(callback, storage_handle)); |
| 175 mtp_client_->CloseStorage( | 177 mtp_client_->CloseStorage( |
| 176 storage_handle, | 178 storage_handle, |
| 177 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorage, | 179 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorage, |
| 178 weak_ptr_factory_.GetWeakPtr()), | 180 weak_ptr_factory_.GetWeakPtr()), |
| 179 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorageError, | 181 base::Bind(&MediaTransferProtocolManagerImpl::OnCloseStorageError, |
| 180 weak_ptr_factory_.GetWeakPtr())); | 182 weak_ptr_factory_.GetWeakPtr())); |
| 181 } | 183 } |
| 182 | 184 |
| 183 void CreateDirectory(const std::string& storage_handle, | 185 void CreateDirectory(const std::string& storage_handle, |
| 184 const uint32 parent_id, | 186 const uint32_t parent_id, |
| 185 const std::string& directory_name, | 187 const std::string& directory_name, |
| 186 const CreateDirectoryCallback& callback) override { | 188 const CreateDirectoryCallback& callback) override { |
| 187 DCHECK(thread_checker_.CalledOnValidThread()); | 189 DCHECK(thread_checker_.CalledOnValidThread()); |
| 188 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 190 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 189 callback.Run(true /* error */); | 191 callback.Run(true /* error */); |
| 190 return; | 192 return; |
| 191 } | 193 } |
| 192 create_directory_callbacks_.push(callback); | 194 create_directory_callbacks_.push(callback); |
| 193 mtp_client_->CreateDirectory( | 195 mtp_client_->CreateDirectory( |
| 194 storage_handle, parent_id, directory_name, | 196 storage_handle, parent_id, directory_name, |
| 195 base::Bind(&MediaTransferProtocolManagerImpl::OnCreateDirectory, | 197 base::Bind(&MediaTransferProtocolManagerImpl::OnCreateDirectory, |
| 196 weak_ptr_factory_.GetWeakPtr()), | 198 weak_ptr_factory_.GetWeakPtr()), |
| 197 base::Bind(&MediaTransferProtocolManagerImpl::OnCreateDirectoryError, | 199 base::Bind(&MediaTransferProtocolManagerImpl::OnCreateDirectoryError, |
| 198 weak_ptr_factory_.GetWeakPtr())); | 200 weak_ptr_factory_.GetWeakPtr())); |
| 199 } | 201 } |
| 200 | 202 |
| 201 // MediaTransferProtocolManager override. | 203 // MediaTransferProtocolManager override. |
| 202 void ReadDirectory(const std::string& storage_handle, | 204 void ReadDirectory(const std::string& storage_handle, |
| 203 const uint32 file_id, | 205 const uint32_t file_id, |
| 204 const size_t max_size, | 206 const size_t max_size, |
| 205 const ReadDirectoryCallback& callback) override { | 207 const ReadDirectoryCallback& callback) override { |
| 206 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 207 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 209 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 208 callback.Run(std::vector<MtpFileEntry>(), | 210 callback.Run(std::vector<MtpFileEntry>(), |
| 209 false /* no more entries */, | 211 false /* no more entries */, |
| 210 true /* error */); | 212 true /* error */); |
| 211 return; | 213 return; |
| 212 } | 214 } |
| 213 read_directory_callbacks_.push(callback); | 215 read_directory_callbacks_.push(callback); |
| 214 mtp_client_->ReadDirectoryEntryIds( | 216 mtp_client_->ReadDirectoryEntryIds( |
| 215 storage_handle, file_id, | 217 storage_handle, file_id, |
| 216 base::Bind(&MediaTransferProtocolManagerImpl:: | 218 base::Bind(&MediaTransferProtocolManagerImpl:: |
| 217 OnReadDirectoryEntryIdsToReadDirectory, | 219 OnReadDirectoryEntryIdsToReadDirectory, |
| 218 weak_ptr_factory_.GetWeakPtr(), storage_handle, max_size), | 220 weak_ptr_factory_.GetWeakPtr(), storage_handle, max_size), |
| 219 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, | 221 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, |
| 220 weak_ptr_factory_.GetWeakPtr())); | 222 weak_ptr_factory_.GetWeakPtr())); |
| 221 } | 223 } |
| 222 | 224 |
| 223 // MediaTransferProtocolManager override. | 225 // MediaTransferProtocolManager override. |
| 224 void ReadFileChunk(const std::string& storage_handle, | 226 void ReadFileChunk(const std::string& storage_handle, |
| 225 uint32 file_id, | 227 uint32_t file_id, |
| 226 uint32 offset, | 228 uint32_t offset, |
| 227 uint32 count, | 229 uint32_t count, |
| 228 const ReadFileCallback& callback) override { | 230 const ReadFileCallback& callback) override { |
| 229 DCHECK(thread_checker_.CalledOnValidThread()); | 231 DCHECK(thread_checker_.CalledOnValidThread()); |
| 230 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 232 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 231 callback.Run(std::string(), true); | 233 callback.Run(std::string(), true); |
| 232 return; | 234 return; |
| 233 } | 235 } |
| 234 read_file_callbacks_.push(callback); | 236 read_file_callbacks_.push(callback); |
| 235 mtp_client_->ReadFileChunk( | 237 mtp_client_->ReadFileChunk( |
| 236 storage_handle, file_id, offset, count, | 238 storage_handle, file_id, offset, count, |
| 237 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile, | 239 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFile, |
| 238 weak_ptr_factory_.GetWeakPtr()), | 240 weak_ptr_factory_.GetWeakPtr()), |
| 239 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError, | 241 base::Bind(&MediaTransferProtocolManagerImpl::OnReadFileError, |
| 240 weak_ptr_factory_.GetWeakPtr())); | 242 weak_ptr_factory_.GetWeakPtr())); |
| 241 } | 243 } |
| 242 | 244 |
| 243 void GetFileInfo(const std::string& storage_handle, | 245 void GetFileInfo(const std::string& storage_handle, |
| 244 uint32 file_id, | 246 uint32_t file_id, |
| 245 const GetFileInfoCallback& callback) override { | 247 const GetFileInfoCallback& callback) override { |
| 246 DCHECK(thread_checker_.CalledOnValidThread()); | 248 DCHECK(thread_checker_.CalledOnValidThread()); |
| 247 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 249 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 248 callback.Run(MtpFileEntry(), true); | 250 callback.Run(MtpFileEntry(), true); |
| 249 return; | 251 return; |
| 250 } | 252 } |
| 251 std::vector<uint32> file_ids; | 253 std::vector<uint32_t> file_ids; |
| 252 file_ids.push_back(file_id); | 254 file_ids.push_back(file_id); |
| 253 get_file_info_callbacks_.push(callback); | 255 get_file_info_callbacks_.push(callback); |
| 254 mtp_client_->GetFileInfo( | 256 mtp_client_->GetFileInfo( |
| 255 storage_handle, | 257 storage_handle, |
| 256 file_ids, | 258 file_ids, |
| 257 kInitialOffset, | 259 kInitialOffset, |
| 258 file_ids.size(), | 260 file_ids.size(), |
| 259 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, | 261 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfo, |
| 260 weak_ptr_factory_.GetWeakPtr()), | 262 weak_ptr_factory_.GetWeakPtr()), |
| 261 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, | 263 base::Bind(&MediaTransferProtocolManagerImpl::OnGetFileInfoError, |
| 262 weak_ptr_factory_.GetWeakPtr())); | 264 weak_ptr_factory_.GetWeakPtr())); |
| 263 } | 265 } |
| 264 | 266 |
| 265 void RenameObject(const std::string& storage_handle, | 267 void RenameObject(const std::string& storage_handle, |
| 266 const uint32 object_id, | 268 const uint32_t object_id, |
| 267 const std::string& new_name, | 269 const std::string& new_name, |
| 268 const RenameObjectCallback& callback) override { | 270 const RenameObjectCallback& callback) override { |
| 269 DCHECK(thread_checker_.CalledOnValidThread()); | 271 DCHECK(thread_checker_.CalledOnValidThread()); |
| 270 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 272 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 271 callback.Run(true /* error */); | 273 callback.Run(true /* error */); |
| 272 return; | 274 return; |
| 273 } | 275 } |
| 274 rename_object_callbacks_.push(callback); | 276 rename_object_callbacks_.push(callback); |
| 275 mtp_client_->RenameObject( | 277 mtp_client_->RenameObject( |
| 276 storage_handle, object_id, new_name, | 278 storage_handle, object_id, new_name, |
| 277 base::Bind(&MediaTransferProtocolManagerImpl::OnRenameObject, | 279 base::Bind(&MediaTransferProtocolManagerImpl::OnRenameObject, |
| 278 weak_ptr_factory_.GetWeakPtr()), | 280 weak_ptr_factory_.GetWeakPtr()), |
| 279 base::Bind(&MediaTransferProtocolManagerImpl::OnRenameObjectError, | 281 base::Bind(&MediaTransferProtocolManagerImpl::OnRenameObjectError, |
| 280 weak_ptr_factory_.GetWeakPtr())); | 282 weak_ptr_factory_.GetWeakPtr())); |
| 281 } | 283 } |
| 282 | 284 |
| 283 void CopyFileFromLocal(const std::string& storage_handle, | 285 void CopyFileFromLocal(const std::string& storage_handle, |
| 284 const int source_file_descriptor, | 286 const int source_file_descriptor, |
| 285 const uint32 parent_id, | 287 const uint32_t parent_id, |
| 286 const std::string& file_name, | 288 const std::string& file_name, |
| 287 const CopyFileFromLocalCallback& callback) override { | 289 const CopyFileFromLocalCallback& callback) override { |
| 288 DCHECK(thread_checker_.CalledOnValidThread()); | 290 DCHECK(thread_checker_.CalledOnValidThread()); |
| 289 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 291 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 290 callback.Run(true /* error */); | 292 callback.Run(true /* error */); |
| 291 return; | 293 return; |
| 292 } | 294 } |
| 293 copy_file_from_local_callbacks_.push(callback); | 295 copy_file_from_local_callbacks_.push(callback); |
| 294 mtp_client_->CopyFileFromLocal( | 296 mtp_client_->CopyFileFromLocal( |
| 295 storage_handle, source_file_descriptor, parent_id, file_name, | 297 storage_handle, source_file_descriptor, parent_id, file_name, |
| 296 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocal, | 298 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocal, |
| 297 weak_ptr_factory_.GetWeakPtr()), | 299 weak_ptr_factory_.GetWeakPtr()), |
| 298 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocalError, | 300 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocalError, |
| 299 weak_ptr_factory_.GetWeakPtr())); | 301 weak_ptr_factory_.GetWeakPtr())); |
| 300 } | 302 } |
| 301 | 303 |
| 302 void DeleteObject(const std::string& storage_handle, | 304 void DeleteObject(const std::string& storage_handle, |
| 303 const uint32 object_id, | 305 const uint32_t object_id, |
| 304 const DeleteObjectCallback& callback) override { | 306 const DeleteObjectCallback& callback) override { |
| 305 DCHECK(thread_checker_.CalledOnValidThread()); | 307 DCHECK(thread_checker_.CalledOnValidThread()); |
| 306 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { | 308 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { |
| 307 callback.Run(true /* error */); | 309 callback.Run(true /* error */); |
| 308 return; | 310 return; |
| 309 } | 311 } |
| 310 delete_object_callbacks_.push(callback); | 312 delete_object_callbacks_.push(callback); |
| 311 mtp_client_->DeleteObject( | 313 mtp_client_->DeleteObject( |
| 312 storage_handle, object_id, | 314 storage_handle, object_id, |
| 313 base::Bind(&MediaTransferProtocolManagerImpl::OnDeleteObject, | 315 base::Bind(&MediaTransferProtocolManagerImpl::OnDeleteObject, |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 | 458 |
| 457 void OnCreateDirectoryError() { | 459 void OnCreateDirectoryError() { |
| 458 DCHECK(thread_checker_.CalledOnValidThread()); | 460 DCHECK(thread_checker_.CalledOnValidThread()); |
| 459 create_directory_callbacks_.front().Run(true /* error */); | 461 create_directory_callbacks_.front().Run(true /* error */); |
| 460 create_directory_callbacks_.pop(); | 462 create_directory_callbacks_.pop(); |
| 461 } | 463 } |
| 462 | 464 |
| 463 void OnReadDirectoryEntryIdsToReadDirectory( | 465 void OnReadDirectoryEntryIdsToReadDirectory( |
| 464 const std::string& storage_handle, | 466 const std::string& storage_handle, |
| 465 const size_t max_size, | 467 const size_t max_size, |
| 466 const std::vector<uint32>& file_ids) { | 468 const std::vector<uint32_t>& file_ids) { |
| 467 DCHECK(thread_checker_.CalledOnValidThread()); | 469 DCHECK(thread_checker_.CalledOnValidThread()); |
| 468 | 470 |
| 469 if (file_ids.empty()) { | 471 if (file_ids.empty()) { |
| 470 OnGotDirectoryEntries(storage_handle, file_ids, kInitialOffset, max_size, | 472 OnGotDirectoryEntries(storage_handle, file_ids, kInitialOffset, max_size, |
| 471 file_ids, std::vector<MtpFileEntry>()); | 473 file_ids, std::vector<MtpFileEntry>()); |
| 472 return; | 474 return; |
| 473 } | 475 } |
| 474 | 476 |
| 475 std::vector<uint32> sorted_file_ids = file_ids; | 477 std::vector<uint32_t> sorted_file_ids = file_ids; |
| 476 std::sort(sorted_file_ids.begin(), sorted_file_ids.end()); | 478 std::sort(sorted_file_ids.begin(), sorted_file_ids.end()); |
| 477 | 479 |
| 478 const size_t chunk_size = | 480 const size_t chunk_size = |
| 479 max_size == 0 ? kFileInfoToFetchChunkSize | 481 max_size == 0 ? kFileInfoToFetchChunkSize |
| 480 : std::min(max_size, kFileInfoToFetchChunkSize); | 482 : std::min(max_size, kFileInfoToFetchChunkSize); |
| 481 | 483 |
| 482 mtp_client_->GetFileInfo( | 484 mtp_client_->GetFileInfo( |
| 483 storage_handle, file_ids, kInitialOffset, chunk_size, | 485 storage_handle, file_ids, kInitialOffset, chunk_size, |
| 484 base::Bind(&MediaTransferProtocolManagerImpl::OnGotDirectoryEntries, | 486 base::Bind(&MediaTransferProtocolManagerImpl::OnGotDirectoryEntries, |
| 485 weak_ptr_factory_.GetWeakPtr(), storage_handle, file_ids, | 487 weak_ptr_factory_.GetWeakPtr(), storage_handle, file_ids, |
| 486 kInitialOffset, max_size, sorted_file_ids), | 488 kInitialOffset, max_size, sorted_file_ids), |
| 487 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, | 489 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, |
| 488 weak_ptr_factory_.GetWeakPtr())); | 490 weak_ptr_factory_.GetWeakPtr())); |
| 489 } | 491 } |
| 490 | 492 |
| 491 void OnGotDirectoryEntries(const std::string& storage_handle, | 493 void OnGotDirectoryEntries(const std::string& storage_handle, |
| 492 const std::vector<uint32>& file_ids, | 494 const std::vector<uint32_t>& file_ids, |
| 493 const size_t offset, | 495 const size_t offset, |
| 494 const size_t max_size, | 496 const size_t max_size, |
| 495 const std::vector<uint32>& sorted_file_ids, | 497 const std::vector<uint32_t>& sorted_file_ids, |
| 496 const std::vector<MtpFileEntry>& file_entries) { | 498 const std::vector<MtpFileEntry>& file_entries) { |
| 497 DCHECK(thread_checker_.CalledOnValidThread()); | 499 DCHECK(thread_checker_.CalledOnValidThread()); |
| 498 DCHECK_EQ(file_ids.size(), sorted_file_ids.size()); | 500 DCHECK_EQ(file_ids.size(), sorted_file_ids.size()); |
| 499 | 501 |
| 500 // Use |sorted_file_ids| to sanity check and make sure the results are a | 502 // Use |sorted_file_ids| to sanity check and make sure the results are a |
| 501 // subset of the requested file ids. | 503 // subset of the requested file ids. |
| 502 for (size_t i = 0; i < file_entries.size(); ++i) { | 504 for (size_t i = 0; i < file_entries.size(); ++i) { |
| 503 std::vector<uint32>::const_iterator it = | 505 std::vector<uint32_t>::const_iterator it = |
| 504 std::lower_bound(sorted_file_ids.begin(), | 506 std::lower_bound(sorted_file_ids.begin(), sorted_file_ids.end(), |
| 505 sorted_file_ids.end(), | |
| 506 file_entries[i].item_id()); | 507 file_entries[i].item_id()); |
| 507 if (it == sorted_file_ids.end()) { | 508 if (it == sorted_file_ids.end()) { |
| 508 OnReadDirectoryError(); | 509 OnReadDirectoryError(); |
| 509 return; | 510 return; |
| 510 } | 511 } |
| 511 } | 512 } |
| 512 | 513 |
| 513 const size_t directory_size = | 514 const size_t directory_size = |
| 514 max_size == 0 ? file_ids.size() : std::min(file_ids.size(), max_size); | 515 max_size == 0 ? file_ids.size() : std::min(file_ids.size(), max_size); |
| 515 size_t next_offset = directory_size; | 516 size_t next_offset = directory_size; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 DCHECK(!g_media_transfer_protocol_manager); | 711 DCHECK(!g_media_transfer_protocol_manager); |
| 711 | 712 |
| 712 g_media_transfer_protocol_manager = | 713 g_media_transfer_protocol_manager = |
| 713 new MediaTransferProtocolManagerImpl(task_runner); | 714 new MediaTransferProtocolManagerImpl(task_runner); |
| 714 VLOG(1) << "MediaTransferProtocolManager initialized"; | 715 VLOG(1) << "MediaTransferProtocolManager initialized"; |
| 715 | 716 |
| 716 return g_media_transfer_protocol_manager; | 717 return g_media_transfer_protocol_manager; |
| 717 } | 718 } |
| 718 | 719 |
| 719 } // namespace device | 720 } // namespace device |
| OLD | NEW |