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