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 "chromeos/dbus/fake_bluetooth_input_client.h" |
| 13 #include "dbus/bus.h" |
| 14 #include "dbus/message.h" |
| 15 #include "dbus/object_manager.h" |
| 16 #include "dbus/object_path.h" |
| 17 #include "dbus/object_proxy.h" |
| 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 19 |
| 20 namespace chromeos { |
| 21 |
| 22 ExperimentalBluetoothInputClient::Properties::Properties( |
| 23 dbus::ObjectProxy* object_proxy, |
| 24 const std::string& interface_name, |
| 25 const PropertyChangedCallback& callback) |
| 26 : dbus::PropertySet(object_proxy, interface_name, callback) { |
| 27 RegisterProperty(bluetooth_input::kReconnectModeProperty, &reconnect_mode); |
| 28 } |
| 29 |
| 30 ExperimentalBluetoothInputClient::Properties::~Properties() { |
| 31 } |
| 32 |
| 33 |
| 34 // The ExperimentalBluetoothInputClient implementation used in production. |
| 35 class ExperimentalBluetoothInputClientImpl |
| 36 : public ExperimentalBluetoothInputClient, |
| 37 public dbus::ObjectManager::Interface { |
| 38 public: |
| 39 explicit ExperimentalBluetoothInputClientImpl(dbus::Bus* bus) |
| 40 : bus_(bus), |
| 41 weak_ptr_factory_(this) { |
| 42 object_manager_ = bus_->GetObjectManager( |
| 43 bluetooth_manager::kBluetoothManagerServiceName, |
| 44 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath)); |
| 45 object_manager_->RegisterInterface( |
| 46 bluetooth_input::kExperimentalBluetoothInputInterface, this); |
| 47 } |
| 48 |
| 49 virtual ~ExperimentalBluetoothInputClientImpl() { |
| 50 object_manager_->UnregisterInterface( |
| 51 bluetooth_input::kExperimentalBluetoothInputInterface); |
| 52 } |
| 53 |
| 54 // ExperimentalBluetoothInputClient override. |
| 55 virtual void AddObserver( |
| 56 ExperimentalBluetoothInputClient::Observer* observer) OVERRIDE { |
| 57 DCHECK(observer); |
| 58 observers_.AddObserver(observer); |
| 59 } |
| 60 |
| 61 // ExperimentalBluetoothInputClient override. |
| 62 virtual void RemoveObserver( |
| 63 ExperimentalBluetoothInputClient::Observer* observer) OVERRIDE { |
| 64 DCHECK(observer); |
| 65 observers_.RemoveObserver(observer); |
| 66 } |
| 67 |
| 68 // dbus::ObjectManager::Interface override. |
| 69 virtual dbus::PropertySet* CreateProperties( |
| 70 dbus::ObjectProxy* object_proxy, |
| 71 const dbus::ObjectPath& object_path, |
| 72 const std::string& interface_name) { |
| 73 Properties* properties = new Properties( |
| 74 object_proxy, interface_name, |
| 75 base::Bind(&ExperimentalBluetoothInputClientImpl::OnPropertyChanged, |
| 76 weak_ptr_factory_.GetWeakPtr(), |
| 77 object_path)); |
| 78 return static_cast<dbus::PropertySet*>(properties); |
| 79 } |
| 80 |
| 81 // ExperimentalBluetoothInputClient override. |
| 82 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) |
| 83 OVERRIDE { |
| 84 return static_cast<Properties*>( |
| 85 object_manager_->GetProperties( |
| 86 object_path, |
| 87 bluetooth_input::kExperimentalBluetoothInputInterface)); |
| 88 } |
| 89 |
| 90 private: |
| 91 // Called by dbus::ObjectManager when an object with the input interface |
| 92 // is created. Informs observers. |
| 93 void ObjectAdded(const dbus::ObjectPath& object_path, |
| 94 const std::string& interface_name) OVERRIDE { |
| 95 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
| 96 InputAdded(object_path)); |
| 97 } |
| 98 |
| 99 // Called by dbus::ObjectManager when an object with the input interface |
| 100 // is removed. Informs observers. |
| 101 void ObjectRemoved(const dbus::ObjectPath& object_path, |
| 102 const std::string& interface_name) OVERRIDE { |
| 103 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
| 104 InputRemoved(object_path)); |
| 105 } |
| 106 |
| 107 // Called by BluetoothPropertySet when a property value is changed, |
| 108 // either by result of a signal or response to a GetAll() or Get() |
| 109 // call. Informs observers. |
| 110 void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 111 const std::string& property_name) { |
| 112 FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
| 113 InputPropertyChanged(object_path, property_name)); |
| 114 } |
| 115 |
| 116 dbus::Bus* bus_; |
| 117 dbus::ObjectManager* object_manager_; |
| 118 |
| 119 // List of observers interested in event notifications from us. |
| 120 ObserverList<ExperimentalBluetoothInputClient::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<ExperimentalBluetoothInputClientImpl> weak_ptr_factory_; |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothInputClientImpl); |
| 129 }; |
| 130 |
| 131 ExperimentalBluetoothInputClient::ExperimentalBluetoothInputClient() { |
| 132 } |
| 133 |
| 134 ExperimentalBluetoothInputClient::~ExperimentalBluetoothInputClient() { |
| 135 } |
| 136 |
| 137 ExperimentalBluetoothInputClient* ExperimentalBluetoothInputClient::Create( |
| 138 DBusClientImplementationType type, |
| 139 dbus::Bus* bus) { |
| 140 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 141 return new ExperimentalBluetoothInputClientImpl(bus); |
| 142 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 143 return new FakeBluetoothInputClient(); |
| 144 } |
| 145 |
| 146 } // namespace chromeos |
OLD | NEW |