1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// 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/dbus/bluetooth_device_client.h"
#include <map>
#include "base/bind.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h"
#include "chrome/browser/chromeos/system/runtime_environment.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 {
// The BluetoothDeviceClient implementation used in production.
class BluetoothDeviceClientImpl: public BluetoothDeviceClient,
private BluetoothAdapterClient::Observer {
public:
BluetoothDeviceClientImpl(dbus::Bus* bus,
BluetoothAdapterClient* adapter_client)
: weak_ptr_factory_(this),
bus_(bus) {
VLOG(1) << "Creating BluetoothDeviceClientImpl";
DCHECK(adapter_client);
adapter_client->AddObserver(this);
}
virtual ~BluetoothDeviceClientImpl() {
}
// BluetoothDeviceClient override.
virtual void AddObserver(BluetoothDeviceClient::Observer* observer)
OVERRIDE {
VLOG(1) << "AddObserver";
DCHECK(observer);
observers_.AddObserver(observer);
}
// BluetoothDeviceClient override.
virtual void RemoveObserver(BluetoothDeviceClient::Observer* observer)
OVERRIDE {
VLOG(1) << "RemoveObserver";
DCHECK(observer);
observers_.RemoveObserver(observer);
}
private:
// BluetoothAdapterClient::Observer override.
virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "DeviceCreated: " << object_path.value();
}
// BluetoothAdapterClient::Observer override.
virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "DeviceRemoved: " << object_path.value();
RemoveObjectProxyForPath(object_path);
}
// Ensures that we have a dbus object proxy for a device with dbus
// object path |object_path|, and if not, creates it stores it in
// our |proxy_map_| map.
dbus::ObjectProxy* GetObjectProxyForPath(
const dbus::ObjectPath& object_path) {
VLOG(1) << "GetObjectProxyForPath: " << object_path.value();
ProxyMap::iterator it = proxy_map_.find(object_path);
if (it != proxy_map_.end())
return it->second;
DCHECK(bus_);
dbus::ObjectProxy* device_proxy = bus_->GetObjectProxy(
bluetooth_device::kBluetoothDeviceServiceName, object_path);
proxy_map_[object_path] = device_proxy;
return device_proxy;
}
// Removes the dbus object proxy for the device with dbus object path
// |object_path| from our |proxy_map_| map.
void RemoveObjectProxyForPath(const dbus::ObjectPath& object_path) {
VLOG(1) << "RemoveObjectProxyForPath: " << object_path.value();
proxy_map_.erase(object_path);
}
// Weak pointer factory for generating 'this' pointers that might live longer
// than we do.
base::WeakPtrFactory<BluetoothDeviceClientImpl> weak_ptr_factory_;
dbus::Bus* bus_;
// We maintain a collection of dbus object proxies, one for each device.
typedef std::map<const dbus::ObjectPath, dbus::ObjectProxy*> ProxyMap;
ProxyMap proxy_map_;
// List of observers interested in event notifications from us.
ObserverList<BluetoothDeviceClient::Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceClientImpl);
};
// The BluetoothDeviceClient implementation used on Linux desktop, which does
// nothing.
class BluetoothDeviceClientStubImpl : public BluetoothDeviceClient {
public:
// BluetoothDeviceClient override.
virtual void AddObserver(Observer* observer) OVERRIDE {
VLOG(1) << "AddObserver";
}
// BluetoothDeviceClient override.
virtual void RemoveObserver(Observer* observer) OVERRIDE {
VLOG(1) << "RemoveObserver";
}
};
BluetoothDeviceClient::BluetoothDeviceClient() {
}
BluetoothDeviceClient::~BluetoothDeviceClient() {
}
BluetoothDeviceClient* BluetoothDeviceClient::Create(
dbus::Bus* bus,
BluetoothAdapterClient* adapter_client) {
if (system::runtime_environment::IsRunningOnChromeOS()) {
return new BluetoothDeviceClientImpl(bus, adapter_client);
} else {
return new BluetoothDeviceClientStubImpl();
}
}
} // namespace chromeos
|