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

Side by Side Diff: device/media_transfer_protocol/media_transfer_protocol_manager.cc

Issue 982283002: Implement DeleteFile and DeleteDirectory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit_tests. Created 5 years, 9 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 "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>
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 const ReadDirectoryCallback& callback) override { 165 const ReadDirectoryCallback& callback) override {
166 DCHECK(thread_checker_.CalledOnValidThread()); 166 DCHECK(thread_checker_.CalledOnValidThread());
167 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { 167 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) {
168 callback.Run(std::vector<MtpFileEntry>(), 168 callback.Run(std::vector<MtpFileEntry>(),
169 false /* no more entries */, 169 false /* no more entries */,
170 true /* error */); 170 true /* error */);
171 return; 171 return;
172 } 172 }
173 read_directory_callbacks_.push(callback); 173 read_directory_callbacks_.push(callback);
174 mtp_client_->ReadDirectoryEntryIds( 174 mtp_client_->ReadDirectoryEntryIds(
175 storage_handle, 175 storage_handle, file_id,
176 file_id, 176 base::Bind(&MediaTransferProtocolManagerImpl::
177 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryEntryIds, 177 OnReadDirectoryEntryIdsToReadDirectory,
178 weak_ptr_factory_.GetWeakPtr(), 178 weak_ptr_factory_.GetWeakPtr(), storage_handle),
179 storage_handle),
180 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, 179 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError,
181 weak_ptr_factory_.GetWeakPtr())); 180 weak_ptr_factory_.GetWeakPtr()));
182 } 181 }
183 182
183 void ReadDirectoryEntryIds(
184 const std::string& storage_handle,
185 const uint32 file_id,
186 const ReadDirectoryEntryIdsCallback& callback) override {
187 DCHECK(thread_checker_.CalledOnValidThread());
188 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) {
189 callback.Run(std::vector<uint32>(), true /* error */);
190 return;
191 }
192 read_directory_entry_ids_callbacks_.push(callback);
193 mtp_client_->ReadDirectoryEntryIds(
194 storage_handle, file_id,
195 base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryEntryIds,
196 weak_ptr_factory_.GetWeakPtr()),
197 base::Bind(
198 &MediaTransferProtocolManagerImpl::OnReadDirectoryEntryIdsError,
199 weak_ptr_factory_.GetWeakPtr()));
200 }
201
184 // MediaTransferProtocolManager override. 202 // MediaTransferProtocolManager override.
185 void ReadFileChunk(const std::string& storage_handle, 203 void ReadFileChunk(const std::string& storage_handle,
186 uint32 file_id, 204 uint32 file_id,
187 uint32 offset, 205 uint32 offset,
188 uint32 count, 206 uint32 count,
189 const ReadFileCallback& callback) override { 207 const ReadFileCallback& callback) override {
190 DCHECK(thread_checker_.CalledOnValidThread()); 208 DCHECK(thread_checker_.CalledOnValidThread());
191 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { 209 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) {
192 callback.Run(std::string(), true); 210 callback.Run(std::string(), true);
193 return; 211 return;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 253 }
236 copy_file_from_local_callbacks_.push(callback); 254 copy_file_from_local_callbacks_.push(callback);
237 mtp_client_->CopyFileFromLocal( 255 mtp_client_->CopyFileFromLocal(
238 storage_handle, source_file_descriptor, parent_id, file_name, 256 storage_handle, source_file_descriptor, parent_id, file_name,
239 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocal, 257 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocal,
240 weak_ptr_factory_.GetWeakPtr()), 258 weak_ptr_factory_.GetWeakPtr()),
241 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocalError, 259 base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocalError,
242 weak_ptr_factory_.GetWeakPtr())); 260 weak_ptr_factory_.GetWeakPtr()));
243 } 261 }
244 262
263 void DeleteObject(const std::string& storage_handle,
264 const uint32 object_id,
265 const DeleteObjectCallback& callback) override {
266 DCHECK(thread_checker_.CalledOnValidThread());
267 if (!ContainsKey(handles_, storage_handle) || !mtp_client_) {
268 callback.Run(true /* error */);
269 return;
270 }
271 delete_object_callbacks_.push(callback);
272 mtp_client_->DeleteObject(
273 storage_handle, object_id,
274 base::Bind(&MediaTransferProtocolManagerImpl::OnDeleteObject,
275 weak_ptr_factory_.GetWeakPtr()),
276 base::Bind(&MediaTransferProtocolManagerImpl::OnDeleteObjectError,
277 weak_ptr_factory_.GetWeakPtr()));
278 }
279
245 private: 280 private:
246 // Map of storage names to storage info. 281 // Map of storage names to storage info.
247 typedef std::map<std::string, MtpStorageInfo> StorageInfoMap; 282 typedef std::map<std::string, MtpStorageInfo> StorageInfoMap;
248 // Callback queues - DBus communication is in-order, thus callbacks are 283 // Callback queues - DBus communication is in-order, thus callbacks are
249 // received in the same order as the requests. 284 // received in the same order as the requests.
250 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue; 285 typedef std::queue<OpenStorageCallback> OpenStorageCallbackQueue;
251 // (callback, handle) 286 // (callback, handle)
252 typedef std::queue<std::pair<CloseStorageCallback, std::string> 287 typedef std::queue<std::pair<CloseStorageCallback, std::string>
253 > CloseStorageCallbackQueue; 288 > CloseStorageCallbackQueue;
254 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue; 289 typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue;
290 typedef std::queue<ReadDirectoryEntryIdsCallback>
291 ReadDirectoryEntryIdsCallbackQueue;
255 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue; 292 typedef std::queue<ReadFileCallback> ReadFileCallbackQueue;
256 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; 293 typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue;
257 typedef std::queue<CopyFileFromLocalCallback> CopyFileFromLocalCallbackQueue; 294 typedef std::queue<CopyFileFromLocalCallback> CopyFileFromLocalCallbackQueue;
295 typedef std::queue<DeleteObjectCallback> DeleteObjectCallbackQueue;
258 296
259 void OnStorageAttached(const std::string& storage_name) { 297 void OnStorageAttached(const std::string& storage_name) {
260 DCHECK(thread_checker_.CalledOnValidThread()); 298 DCHECK(thread_checker_.CalledOnValidThread());
261 mtp_client_->GetStorageInfo( 299 mtp_client_->GetStorageInfo(
262 storage_name, 300 storage_name,
263 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo, 301 base::Bind(&MediaTransferProtocolManagerImpl::OnGetStorageInfo,
264 weak_ptr_factory_.GetWeakPtr()), 302 weak_ptr_factory_.GetWeakPtr()),
265 base::Bind(&base::DoNothing)); 303 base::Bind(&base::DoNothing));
266 } 304 }
267 305
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 } 388 }
351 close_storage_callbacks_.pop(); 389 close_storage_callbacks_.pop();
352 } 390 }
353 391
354 void OnCloseStorageError() { 392 void OnCloseStorageError() {
355 DCHECK(thread_checker_.CalledOnValidThread()); 393 DCHECK(thread_checker_.CalledOnValidThread());
356 close_storage_callbacks_.front().first.Run(true); 394 close_storage_callbacks_.front().first.Run(true);
357 close_storage_callbacks_.pop(); 395 close_storage_callbacks_.pop();
358 } 396 }
359 397
360 void OnReadDirectoryEntryIds(const std::string& storage_handle, 398 void OnReadDirectoryEntryIdsToReadDirectory(
361 const std::vector<uint32>& file_ids) { 399 const std::string& storage_handle,
400 const std::vector<uint32>& file_ids) {
362 DCHECK(thread_checker_.CalledOnValidThread()); 401 DCHECK(thread_checker_.CalledOnValidThread());
363 402
364 if (file_ids.empty()) { 403 if (file_ids.empty()) {
365 OnGotDirectoryEntries(storage_handle, 404 OnGotDirectoryEntries(storage_handle,
366 file_ids, 405 file_ids,
367 kInitialOffset, 406 kInitialOffset,
368 file_ids, 407 file_ids,
369 std::vector<MtpFileEntry>()); 408 std::vector<MtpFileEntry>());
370 return; 409 return;
371 } 410 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 } 475 }
437 476
438 void OnReadDirectoryError() { 477 void OnReadDirectoryError() {
439 DCHECK(thread_checker_.CalledOnValidThread()); 478 DCHECK(thread_checker_.CalledOnValidThread());
440 read_directory_callbacks_.front().Run(std::vector<MtpFileEntry>(), 479 read_directory_callbacks_.front().Run(std::vector<MtpFileEntry>(),
441 false /* no more entries */, 480 false /* no more entries */,
442 true /* error */); 481 true /* error */);
443 read_directory_callbacks_.pop(); 482 read_directory_callbacks_.pop();
444 } 483 }
445 484
485 void OnReadDirectoryEntryIds(const std::vector<uint32>& file_ids) {
486 DCHECK(thread_checker_.CalledOnValidThread());
487 read_directory_entry_ids_callbacks_.front().Run(file_ids,
488 false /* no error */);
489 read_directory_entry_ids_callbacks_.pop();
490 }
491
492 void OnReadDirectoryEntryIdsError() {
493 DCHECK(thread_checker_.CalledOnValidThread());
494 read_directory_entry_ids_callbacks_.front().Run(std::vector<uint32>(),
495 true /* error */);
496 read_directory_entry_ids_callbacks_.pop();
497 }
498
446 void OnReadFile(const std::string& data) { 499 void OnReadFile(const std::string& data) {
447 DCHECK(thread_checker_.CalledOnValidThread()); 500 DCHECK(thread_checker_.CalledOnValidThread());
448 read_file_callbacks_.front().Run(data, false); 501 read_file_callbacks_.front().Run(data, false);
449 read_file_callbacks_.pop(); 502 read_file_callbacks_.pop();
450 } 503 }
451 504
452 void OnReadFileError() { 505 void OnReadFileError() {
453 DCHECK(thread_checker_.CalledOnValidThread()); 506 DCHECK(thread_checker_.CalledOnValidThread());
454 read_file_callbacks_.front().Run(std::string(), true); 507 read_file_callbacks_.front().Run(std::string(), true);
455 read_file_callbacks_.pop(); 508 read_file_callbacks_.pop();
(...skipping 20 matching lines...) Expand all
476 copy_file_from_local_callbacks_.front().Run(false /* no error */); 529 copy_file_from_local_callbacks_.front().Run(false /* no error */);
477 copy_file_from_local_callbacks_.pop(); 530 copy_file_from_local_callbacks_.pop();
478 } 531 }
479 532
480 void OnCopyFileFromLocalError() { 533 void OnCopyFileFromLocalError() {
481 DCHECK(thread_checker_.CalledOnValidThread()); 534 DCHECK(thread_checker_.CalledOnValidThread());
482 copy_file_from_local_callbacks_.front().Run(true /* error */); 535 copy_file_from_local_callbacks_.front().Run(true /* error */);
483 copy_file_from_local_callbacks_.pop(); 536 copy_file_from_local_callbacks_.pop();
484 } 537 }
485 538
539 void OnDeleteObject() {
540 DCHECK(thread_checker_.CalledOnValidThread());
541 delete_object_callbacks_.front().Run(false /* no error */);
542 delete_object_callbacks_.pop();
543 }
544
545 void OnDeleteObjectError() {
546 DCHECK(thread_checker_.CalledOnValidThread());
547 delete_object_callbacks_.front().Run(true /* error */);
548 delete_object_callbacks_.pop();
549 }
550
486 // Get the Bus object used to communicate with mtpd. 551 // Get the Bus object used to communicate with mtpd.
487 dbus::Bus* GetBus() { 552 dbus::Bus* GetBus() {
488 DCHECK(thread_checker_.CalledOnValidThread()); 553 DCHECK(thread_checker_.CalledOnValidThread());
489 #if defined(OS_CHROMEOS) 554 #if defined(OS_CHROMEOS)
490 return chromeos::DBusThreadManager::Get()->GetSystemBus(); 555 return chromeos::DBusThreadManager::Get()->GetSystemBus();
491 #else 556 #else
492 return session_bus_.get(); 557 return session_bus_.get();
493 #endif 558 #endif
494 } 559 }
495 560
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 std::set<std::string> handles_; 620 std::set<std::string> handles_;
556 621
557 dbus::Bus::GetServiceOwnerCallback mtpd_owner_changed_callback_; 622 dbus::Bus::GetServiceOwnerCallback mtpd_owner_changed_callback_;
558 623
559 std::string current_mtpd_owner_; 624 std::string current_mtpd_owner_;
560 625
561 // Queued callbacks. 626 // Queued callbacks.
562 OpenStorageCallbackQueue open_storage_callbacks_; 627 OpenStorageCallbackQueue open_storage_callbacks_;
563 CloseStorageCallbackQueue close_storage_callbacks_; 628 CloseStorageCallbackQueue close_storage_callbacks_;
564 ReadDirectoryCallbackQueue read_directory_callbacks_; 629 ReadDirectoryCallbackQueue read_directory_callbacks_;
630 ReadDirectoryEntryIdsCallbackQueue read_directory_entry_ids_callbacks_;
565 ReadFileCallbackQueue read_file_callbacks_; 631 ReadFileCallbackQueue read_file_callbacks_;
566 GetFileInfoCallbackQueue get_file_info_callbacks_; 632 GetFileInfoCallbackQueue get_file_info_callbacks_;
567 CopyFileFromLocalCallbackQueue copy_file_from_local_callbacks_; 633 CopyFileFromLocalCallbackQueue copy_file_from_local_callbacks_;
634 DeleteObjectCallbackQueue delete_object_callbacks_;
568 635
569 base::ThreadChecker thread_checker_; 636 base::ThreadChecker thread_checker_;
570 637
571 base::WeakPtrFactory<MediaTransferProtocolManagerImpl> weak_ptr_factory_; 638 base::WeakPtrFactory<MediaTransferProtocolManagerImpl> weak_ptr_factory_;
572 639
573 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl); 640 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl);
574 }; 641 };
575 642
576 } // namespace 643 } // namespace
577 644
578 // static 645 // static
579 MediaTransferProtocolManager* MediaTransferProtocolManager::Initialize( 646 MediaTransferProtocolManager* MediaTransferProtocolManager::Initialize(
580 scoped_refptr<base::SequencedTaskRunner> task_runner) { 647 scoped_refptr<base::SequencedTaskRunner> task_runner) {
581 DCHECK(!g_media_transfer_protocol_manager); 648 DCHECK(!g_media_transfer_protocol_manager);
582 649
583 g_media_transfer_protocol_manager = 650 g_media_transfer_protocol_manager =
584 new MediaTransferProtocolManagerImpl(task_runner); 651 new MediaTransferProtocolManagerImpl(task_runner);
585 VLOG(1) << "MediaTransferProtocolManager initialized"; 652 VLOG(1) << "MediaTransferProtocolManager initialized";
586 653
587 return g_media_transfer_protocol_manager; 654 return g_media_transfer_protocol_manager;
588 } 655 }
589 656
590 } // namespace device 657 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698