OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
| 6 |
| 7 #include "base/observer_list.h" |
| 8 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| 9 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h" |
| 10 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| 11 |
| 12 namespace chromeos { |
| 13 |
| 14 class BluetoothAdapterImpl : public BluetoothAdapter, |
| 15 public BluetoothAdapterClient::Observer { |
| 16 public: |
| 17 explicit BluetoothAdapterImpl(const std::string& id) : id_(id) { |
| 18 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get(); |
| 19 CHECK(dbus_thread_manager != NULL); |
| 20 bluetooth_adapter_client_ = dbus_thread_manager->bluetooth_adapter_client(); |
| 21 CHECK(bluetooth_adapter_client_ != NULL); |
| 22 bluetooth_adapter_client_->AddObserver(this, id_); |
| 23 } |
| 24 |
| 25 virtual ~BluetoothAdapterImpl() { |
| 26 CHECK(bluetooth_adapter_client_ != NULL); |
| 27 bluetooth_adapter_client_->RemoveObserver(this, id_); |
| 28 } |
| 29 |
| 30 virtual void AddObserver(BluetoothAdapter::Observer* observer) { |
| 31 VLOG(1) << id_ << ": AddObserver"; |
| 32 CHECK(observer != NULL); |
| 33 observers_.AddObserver(observer); |
| 34 } |
| 35 |
| 36 virtual void RemoveObserver(BluetoothAdapter::Observer* observer) { |
| 37 VLOG(1) << id_ << ": RemoveObserver"; |
| 38 CHECK(observer != NULL); |
| 39 observers_.RemoveObserver(observer); |
| 40 } |
| 41 |
| 42 virtual const std::string& id() const { |
| 43 return id_; |
| 44 } |
| 45 |
| 46 virtual void StartDiscovery() { |
| 47 VLOG(1) << id_ << ": StartDiscovery"; |
| 48 CHECK(bluetooth_adapter_client_ != NULL); |
| 49 bluetooth_adapter_client_->StartDiscovery(id_); |
| 50 } |
| 51 |
| 52 virtual void StopDiscovery() { |
| 53 VLOG(1) << id_ << ": StopDiscovery"; |
| 54 CHECK(bluetooth_adapter_client_ != NULL); |
| 55 bluetooth_adapter_client_->StopDiscovery(id_); |
| 56 } |
| 57 |
| 58 // BluetoothAdapterClient::Observer override. |
| 59 virtual void DiscoveringPropertyChanged(const std::string& path, |
| 60 bool discovering) { |
| 61 VLOG(1) << id_ << ": path = " << path << ", Discovering = " << discovering; |
| 62 if (path != id_) { |
| 63 return; |
| 64 } |
| 65 if (discovering) { |
| 66 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 67 DiscoveryStarted(path)); |
| 68 } else { |
| 69 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 70 DiscoveryEnded(path)); |
| 71 } |
| 72 } |
| 73 |
| 74 // BluetoothAdapterClient::Observer override. |
| 75 virtual void DeviceFound(const std::string& path, |
| 76 const std::string& address, |
| 77 const DictionaryValue& device_properties) { |
| 78 VLOG(1) << id_ << ": path = " << path << ", Device found: " << address |
| 79 << " (with " << device_properties.size() << " properties)"; |
| 80 if (path != id_) { |
| 81 return; |
| 82 } |
| 83 BluetoothDevice *device = BluetoothDevice::Create(device_properties); |
| 84 if (device != NULL) { |
| 85 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 86 DeviceFound(path, device)); |
| 87 } else { |
| 88 LOG(WARNING) << "Could not create BluetoothDevice from properties."; |
| 89 } |
| 90 // TODO(vlaviano): later, we will want to persist the device. |
| 91 delete device; |
| 92 } |
| 93 |
| 94 // BluetoothAdapterClient::Observer override. |
| 95 virtual void DeviceDisappeared(const std::string& path, |
| 96 const std::string& address) { |
| 97 VLOG(1) << id_ << ": path = " << path << ", Device disappeared: " |
| 98 << address; |
| 99 if (path != id_) { |
| 100 return; |
| 101 } |
| 102 // For now, we don't propagate this event to our observers. |
| 103 } |
| 104 |
| 105 private: |
| 106 // Owned by the dbus thread manager. |
| 107 BluetoothAdapterClient* bluetooth_adapter_client_; |
| 108 |
| 109 ObserverList<BluetoothAdapter::Observer> observers_; |
| 110 |
| 111 // An opaque identifier that we provide to clients. |
| 112 // We use the dbus object path for the adapter as the id. |
| 113 const std::string id_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterImpl); |
| 116 }; |
| 117 |
| 118 BluetoothAdapter::BluetoothAdapter() { |
| 119 } |
| 120 |
| 121 BluetoothAdapter::~BluetoothAdapter() { |
| 122 } |
| 123 |
| 124 // static |
| 125 BluetoothAdapter* BluetoothAdapter::Create(const std::string& id) { |
| 126 return new BluetoothAdapterImpl(id); |
| 127 } |
| 128 |
| 129 } // namespace chromeos |
OLD | NEW |