Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4553)

Unified Diff: chrome/browser/chromeos/dbus/bluetooth_adapter_client.cc

Issue 8233042: Chrome OS: Attach bluetooth UI to device discovery API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: replace empty device names with mac address Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/dbus/bluetooth_adapter_client.cc
diff --git a/chrome/browser/chromeos/dbus/bluetooth_adapter_client.cc b/chrome/browser/chromeos/dbus/bluetooth_adapter_client.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6c5f4903bc02e57ba99e083db25bf66ecfd8583c
--- /dev/null
+++ b/chrome/browser/chromeos/dbus/bluetooth_adapter_client.cc
@@ -0,0 +1,309 @@
+// 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/dbus/bluetooth_adapter_client.h"
+
+#include <map>
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/stl_util.h"
+#include "chrome/browser/chromeos/system/runtime_environment.h"
+#include "dbus/bus.h"
+#include "dbus/message.h"
+#include "dbus/object_proxy.h"
+#include "third_party/cros_system_api/dbus/service_constants.h"
+
+namespace chromeos {
+
+// The BluetoothAdapterClient implementation used in production.
+class BluetoothAdapterClientImpl: public BluetoothAdapterClient {
+ public:
+ explicit BluetoothAdapterClientImpl(dbus::Bus* bus)
+ : weak_ptr_factory_(this),
+ bus_(bus) {
+ VLOG(1) << "BluetoothAdapterClientImpl ctor";
+ }
+
+ virtual ~BluetoothAdapterClientImpl() {
+ proxies_.clear();
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void AddObserver(Observer* observer, const std::string& path) {
+ VLOG(1) << "AddObserver: " << path;
+ CHECK(observer != NULL);
+ observers_.AddObserver(observer);
+ AddObjectProxyForPath(path);
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void RemoveObserver(Observer* observer, const std::string& path) {
+ VLOG(1) << "RemoveObserver: " << path;
+ CHECK(observer != NULL);
+ observers_.RemoveObserver(observer);
+ RemoveObjectProxyForPath(path);
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void StartDiscovery(const std::string& path) {
+ VLOG(1) << "StartDiscovery: " << path;
+
+ dbus::MethodCall method_call(
+ bluetooth_adapter::kBluetoothAdapterInterface,
+ bluetooth_adapter::kStartDiscovery);
+
+ ProxyMap::iterator it = proxies_.find(path);
+ if (it == proxies_.end()) {
+ LOG(ERROR) << "Couldn't find proxy for path " << path;
+ return;
+ }
+ dbus::ObjectProxy* adapter_proxy =
+ static_cast<dbus::ObjectProxy*>(it->second);
+ CHECK(adapter_proxy != NULL);
+
+ adapter_proxy->CallMethod(
+ &method_call,
+ dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&BluetoothAdapterClientImpl::OnStartDiscovery,
+ weak_ptr_factory_.GetWeakPtr(), path));
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void StopDiscovery(const std::string& path) {
+ VLOG(1) << "StopDiscovery: " << path;
+
+ dbus::MethodCall method_call(
+ bluetooth_adapter::kBluetoothAdapterInterface,
+ bluetooth_adapter::kStopDiscovery);
+
+ ProxyMap::iterator it = proxies_.find(path);
+ if (it == proxies_.end()) {
+ LOG(ERROR) << "Couldn't find proxy for path " << path;
+ return;
+ }
+ dbus::ObjectProxy* adapter_proxy =
+ static_cast<dbus::ObjectProxy*>(it->second);
+ CHECK(adapter_proxy != NULL);
+
+ adapter_proxy->CallMethod(
+ &method_call,
+ dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&BluetoothAdapterClientImpl::OnStopDiscovery,
+ weak_ptr_factory_.GetWeakPtr(), path));
+ }
+
+ private:
+ // Gets a dbus object proxy for an adapter with dbus path |path| and stores it
+ // in our |proxies_| map.
+ void AddObjectProxyForPath(const std::string& path) {
+ VLOG(1) << "AddObjectProxyForPath: " << path;
+
+ CHECK(bus_ != NULL);
+ dbus::ObjectProxy* adapter_proxy = bus_->GetObjectProxy(
+ bluetooth_adapter::kBluetoothAdapterServiceName, path);
+ CHECK(adapter_proxy != NULL);
+
+ proxies_[path] = adapter_proxy;
+
+ adapter_proxy->ConnectToSignal(
+ bluetooth_adapter::kBluetoothAdapterInterface,
+ bluetooth_adapter::kPropertyChangedSignal,
+ base::Bind(&BluetoothAdapterClientImpl::PropertyChangedReceived,
+ weak_ptr_factory_.GetWeakPtr(), path),
+ base::Bind(&BluetoothAdapterClientImpl::PropertyChangedConnected,
+ weak_ptr_factory_.GetWeakPtr(), path));
+
+ adapter_proxy->ConnectToSignal(
+ bluetooth_adapter::kBluetoothAdapterInterface,
+ bluetooth_adapter::kDeviceFoundSignal,
+ base::Bind(&BluetoothAdapterClientImpl::DeviceFoundReceived,
+ weak_ptr_factory_.GetWeakPtr(), path),
+ base::Bind(&BluetoothAdapterClientImpl::DeviceFoundConnected,
+ weak_ptr_factory_.GetWeakPtr(), path));
+
+ adapter_proxy->ConnectToSignal(
+ bluetooth_adapter::kBluetoothAdapterInterface,
+ bluetooth_adapter::kDeviceDisappearedSignal,
+ base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedReceived,
+ weak_ptr_factory_.GetWeakPtr(), path),
+ base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedConnected,
+ weak_ptr_factory_.GetWeakPtr(), path));
+ }
+
+ // Removes the dbus object proxy for the adapter with dbus path |path| from
+ // our |proxies_| map.
+ void RemoveObjectProxyForPath(const std::string& path) {
+ VLOG(1) << "RemoveObjectProxyForPath: " << path;
+ ProxyMap::iterator it = proxies_.find(path);
+ if (it != proxies_.end()) {
+ proxies_.erase(it);
+ }
+ }
+
+ // Called by dbus:: when a PropertyChanged signal is received.
+ void PropertyChangedReceived(const std::string& path, dbus::Signal* signal) {
+ CHECK(signal != NULL);
+ dbus::MessageReader reader(signal);
+ std::string name;
+ if (!reader.PopString(&name)) {
+ LOG(ERROR) << path
+ << ": PropertyChanged signal has incorrect parameters: "
+ << signal->ToString();
+ return;
+ }
+
+ if (name != bluetooth_adapter::kDiscoveringProperty) {
+ VLOG(1) << path << ": PropertyChanged: " << name;
+ // We don't care.
+ return;
+ }
+
+ bool discovering;
+ if (!reader.PopVariantOfBool(&discovering)) {
+ LOG(ERROR) << path
+ << ": PropertyChanged signal has incorrect parameters: "
+ << signal->ToString();
+ return;
+ }
+ VLOG(1) << path << ": PropertyChanged: Discovering = " << discovering;
+
+ FOR_EACH_OBSERVER(Observer, observers_,
+ DiscoveringPropertyChanged(path, discovering));
+ }
+
+ // Called by dbus:: when the PropertyChanged signal is initially connected.
+ void PropertyChangedConnected(const std::string& path,
+ const std::string& interface_name,
+ const std::string& signal_name,
+ bool success) {
+ LOG_IF(WARNING, !success) << path
+ << ": Failed to connect to PropertyChanged signal.";
+ }
+
+ // Called by dbus:: when a DeviceFound signal is received.
+ void DeviceFoundReceived(const std::string& path, dbus::Signal* signal) {
+ CHECK(signal != NULL);
+ dbus::MessageReader reader(signal);
+ std::string address;
+ if (!reader.PopString(&address)) {
+ LOG(ERROR) << path << ": DeviceFound signal has incorrect parameters: "
+ << signal->ToString();
+ return;
+ }
+ VLOG(1) << path << ": Device found: " << address;
+
+ DictionaryValue device_properties;
+ if (!reader.PopArrayOfDictEntries(&device_properties)) {
+ LOG(ERROR) << path << ": DeviceFound signal has incorrect parameters: "
+ << signal->ToString();
+ return;
+ }
+
+ FOR_EACH_OBSERVER(Observer, observers_, DeviceFound(path, address,
+ device_properties));
+ }
+
+ // Called by dbus:: when the DeviceFound signal is initially connected.
+ void DeviceFoundConnected(const std::string& path,
+ const std::string& interface_name,
+ const std::string& signal_name,
+ bool success) {
+ LOG_IF(WARNING, !success) << path
+ << ": Failed to connect to DeviceFound signal.";
+ }
+
+ // Called by dbus:: when a DeviceDisappeared signal is received.
+ void DeviceDisappearedReceived(const std::string& path,
+ dbus::Signal* signal) {
+ CHECK(signal != NULL);
+ dbus::MessageReader reader(signal);
+ std::string address;
+ if (!reader.PopString(&address)) {
+ LOG(ERROR) << path
+ << ": DeviceDisappeared signal has incorrect parameters: "
+ << signal->ToString();
+ return;
+ }
+ VLOG(1) << path << ": Device disappeared: " << address;
+ FOR_EACH_OBSERVER(Observer, observers_, DeviceDisappeared(path, address));
+ }
+
+ // Called by dbus:: when the DeviceDisappeared signal is initially connected.
+ void DeviceDisappearedConnected(const std::string& path,
+ const std::string& interface_name,
+ const std::string& signal_name,
+ bool success) {
+ LOG_IF(WARNING, !success) << path
+ << ": Failed to connect to DeviceDisappeared signal.";
+ }
+
+ // Called when a response for StartDiscovery() is received.
+ void OnStartDiscovery(const std::string& path, dbus::Response* response) {
+ VLOG(1) << "OnStartDiscovery: " << path;
+ CHECK(response != NULL);
+ }
+
+ // Called when a response for StopDiscovery() is received.
+ void OnStopDiscovery(const std::string& path, dbus::Response* response) {
+ VLOG(1) << "OnStopDiscovery: " << path;
+ CHECK(response != NULL);
+ }
+
+ // Weak pointer factory for generating 'this' pointers that might live longer
+ // than we do.
+ base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_;
+
+ dbus::Bus* bus_;
+
+ // We maintain a collection of dbus object proxies, one for each adapter.
+ typedef std::map<const std::string, dbus::ObjectProxy*> ProxyMap;
+ ProxyMap proxies_;
+
+ // List of observers interested in event notifications from us.
+ ObserverList<Observer> observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl);
+};
+
+// The BluetoothAdapterClient implementation used on Linux desktop, which does
+// nothing.
+class BluetoothAdapterClientStubImpl : public BluetoothAdapterClient {
+ public:
+ // BluetoothAdapterClient override.
+ virtual void AddObserver(Observer* observer, const std::string& path) {
+ VLOG(1) << "AddObserver: " << path;
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void RemoveObserver(Observer* observer, const std::string& path) {
+ VLOG(1) << "RemoveObserver: " << path;
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void StartDiscovery(const std::string& path) {
+ VLOG(1) << "StartDiscovery: " << path;
+ }
+
+ // BluetoothAdapterClient override.
+ virtual void StopDiscovery(const std::string& path) {
+ VLOG(1) << "StopDiscovery: " << path;
+ }
+};
+
+BluetoothAdapterClient::BluetoothAdapterClient() {
+}
+
+BluetoothAdapterClient::~BluetoothAdapterClient() {
+}
+
+BluetoothAdapterClient* BluetoothAdapterClient::Create(dbus::Bus* bus) {
+ if (system::runtime_environment::IsRunningOnChromeOS()) {
+ return new BluetoothAdapterClientImpl(bus);
+ } else {
+ return new BluetoothAdapterClientStubImpl();
+ }
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698