diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-06 01:54:36 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-06 01:54:36 +0000 |
commit | 64e199254481d6132c3a7ccdf9b3a743bff86a88 (patch) | |
tree | c10cdf03cbde8439d1d8a7070a6c7f9fdf10f517 /chromeos/dbus/bluetooth_node_client.cc | |
parent | 81a476c9ce1088345db02cf1647399d0e673d487 (diff) | |
download | chromium_src-64e199254481d6132c3a7ccdf9b3a743bff86a88.zip chromium_src-64e199254481d6132c3a7ccdf9b3a743bff86a88.tar.gz chromium_src-64e199254481d6132c3a7ccdf9b3a743bff86a88.tar.bz2 |
Move files inside chrome/browser/chromeos/dbus to chromeos/dbus
Move files in chrome/browser/chromeos/dbus/ to chromeos/dbus
Add chromeos/dbus/DEPS
Add chromeos.gyp:chromeos_test_support and chromeos.gyp:chromeos_unittests
Add CHROMEOS_EXPORT to classes
Move power related proto targets to chromeos.gyp
Rewrite and sort #includes
BUG=119583
TEST=component chromeos build success, checkdeps success
Review URL: https://chromiumcodereview.appspot.com/9838085
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131065 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos/dbus/bluetooth_node_client.cc')
-rw-r--r-- | chromeos/dbus/bluetooth_node_client.cc | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/chromeos/dbus/bluetooth_node_client.cc b/chromeos/dbus/bluetooth_node_client.cc new file mode 100644 index 0000000..52959cf --- /dev/null +++ b/chromeos/dbus/bluetooth_node_client.cc @@ -0,0 +1,201 @@ +// 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 "chromeos/dbus/bluetooth_node_client.h" + +#include <map> + +#include "base/bind.h" +#include "base/logging.h" +#include "base/stl_util.h" +#include "chromeos/dbus/bluetooth_device_client.h" +#include "chromeos/dbus/bluetooth_property.h" +#include "dbus/bus.h" +#include "dbus/message.h" +#include "dbus/object_path.h" +#include "dbus/object_proxy.h" +#include "third_party/cros_system_api/dbus/service_constants.h" + +namespace chromeos { + +BluetoothNodeClient::Properties::Properties(dbus::ObjectProxy* object_proxy, + PropertyChangedCallback callback) + : BluetoothPropertySet(object_proxy, + bluetooth_node::kBluetoothNodeInterface, + callback) { + RegisterProperty(bluetooth_node::kNameProperty, &name); + RegisterProperty(bluetooth_node::kDeviceProperty, &device); +} + +BluetoothNodeClient::Properties::~Properties() { +} + + +// The BluetoothNodeClient implementation used in production. +class BluetoothNodeClientImpl: public BluetoothNodeClient, + private BluetoothDeviceClient::Observer { + public: + BluetoothNodeClientImpl(dbus::Bus* bus, + BluetoothDeviceClient* device_client) + : weak_ptr_factory_(this), + bus_(bus) { + DVLOG(1) << "Creating BluetoothNodeClientImpl"; + + DCHECK(device_client); + device_client->AddObserver(this); + } + + virtual ~BluetoothNodeClientImpl() { + // Clean up Properties structures + for (ObjectMap::iterator iter = object_map_.begin(); + iter != object_map_.end(); ++iter) { + Object object = iter->second; + Properties* properties = object.second; + delete properties; + } + } + + // BluetoothNodeClient override. + virtual void AddObserver(BluetoothNodeClient::Observer* observer) + OVERRIDE { + DCHECK(observer); + observers_.AddObserver(observer); + } + + // BluetoothNodeClient override. + virtual void RemoveObserver(BluetoothNodeClient::Observer* observer) + OVERRIDE { + DCHECK(observer); + observers_.RemoveObserver(observer); + } + + // BluetoothNodeClient override. + virtual Properties* GetProperties(const dbus::ObjectPath& object_path) + OVERRIDE { + return GetObject(object_path).second; + } + + private: + // We maintain a collection of dbus object proxies and properties structures + // for each node binding. + typedef std::pair<dbus::ObjectProxy*, Properties*> Object; + typedef std::map<const dbus::ObjectPath, Object> ObjectMap; + ObjectMap object_map_; + + // BluetoothDeviceClient::Observer override. + virtual void NodeCreated(const dbus::ObjectPath& device_path, + const dbus::ObjectPath& object_path) OVERRIDE { + } + + // BluetoothDeviceClient::Observer override. + virtual void NodeRemoved(const dbus::ObjectPath& device_path, + const dbus::ObjectPath& object_path) OVERRIDE { + RemoveObject(object_path); + } + + // Ensures that we have an object proxy and properties structure for + // a node binding with object path |object_path|, creating it if not and + // storing it in our |object_map_| map. + Object GetObject(const dbus::ObjectPath& object_path) { + ObjectMap::iterator iter = object_map_.find(object_path); + if (iter != object_map_.end()) + return iter->second; + + // Create the object proxy. + DCHECK(bus_); + dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy( + bluetooth_node::kBluetoothNodeServiceName, object_path); + + // Create the properties structure. + Properties* properties = new Properties( + object_proxy, + base::Bind(&BluetoothNodeClientImpl::OnPropertyChanged, + weak_ptr_factory_.GetWeakPtr(), object_path)); + + properties->ConnectSignals(); + properties->GetAll(); + + Object object = std::make_pair(object_proxy, properties); + object_map_[object_path] = object; + return object; + } + + // Removes the dbus object proxy and properties for the node binding with + // dbus object path |object_path| from our |object_map_| map. + void RemoveObject(const dbus::ObjectPath& object_path) { + ObjectMap::iterator iter = object_map_.find(object_path); + if (iter != object_map_.end()) { + // Clean up the Properties structure. + Object object = iter->second; + Properties* properties = object.second; + delete properties; + + object_map_.erase(iter); + } + } + + // Returns a pointer to the object proxy for |object_path|, creating + // it if necessary. + dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) { + return GetObject(object_path).first; + } + + // 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(BluetoothNodeClient::Observer, observers_, + NodePropertyChanged(object_path, property_name)); + } + + // Weak pointer factory for generating 'this' pointers that might live longer + // than we do. + base::WeakPtrFactory<BluetoothNodeClientImpl> weak_ptr_factory_; + + dbus::Bus* bus_; + + // List of observers interested in event notifications from us. + ObserverList<BluetoothNodeClient::Observer> observers_; + + DISALLOW_COPY_AND_ASSIGN(BluetoothNodeClientImpl); +}; + +// The BluetoothNodeClient implementation used on Linux desktop, which does +// nothing. +class BluetoothNodeClientStubImpl : public BluetoothNodeClient { + public: + // BluetoothNodeClient override. + virtual void AddObserver(Observer* observer) OVERRIDE { + } + + // BluetoothNodeClient override. + virtual void RemoveObserver(Observer* observer) OVERRIDE { + } + + // BluetoothNodeClient override. + virtual Properties* GetProperties(const dbus::ObjectPath& object_path) + OVERRIDE { + VLOG(1) << "GetProperties: " << object_path.value(); + return NULL; + } +}; + +BluetoothNodeClient::BluetoothNodeClient() { +} + +BluetoothNodeClient::~BluetoothNodeClient() { +} + +BluetoothNodeClient* BluetoothNodeClient::Create( + DBusClientImplementationType type, + dbus::Bus* bus, + BluetoothDeviceClient* adapter_client) { + if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) + return new BluetoothNodeClientImpl(bus, adapter_client); + DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); + return new BluetoothNodeClientStubImpl(); +} + +} // namespace chromeos |