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 <map> | 7 #include <map> |
8 #include <queue> | 8 #include <queue> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #else | 25 #else |
26 #include "dbus/bus.h" | 26 #include "dbus/bus.h" |
27 #endif | 27 #endif |
28 | 28 |
29 namespace device { | 29 namespace device { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; | 33 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; |
34 | 34 |
35 // static | |
36 void Shutdown() { | |
37 DCHECK(g_media_transfer_protocol_manager); | |
38 g_media_transfer_protocol_manager = NULL; | |
39 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; | |
40 } | |
41 | |
35 // The MediaTransferProtocolManager implementation. | 42 // The MediaTransferProtocolManager implementation. |
36 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { | 43 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
37 public: | 44 public: |
38 MediaTransferProtocolManagerImpl( | 45 explicit MediaTransferProtocolManagerImpl( |
39 scoped_refptr<base::MessageLoopProxy> loop_proxy) | 46 scoped_refptr<base::MessageLoopProxy> loop_proxy) |
40 : weak_ptr_factory_(this) { | 47 : weak_ptr_factory_(this) { |
41 dbus::Bus* bus = NULL; | 48 dbus::Bus* bus = NULL; |
42 #if defined(OS_CHROMEOS) | 49 #if defined(OS_CHROMEOS) |
43 DCHECK(!loop_proxy.get()); | 50 DCHECK(!loop_proxy.get()); |
44 chromeos::DBusThreadManager* dbus_thread_manager = | 51 chromeos::DBusThreadManager* dbus_thread_manager = |
45 chromeos::DBusThreadManager::Get(); | 52 chromeos::DBusThreadManager::Get(); |
46 bus = dbus_thread_manager->GetSystemBus(); | 53 bus = dbus_thread_manager->GetSystemBus(); |
47 if (!bus) | 54 if (!bus) |
48 return; | 55 return; |
(...skipping 15 matching lines...) Expand all Loading... | |
64 mtp_client_->SetUpConnections( | 71 mtp_client_->SetUpConnections( |
65 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged, | 72 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged, |
66 weak_ptr_factory_.GetWeakPtr())); | 73 weak_ptr_factory_.GetWeakPtr())); |
67 mtp_client_->EnumerateStorages( | 74 mtp_client_->EnumerateStorages( |
68 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorages, | 75 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorages, |
69 weak_ptr_factory_.GetWeakPtr()), | 76 weak_ptr_factory_.GetWeakPtr()), |
70 base::Bind(&base::DoNothing)); | 77 base::Bind(&base::DoNothing)); |
71 } | 78 } |
72 | 79 |
73 virtual ~MediaTransferProtocolManagerImpl() { | 80 virtual ~MediaTransferProtocolManagerImpl() { |
81 Shutdown(); | |
74 } | 82 } |
75 | 83 |
76 // MediaTransferProtocolManager override. | 84 // MediaTransferProtocolManager override. |
77 virtual void AddObserver(Observer* observer) OVERRIDE { | 85 virtual void AddObserver(Observer* observer) OVERRIDE { |
78 observers_.AddObserver(observer); | 86 observers_.AddObserver(observer); |
79 } | 87 } |
80 | 88 |
81 // MediaTransferProtocolManager override. | 89 // MediaTransferProtocolManager override. |
82 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 90 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
83 observers_.RemoveObserver(observer); | 91 observers_.RemoveObserver(observer); |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 CloseStorageCallbackQueue close_storage_callbacks_; | 432 CloseStorageCallbackQueue close_storage_callbacks_; |
425 ReadDirectoryCallbackQueue read_directory_callbacks_; | 433 ReadDirectoryCallbackQueue read_directory_callbacks_; |
426 ReadFileCallbackQueue read_file_callbacks_; | 434 ReadFileCallbackQueue read_file_callbacks_; |
427 GetFileInfoCallbackQueue get_file_info_callbacks_; | 435 GetFileInfoCallbackQueue get_file_info_callbacks_; |
428 | 436 |
429 base::ThreadChecker thread_checker_; | 437 base::ThreadChecker thread_checker_; |
430 | 438 |
431 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl); | 439 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl); |
432 }; | 440 }; |
433 | 441 |
442 // Dummy MediaTransferProtocolManager implementation for testing. | |
443 class MediaTransferProtocolManagerDummyImpl | |
Greg Billock
2013/04/10 18:27:25
If we have this, can we stick it in a test-util so
Lei Zhang
2013/04/11 06:54:04
Done.
| |
444 : public MediaTransferProtocolManager { | |
445 public: | |
446 MediaTransferProtocolManagerDummyImpl() {} | |
447 | |
448 virtual ~MediaTransferProtocolManagerDummyImpl() { | |
449 Shutdown(); | |
450 } | |
451 | |
452 // MediaTransferProtocolManager overrides. | |
453 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
454 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
455 virtual const std::vector<std::string> GetStorages() const OVERRIDE { | |
456 std::vector<std::string> storages; | |
457 return storages; | |
458 } | |
459 virtual const MtpStorageInfo* GetStorageInfo( | |
460 const std::string& storage_name) const OVERRIDE { | |
461 return NULL; | |
462 } | |
463 virtual void OpenStorage(const std::string& storage_name, | |
464 const std::string& mode, | |
465 const OpenStorageCallback& callback) OVERRIDE { | |
466 callback.Run("", true); | |
467 } | |
468 virtual void CloseStorage(const std::string& storage_handle, | |
469 const CloseStorageCallback& callback) OVERRIDE { | |
470 callback.Run(true); | |
471 } | |
472 virtual void ReadDirectoryByPath( | |
473 const std::string& storage_handle, | |
474 const std::string& path, | |
475 const ReadDirectoryCallback& callback) OVERRIDE { | |
476 callback.Run(std::vector<MtpFileEntry>(), true); | |
477 } | |
478 virtual void ReadDirectoryById( | |
479 const std::string& storage_handle, | |
480 uint32 file_id, | |
481 const ReadDirectoryCallback& callback) OVERRIDE { | |
482 callback.Run(std::vector<MtpFileEntry>(), true); | |
483 } | |
484 virtual void ReadFileChunkByPath(const std::string& storage_handle, | |
485 const std::string& path, | |
486 uint32 offset, | |
487 uint32 count, | |
488 const ReadFileCallback& callback) OVERRIDE { | |
489 callback.Run(std::string(), true); | |
490 } | |
491 virtual void ReadFileChunkById(const std::string& storage_handle, | |
492 uint32 file_id, | |
493 uint32 offset, | |
494 uint32 count, | |
495 const ReadFileCallback& callback) OVERRIDE { | |
496 callback.Run(std::string(), true); | |
497 } | |
498 virtual void GetFileInfoByPath(const std::string& storage_handle, | |
499 const std::string& path, | |
500 const GetFileInfoCallback& callback) OVERRIDE { | |
501 callback.Run(MtpFileEntry(), true); | |
502 } | |
503 virtual void GetFileInfoById(const std::string& storage_handle, | |
504 uint32 file_id, | |
505 const GetFileInfoCallback& callback) OVERRIDE { | |
506 callback.Run(MtpFileEntry(), true); | |
507 } | |
508 | |
509 private: | |
510 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerDummyImpl); | |
511 }; | |
512 | |
434 } // namespace | 513 } // namespace |
435 | 514 |
436 // static | 515 // static |
437 void MediaTransferProtocolManager::Initialize( | 516 MediaTransferProtocolManager* MediaTransferProtocolManager::Initialize( |
438 scoped_refptr<base::MessageLoopProxy> loop_proxy) { | 517 scoped_refptr<base::MessageLoopProxy> loop_proxy, bool is_dummy) { |
439 if (g_media_transfer_protocol_manager) { | 518 DCHECK(!g_media_transfer_protocol_manager); |
440 LOG(WARNING) << "MediaTransferProtocolManager was already initialized"; | 519 |
441 return; | 520 if (is_dummy) { |
521 g_media_transfer_protocol_manager = | |
522 new MediaTransferProtocolManagerDummyImpl(); | |
523 } else { | |
524 g_media_transfer_protocol_manager = | |
525 new MediaTransferProtocolManagerImpl(loop_proxy); | |
442 } | 526 } |
443 g_media_transfer_protocol_manager = | |
444 new MediaTransferProtocolManagerImpl(loop_proxy); | |
445 VLOG(1) << "MediaTransferProtocolManager initialized"; | 527 VLOG(1) << "MediaTransferProtocolManager initialized"; |
446 } | |
447 | 528 |
448 // static | |
449 void MediaTransferProtocolManager::Shutdown() { | |
450 if (!g_media_transfer_protocol_manager) { | |
451 LOG(WARNING) << "MediaTransferProtocolManager::Shutdown() called with " | |
452 << "NULL manager"; | |
453 return; | |
454 } | |
455 delete g_media_transfer_protocol_manager; | |
456 g_media_transfer_protocol_manager = NULL; | |
457 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; | |
458 } | |
459 | |
460 // static | |
461 MediaTransferProtocolManager* MediaTransferProtocolManager::GetInstance() { | |
462 return g_media_transfer_protocol_manager; | 529 return g_media_transfer_protocol_manager; |
463 } | 530 } |
464 | 531 |
465 } // namespace device | 532 } // namespace device |
OLD | NEW |