summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/bluetooth
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-09 00:03:01 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-09 00:03:01 +0000
commitc21aeaf4b0a7cacc6137163cfe2789f44281624b (patch)
tree3631a7e22b9cbbc0a72afb85e0abf484f4c283bd /chrome/browser/chromeos/bluetooth
parent03dc888f5d0e636fd4e55f8f04bb635bfad29b42 (diff)
downloadchromium_src-c21aeaf4b0a7cacc6137163cfe2789f44281624b.zip
chromium_src-c21aeaf4b0a7cacc6137163cfe2789f44281624b.tar.gz
chromium_src-c21aeaf4b0a7cacc6137163cfe2789f44281624b.tar.bz2
Revert 125725 - bluetooth: replace interface with new API
[It broke: http://build.chromium.org/p/chromium.chromiumos/builders/Linux%20ChromiumOS%20%28Clang%20dbg%29/builds/9217 ./chrome/browser/chromeos/dbus/bluetooth_property.h:76:5: error: use of undeclared identifier 'AppendToWriter' AppendToWriter(&writer, value); ^ this-> chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc:151:44: note: in instantiation of member function 'chromeos::BluetoothProperty<bool>::Set' requested here ] Replace the Chrome Bluetooth interface with a completely new API. Compared to the previous code, this gives us a much cleaner interface for the functionality we need and completely hides that there is BlueZ and D-Bus underneath. BluetoothManager is replaced by BluetoothAdapter::CreateDefaultAdapter(), the adapter code itself is responsible for tracking the default adapter removing the need for the singleton object that persists for the Chrome lifetime. It's perfectly acceptable to have multiple instances of BluetoothAdapter for one device as they share the same Bluetooth*Client instances and dbus::ObjectProxy instances. Neither BluetoothAdapter or BluetoothDevice rely on arbitrary "unique ids" that were really D-Bus object paths. An owner of a BluetoothAdapter instance may simply store and use the instance; for BluetoothDevice one just uses the address() property to identify it. BluetoothDevice now provides some functionality that was previously implemented in JavaScript, such as setting the name of devices before the real name or alias is available, and determining whether or not the device is supported. This allows us to use in multiple places in the UI. Support for devices that are not paired, but are connected or have been connected without being paired in the past is implemented. This may be used for mice, and other devices where pairing is not usual. UI Options Handler code has been updated to use the new interfaces, support for handling USB adapter removal and insertion, control of the adapter radio power, and populating the list of known devices has been added. BUG=chromium-os:27103,chromium-os:27102,chromium-os:23209,chromium-os:26265 TEST=tested on devices Change-Id: I4d8fa830c8d48877ce4b24fee9168ee8d5386031 Review URL: http://codereview.chromium.org/9572025 TBR=keybuk@chromium.org Review URL: https://chromiumcodereview.appspot.com/9646014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@125729 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/bluetooth')
-rw-r--r--chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc492
-rw-r--r--chrome/browser/chromeos/bluetooth/bluetooth_adapter.h277
-rw-r--r--chrome/browser/chromeos/bluetooth/bluetooth_device.cc191
-rw-r--r--chrome/browser/chromeos/bluetooth/bluetooth_device.h145
-rw-r--r--chrome/browser/chromeos/bluetooth/bluetooth_manager.cc139
-rw-r--r--chrome/browser/chromeos/bluetooth/bluetooth_manager.h61
6 files changed, 415 insertions, 890 deletions
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc b/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc
index 9c74a1c..2f995ab9 100644
--- a/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_adapter.cc
@@ -4,437 +4,157 @@
#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
-#include "base/bind.h"
-#include "base/logging.h"
-#include "base/stl_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
#include "base/values.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
#include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h"
-#include "chrome/browser/chromeos/dbus/bluetooth_device_client.h"
-#include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h"
#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
-#include "dbus/object_path.h"
-namespace chromeos {
+namespace {
-BluetoothAdapter::BluetoothAdapter() : weak_ptr_factory_(this),
- track_default_(false),
- powered_(false),
- discovering_(false) {
- DBusThreadManager::Get()->GetBluetoothManagerClient()->
- AddObserver(this);
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- AddObserver(this);
- DBusThreadManager::Get()->GetBluetoothDeviceClient()->
- AddObserver(this);
+// Used to ignore whether or not our method calls work, since we were
+// ignoring them before I added callbacks to the methods.
+void EmptyAdapterCallback(const dbus::ObjectPath& object_path,
+ bool success) {
}
-BluetoothAdapter::~BluetoothAdapter() {
- DBusThreadManager::Get()->GetBluetoothDeviceClient()->
- RemoveObserver(this);
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- RemoveObserver(this);
- DBusThreadManager::Get()->GetBluetoothManagerClient()->
- RemoveObserver(this);
-
- STLDeleteValues(&devices_);
-}
+};
-void BluetoothAdapter::AddObserver(Observer* observer) {
- DCHECK(observer);
- observers_.AddObserver(observer);
-}
-
-void BluetoothAdapter::RemoveObserver(Observer* observer) {
- DCHECK(observer);
- observers_.RemoveObserver(observer);
-}
-
-void BluetoothAdapter::DefaultAdapter() {
- DVLOG(1) << "Tracking default adapter";
- track_default_ = true;
- DBusThreadManager::Get()->GetBluetoothManagerClient()->
- DefaultAdapter(base::Bind(&BluetoothAdapter::AdapterCallback,
- weak_ptr_factory_.GetWeakPtr()));
-}
-
-void BluetoothAdapter::FindAdapter(const std::string& address) {
- DVLOG(1) << "Using adapter " << address;
- track_default_ = false;
- DBusThreadManager::Get()->GetBluetoothManagerClient()->
- FindAdapter(address,
- base::Bind(&BluetoothAdapter::AdapterCallback,
- weak_ptr_factory_.GetWeakPtr()));
-}
+namespace chromeos {
-void BluetoothAdapter::AdapterCallback(const dbus::ObjectPath& adapter_path,
- bool success) {
- if (success) {
- ChangeAdapter(adapter_path);
- } else if (!object_path_.value().empty()) {
- RemoveAdapter();
+class BluetoothAdapterImpl : public BluetoothAdapter,
+ public BluetoothAdapterClient::Observer {
+ public:
+ explicit BluetoothAdapterImpl(const std::string& id) : id_(id) {
+ DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
+ DCHECK(dbus_thread_manager);
+ bluetooth_adapter_client_ =
+ dbus_thread_manager->GetBluetoothAdapterClient();
+ DCHECK(bluetooth_adapter_client_);
+ bluetooth_adapter_client_->AddObserver(this);
}
-}
-void BluetoothAdapter::DefaultAdapterChanged(
- const dbus::ObjectPath& adapter_path) {
- if (track_default_)
- ChangeAdapter(adapter_path);
-}
-
-void BluetoothAdapter::AdapterRemoved(const dbus::ObjectPath& adapter_path) {
- if (adapter_path == object_path_)
- RemoveAdapter();
-}
-
-void BluetoothAdapter::ChangeAdapter(const dbus::ObjectPath& adapter_path) {
- if (adapter_path == object_path_)
- return;
-
- // Determine whether this is a change of adapter or gaining an adapter,
- // remember for later so we can send the right notification.
- bool was_present = false;
- if (object_path_.value().empty()) {
- DVLOG(1) << "Adapter path initialized to " << adapter_path.value();
- was_present = false;
- } else {
- DVLOG(1) << "Adapter path changed from " << object_path_.value()
- << " to " << adapter_path.value();
- was_present = true;
-
- // Invalidate the devices list, since the property update does not
- // remove them.
- ClearDevices();
+ virtual ~BluetoothAdapterImpl() {
+ DCHECK(bluetooth_adapter_client_);
+ bluetooth_adapter_client_->RemoveObserver(this);
}
- object_path_ = adapter_path;
-
- // Update properties to their new values.
- BluetoothAdapterClient::Properties* properties =
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- GetProperties(object_path_);
-
- PoweredChanged(properties->powered.value());
- DiscoveringChanged(properties->discovering.value());
- DevicesChanged(properties->devices.value());
-
- // Notify observers if we did not have an adapter before, the case of
- // moving from one to another is hidden from layers above.
- if (!was_present) {
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- AdapterPresentChanged(this, true));
+ // BluetoothAdapter override.
+ virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE {
+ DCHECK(observer);
+ observers_.AddObserver(observer);
}
-}
-
-void BluetoothAdapter::RemoveAdapter() {
- DVLOG(1) << "Adapter lost.";
- PoweredChanged(false);
- DiscoveringChanged(false);
- ClearDevices();
-
- object_path_ = dbus::ObjectPath("");
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- AdapterPresentChanged(this, false));
-}
-
-bool BluetoothAdapter::IsPresent() const {
- return !object_path_.value().empty();
-}
-
-bool BluetoothAdapter::IsPowered() const {
- return powered_;
-}
-
-void BluetoothAdapter::SetPowered(bool powered, ErrorCallback callback) {
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- GetProperties(object_path_)->powered.Set(
- powered,
- base::Bind(&BluetoothAdapter::OnSetPowered,
- weak_ptr_factory_.GetWeakPtr(),
- callback));
-}
-
-void BluetoothAdapter::OnSetPowered(ErrorCallback callback, bool success) {
- if (!success)
- callback.Run();
-}
-
-void BluetoothAdapter::PoweredChanged(bool powered) {
- if (powered == powered_)
- return;
-
- powered_ = powered;
-
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- AdapterPoweredChanged(this, powered_));
-}
-
-bool BluetoothAdapter::IsDiscovering() const {
- return discovering_;
-}
-
-void BluetoothAdapter::SetDiscovering(bool discovering,
- ErrorCallback callback) {
- if (discovering) {
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- StartDiscovery(object_path_,
- base::Bind(&BluetoothAdapter::OnStartDiscovery,
- weak_ptr_factory_.GetWeakPtr(),
- callback));
- } else {
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- StopDiscovery(object_path_,
- base::Bind(&BluetoothAdapter::OnStopDiscovery,
- weak_ptr_factory_.GetWeakPtr(),
- callback));
+ // BluetoothAdapter override.
+ virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE {
+ DCHECK(observer);
+ observers_.RemoveObserver(observer);
}
-}
-
-void BluetoothAdapter::OnStartDiscovery(ErrorCallback callback,
- const dbus::ObjectPath& adapter_path,
- bool success) {
- if (success) {
- DVLOG(1) << object_path_.value() << ": started discovery.";
- // Clear devices found in previous discovery attempts
- ClearDiscoveredDevices();
- } else {
- // TODO(keybuk): in future, don't run the callback if the error was just
- // that we were already discovering.
- callback.Run();
+ // BluetoothAdapter override.
+ virtual const std::string& Id() const OVERRIDE {
+ return id_;
}
-}
-
-void BluetoothAdapter::OnStopDiscovery(ErrorCallback callback,
- const dbus::ObjectPath& adapter_path,
- bool success) {
- if (success) {
- DVLOG(1) << object_path_.value() << ": stopped discovery.";
- // Leave found devices available for perusing.
- } else {
- // TODO(keybuk): in future, don't run the callback if the error was just
- // that we weren't discovering.
- callback.Run();
+ // BluetoothAdapter override.
+ virtual void StartDiscovery() OVERRIDE {
+ DCHECK(bluetooth_adapter_client_);
+ bluetooth_adapter_client_->StartDiscovery(
+ dbus::ObjectPath(id_),
+ base::Bind(&EmptyAdapterCallback));
}
-}
-void BluetoothAdapter::DiscoveringChanged(bool discovering) {
- if (discovering == discovering_)
- return;
-
- discovering_ = discovering;
-
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- AdapterDiscoveringChanged(this, discovering_));
-}
-
-void BluetoothAdapter::AdapterPropertyChanged(
- const dbus::ObjectPath& adapter_path,
- const std::string& property_name) {
- if (adapter_path != object_path_)
- return;
-
- BluetoothAdapterClient::Properties* properties =
- DBusThreadManager::Get()->GetBluetoothAdapterClient()->
- GetProperties(object_path_);
-
- if (property_name == properties->powered.name()) {
- PoweredChanged(properties->powered.value());
-
- } else if (property_name == properties->discovering.name()) {
- DiscoveringChanged(properties->discovering.value());
-
- } else if (property_name == properties->devices.name()) {
- DevicesChanged(properties->devices.value());
-
- }
-}
-
-void BluetoothAdapter::DevicePropertyChanged(
- const dbus::ObjectPath& device_path,
- const std::string& property_name) {
- UpdateDevice(device_path);
-}
-
-void BluetoothAdapter::UpdateDevice(const dbus::ObjectPath& device_path) {
- BluetoothDeviceClient::Properties* properties =
- DBusThreadManager::Get()->GetBluetoothDeviceClient()->
- GetProperties(device_path);
-
- // When we first see a device, we may not know the address yet and need to
- // wait for the DevicePropertyChanged signal before adding the device.
- const std::string address = properties->address.value();
- if (address.empty())
- return;
-
- // If the device is already known this may be just an update to properties,
- // or it may be the device going from discovered to connected and gaining
- // an object path. Update the existing object and notify observers.
- DevicesMap::iterator iter = devices_.find(address);
- if (iter != devices_.end()){
- BluetoothDevice* device = iter->second;
-
- if (device->WasDiscovered())
- device->SetObjectPath(device_path);
- device->Update(properties, true);
-
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceChanged(this, device));
- return;
- }
-
- // Device has an address and was not previously known, add to the map
- // and notify observers.
- BluetoothDevice* device = BluetoothDevice::CreateBound(device_path,
- properties);
- devices_[address] = device;
-
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceAdded(this, device));
-}
-
-BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() {
- DeviceList devices;
- for (DevicesMap::iterator iter = devices_.begin();
- iter != devices_.end(); ++iter)
- devices.push_back(iter->second);
-
- return devices;
-}
-
-void BluetoothAdapter::ClearDevices() {
- for (DevicesMap::iterator iter = devices_.begin();
- iter != devices_.end(); ++iter) {
- BluetoothDevice* device = iter->second;
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceRemoved(this, device));
-
- delete device;
+ // BluetoothAdapter override.
+ virtual void StopDiscovery() OVERRIDE {
+ DCHECK(bluetooth_adapter_client_);
+ bluetooth_adapter_client_->StopDiscovery(dbus::ObjectPath(id_),
+ base::Bind(&EmptyAdapterCallback));
}
- devices_.clear();
-}
-
-void BluetoothAdapter::DeviceCreated(const dbus::ObjectPath& adapter_path,
- const dbus::ObjectPath& device_path) {
- if (adapter_path != object_path_)
- return;
-
- UpdateDevice(device_path);
-}
-
-void BluetoothAdapter::DeviceRemoved(const dbus::ObjectPath& adapter_path,
- const dbus::ObjectPath& device_path) {
- if (adapter_path != object_path_)
- return;
-
- const std::string address = DBusThreadManager::Get()->
- GetBluetoothDeviceClient()->GetProperties(device_path)->address.value();
- DevicesMap::iterator iter = devices_.find(address);
- if (iter == devices_.end())
- return;
+ // BluetoothAdapterClient::Observer override.
+ virtual void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
+ const std::string& property_name)
+ OVERRIDE {
+ if (object_path.value() != id_)
+ return;
- BluetoothDevice* device = iter->second;
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceRemoved(this, device));
+ DCHECK(bluetooth_adapter_client_);
+ BluetoothAdapterClient::Properties* properties =
+ bluetooth_adapter_client_->GetProperties(object_path);
- delete device;
- devices_.erase(iter);
-}
-
-void BluetoothAdapter::DevicesChanged(
- const std::vector<dbus::ObjectPath>& devices) {
- for (std::vector<dbus::ObjectPath>::const_iterator iter =
- devices.begin(); iter != devices.end(); ++iter)
- UpdateDevice(*iter);
-}
+ DCHECK(properties);
+ if (property_name != properties->discovering.name())
+ return;
-void BluetoothAdapter::ClearDiscoveredDevices() {
- DevicesMap::iterator iter = devices_.begin();
- while (iter != devices_.end()) {
- BluetoothDevice* device = iter->second;
- DevicesMap::iterator temp = iter;
- ++iter;
+ bool discovering = properties->discovering.value();
- if (device->WasDiscovered()) {
+ DVLOG(1) << id_ << ": object_path = " << object_path.value()
+ << ", Discovering = " << discovering;
+ if (discovering) {
FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceRemoved(this, device));
-
- delete device;
- devices_.erase(temp);
+ DiscoveryStarted(object_path.value()));
+ } else {
+ FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
+ DiscoveryEnded(object_path.value()));
}
}
-}
-void BluetoothAdapter::DeviceFound(
- const dbus::ObjectPath& adapter_path, const std::string& address,
- const BluetoothDeviceClient::Properties& properties) {
- if (adapter_path != object_path_)
- return;
-
- // DeviceFound can also be called to indicate that a device we've
- // paired with or previously connected to is now visible to the adapter,
- // so check it's not already in the list and just update if it is.
- BluetoothDevice* device;
- DevicesMap::iterator iter = devices_.find(address);
- if (iter == devices_.end()) {
- device = BluetoothDevice::CreateUnbound(&properties);
- devices_[address] = device;
-
- if (device->IsSupported())
+ // BluetoothAdapterClient::Observer override.
+ virtual void DeviceFound(const dbus::ObjectPath& object_path,
+ const std::string& address,
+ const BluetoothDeviceClient::Properties& properties)
+ OVERRIDE {
+ if (object_path.value() != id_) {
+ return;
+ }
+ // TODO(vlaviano): later, we will want to persist the device.
+ scoped_ptr<BluetoothDevice> device(
+ BluetoothDevice::Create(properties.address.value(),
+ properties.bluetooth_class.value(),
+ properties.icon.value(),
+ properties.name.value(),
+ properties.paired.value(),
+ properties.connected.value()));
+ if (device.get() != NULL) {
FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceAdded(this, device));
- } else {
- device = iter->second;
- device->Update(&properties, false);
+ DeviceFound(object_path.value(), device.get()));
+ } else {
+ LOG(WARNING) << "Could not create BluetoothDevice from properties.";
+ }
+ }
- if (device->IsSupported() || !device->WasDiscovered())
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceChanged(this, device));
+ // BluetoothAdapterClient::Observer override.
+ virtual void DeviceDisappeared(const dbus::ObjectPath& object_path,
+ const std::string& address) OVERRIDE {
+ if (object_path.value() != id_) {
+ return;
+ }
+ // For now, we don't propagate this event to our observers.
}
-}
-void BluetoothAdapter::DeviceDisappeared(const dbus::ObjectPath& adapter_path,
- const std::string& address) {
- if (adapter_path != object_path_)
- return;
+ private:
+ // Owned by the dbus thread manager.
+ BluetoothAdapterClient* bluetooth_adapter_client_;
- DevicesMap::iterator iter = devices_.find(address);
- if (iter == devices_.end())
- return;
+ ObserverList<BluetoothAdapter::Observer> observers_;
- BluetoothDevice* device = iter->second;
+ // An opaque identifier that we provide to clients.
+ // We use the dbus object path for the adapter as the id.
+ const std::string id_;
- // DeviceDisappeared can also be called to indicate that a device we've
- // paired with or previously connected to is no longer visible to the
- // adapter, so only delete discovered devices.
- if (device->WasDiscovered()) {
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceRemoved(this, device));
+ DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterImpl);
+};
- delete device;
- devices_.erase(iter);
- } else {
- FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
- DeviceChanged(this, device));
- }
+BluetoothAdapter::BluetoothAdapter() {
}
-
-// static
-BluetoothAdapter* BluetoothAdapter::CreateDefaultAdapter() {
- BluetoothAdapter* adapter = new BluetoothAdapter;
- adapter->DefaultAdapter();
- return adapter;
+BluetoothAdapter::~BluetoothAdapter() {
}
// static
-BluetoothAdapter* BluetoothAdapter::Create(const std::string& address) {
- BluetoothAdapter* adapter = new BluetoothAdapter;
- adapter->FindAdapter(address);
- return adapter;
+BluetoothAdapter* BluetoothAdapter::Create(const std::string& id) {
+ return new BluetoothAdapterImpl(id);
}
} // namespace chromeos
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_adapter.h b/chrome/browser/chromeos/bluetooth/bluetooth_adapter.h
index 6a007b5..196da4b 100644
--- a/chrome/browser/chromeos/bluetooth/bluetooth_adapter.h
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_adapter.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// 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.
@@ -6,281 +6,60 @@
#define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_
#pragma once
-#include <map>
#include <string>
-#include <vector>
#include "base/basictypes.h"
-#include "base/callback.h"
-#include "base/observer_list.h"
-#include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h"
-#include "chrome/browser/chromeos/dbus/bluetooth_device_client.h"
-#include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h"
-#include "dbus/object_path.h"
namespace chromeos {
class BluetoothDevice;
-// The BluetoothAdapter class represents a local Bluetooth adapter which
-// may be used to interact with remote Bluetooth devices. As well as
-// providing support for determining whether an adapter is present, and
-// whether the radio is powered, this class also provides support for
-// obtaining the list of remote devices known to the adapter, discovering
-// new devices, and providing notification of updates to device information.
-//
-// The class may be instantiated for either a specific adapter, or for the
-// generic "default adapter" which may change depending on availability.
-class BluetoothAdapter : public BluetoothManagerClient::Observer,
- public BluetoothAdapterClient::Observer,
- public BluetoothDeviceClient::Observer {
+// BluetoothAdapter is an interface class representing a local bluetoooth
+// interface through which we can discover and interact with remote bluetooth
+// devices.
+class BluetoothAdapter {
public:
// Interface for observing changes from bluetooth adapters.
class Observer {
public:
- virtual ~Observer() {}
-
- // Called when the presence of the adapter |adapter| changes, when
- // |present| is true the adapter is now present, false means the adapter
- // has been removed from the system.
- virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
- bool present) {}
-
- // Called when the radio power state of the adapter |adapter| changes,
- // when |powered| is true the adapter radio is powered, false means the
- // adapter radio is off.
- virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
- bool powered) {}
+ // The discovery process has started on adapter |adapter_id|.
+ virtual void DiscoveryStarted(const std::string& adapter_id) {}
- // Called when the discovering state of the adapter |adapter| changes,
- // when |discovering| is true the adapter is seeking new devices, false
- // means it is not. Note that device discovery involves both states when
- // the adapter is seeking new devices and states when it is not because
- // it is interrogating the devices it found.
- virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
- bool discovering) {}
+ // The discovery process has ended on adapter |adapter_id|.
+ virtual void DiscoveryEnded(const std::string& adapter_id) {}
- // Called when a new device |device| is added to the adapter |adapter|,
- // either because it has been discovered or a connection made. |device|
- // should not be cached, instead copy its address.
- virtual void DeviceAdded(BluetoothAdapter* adapter,
+ // A device has been discovered on adapter |adapter_id|.
+ // The observer should not cache the |device| pointer.
+ virtual void DeviceFound(const std::string& adapter_id,
BluetoothDevice* device) {}
- // Called when properties of the device |device| known to the adapter
- // |adapter| change. |device| should not be cached, instead copy its
- // address.
- virtual void DeviceChanged(BluetoothAdapter* adapter,
- BluetoothDevice* device) {}
-
- // Called when the device |device| is removed from the adapter |adapter|,
- // either as a result of a discovered device being lost between discovering
- // phases or pairing information deleted. |device| should not be cached.
- virtual void DeviceRemoved(BluetoothAdapter* adapter,
- BluetoothDevice* device) {}
+ virtual ~Observer() {}
};
virtual ~BluetoothAdapter();
- // Adds and removes observers for events on this bluetooth adapter,
- // if monitoring multiple adapters check the |adapter| parameter of
- // observer methods to determine which adapter is issuing the event.
- void AddObserver(Observer* observer);
- void RemoveObserver(Observer* observer);
-
- // The ErrorCallback is used for methods that can fail in which case it
- // is called, in the success case the callback is simply not called.
- typedef base::Callback<void()> ErrorCallback;
-
- // Indicates whether the adapter is actually present on the system, for
- // the default adapter this indicates whether any adapter is present.
- bool IsPresent() const;
-
- // Indicates whether the adapter radio is powered.
- bool IsPowered() const;
-
- // Requests a change to the adapter radio power, setting |powered| to
- // true will turn on the radio and false will turn it off. |callback|
- // will only be called if the request fails.
- void SetPowered(bool powered, ErrorCallback callback);
-
- // Indicates whether the adapter is currently discovering new devices,
- // note that a typical discovery process has phases of this being true
- // followed by phases of being false when the adapter interrogates the
- // devices found.
- bool IsDiscovering() const;
+ // Adds and removes the observer.
+ virtual void AddObserver(Observer* observer) = 0;
+ virtual void RemoveObserver(Observer* observer) = 0;
- // Requests that the adapter either begin discovering new devices when
- // |discovering| is true, or cease any discovery when false. |callback|
- // will only be called if the request fails.
- void SetDiscovering(bool discovering, ErrorCallback callback);
+ // Returns the unique identifier for this adapter.
+ virtual const std::string& Id() const = 0;
- // Requests the list of devices from the adapter, all are returned
- // including those currently connected, those that have been connected or
- // paired in the past and those that have only been discovered. Use the
- // returned device pointers to determine which they are.
- typedef std::vector<BluetoothDevice*> DeviceList;
- DeviceList GetDevices();
+ // Starts a device discovery on this adapter.
+ // Caller should call AddObserver() first since results will be received via
+ // the Observer interface.
+ virtual void StartDiscovery() = 0;
- // Creates the instance for the default adapter, whichever that may
- // be at the time. Use IsPresent() and the AdapterPresentChanged() observer
- // method to determine whether an adapter is actually available or not.
- static BluetoothAdapter* CreateDefaultAdapter();
+ // Cancels any previous device discovery on this adapter.
+ virtual void StopDiscovery() = 0;
- // Creates an instance for a specific adapter named by |address|, which
- // may be the bluetooth address of the adapter or a device name such as
- // "hci0".
- static BluetoothAdapter* Create(const std::string& address);
+ // Creates an adapter with id |id|.
+ static BluetoothAdapter* Create(const std::string& id);
- private:
+ protected:
BluetoothAdapter();
- // Obtains the default adapter object path from the Bluetooth Daemon
- // and tracks future changes to it.
- void DefaultAdapter();
-
- // Obtains the object paht for the adapter named by |address| from the
- // Bluetooth Daemon.
- void FindAdapter(const std::string& address);
-
- // Called by dbus:: in response to the method call sent by both
- // DefaultAdapter() and FindAdapter(), |object_path| will contain the
- // dbus object path of the requested adapter when |success| is true.
- void AdapterCallback(const dbus::ObjectPath& adapter_path, bool success);
-
- // BluetoothManagerClient::Observer override.
- //
- // Called when the default local bluetooth adapter changes.
- // |object_path| is the dbus object path of the new default adapter.
- // Not called if all adapters are removed.
- virtual void DefaultAdapterChanged(const dbus::ObjectPath& adapter_path)
- OVERRIDE;
-
- // BluetoothManagerClient::Observer override.
- //
- // Called when a local bluetooth adapter is removed.
- // |object_path| is the dbus object path of the adapter.
- virtual void AdapterRemoved(const dbus::ObjectPath& adapter_path) OVERRIDE;
-
- // Changes the tracked adapter to the dbus object path |adapter_path|,
- // clearing information from the previously tracked adapter and updating
- // to the new adapter.
- void ChangeAdapter(const dbus::ObjectPath& adapter_path);
-
- // Clears the tracked adapter information.
- void RemoveAdapter();
-
- // Called by dbus:: in response to the method call send by SetPowered().
- void OnSetPowered(ErrorCallback callback, bool success);
-
- // Updates the tracked state of the adapter's radio power to |powered|
- // and notifies observers. Called on receipt of a property changed signal,
- // and directly using values obtained from properties.
- void PoweredChanged(bool powered);
-
- // Called by dbus:: in response to the method calls send by SetDiscovering().
- void OnStartDiscovery(ErrorCallback callback,
- const dbus::ObjectPath& adapter_path, bool success);
- void OnStopDiscovery(ErrorCallback callback,
- const dbus::ObjectPath& adapter_path, bool success);
-
- // Updates the tracked state of the adapter's discovering state to
- // |discovering| and notifies observers. Called on receipt of a property
- // changed signal, and directly using values obtained from properties.
- void DiscoveringChanged(bool discovering);
-
- // BluetoothAdapterClient::Observer override.
- //
- // Called when the adapter with object path |adapter_path| has a
- // change in value of the property named |property_name|.
- virtual void AdapterPropertyChanged(const dbus::ObjectPath& adapter_path,
- const std::string& property_name)
- OVERRIDE;
-
- // BluetoothDeviceClient::Observer override.
- //
- // Called when the device with object path |device_path| has a
- // change in value of the property named |property_name|.
- virtual void DevicePropertyChanged(const dbus::ObjectPath& device_path,
- const std::string& property_name)
- OVERRIDE;
-
- // Updates information on the device with object path |device_path|,
- // adding it to the |devices_| map if not already present.
- void UpdateDevice(const dbus::ObjectPath& device_path);
-
- // Clears the |devices_| list, notifying obsevers of the device removal.
- void ClearDevices();
-
- // BluetoothAdapterClient::Observer override.
- //
- // Called when the adapter with object path |object_path| has a
- // new known device with object path |object_path|.
- virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
- const dbus::ObjectPath& device_path) OVERRIDE;
-
- // BluetoothAdapterClient::Observer override.
- //
- // Called when the adapter with object path |object_path| removes
- // the known device with object path |object_path|.
- virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
- const dbus::ObjectPath& device_path) OVERRIDE;
-
- // Updates the adapter |devices_| list, adding or updating devices using
- // the object paths in the|devices| list. This doesn't remove devices,
- // relying instead on the DeviceRemoved() signal for that. Called on
- // receipt of a property changed signal, and directly using values obtained
- // from properties.
- void DevicesChanged(const std::vector<dbus::ObjectPath>& devices);
-
- // Clears discovered devices from the |devices_| list, notifying
- // observers, and leaving only those devices with a dbus object path.
- void ClearDiscoveredDevices();
-
- // BluetoothAdapterClient::Observer override.
- //
- // Called when the adapter with object path |object_path| discovers
- // a new remote device with address |address| and properties
- // |properties|, there is no device object path until connected.
- //
- // |properties| supports only value() calls, not Get() or Set(), and
- // should be copied if needed.
- virtual void DeviceFound(
- const dbus::ObjectPath& adapter_path, const std::string& address,
- const BluetoothDeviceClient::Properties& properties) OVERRIDE;
-
- // BluetoothAdapterClient::Observer override.
- //
- // Called when the adapter with object path |object_path| can no
- // longer communicate with the discovered removed device with
- // address |address|.
- virtual void DeviceDisappeared(const dbus::ObjectPath& object_path,
- const std::string& address) OVERRIDE;
-
- // List of observers interested in event notifications from us.
- ObserverList<BluetoothAdapter::Observer> observers_;
-
- // Weak pointer factory for generating 'this' pointers that might live longer
- // than we do.
- base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_;
-
- // Object path of adapter for this instance, this is fixed at creation time
- // unless |track_default_| is true in which case we update it to always
- // point at the default adapter.
- bool track_default_;
- dbus::ObjectPath object_path_;
-
- // Tracked adapter state, cached locally so we only send change notifications
- // to observers on a genuine change.
- bool powered_;
- bool discovering_;
-
- // Devices paired with, connected to, previously connected to, discovered
- // and visible to the adapter. The key is the Bluetooth address of the device
- // and the value is the BluetoothDevice object whose lifetime is managed
- // by the adapter instance.
- typedef std::map<const std::string, BluetoothDevice*> DevicesMap;
- DevicesMap devices_;
-
+ private:
DISALLOW_COPY_AND_ASSIGN(BluetoothAdapter);
};
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_device.cc b/chrome/browser/chromeos/bluetooth/bluetooth_device.cc
index 7253b76..2434cfa 100644
--- a/chrome/browser/chromeos/bluetooth/bluetooth_device.cc
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_device.cc
@@ -4,169 +4,82 @@
#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
-#include "base/bind.h"
#include "base/logging.h"
-#include "base/string16.h"
-#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/chromeos/dbus/bluetooth_device_client.h"
-#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
-#include "dbus/object_path.h"
-#include "grit/generated_resources.h"
-#include "ui/base/l10n/l10n_util.h"
+#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
-BluetoothDevice::BluetoothDevice() : bluetooth_class_(0),
- paired_(false),
- connected_(false) {
-}
-
-BluetoothDevice::~BluetoothDevice() {
-}
-
-void BluetoothDevice::SetObjectPath(const dbus::ObjectPath& object_path) {
- DCHECK(object_path_ == dbus::ObjectPath(""));
- object_path_ = object_path;
-}
+class BluetoothDeviceImpl : public BluetoothDevice {
+ public:
+ BluetoothDeviceImpl(const std::string& address,
+ uint32 bluetooth_class,
+ const std::string& icon,
+ const std::string& name,
+ bool paired,
+ bool connected)
+ : address_(address),
+ bluetooth_class_(bluetooth_class),
+ icon_(icon),
+ name_(name),
+ paired_(paired),
+ connected_(connected) {
+ }
-void BluetoothDevice::Update(
- const BluetoothDeviceClient::Properties* properties,
- bool update_state) {
- address_ = properties->address.value();
- name_ = properties->name.value();
- bluetooth_class_ = properties->bluetooth_class.value();
+ virtual ~BluetoothDeviceImpl() {
+ }
- if (update_state) {
- paired_ = properties->paired.value();
- connected_ = properties->connected.value();
+ virtual const std::string& GetAddress() const {
+ return address_;
}
-}
-string16 BluetoothDevice::GetName() const {
- if (!name_.empty()) {
- return UTF8ToUTF16(name_);
- } else {
- return GetAddressWithLocalizedDeviceTypeName();
+ virtual uint32 GetBluetoothClass() const {
+ return bluetooth_class_;
}
-}
-BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const {
- // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
- switch ((bluetooth_class_ & 0x1f00) >> 8) {
- case 0x01:
- // Computer major device class.
- return DEVICE_COMPUTER;
- case 0x02:
- // Phone major device class.
- switch ((bluetooth_class_ & 0xfc) >> 2) {
- case 0x01:
- case 0x02:
- case 0x03:
- // Cellular, cordless and smart phones.
- return DEVICE_PHONE;
- case 0x04:
- case 0x05:
- // Modems: wired or voice gateway and common ISDN access.
- return DEVICE_MODEM;
- }
- break;
- case 0x05:
- // Peripheral major device class.
- switch ((bluetooth_class_ & 0xc0) >> 6) {
- case 0x00:
- // "Not a keyboard or pointing device."
- return DEVICE_PERIPHERAL;
- case 0x01:
- // Keyboard.
- return DEVICE_KEYBOARD;
- case 0x02:
- // Pointing device.
- switch ((bluetooth_class_ & 0x01e) >> 2) {
- case 0x05:
- // Digitizer tablet.
- return DEVICE_TABLET;
- default:
- // Mouse.
- return DEVICE_MOUSE;
- }
- break;
- case 0x03:
- // Combo device.
- return DEVICE_KEYBOARD_MOUSE_COMBO;
- }
- break;
+ virtual const std::string& GetIcon() const {
+ return icon_;
}
- return DEVICE_UNKNOWN;
-}
+ virtual const std::string& GetName() const {
+ return name_;
+ }
-bool BluetoothDevice::IsSupported() const {
- DeviceType device_type = GetDeviceType();
- return (device_type == DEVICE_COMPUTER ||
- device_type == DEVICE_PHONE ||
- device_type == DEVICE_KEYBOARD ||
- device_type == DEVICE_MOUSE ||
- device_type == DEVICE_TABLET ||
- device_type == DEVICE_KEYBOARD_MOUSE_COMBO);
-}
+ virtual bool IsPaired() const {
+ return paired_;
+ }
-string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const {
- string16 address = UTF8ToUTF16(address_);
- DeviceType device_type = GetDeviceType();
- switch (device_type) {
- case DEVICE_COMPUTER:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER,
- address);
- case DEVICE_PHONE:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE,
- address);
- case DEVICE_MODEM:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM,
- address);
- case DEVICE_KEYBOARD:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_KEYBOARD,
- address);
- case DEVICE_MOUSE:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MOUSE,
- address);
- case DEVICE_TABLET:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET,
- address);
- case DEVICE_KEYBOARD_MOUSE_COMBO:
- return l10n_util::GetStringFUTF16(
- IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address);
- default:
- return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, address);
+ virtual bool IsConnected() const {
+ return connected_;
}
-}
-bool BluetoothDevice::WasDiscovered() const {
- return object_path_.value().empty();
-}
+ private:
+ const std::string address_;
+ uint32 bluetooth_class_;
+ const std::string icon_;
+ const std::string name_;
+ bool paired_;
+ bool connected_;
-bool BluetoothDevice::IsConnected() const {
- // TODO(keybuk): examine protocol-specific connected state, such as Input
- return connected_;
-}
+ DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceImpl);
+};
+BluetoothDevice::BluetoothDevice() {
+}
-// static
-BluetoothDevice* BluetoothDevice::CreateBound(
- const dbus::ObjectPath& object_path,
- const BluetoothDeviceClient::Properties* properties) {
- BluetoothDevice* device = new BluetoothDevice;
- device->SetObjectPath(object_path);
- device->Update(properties, true);
- return device;
+BluetoothDevice::~BluetoothDevice() {
}
// static
-BluetoothDevice* BluetoothDevice::CreateUnbound(
- const BluetoothDeviceClient::Properties* properties) {
- BluetoothDevice* device = new BluetoothDevice;
- device->Update(properties, false);
- return device;
+BluetoothDevice* BluetoothDevice::Create(const std::string& address,
+ uint32 bluetooth_class,
+ const std::string& icon,
+ const std::string& name,
+ bool paired,
+ bool connected) {
+ return new BluetoothDeviceImpl(address, bluetooth_class, icon,
+ name, paired, connected);
}
} // namespace chromeos
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_device.h b/chrome/browser/chromeos/bluetooth/bluetooth_device.h
index 5a8713a..a20c545 100644
--- a/chrome/browser/chromeos/bluetooth/bluetooth_device.h
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_device.h
@@ -6,138 +6,51 @@
#define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_H_
#pragma once
+#include <string>
+
#include "base/basictypes.h"
-#include "base/string16.h"
-#include "chrome/browser/chromeos/dbus/bluetooth_device_client.h"
-#include "dbus/object_path.h"
namespace chromeos {
-class BluetoothAdapter;
-
-// The BluetoothDevice class represents a remote Bluetooth device, both
-// its properties and capabilities as discovered by a local adapter and
-// actions that may be performed on the remove device such as pairing,
-// connection and disconnection.
-//
-// The class is instantiated and managed by the BluetoothAdapter class
-// and pointers should only be obtained from that class and not cached,
-// instead use the address() method as a unique key for a device.
-//
-// Since the lifecycle of BluetoothDevice instances is managed by
-// BluetoothAdapter, that class rather than this provides observer methods
-// for devices coming and going, as well as properties being updated.
-class BluetoothDevice : public BluetoothDeviceClient::Observer {
+class BluetoothDevice {
public:
- // Possible values that may be returned by GetDeviceType(), representing
- // different types of bluetooth device that we support or are aware of
- // decoded from the bluetooth class information.
- enum DeviceType {
- DEVICE_UNKNOWN,
- DEVICE_COMPUTER,
- DEVICE_PHONE,
- DEVICE_MODEM,
- DEVICE_PERIPHERAL,
- DEVICE_KEYBOARD,
- DEVICE_MOUSE,
- DEVICE_TABLET,
- DEVICE_KEYBOARD_MOUSE_COMBO
- };
-
- // Interface for observing changes from bluetooth devices.
- class Observer {
- public:
- virtual ~Observer() {}
-
- // TODO(keybuk): add observers for pairing and connection.
- };
-
virtual ~BluetoothDevice();
- // Returns the Bluetooth of address the device. This should be used as
- // a unique key to identify the device and copied where needed.
- const std::string& address() const { return address_; }
+ // Returns the MAC address for this device.
+ virtual const std::string& GetAddress() const = 0;
- // Returns the name of the device suitable for displaying, this may
- // be a synthesied string containing the address and localized type name
- // if the device has no obtained name.
- string16 GetName() const;
+ // Returns the bluetooth class for this device.
+ // This is a bitfield consisting of 3 subfields representing service class,
+ // major device class, and minor device class.
+ // See www.bluetooth.org/spec/ for details.
+ // TODO(vlaviano): provide a better API for this.
+ virtual uint32 GetBluetoothClass() const = 0;
- // Returns the type of the device, limited to those we support or are
- // aware of, by decoding the bluetooth class information. The returned
- // values are unique, and do not overlap, so DEVICE_KEYBOARD is not also
- // DEVICE_PERIPHERAL.
- DeviceType GetDeviceType() const;
+ // Returns the suggested icon type for this device.
+ // See http://standards.freedesktop.org/icon-naming-spec/ for details.
+ virtual const std::string& GetIcon() const = 0;
- // Returns a localized string containing the device's bluetooth address and
- // a device type for display when |name_| is empty.
- string16 GetAddressWithLocalizedDeviceTypeName() const;
+ // Returns the name for this device.
+ virtual const std::string& GetName() const = 0;
- // Indicates whether the class of this device is supported by Chrome OS.
- bool IsSupported() const;
+ // Returns whether or not this device is paired.
+ virtual bool IsPaired() const = 0;
- // Indicates if the device was one discovered by the adapter, and not yet
- // paired with or connected to.
- bool WasDiscovered() const;
+ // Returns whether or not this device is connected.
+ virtual bool IsConnected() const = 0;
- // Indicates whether the device is paired to the adapter, note that some
- // devices can be connected to and used without pairing so use
- // WasDiscovered() rather than this method to determine whether or not
- // to display in a list of devices.
- bool IsPaired() const { return paired_; }
-
- // Indicates whether the device is currently connected to the adapter
- // and at least one service available for use.
- bool IsConnected() const;
-
- private:
- friend class BluetoothAdapter;
+ // Creates a device with property values based on |properties|.
+ static BluetoothDevice* Create(const std::string& address,
+ uint32 bluetooth_class,
+ const std::string& icon,
+ const std::string& name,
+ bool paired,
+ bool connected);
+ protected:
BluetoothDevice();
- // Sets the dbus object path for the device to |object_path|, indicating
- // that the device has gone from being discovered to paired or connected.
- void SetObjectPath(const dbus::ObjectPath& object_path);
-
- // Updates device information from the properties in |properties|, device
- // state properties such as |paired_| and |connected_| are ignored unless
- // |update_state| is true.
- void Update(const BluetoothDeviceClient::Properties* properties,
- bool update_state);
-
- // Creates a new BluetoothDevice object bound to the information of the
- // dbus object path |object_path|, representing a paired device or one
- // that is currently or previously connected, with initial properties set
- // from |properties|.
- static BluetoothDevice* CreateBound(
- const dbus::ObjectPath& object_path,
- const BluetoothDeviceClient::Properties* properties);
-
- // Creates a new BluetoothDevice object not bound to a dbus object path,
- // representing a discovered device that has not yet been connected to,
- // with initial properties set from |properties|.
- static BluetoothDevice* CreateUnbound(
- const BluetoothDeviceClient::Properties* properties);
-
- // The dbus object path of the device, will be empty if the device has only
- // been discovered and not yet paired with or connected to.
- dbus::ObjectPath object_path_;
-
- // The Bluetooth address of the device.
- std::string address_;
-
- // The name of the device, as supplied by the remote device.
- std::string name_;
-
- // The Bluetooth class of the device, a bitmask that may be decoded using
- // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
- uint32 bluetooth_class_;
-
- // Tracked device state, updated by the adapter managing the lifecyle of
- // the device.
- bool paired_;
- bool connected_;
-
+ private:
DISALLOW_COPY_AND_ASSIGN(BluetoothDevice);
};
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_manager.cc b/chrome/browser/chromeos/bluetooth/bluetooth_manager.cc
new file mode 100644
index 0000000..34687fb
--- /dev/null
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_manager.cc
@@ -0,0 +1,139 @@
+// Copyright (c) 2012 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_manager.h"
+
+#include "base/bind.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
+#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
+#include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h"
+#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
+#include "dbus/object_path.h"
+
+namespace chromeos {
+
+static BluetoothManager* g_bluetooth_manager = NULL;
+
+class BluetoothManagerImpl : public BluetoothManager,
+ public BluetoothManagerClient::Observer {
+ public:
+ BluetoothManagerImpl() : weak_ptr_factory_(this) {
+ DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
+ DCHECK(dbus_thread_manager);
+ bluetooth_manager_client_ =
+ dbus_thread_manager->GetBluetoothManagerClient();
+ DCHECK(bluetooth_manager_client_);
+
+ bluetooth_manager_client_->AddObserver(this);
+
+ bluetooth_manager_client_->DefaultAdapter(
+ base::Bind(&BluetoothManagerImpl::OnDefaultAdapter,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+
+ // BluetoothManager override.
+ virtual void AddObserver(BluetoothManager::Observer* observer) OVERRIDE {
+ DCHECK(observer);
+ observers_.AddObserver(observer);
+ }
+
+ // BluetoothManager override.
+ virtual void RemoveObserver(BluetoothManager::Observer* observer) OVERRIDE {
+ DCHECK(observer);
+ observers_.RemoveObserver(observer);
+ }
+
+ // BluetoothManager override.
+ virtual BluetoothAdapter* DefaultAdapter() OVERRIDE {
+ return default_adapter_.get();
+ }
+
+ // BluetoothManagerClient::Observer override.
+ virtual void AdapterRemoved(const dbus::ObjectPath& adapter) OVERRIDE {
+ if (default_adapter_.get() == NULL
+ || default_adapter_->Id() != adapter.value()) {
+ return;
+ }
+ // The default adapter was removed.
+ default_adapter_.reset();
+ FOR_EACH_OBSERVER(BluetoothManager::Observer, observers_,
+ DefaultAdapterChanged(default_adapter_.get()));
+ }
+
+ // BluetoothManagerClient::Observer override.
+ virtual void DefaultAdapterChanged(const dbus::ObjectPath& adapter) OVERRIDE {
+ OnNewDefaultAdapter(adapter);
+ }
+
+ private:
+ virtual ~BluetoothManagerImpl() {
+ bluetooth_manager_client_->RemoveObserver(this);
+ }
+
+ // We have updated info about the default adapter.
+ void OnNewDefaultAdapter(const dbus::ObjectPath& adapter) {
+ if (default_adapter_.get() != NULL
+ && default_adapter_->Id() == adapter.value()) {
+ return;
+ }
+ default_adapter_.reset(BluetoothAdapter::Create(adapter.value()));
+ DCHECK(default_adapter_.get());
+ FOR_EACH_OBSERVER(BluetoothManager::Observer, observers_,
+ DefaultAdapterChanged(default_adapter_.get()));
+ }
+
+ // Called by bluetooth_manager_client when our DefaultAdapter request is
+ // complete
+ void OnDefaultAdapter(const dbus::ObjectPath& adapter, bool success) {
+ if (!success) {
+ LOG(ERROR) << "OnDefaultAdapter: failed.";
+ return;
+ }
+ OnNewDefaultAdapter(adapter);
+ }
+
+ base::WeakPtrFactory<BluetoothManagerImpl> weak_ptr_factory_;
+
+ // Owned by the dbus thread manager. Storing this is ok only because our
+ // lifetime is a subset of the thread manager's lifetime.
+ BluetoothManagerClient* bluetooth_manager_client_;
+
+ ObserverList<BluetoothManager::Observer> observers_;
+
+ scoped_ptr<BluetoothAdapter> default_adapter_;
+
+ DISALLOW_COPY_AND_ASSIGN(BluetoothManagerImpl);
+};
+
+BluetoothManager::BluetoothManager() {
+}
+
+BluetoothManager::~BluetoothManager() {
+}
+
+// static
+// TODO(vlaviano): allow creation of stub impl for ui testing
+void BluetoothManager::Initialize() {
+ VLOG(1) << "BluetoothManager::Initialize";
+ DCHECK(!g_bluetooth_manager);
+ g_bluetooth_manager = new BluetoothManagerImpl();
+ DCHECK(g_bluetooth_manager);
+}
+
+// static
+void BluetoothManager::Shutdown() {
+ VLOG(1) << "BluetoothManager::Shutdown";
+ if (g_bluetooth_manager) {
+ delete g_bluetooth_manager;
+ g_bluetooth_manager = NULL;
+ }
+}
+
+// static
+BluetoothManager* BluetoothManager::GetInstance() {
+ return g_bluetooth_manager;
+}
+
+} // namespace chromeos
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_manager.h b/chrome/browser/chromeos/bluetooth/bluetooth_manager.h
new file mode 100644
index 0000000..fea9648
--- /dev/null
+++ b/chrome/browser/chromeos/bluetooth/bluetooth_manager.h
@@ -0,0 +1,61 @@
+// 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.
+
+#ifndef CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_MANAGER_H_
+#define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_MANAGER_H_
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+
+namespace chromeos {
+
+class BluetoothAdapter;
+
+// BluetoothManager provides the Chrome OS bluetooth API.
+// This API is simplified relative to what's available via D-Bus.
+// We'll export additional functionality as needed.
+class BluetoothManager {
+ public:
+ // Interface for observing changes from the bluetooth manager.
+ class Observer {
+ public:
+ // Notification that the default adapter has changed. If |adapter| is NULL,
+ // it means that there are no longer any adapters.
+ // Clients should not cache this pointer.
+ virtual void DefaultAdapterChanged(BluetoothAdapter* adapter) {}
+ virtual ~Observer() {}
+ };
+
+ // Adds and removes the observer.
+ virtual void AddObserver(Observer* observer) = 0;
+ virtual void RemoveObserver(Observer* observer) = 0;
+
+ // Returns the default local bluetooth adapter or NULL if there is none.
+ // Clients should not cache this pointer.
+ virtual BluetoothAdapter* DefaultAdapter() = 0;
+
+ // Creates the global BluetoothManager instance.
+ static void Initialize();
+
+ // Destroys the global BluetoothManager instance if it exists.
+ static void Shutdown();
+
+ // Returns a pointer to the global BluetoothManager instance.
+ // Initialize() should already have been called.
+ static BluetoothManager* GetInstance();
+
+ protected:
+ BluetoothManager();
+ virtual ~BluetoothManager();
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(BluetoothManager);
+};
+
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_MANAGER_H_