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

Side by Side Diff: chrome/browser/chromeos/dbus/bluetooth_node_client.cc

Issue 9838085: Move files inside chrome/browser/chromeos/dbus to chromeos/dbus (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: _ Created 8 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/dbus/bluetooth_node_client.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/chromeos/chromeos_version.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "chrome/browser/chromeos/dbus/bluetooth_device_client.h"
14 #include "chrome/browser/chromeos/dbus/bluetooth_property.h"
15 #include "dbus/bus.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
20
21 namespace chromeos {
22
23 BluetoothNodeClient::Properties::Properties(dbus::ObjectProxy* object_proxy,
24 PropertyChangedCallback callback)
25 : BluetoothPropertySet(object_proxy,
26 bluetooth_node::kBluetoothNodeInterface,
27 callback) {
28 RegisterProperty(bluetooth_node::kNameProperty, &name);
29 RegisterProperty(bluetooth_node::kDeviceProperty, &device);
30 }
31
32 BluetoothNodeClient::Properties::~Properties() {
33 }
34
35
36 // The BluetoothNodeClient implementation used in production.
37 class BluetoothNodeClientImpl: public BluetoothNodeClient,
38 private BluetoothDeviceClient::Observer {
39 public:
40 BluetoothNodeClientImpl(dbus::Bus* bus,
41 BluetoothDeviceClient* device_client)
42 : weak_ptr_factory_(this),
43 bus_(bus) {
44 DVLOG(1) << "Creating BluetoothNodeClientImpl";
45
46 DCHECK(device_client);
47 device_client->AddObserver(this);
48 }
49
50 virtual ~BluetoothNodeClientImpl() {
51 // Clean up Properties structures
52 for (ObjectMap::iterator iter = object_map_.begin();
53 iter != object_map_.end(); ++iter) {
54 Object object = iter->second;
55 Properties* properties = object.second;
56 delete properties;
57 }
58 }
59
60 // BluetoothNodeClient override.
61 virtual void AddObserver(BluetoothNodeClient::Observer* observer)
62 OVERRIDE {
63 DCHECK(observer);
64 observers_.AddObserver(observer);
65 }
66
67 // BluetoothNodeClient override.
68 virtual void RemoveObserver(BluetoothNodeClient::Observer* observer)
69 OVERRIDE {
70 DCHECK(observer);
71 observers_.RemoveObserver(observer);
72 }
73
74 // BluetoothNodeClient override.
75 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
76 OVERRIDE {
77 return GetObject(object_path).second;
78 }
79
80 private:
81 // We maintain a collection of dbus object proxies and properties structures
82 // for each node binding.
83 typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
84 typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
85 ObjectMap object_map_;
86
87 // BluetoothDeviceClient::Observer override.
88 virtual void NodeCreated(const dbus::ObjectPath& device_path,
89 const dbus::ObjectPath& object_path) OVERRIDE {
90 }
91
92 // BluetoothDeviceClient::Observer override.
93 virtual void NodeRemoved(const dbus::ObjectPath& device_path,
94 const dbus::ObjectPath& object_path) OVERRIDE {
95 RemoveObject(object_path);
96 }
97
98 // Ensures that we have an object proxy and properties structure for
99 // a node binding with object path |object_path|, creating it if not and
100 // storing it in our |object_map_| map.
101 Object GetObject(const dbus::ObjectPath& object_path) {
102 ObjectMap::iterator iter = object_map_.find(object_path);
103 if (iter != object_map_.end())
104 return iter->second;
105
106 // Create the object proxy.
107 DCHECK(bus_);
108 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
109 bluetooth_node::kBluetoothNodeServiceName, object_path);
110
111 // Create the properties structure.
112 Properties* properties = new Properties(
113 object_proxy,
114 base::Bind(&BluetoothNodeClientImpl::OnPropertyChanged,
115 weak_ptr_factory_.GetWeakPtr(), object_path));
116
117 properties->ConnectSignals();
118 properties->GetAll();
119
120 Object object = std::make_pair(object_proxy, properties);
121 object_map_[object_path] = object;
122 return object;
123 }
124
125 // Removes the dbus object proxy and properties for the node binding with
126 // dbus object path |object_path| from our |object_map_| map.
127 void RemoveObject(const dbus::ObjectPath& object_path) {
128 ObjectMap::iterator iter = object_map_.find(object_path);
129 if (iter != object_map_.end()) {
130 // Clean up the Properties structure.
131 Object object = iter->second;
132 Properties* properties = object.second;
133 delete properties;
134
135 object_map_.erase(iter);
136 }
137 }
138
139 // Returns a pointer to the object proxy for |object_path|, creating
140 // it if necessary.
141 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
142 return GetObject(object_path).first;
143 }
144
145 // Called by BluetoothPropertySet when a property value is changed,
146 // either by result of a signal or response to a GetAll() or Get()
147 // call. Informs observers.
148 void OnPropertyChanged(const dbus::ObjectPath& object_path,
149 const std::string& property_name) {
150 FOR_EACH_OBSERVER(BluetoothNodeClient::Observer, observers_,
151 NodePropertyChanged(object_path, property_name));
152 }
153
154 // Weak pointer factory for generating 'this' pointers that might live longer
155 // than we do.
156 base::WeakPtrFactory<BluetoothNodeClientImpl> weak_ptr_factory_;
157
158 dbus::Bus* bus_;
159
160 // List of observers interested in event notifications from us.
161 ObserverList<BluetoothNodeClient::Observer> observers_;
162
163 DISALLOW_COPY_AND_ASSIGN(BluetoothNodeClientImpl);
164 };
165
166 // The BluetoothNodeClient implementation used on Linux desktop, which does
167 // nothing.
168 class BluetoothNodeClientStubImpl : public BluetoothNodeClient {
169 public:
170 // BluetoothNodeClient override.
171 virtual void AddObserver(Observer* observer) OVERRIDE {
172 }
173
174 // BluetoothNodeClient override.
175 virtual void RemoveObserver(Observer* observer) OVERRIDE {
176 }
177
178 // BluetoothNodeClient override.
179 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
180 OVERRIDE {
181 VLOG(1) << "GetProperties: " << object_path.value();
182 return NULL;
183 }
184 };
185
186 BluetoothNodeClient::BluetoothNodeClient() {
187 }
188
189 BluetoothNodeClient::~BluetoothNodeClient() {
190 }
191
192 BluetoothNodeClient* BluetoothNodeClient::Create(
193 dbus::Bus* bus,
194 BluetoothDeviceClient* adapter_client) {
195 if (base::chromeos::IsRunningOnChromeOS()) {
196 return new BluetoothNodeClientImpl(bus, adapter_client);
197 } else {
198 return new BluetoothNodeClientStubImpl();
199 }
200 }
201
202 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698