Index: chromeos/dbus/experimental_bluetooth_input_client.cc |
diff --git a/chromeos/dbus/experimental_bluetooth_input_client.cc b/chromeos/dbus/experimental_bluetooth_input_client.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f56a31ccdb0ccdd4cbdde494a9174e6166352300 |
--- /dev/null |
+++ b/chromeos/dbus/experimental_bluetooth_input_client.cc |
@@ -0,0 +1,231 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/dbus/experimental_bluetooth_input_client.h" |
+ |
+#include <map> |
+ |
+#include "base/logging.h" |
+#include "base/stl_util.h" |
+#include "chromeos/dbus/bluetooth_property.h" |
+#include "dbus/bus.h" |
+#include "dbus/message.h" |
+#include "dbus/object_manager.h" |
+#include "dbus/object_path.h" |
+#include "dbus/object_proxy.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace chromeos { |
+ |
+ExperimentalBluetoothInputClient::Properties::Properties( |
+ dbus::ObjectProxy* object_proxy, |
+ const std::string& interface_name, |
+ const PropertyChangedCallback& callback) |
+ : dbus::PropertySet(object_proxy, interface_name, callback) { |
+ RegisterProperty(bluetooth_input::kReconnectModeProperty, &reconnect_mode); |
+} |
+ |
+ExperimentalBluetoothInputClient::Properties::~Properties() { |
+} |
+ |
+ |
+// The ExperimentalBluetoothInputClient implementation used in production. |
+class ExperimentalBluetoothInputClientImpl |
+ : public ExperimentalBluetoothInputClient, |
+ public dbus::ObjectManager::Interface { |
+ public: |
+ explicit ExperimentalBluetoothInputClientImpl(dbus::Bus* bus) |
+ : bus_(bus), |
+ weak_ptr_factory_(this) { |
+ object_manager_ = bus_->GetObjectManager( |
+ bluetooth_manager::kBluetoothManagerServiceName, |
+ dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath)); |
+ object_manager_->RegisterInterface( |
+ bluetooth_input::kExperimentalBluetoothInputInterface, this); |
+ } |
+ |
+ virtual ~ExperimentalBluetoothInputClientImpl() { |
+ object_manager_->UnregisterInterface( |
+ bluetooth_input::kExperimentalBluetoothInputInterface); |
+ } |
+ |
+ // ExperimentalBluetoothInputClient override. |
+ virtual void AddObserver( |
+ ExperimentalBluetoothInputClient::Observer* observer) OVERRIDE { |
+ DCHECK(observer); |
+ observers_.AddObserver(observer); |
+ } |
+ |
+ // ExperimentalBluetoothInputClient override. |
+ virtual void RemoveObserver( |
+ ExperimentalBluetoothInputClient::Observer* observer) OVERRIDE { |
+ DCHECK(observer); |
+ observers_.RemoveObserver(observer); |
+ } |
+ |
+ // dbus::ObjectManager::Interface override. |
+ virtual dbus::PropertySet* CreateProperties( |
+ dbus::ObjectProxy* object_proxy, |
+ const dbus::ObjectPath& object_path, |
+ const std::string& interface_name) { |
+ Properties* properties = new Properties( |
+ object_proxy, interface_name, |
+ base::Bind(&ExperimentalBluetoothInputClientImpl::OnPropertyChanged, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ object_path)); |
+ return static_cast<dbus::PropertySet*>(properties); |
+ } |
+ |
+ // ExperimentalBluetoothInputClient override. |
+ virtual Properties* GetProperties(const dbus::ObjectPath& object_path) |
+ OVERRIDE { |
+ return static_cast<Properties*>( |
+ object_manager_->GetProperties( |
+ object_path, |
+ bluetooth_input::kExperimentalBluetoothInputInterface)); |
+ } |
+ |
+ private: |
+ // Called by dbus::ObjectManager when an object with the input interface |
+ // is created. Informs observers. |
+ void ObjectAdded(const dbus::ObjectPath& object_path, |
+ const std::string& interface_name) OVERRIDE { |
+ FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
+ InputAdded(object_path)); |
+ } |
+ |
+ // Called by dbus::ObjectManager when an object with the input interface |
+ // is removed. Informs observers. |
+ void ObjectRemoved(const dbus::ObjectPath& object_path, |
+ const std::string& interface_name) OVERRIDE { |
+ FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
+ InputRemoved(object_path)); |
+ } |
+ |
+ // Called by BluetoothPropertySet when a property value is changed, |
+ // either by result of a signal or response to a GetAll() or Get() |
+ // call. Informs observers. |
+ void OnPropertyChanged(const dbus::ObjectPath& object_path, |
+ const std::string& property_name) { |
+ FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
+ InputPropertyChanged(object_path, property_name)); |
+ } |
+ |
+ dbus::Bus* bus_; |
+ dbus::ObjectManager* object_manager_; |
+ |
+ // List of observers interested in event notifications from us. |
+ ObserverList<ExperimentalBluetoothInputClient::Observer> observers_; |
+ |
+ // Weak pointer factory for generating 'this' pointers that might live longer |
+ // than we do. |
+ // Note: This should remain the last member so it'll be destroyed and |
+ // invalidate its weak pointers before any other members are destroyed. |
+ base::WeakPtrFactory<ExperimentalBluetoothInputClientImpl> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothInputClientImpl); |
+}; |
+ |
+// The ExperimentalBluetoothInputClient implementation used on Linux desktop, |
+// which does nothing. |
+class ExperimentalBluetoothInputClientStubImpl |
+ : public ExperimentalBluetoothInputClient { |
keybuk
2013/04/13 03:35:00
This should now be named FakeBluetoothInputClient
|
+ public: |
+ struct Properties : public ExperimentalBluetoothInputClient::Properties { |
+ explicit Properties(const PropertyChangedCallback& callback) |
+ : ExperimentalBluetoothInputClient::Properties( |
+ NULL, |
+ bluetooth_input::kExperimentalBluetoothInputInterface, |
+ callback) { |
+ } |
+ |
+ virtual ~Properties() { |
+ } |
+ |
+ virtual void Get(dbus::PropertyBase* property, |
+ dbus::PropertySet::GetCallback callback) OVERRIDE { |
+ VLOG(1) << "Get " << property->name(); |
+ callback.Run(false); |
+ } |
+ |
+ virtual void GetAll() OVERRIDE { |
+ VLOG(1) << "GetAll"; |
+ } |
+ |
+ virtual void Set(dbus::PropertyBase *property, |
+ dbus::PropertySet::SetCallback callback) OVERRIDE { |
+ VLOG(1) << "Set " << property->name(); |
+ callback.Run(false); |
+ } |
+ }; |
+ |
+ ExperimentalBluetoothInputClientStubImpl() { |
+ dbus::ObjectPath dev0("/fake/hci0/dev0"); |
+ |
+ Properties* properties = new Properties(base::Bind( |
+ &ExperimentalBluetoothInputClientStubImpl::OnPropertyChanged, |
+ base::Unretained(this), |
+ dev0)); |
+ properties->reconnect_mode.ReplaceValue( |
+ bluetooth_input::kAnyReconnectModeProperty); |
+ |
+ properties_map_[dev0] = properties; |
+ } |
+ |
+ virtual ~ExperimentalBluetoothInputClientStubImpl() { |
+ // Clean up Properties structures |
+ STLDeleteValues(&properties_map_); |
+ } |
+ |
+ // ExperimentalBluetoothInputClient override. |
+ virtual void AddObserver(Observer* observer) OVERRIDE { |
+ observers_.AddObserver(observer); |
+ } |
+ |
+ // ExperimentalBluetoothInputClient override. |
+ virtual void RemoveObserver(Observer* observer) OVERRIDE { |
+ observers_.RemoveObserver(observer); |
+ } |
+ |
+ // ExperimentalBluetoothInputClient override. |
+ virtual Properties* GetProperties(const dbus::ObjectPath& object_path) |
+ OVERRIDE { |
+ VLOG(1) << "GetProperties: " << object_path.value(); |
keybuk
2013/04/13 03:35:00
The continual GetProperties log messages will driv
|
+ PropertiesMap::iterator iter = properties_map_.find(object_path); |
+ if (iter != properties_map_.end()) |
+ return iter->second; |
+ return NULL; |
+ } |
+ |
+ private: |
+ void OnPropertyChanged(dbus::ObjectPath object_path, |
+ const std::string& property_name) { |
+ FOR_EACH_OBSERVER(ExperimentalBluetoothInputClient::Observer, observers_, |
+ InputPropertyChanged(object_path, property_name)); |
+ } |
+ |
+ // List of observers interested in event notifications from us. |
+ ObserverList<Observer> observers_; |
+ |
+ // Static properties we typedef. |
+ typedef std::map<const dbus::ObjectPath, Properties *> PropertiesMap; |
+ PropertiesMap properties_map_; |
+}; |
+ |
+ExperimentalBluetoothInputClient::ExperimentalBluetoothInputClient() { |
+} |
+ |
+ExperimentalBluetoothInputClient::~ExperimentalBluetoothInputClient() { |
+} |
+ |
+ExperimentalBluetoothInputClient* ExperimentalBluetoothInputClient::Create( |
+ DBusClientImplementationType type, |
+ dbus::Bus* bus) { |
+ if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
+ return new ExperimentalBluetoothInputClientImpl(bus); |
+ DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
+ return new ExperimentalBluetoothInputClientStubImpl(); |
keybuk
2013/04/13 03:35:00
new Fake...
|
+} |
+ |
+} // namespace chromeos |