OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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/experimental_bluetooth_input_client.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 #include "chromeos/dbus/bluetooth_property.h" | |
12 #include "dbus/bus.h" | |
13 #include "dbus/message.h" | |
14 #include "dbus/object_manager.h" | |
15 #include "dbus/object_path.h" | |
16 #include "dbus/object_proxy.h" | |
17 #include "third_party/cros_system_api/dbus/service_constants.h" | |
18 | |
19 namespace chromeos { | |
20 | |
21 ExperimentalBluetoothInputClient::Properties::Properties( | |
22 dbus::ObjectProxy* object_proxy, | |
23 const std::string& interface_name, | |
24 const PropertyChangedCallback& callback) | |
25 : dbus::PropertySet(object_proxy, interface_name, callback) { | |
26 RegisterProperty(bluetooth_input::kReconnectModeProperty, &reconnect_mode); | |
27 } | |
28 | |
29 ExperimentalBluetoothInputClient::Properties::~Properties() { | |
30 } | |
31 | |
32 | |
33 // The ExperimentalBluetoothInputClient implementation used in production. | |
34 class ExperimentalBluetoothInputClientImpl | |
35 : public ExperimentalBluetoothInputClient, | |
36 public dbus::ObjectManager::Interface { | |
37 public: | |
38 explicit ExperimentalBluetoothInputClientImpl(dbus::Bus* bus) | |
39 : bus_(bus), | |
40 weak_ptr_factory_(this) { | |
41 object_manager_ = bus_->GetObjectManager( | |
42 bluetooth_manager::kBluetoothManagerServiceName, | |
43 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath)); | |
44 object_manager_->RegisterInterface( | |
45 bluetooth_input::kExperimentalBluetoothInputInterface, this); | |
46 } | |
47 | |
48 virtual ~ExperimentalBluetoothInputClientImpl() { | |
49 object_manager_->UnregisterInterface( | |
50 bluetooth_input::kExperimentalBluetoothInputInterface); | |
51 } | |
52 | |
53 // ExperimentalBluetoothInputClient override. | |
54 virtual void AddObserver( | |
55 ExperimentalBluetoothInputClient::Observer* observer) OVERRIDE { | |
56 DCHECK(observer); | |
57 observers_.AddObserver(observer); | |
58 } | |
59 | |
60 // ExperimentalBluetoothInputClient override. | |
61 virtual void RemoveObserver( | |
62 ExperimentalBluetoothInputClient::Observer* observer) OVERRIDE { | |
63 DCHECK(observer); | |
64 observers_.RemoveObserver(observer); | |
65 } | |
66 | |
67 // dbus::ObjectManager::Interface override. | |
68 virtual dbus::PropertySet* CreateProperties( | |
69 dbus::ObjectProxy* object_proxy, | |
70 const dbus::ObjectPath& object_path, | |
71 const std::string& interface_name) { | |
72 Properties* properties = new Properties( | |
73 object_proxy, interface_name, | |
74 base::Bind(&ExperimentalBluetoothInputClientImpl::OnPropertyChanged, | |
75 weak_ptr_factory_.GetWeakPtr(), | |
76 object_path)); | |
77 return static_cast<dbus::PropertySet*>(properties); | |
78 } | |
79 | |
80 // ExperimentalBluetoothInputClient override. | |
81 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
82 OVERRIDE { | |
83 return static_cast<Properties*>( | |
84 object_manager_->GetProperties( | |
85 object_path, | |
86 bluetooth_input::kExperimentalBluetoothInputInterface)); | |
87 } | |
88 | |
89 private: | |
90 // Called by dbus::ObjectManager when an object with the input interface | |
91 // is created. Informs observers. | |
92 void ObjectAdded(const dbus::ObjectPath& object_path, | |
93 const std::string& interface_name) OVERRIDE { | |
94 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, | |
95 InputAdded(object_path)); | |
96 } | |
97 | |
98 // Called by dbus::ObjectManager when an object with the input interface | |
99 // is removed. Informs observers. | |
100 void ObjectRemoved(const dbus::ObjectPath& object_path, | |
101 const std::string& interface_name) OVERRIDE { | |
102 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, | |
103 InputRemoved(object_path)); | |
104 } | |
105 | |
106 // Called by BluetoothPropertySet when a property value is changed, | |
107 // either by result of a signal or response to a GetAll() or Get() | |
108 // call. Informs observers. | |
109 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
110 const std::string& property_name) { | |
111 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, | |
112 InputPropertyChanged(object_path, property_name)); | |
113 } | |
114 | |
115 dbus::Bus* bus_; | |
116 dbus::ObjectManager* object_manager_; | |
117 | |
118 // List of observers interested in event notifications from us. | |
119 ObserverList<ExperimentalBluetoothInputClient::Observer> observers_; | |
120 | |
121 // Weak pointer factory for generating 'this' pointers that might live longer | |
122 // than we do. | |
123 // Note: This should remain the last member so it'll be destroyed and | |
124 // invalidate its weak pointers before any other members are destroyed. | |
125 base::WeakPtrFactory<ExperimentalBluetoothInputClientImpl> weak_ptr_factory_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothInputClientImpl); | |
128 }; | |
129 | |
130 // The ExperimentalBluetoothInputClient implementation used on Linux desktop, | |
131 // which does nothing. | |
132 class ExperimentalBluetoothInputClientStubImpl | |
133 : public ExperimentalBluetoothInputClient { | |
keybuk
2013/04/13 03:35:00
This should now be named FakeBluetoothInputClient
| |
134 public: | |
135 struct Properties : public ExperimentalBluetoothInputClient::Properties { | |
136 explicit Properties(const PropertyChangedCallback& callback) | |
137 : ExperimentalBluetoothInputClient::Properties( | |
138 NULL, | |
139 bluetooth_input::kExperimentalBluetoothInputInterface, | |
140 callback) { | |
141 } | |
142 | |
143 virtual ~Properties() { | |
144 } | |
145 | |
146 virtual void Get(dbus::PropertyBase* property, | |
147 dbus::PropertySet::GetCallback callback) OVERRIDE { | |
148 VLOG(1) << "Get " << property->name(); | |
149 callback.Run(false); | |
150 } | |
151 | |
152 virtual void GetAll() OVERRIDE { | |
153 VLOG(1) << "GetAll"; | |
154 } | |
155 | |
156 virtual void Set(dbus::PropertyBase *property, | |
157 dbus::PropertySet::SetCallback callback) OVERRIDE { | |
158 VLOG(1) << "Set " << property->name(); | |
159 callback.Run(false); | |
160 } | |
161 }; | |
162 | |
163 ExperimentalBluetoothInputClientStubImpl() { | |
164 dbus::ObjectPath dev0("/fake/hci0/dev0"); | |
165 | |
166 Properties* properties = new Properties(base::Bind( | |
167 &ExperimentalBluetoothInputClientStubImpl::OnPropertyChanged, | |
168 base::Unretained(this), | |
169 dev0)); | |
170 properties->reconnect_mode.ReplaceValue( | |
171 bluetooth_input::kAnyReconnectModeProperty); | |
172 | |
173 properties_map_[dev0] = properties; | |
174 } | |
175 | |
176 virtual ~ExperimentalBluetoothInputClientStubImpl() { | |
177 // Clean up Properties structures | |
178 STLDeleteValues(&properties_map_); | |
179 } | |
180 | |
181 // ExperimentalBluetoothInputClient override. | |
182 virtual void AddObserver(Observer* observer) OVERRIDE { | |
183 observers_.AddObserver(observer); | |
184 } | |
185 | |
186 // ExperimentalBluetoothInputClient override. | |
187 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
188 observers_.RemoveObserver(observer); | |
189 } | |
190 | |
191 // ExperimentalBluetoothInputClient override. | |
192 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
193 OVERRIDE { | |
194 VLOG(1) << "GetProperties: " << object_path.value(); | |
keybuk
2013/04/13 03:35:00
The continual GetProperties log messages will driv
| |
195 PropertiesMap::iterator iter = properties_map_.find(object_path); | |
196 if (iter != properties_map_.end()) | |
197 return iter->second; | |
198 return NULL; | |
199 } | |
200 | |
201 private: | |
202 void OnPropertyChanged(dbus::ObjectPath object_path, | |
203 const std::string& property_name) { | |
204 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, | |
205 InputPropertyChanged(object_path, property_name)); | |
206 } | |
207 | |
208 // List of observers interested in event notifications from us. | |
209 ObserverList<Observer> observers_; | |
210 | |
211 // Static properties we typedef. | |
212 typedef std::map<const dbus::ObjectPath, Properties *> PropertiesMap; | |
213 PropertiesMap properties_map_; | |
214 }; | |
215 | |
216 ExperimentalBluetoothInputClient::ExperimentalBluetoothInputClient() { | |
217 } | |
218 | |
219 ExperimentalBluetoothInputClient::~ExperimentalBluetoothInputClient() { | |
220 } | |
221 | |
222 ExperimentalBluetoothInputClient* ExperimentalBluetoothInputClient::Create( | |
223 DBusClientImplementationType type, | |
224 dbus::Bus* bus) { | |
225 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
226 return new ExperimentalBluetoothInputClientImpl(bus); | |
227 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
228 return new ExperimentalBluetoothInputClientStubImpl(); | |
keybuk
2013/04/13 03:35:00
new Fake...
| |
229 } | |
230 | |
231 } // namespace chromeos | |
OLD | NEW |