| 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> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/location.h" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 16 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 18 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 20 #include "dbus/bus.h" |
| 19 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" | 21 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" |
| 20 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" | 22 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" |
| 21 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" | 23 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" |
| 24 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 22 | 25 |
| 23 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
| 24 #include "chromeos/dbus/dbus_thread_manager.h" | 27 #include "chromeos/dbus/dbus_thread_manager.h" |
| 25 #else | |
| 26 #include "dbus/bus.h" | |
| 27 #endif | 28 #endif |
| 28 | 29 |
| 29 namespace device { | 30 namespace device { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; | 34 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; |
| 34 | 35 |
| 35 // The MediaTransferProtocolManager implementation. | 36 // The MediaTransferProtocolManager implementation. |
| 36 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { | 37 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| 37 public: | 38 public: |
| 38 explicit MediaTransferProtocolManagerImpl( | 39 explicit MediaTransferProtocolManagerImpl( |
| 39 scoped_refptr<base::SequencedTaskRunner> task_runner) | 40 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 40 : weak_ptr_factory_(this) { | 41 : weak_ptr_factory_(this) { |
| 41 dbus::Bus* bus = NULL; | |
| 42 #if defined(OS_CHROMEOS) | 42 #if defined(OS_CHROMEOS) |
| 43 DCHECK(!task_runner.get()); | 43 DCHECK(!task_runner.get()); |
| 44 chromeos::DBusThreadManager* dbus_thread_manager = | |
| 45 chromeos::DBusThreadManager::Get(); | |
| 46 bus = dbus_thread_manager->GetSystemBus(); | |
| 47 if (!bus) | |
| 48 return; | |
| 49 #else | 44 #else |
| 50 DCHECK(task_runner.get()); | 45 DCHECK(task_runner.get()); |
| 51 dbus::Bus::Options options; | 46 dbus::Bus::Options options; |
| 52 options.bus_type = dbus::Bus::SYSTEM; | 47 options.bus_type = dbus::Bus::SYSTEM; |
| 53 options.connection_type = dbus::Bus::PRIVATE; | 48 options.connection_type = dbus::Bus::PRIVATE; |
| 54 options.dbus_task_runner = task_runner; | 49 options.dbus_task_runner = task_runner; |
| 55 session_bus_ = new dbus::Bus(options); | 50 session_bus_ = new dbus::Bus(options); |
| 56 bus = session_bus_.get(); | |
| 57 #endif | 51 #endif |
| 58 | 52 |
| 59 DCHECK(bus); | 53 dbus::Bus::GetServiceOwnerCallback reply_task = |
| 60 mtp_client_.reset( | 54 base::Bind(&MediaTransferProtocolManagerImpl::FinishSetupOnOriginThread, |
| 61 MediaTransferProtocolDaemonClient::Create(bus, false /* not stub */)); | 55 weak_ptr_factory_.GetWeakPtr()); |
| 62 | 56 GetBus()->GetServiceOwner(mtpd::kMtpdServiceName, reply_task); |
| 63 // Set up signals and start initializing |storage_info_map_|. | |
| 64 mtp_client_->SetUpConnections( | |
| 65 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged, | |
| 66 weak_ptr_factory_.GetWeakPtr())); | |
| 67 mtp_client_->EnumerateStorages( | |
| 68 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorages, | |
| 69 weak_ptr_factory_.GetWeakPtr()), | |
| 70 base::Bind(&base::DoNothing)); | |
| 71 } | 57 } |
| 72 | 58 |
| 73 virtual ~MediaTransferProtocolManagerImpl() { | 59 virtual ~MediaTransferProtocolManagerImpl() { |
| 74 DCHECK(g_media_transfer_protocol_manager); | 60 DCHECK(g_media_transfer_protocol_manager); |
| 75 g_media_transfer_protocol_manager = NULL; | 61 g_media_transfer_protocol_manager = NULL; |
| 76 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; | 62 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; |
| 77 } | 63 } |
| 78 | 64 |
| 79 // MediaTransferProtocolManager override. | 65 // MediaTransferProtocolManager override. |
| 80 virtual void AddObserver(Observer* observer) OVERRIDE { | 66 virtual void AddObserver(Observer* observer) OVERRIDE { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 96 storages.push_back(it->first); | 82 storages.push_back(it->first); |
| 97 } | 83 } |
| 98 return storages; | 84 return storages; |
| 99 } | 85 } |
| 100 | 86 |
| 101 // MediaTransferProtocolManager override. | 87 // MediaTransferProtocolManager override. |
| 102 virtual const MtpStorageInfo* GetStorageInfo( | 88 virtual const MtpStorageInfo* GetStorageInfo( |
| 103 const std::string& storage_name) const OVERRIDE { | 89 const std::string& storage_name) const OVERRIDE { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | 90 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name); | 91 StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name); |
| 106 if (it == storage_info_map_.end()) | 92 return it != storage_info_map_.end() ? &it->second : NULL; |
| 107 return NULL; | |
| 108 return &it->second; | |
| 109 } | 93 } |
| 110 | 94 |
| 111 // MediaTransferProtocolManager override. | 95 // MediaTransferProtocolManager override. |
| 112 virtual void OpenStorage(const std::string& storage_name, | 96 virtual void OpenStorage(const std::string& storage_name, |
| 113 const std::string& mode, | 97 const std::string& mode, |
| 114 const OpenStorageCallback& callback) OVERRIDE { | 98 const OpenStorageCallback& callback) OVERRIDE { |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 if (!ContainsKey(storage_info_map_, storage_name)) { | 100 if (!ContainsKey(storage_info_map_, storage_name)) { |
| 117 callback.Run(std::string(), true); | 101 callback.Run(std::string(), true); |
| 118 return; | 102 return; |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 get_file_info_callbacks_.front().Run(entry, false); | 378 get_file_info_callbacks_.front().Run(entry, false); |
| 395 get_file_info_callbacks_.pop(); | 379 get_file_info_callbacks_.pop(); |
| 396 } | 380 } |
| 397 | 381 |
| 398 void OnGetFileInfoError() { | 382 void OnGetFileInfoError() { |
| 399 DCHECK(thread_checker_.CalledOnValidThread()); | 383 DCHECK(thread_checker_.CalledOnValidThread()); |
| 400 get_file_info_callbacks_.front().Run(MtpFileEntry(), true); | 384 get_file_info_callbacks_.front().Run(MtpFileEntry(), true); |
| 401 get_file_info_callbacks_.pop(); | 385 get_file_info_callbacks_.pop(); |
| 402 } | 386 } |
| 403 | 387 |
| 388 // Get the Bus object used to communicate with mtpd. |
| 389 dbus::Bus* GetBus() { |
| 390 DCHECK(thread_checker_.CalledOnValidThread()); |
| 391 #if defined(OS_CHROMEOS) |
| 392 return chromeos::DBusThreadManager::Get()->GetSystemBus(); |
| 393 #else |
| 394 return session_bus_.get(); |
| 395 #endif |
| 396 } |
| 397 |
| 398 // Callback to finish initialization after figuring out if the mtp service |
| 399 // has an owner. |
| 400 // |service_owner| contains the name of the current owner, if any. |
| 401 void FinishSetupOnOriginThread(const std::string& service_owner) { |
| 402 DCHECK(thread_checker_.CalledOnValidThread()); |
| 403 |
| 404 if (service_owner.empty()) { |
| 405 #if !defined(OS_CHROMEOS) |
| 406 // |session_bus_| will not get used. Manually shut it down. |
| 407 session_bus_->PostTaskToDBusThread( |
| 408 FROM_HERE, base::Bind(&dbus::Bus::ShutdownAndBlock, session_bus_)); |
| 409 #endif |
| 410 return; |
| 411 } |
| 412 |
| 413 mtp_client_.reset( |
| 414 MediaTransferProtocolDaemonClient::Create(GetBus(), |
| 415 false /* not stub */)); |
| 416 |
| 417 // Set up signals and start initializing |storage_info_map_|. |
| 418 mtp_client_->SetUpConnections( |
| 419 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged, |
| 420 weak_ptr_factory_.GetWeakPtr())); |
| 421 mtp_client_->EnumerateStorages( |
| 422 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorages, |
| 423 weak_ptr_factory_.GetWeakPtr()), |
| 424 base::Bind(&base::DoNothing)); |
| 425 } |
| 426 |
| 404 // Mtpd DBus client. | 427 // Mtpd DBus client. |
| 405 scoped_ptr<MediaTransferProtocolDaemonClient> mtp_client_; | 428 scoped_ptr<MediaTransferProtocolDaemonClient> mtp_client_; |
| 406 | 429 |
| 407 #if !defined(OS_CHROMEOS) | 430 #if !defined(OS_CHROMEOS) |
| 408 // And a D-Bus session for talking to mtpd. | 431 // And a D-Bus session for talking to mtpd. |
| 409 scoped_refptr<dbus::Bus> session_bus_; | 432 scoped_refptr<dbus::Bus> session_bus_; |
| 410 #endif | 433 #endif |
| 411 | 434 |
| 412 // Device attachment / detachment observers. | 435 // Device attachment / detachment observers. |
| 413 ObserverList<Observer> observers_; | 436 ObserverList<Observer> observers_; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 442 DCHECK(!g_media_transfer_protocol_manager); | 465 DCHECK(!g_media_transfer_protocol_manager); |
| 443 | 466 |
| 444 g_media_transfer_protocol_manager = | 467 g_media_transfer_protocol_manager = |
| 445 new MediaTransferProtocolManagerImpl(task_runner); | 468 new MediaTransferProtocolManagerImpl(task_runner); |
| 446 VLOG(1) << "MediaTransferProtocolManager initialized"; | 469 VLOG(1) << "MediaTransferProtocolManager initialized"; |
| 447 | 470 |
| 448 return g_media_transfer_protocol_manager; | 471 return g_media_transfer_protocol_manager; |
| 449 } | 472 } |
| 450 | 473 |
| 451 } // namespace device | 474 } // namespace device |
| OLD | NEW |