Index: chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc |
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc b/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..faa34bc26cb113dbc482af811da34726733ea5ab |
--- /dev/null |
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc |
@@ -0,0 +1,129 @@ |
+// Copyright (c) 2011 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 "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
+ |
+#include "base/observer_list.h" |
+#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
+#include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h" |
+#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
+ |
+namespace chromeos { |
+ |
+class BluetoothAdapterImpl : public BluetoothAdapter, |
+ public BluetoothAdapterClient::Observer { |
+ public: |
+ explicit BluetoothAdapterImpl(const std::string& id) : id_(id) { |
+ DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get(); |
+ CHECK(dbus_thread_manager != NULL); |
+ bluetooth_adapter_client_ = dbus_thread_manager->bluetooth_adapter_client(); |
+ CHECK(bluetooth_adapter_client_ != NULL); |
+ bluetooth_adapter_client_->AddObserver(this, id_); |
+ } |
+ |
+ virtual ~BluetoothAdapterImpl() { |
+ CHECK(bluetooth_adapter_client_ != NULL); |
+ bluetooth_adapter_client_->RemoveObserver(this, id_); |
+ } |
+ |
+ virtual void AddObserver(BluetoothAdapter::Observer* observer) { |
+ VLOG(1) << id_ << ": AddObserver"; |
+ CHECK(observer != NULL); |
+ observers_.AddObserver(observer); |
+ } |
+ |
+ virtual void RemoveObserver(BluetoothAdapter::Observer* observer) { |
+ VLOG(1) << id_ << ": RemoveObserver"; |
+ CHECK(observer != NULL); |
+ observers_.RemoveObserver(observer); |
+ } |
+ |
+ virtual const std::string& id() const { |
+ return id_; |
+ } |
+ |
+ virtual void StartDiscovery() { |
+ VLOG(1) << id_ << ": StartDiscovery"; |
+ CHECK(bluetooth_adapter_client_ != NULL); |
+ bluetooth_adapter_client_->StartDiscovery(id_); |
+ } |
+ |
+ virtual void StopDiscovery() { |
+ VLOG(1) << id_ << ": StopDiscovery"; |
+ CHECK(bluetooth_adapter_client_ != NULL); |
+ bluetooth_adapter_client_->StopDiscovery(id_); |
+ } |
+ |
+ // BluetoothAdapterClient::Observer override. |
+ virtual void DiscoveringPropertyChanged(const std::string& path, |
+ bool discovering) { |
+ VLOG(1) << id_ << ": path = " << path << ", Discovering = " << discovering; |
+ if (path != id_) { |
+ return; |
+ } |
+ if (discovering) { |
+ FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
+ DiscoveryStarted(path)); |
+ } else { |
+ FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
+ DiscoveryEnded(path)); |
+ } |
+ } |
+ |
+ // BluetoothAdapterClient::Observer override. |
+ virtual void DeviceFound(const std::string& path, |
+ const std::string& address, |
+ const DictionaryValue& device_properties) { |
+ VLOG(1) << id_ << ": path = " << path << ", Device found: " << address |
+ << " (with " << device_properties.size() << " properties)"; |
+ if (path != id_) { |
+ return; |
+ } |
+ BluetoothDevice *device = BluetoothDevice::Create(device_properties); |
+ if (device != NULL) { |
+ FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
+ DeviceFound(path, device)); |
+ } else { |
+ LOG(WARNING) << "Could not create BluetoothDevice from properties."; |
+ } |
+ // TODO(vlaviano): later, we will want to persist the device. |
+ delete device; |
+ } |
+ |
+ // BluetoothAdapterClient::Observer override. |
+ virtual void DeviceDisappeared(const std::string& path, |
+ const std::string& address) { |
+ VLOG(1) << id_ << ": path = " << path << ", Device disappeared: " |
+ << address; |
+ if (path != id_) { |
+ return; |
+ } |
+ // For now, we don't propagate this event to our observers. |
+ } |
+ |
+ private: |
+ // Owned by the dbus thread manager. |
+ BluetoothAdapterClient* bluetooth_adapter_client_; |
+ |
+ ObserverList<BluetoothAdapter::Observer> observers_; |
+ |
+ // An opaque identifier that we provide to clients. |
+ // We use the dbus object path for the adapter as the id. |
+ const std::string id_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterImpl); |
+}; |
+ |
+BluetoothAdapter::BluetoothAdapter() { |
+} |
+ |
+BluetoothAdapter::~BluetoothAdapter() { |
+} |
+ |
+// static |
+BluetoothAdapter* BluetoothAdapter::Create(const std::string& id) { |
+ return new BluetoothAdapterImpl(id); |
+} |
+ |
+} // namespace chromeos |