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.
5 #include "chromeos/dbus/bluetooth_input_client.h"
9 #include "base/logging.h"
10 #include "base/stl_util.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"
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
);
27 BluetoothInputClient::Properties::~Properties() {
31 // The BluetoothInputClient implementation used in production.
32 class BluetoothInputClientImpl
33 : public BluetoothInputClient
,
34 public dbus::ObjectManager::Interface
{
36 BluetoothInputClientImpl() : object_manager_(NULL
), weak_ptr_factory_(this) {}
38 ~BluetoothInputClientImpl() override
{
39 object_manager_
->UnregisterInterface(
40 bluetooth_input::kBluetoothInputInterface
);
43 // BluetoothInputClient override.
44 void AddObserver(BluetoothInputClient::Observer
* observer
) override
{
46 observers_
.AddObserver(observer
);
49 // BluetoothInputClient override.
50 void RemoveObserver(BluetoothInputClient::Observer
* observer
) override
{
52 observers_
.RemoveObserver(observer
);
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(
63 base::Bind(&BluetoothInputClientImpl::OnPropertyChanged
,
64 weak_ptr_factory_
.GetWeakPtr(),
66 return static_cast<dbus::PropertySet
*>(properties
);
69 // BluetoothInputClient override.
70 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
{
71 return static_cast<Properties
*>(
72 object_manager_
->GetProperties(
74 bluetooth_input::kBluetoothInputInterface
));
78 void Init(dbus::Bus
* bus
) override
{
79 object_manager_
= bus
->GetObjectManager(
80 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
82 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
83 object_manager_
->RegisterInterface(
84 bluetooth_input::kBluetoothInputInterface
, this);
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
));
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
));
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
));
113 dbus::ObjectManager
* object_manager_
;
115 // List of observers interested in event notifications from us.
116 base::ObserverList
<BluetoothInputClient::Observer
> observers_
;
118 // Weak pointer factory for generating 'this' pointers that might live longer
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_
;
124 DISALLOW_COPY_AND_ASSIGN(BluetoothInputClientImpl
);
127 BluetoothInputClient::BluetoothInputClient() {
130 BluetoothInputClient::~BluetoothInputClient() {
133 BluetoothInputClient
* BluetoothInputClient::Create() {
134 return new BluetoothInputClientImpl();
137 } // namespace chromeos