OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chromeos/dbus/bluetooth_input_client.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 #include "dbus/bus.h" | |
12 #include "dbus/message.h" | |
13 #include "dbus/object_manager.h" | |
14 #include "dbus/object_proxy.h" | |
15 #include "third_party/cros_system_api/dbus/service_constants.h" | |
16 | |
17 namespace chromeos { | |
18 | |
19 BluetoothInputClient::Properties::Properties( | |
20 dbus::ObjectProxy* object_proxy, | |
21 const std::string& interface_name, | |
22 const PropertyChangedCallback& callback) | |
23 : dbus::PropertySet(object_proxy, interface_name, callback) { | |
24 RegisterProperty(bluetooth_input::kReconnectModeProperty, &reconnect_mode); | |
25 } | |
26 | |
27 BluetoothInputClient::Properties::~Properties() { | |
28 } | |
29 | |
30 | |
31 // The BluetoothInputClient implementation used in production. | |
32 class BluetoothInputClientImpl | |
33 : public BluetoothInputClient, | |
34 public dbus::ObjectManager::Interface { | |
35 public: | |
36 BluetoothInputClientImpl() : object_manager_(NULL), weak_ptr_factory_(this) {} | |
37 | |
38 ~BluetoothInputClientImpl() override { | |
39 object_manager_->UnregisterInterface( | |
40 bluetooth_input::kBluetoothInputInterface); | |
41 } | |
42 | |
43 // BluetoothInputClient override. | |
44 void AddObserver(BluetoothInputClient::Observer* observer) override { | |
45 DCHECK(observer); | |
46 observers_.AddObserver(observer); | |
47 } | |
48 | |
49 // BluetoothInputClient override. | |
50 void RemoveObserver(BluetoothInputClient::Observer* observer) override { | |
51 DCHECK(observer); | |
52 observers_.RemoveObserver(observer); | |
53 } | |
54 | |
55 // dbus::ObjectManager::Interface override. | |
56 dbus::PropertySet* CreateProperties( | |
57 dbus::ObjectProxy* object_proxy, | |
58 const dbus::ObjectPath& object_path, | |
59 const std::string& interface_name) override { | |
60 Properties* properties = new Properties( | |
61 object_proxy, | |
62 interface_name, | |
63 base::Bind(&BluetoothInputClientImpl::OnPropertyChanged, | |
64 weak_ptr_factory_.GetWeakPtr(), | |
65 object_path)); | |
66 return static_cast<dbus::PropertySet*>(properties); | |
67 } | |
68 | |
69 // BluetoothInputClient override. | |
70 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | |
71 return static_cast<Properties*>( | |
72 object_manager_->GetProperties( | |
73 object_path, | |
74 bluetooth_input::kBluetoothInputInterface)); | |
75 } | |
76 | |
77 protected: | |
78 void Init(dbus::Bus* bus) override { | |
79 object_manager_ = bus->GetObjectManager( | |
80 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | |
81 dbus::ObjectPath( | |
82 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | |
83 object_manager_->RegisterInterface( | |
84 bluetooth_input::kBluetoothInputInterface, this); | |
85 } | |
86 | |
87 private: | |
88 // Called by dbus::ObjectManager when an object with the input interface | |
89 // is created. Informs observers. | |
90 void ObjectAdded(const dbus::ObjectPath& object_path, | |
91 const std::string& interface_name) override { | |
92 FOR_EACH_OBSERVER(BluetoothInputClient::Observer, observers_, | |
93 InputAdded(object_path)); | |
94 } | |
95 | |
96 // Called by dbus::ObjectManager when an object with the input interface | |
97 // is removed. Informs observers. | |
98 void ObjectRemoved(const dbus::ObjectPath& object_path, | |
99 const std::string& interface_name) override { | |
100 FOR_EACH_OBSERVER(BluetoothInputClient::Observer, observers_, | |
101 InputRemoved(object_path)); | |
102 } | |
103 | |
104 // Called by BluetoothPropertySet when a property value is changed, | |
105 // either by result of a signal or response to a GetAll() or Get() | |
106 // call. Informs observers. | |
107 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
108 const std::string& property_name) { | |
109 FOR_EACH_OBSERVER(BluetoothInputClient::Observer, observers_, | |
110 InputPropertyChanged(object_path, property_name)); | |
111 } | |
112 | |
113 dbus::ObjectManager* object_manager_; | |
114 | |
115 // List of observers interested in event notifications from us. | |
116 base::ObserverList<BluetoothInputClient::Observer> observers_; | |
117 | |
118 // Weak pointer factory for generating 'this' pointers that might live longer | |
119 // than we do. | |
120 // Note: This should remain the last member so it'll be destroyed and | |
121 // invalidate its weak pointers before any other members are destroyed. | |
122 base::WeakPtrFactory<BluetoothInputClientImpl> weak_ptr_factory_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(BluetoothInputClientImpl); | |
125 }; | |
126 | |
127 BluetoothInputClient::BluetoothInputClient() { | |
128 } | |
129 | |
130 BluetoothInputClient::~BluetoothInputClient() { | |
131 } | |
132 | |
133 BluetoothInputClient* BluetoothInputClient::Create() { | |
134 return new BluetoothInputClientImpl(); | |
135 } | |
136 | |
137 } // namespace chromeos | |
OLD | NEW |