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/object_proxy.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 | 28 #else |
26 #include "dbus/bus.h" | 29 #include "dbus/bus.h" |
27 #endif | 30 #endif |
28 | 31 |
29 namespace device { | 32 namespace device { |
30 | 33 |
31 namespace { | 34 namespace { |
32 | 35 |
33 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; | 36 MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; |
34 | 37 |
38 typedef base::Callback<void(dbus::Bus*, bool)> ServiceHasOwnerCallback; | |
39 | |
40 void CheckMtpdServiceHasOwnerOnDBusThread( | |
41 scoped_refptr<dbus::Bus> bus, | |
42 const ServiceHasOwnerCallback& reply_task) { | |
43 bool has_owner = | |
44 dbus::ObjectProxy::ServiceHasOwner(bus, mtpd::kMtpdServiceName); | |
45 bus->PostTaskToOriginThread(FROM_HERE, | |
46 base::Bind(reply_task, bus, has_owner)); | |
47 } | |
48 | |
35 // The MediaTransferProtocolManager implementation. | 49 // The MediaTransferProtocolManager implementation. |
36 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { | 50 class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
37 public: | 51 public: |
38 explicit MediaTransferProtocolManagerImpl( | 52 explicit MediaTransferProtocolManagerImpl( |
39 scoped_refptr<base::SequencedTaskRunner> task_runner) | 53 scoped_refptr<base::SequencedTaskRunner> task_runner) |
40 : weak_ptr_factory_(this) { | 54 : weak_ptr_factory_(this) { |
41 dbus::Bus* bus = NULL; | 55 dbus::Bus* bus = NULL; |
42 #if defined(OS_CHROMEOS) | 56 #if defined(OS_CHROMEOS) |
43 DCHECK(!task_runner.get()); | 57 DCHECK(!task_runner.get()); |
44 chromeos::DBusThreadManager* dbus_thread_manager = | 58 chromeos::DBusThreadManager* dbus_thread_manager = |
45 chromeos::DBusThreadManager::Get(); | 59 chromeos::DBusThreadManager::Get(); |
46 bus = dbus_thread_manager->GetSystemBus(); | 60 bus = dbus_thread_manager->GetSystemBus(); |
47 if (!bus) | 61 if (!bus) |
48 return; | 62 return; |
49 #else | 63 #else |
50 DCHECK(task_runner.get()); | 64 DCHECK(task_runner.get()); |
51 dbus::Bus::Options options; | 65 dbus::Bus::Options options; |
52 options.bus_type = dbus::Bus::SYSTEM; | 66 options.bus_type = dbus::Bus::SYSTEM; |
53 options.connection_type = dbus::Bus::PRIVATE; | 67 options.connection_type = dbus::Bus::PRIVATE; |
54 options.dbus_task_runner = task_runner; | 68 options.dbus_task_runner = task_runner; |
55 session_bus_ = new dbus::Bus(options); | 69 session_bus_ = new dbus::Bus(options); |
56 bus = session_bus_.get(); | 70 bus = session_bus_.get(); |
57 #endif | 71 #endif |
58 | 72 |
59 DCHECK(bus); | 73 DCHECK(bus); |
60 mtp_client_.reset( | |
61 MediaTransferProtocolDaemonClient::Create(bus, false /* not stub */)); | |
62 | 74 |
63 // Set up signals and start initializing |storage_info_map_|. | 75 ServiceHasOwnerCallback reply_task = |
64 mtp_client_->SetUpConnections( | 76 base::Bind(&MediaTransferProtocolManagerImpl::FinishSetupOnOriginThread, |
65 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged, | 77 weak_ptr_factory_.GetWeakPtr()); |
66 weak_ptr_factory_.GetWeakPtr())); | 78 base::Closure dbus_task = base::Bind(&CheckMtpdServiceHasOwnerOnDBusThread, |
67 mtp_client_->EnumerateStorages( | 79 make_scoped_refptr(bus), |
68 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorages, | 80 reply_task); |
69 weak_ptr_factory_.GetWeakPtr()), | 81 bus->PostTaskToDBusThread(FROM_HERE, dbus_task); |
satorux1
2013/04/30 01:53:10
This is awkward. I think we should provide an asyn
| |
70 base::Bind(&base::DoNothing)); | |
71 } | 82 } |
72 | 83 |
73 virtual ~MediaTransferProtocolManagerImpl() { | 84 virtual ~MediaTransferProtocolManagerImpl() { |
74 DCHECK(g_media_transfer_protocol_manager); | 85 DCHECK(g_media_transfer_protocol_manager); |
75 g_media_transfer_protocol_manager = NULL; | 86 g_media_transfer_protocol_manager = NULL; |
76 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; | 87 VLOG(1) << "MediaTransferProtocolManager Shutdown completed"; |
77 } | 88 } |
78 | 89 |
79 // MediaTransferProtocolManager override. | 90 // MediaTransferProtocolManager override. |
80 virtual void AddObserver(Observer* observer) OVERRIDE { | 91 virtual void AddObserver(Observer* observer) OVERRIDE { |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 get_file_info_callbacks_.front().Run(entry, false); | 405 get_file_info_callbacks_.front().Run(entry, false); |
395 get_file_info_callbacks_.pop(); | 406 get_file_info_callbacks_.pop(); |
396 } | 407 } |
397 | 408 |
398 void OnGetFileInfoError() { | 409 void OnGetFileInfoError() { |
399 DCHECK(thread_checker_.CalledOnValidThread()); | 410 DCHECK(thread_checker_.CalledOnValidThread()); |
400 get_file_info_callbacks_.front().Run(MtpFileEntry(), true); | 411 get_file_info_callbacks_.front().Run(MtpFileEntry(), true); |
401 get_file_info_callbacks_.pop(); | 412 get_file_info_callbacks_.pop(); |
402 } | 413 } |
403 | 414 |
415 void FinishSetupOnOriginThread(dbus::Bus* bus, bool service_has_owner) { | |
416 DCHECK(thread_checker_.CalledOnValidThread()); | |
417 | |
418 if (!service_has_owner) { | |
419 #if !defined(OS_CHROMEOS) | |
420 // |session_bus_| will not get used. Manually shut it down. | |
421 session_bus_->PostTaskToDBusThread( | |
422 FROM_HERE, base::Bind(&dbus::Bus::ShutdownAndBlock, session_bus_)); | |
423 #endif | |
424 return; | |
425 } | |
426 | |
427 mtp_client_.reset( | |
428 MediaTransferProtocolDaemonClient::Create(bus, false /* not stub */)); | |
429 | |
430 // Set up signals and start initializing |storage_info_map_|. | |
431 mtp_client_->SetUpConnections( | |
432 base::Bind(&MediaTransferProtocolManagerImpl::OnStorageChanged, | |
433 weak_ptr_factory_.GetWeakPtr())); | |
434 mtp_client_->EnumerateStorages( | |
435 base::Bind(&MediaTransferProtocolManagerImpl::OnEnumerateStorages, | |
436 weak_ptr_factory_.GetWeakPtr()), | |
437 base::Bind(&base::DoNothing)); | |
438 } | |
439 | |
404 // Mtpd DBus client. | 440 // Mtpd DBus client. |
405 scoped_ptr<MediaTransferProtocolDaemonClient> mtp_client_; | 441 scoped_ptr<MediaTransferProtocolDaemonClient> mtp_client_; |
406 | 442 |
407 #if !defined(OS_CHROMEOS) | 443 #if !defined(OS_CHROMEOS) |
408 // And a D-Bus session for talking to mtpd. | 444 // And a D-Bus session for talking to mtpd. |
409 scoped_refptr<dbus::Bus> session_bus_; | 445 scoped_refptr<dbus::Bus> session_bus_; |
410 #endif | 446 #endif |
411 | 447 |
412 // Device attachment / detachment observers. | 448 // Device attachment / detachment observers. |
413 ObserverList<Observer> observers_; | 449 ObserverList<Observer> observers_; |
(...skipping 28 matching lines...) Expand all Loading... | |
442 DCHECK(!g_media_transfer_protocol_manager); | 478 DCHECK(!g_media_transfer_protocol_manager); |
443 | 479 |
444 g_media_transfer_protocol_manager = | 480 g_media_transfer_protocol_manager = |
445 new MediaTransferProtocolManagerImpl(task_runner); | 481 new MediaTransferProtocolManagerImpl(task_runner); |
446 VLOG(1) << "MediaTransferProtocolManager initialized"; | 482 VLOG(1) << "MediaTransferProtocolManager initialized"; |
447 | 483 |
448 return g_media_transfer_protocol_manager; | 484 return g_media_transfer_protocol_manager; |
449 } | 485 } |
450 | 486 |
451 } // namespace device | 487 } // namespace device |
OLD | NEW |