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