OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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_manager_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "chrome/browser/chromeos/system/runtime_environment.h" | |
10 #include "dbus/bus.h" | |
11 #include "dbus/message.h" | |
12 #include "dbus/object_proxy.h" | |
13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
14 | |
15 namespace chromeos { | |
16 | |
17 // The BluetoothManagerClient implementation used in production. | |
18 class BluetoothManagerClientImpl : public BluetoothManagerClient { | |
19 public: | |
20 explicit BluetoothManagerClientImpl(dbus::Bus* bus) | |
21 : weak_ptr_factory_(this), | |
22 bluetooth_manager_proxy_(NULL) { | |
23 VLOG(1) << "Creating BluetoothManagerClientImpl"; | |
24 | |
25 DCHECK(bus != NULL); | |
26 | |
27 bluetooth_manager_proxy_ = bus->GetObjectProxy( | |
28 bluetooth_manager::kBluetoothManagerServiceName, | |
29 bluetooth_manager::kBluetoothManagerServicePath); | |
30 DCHECK(bluetooth_manager_proxy_ != NULL); | |
satorux1
2011/10/25 19:55:17
I'd remove this.
Vince Laviano
2011/10/25 20:31:42
Done.
| |
31 | |
32 bluetooth_manager_proxy_->ConnectToSignal( | |
33 bluetooth_manager::kBluetoothManagerInterface, | |
34 bluetooth_manager::kAdapterRemovedSignal, | |
35 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedReceived, | |
36 weak_ptr_factory_.GetWeakPtr()), | |
37 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedConnected, | |
38 weak_ptr_factory_.GetWeakPtr())); | |
39 | |
40 bluetooth_manager_proxy_->ConnectToSignal( | |
41 bluetooth_manager::kBluetoothManagerInterface, | |
42 bluetooth_manager::kDefaultAdapterChangedSignal, | |
43 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedReceived, | |
44 weak_ptr_factory_.GetWeakPtr()), | |
45 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedConnected, | |
46 weak_ptr_factory_.GetWeakPtr())); | |
47 } | |
48 | |
49 virtual ~BluetoothManagerClientImpl() { | |
50 } | |
51 | |
52 // BluetoothManagerClient override. | |
53 virtual void AddObserver(Observer* observer) { | |
54 VLOG(1) << "AddObserver"; | |
55 DCHECK(observer != NULL); | |
56 observers_.AddObserver(observer); | |
57 } | |
58 | |
59 // BluetoothManagerClient override. | |
60 virtual void RemoveObserver(Observer* observer) { | |
61 VLOG(1) << "RemoveObserver"; | |
62 DCHECK(observer != NULL); | |
63 observers_.RemoveObserver(observer); | |
64 } | |
65 | |
66 // BluetoothManagerClient override. | |
67 virtual void DefaultAdapter(const DefaultAdapterCallback& callback) { | |
68 LOG(INFO) << "DefaultAdapter"; | |
69 | |
70 dbus::MethodCall method_call( | |
71 bluetooth_manager::kBluetoothManagerInterface, | |
72 bluetooth_manager::kDefaultAdapter); | |
73 | |
74 DCHECK(bluetooth_manager_proxy_ != NULL); | |
75 bluetooth_manager_proxy_->CallMethod( | |
76 &method_call, | |
77 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
78 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter, | |
79 weak_ptr_factory_.GetWeakPtr(), callback)); | |
80 } | |
81 | |
82 private: | |
83 // Called by dbus:: when an AdapterRemoved signal is received. | |
84 void AdapterRemovedReceived(dbus::Signal* signal) { | |
85 DCHECK(signal != NULL); | |
86 dbus::MessageReader reader(signal); | |
87 std::string adapter; | |
88 if (!reader.PopObjectPath(&adapter)) { | |
89 LOG(ERROR) << "AdapterRemoved signal has incorrect parameters: " | |
90 << signal->ToString(); | |
91 return; | |
92 } | |
93 VLOG(1) << "Adapter removed: " << adapter; | |
94 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(adapter)); | |
95 } | |
96 | |
97 // Called by dbus:: when the AdapterRemoved signal is initially connected. | |
98 void AdapterRemovedConnected(const std::string& interface_name, | |
99 const std::string& signal_name, | |
100 bool success) { | |
101 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal."; | |
102 } | |
103 | |
104 // Called by dbus:: when a DefaultAdapterChanged signal is received. | |
105 void DefaultAdapterChangedReceived(dbus::Signal* signal) { | |
106 DCHECK(signal != NULL); | |
107 dbus::MessageReader reader(signal); | |
108 std::string adapter; | |
109 if (!reader.PopObjectPath(&adapter)) { | |
110 LOG(ERROR) << "DefaultAdapterChanged signal has incorrect parameters: " | |
111 << signal->ToString(); | |
112 return; | |
113 } | |
114 VLOG(1) << "Default adapter changed: " << adapter; | |
115 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(adapter)); | |
116 } | |
117 | |
118 // Called by dbus:: when the DefaultAdapterChanged signal is initially | |
119 // connected. | |
120 void DefaultAdapterChangedConnected(const std::string& interface_name, | |
121 const std::string& signal_name, | |
122 bool success) { | |
123 LOG_IF(WARNING, !success) | |
124 << "Failed to connect to DefaultAdapterChanged signal."; | |
125 } | |
126 | |
127 // Called when a response for DefaultAdapter() is received. | |
128 void OnDefaultAdapter(const DefaultAdapterCallback& callback, | |
129 dbus::Response* response) { | |
130 // Parse response. | |
131 bool success = false; | |
132 std::string adapter; | |
133 if (response != NULL) { | |
134 dbus::MessageReader reader(response); | |
135 if (!reader.PopObjectPath(&adapter)) { | |
136 LOG(ERROR) << "DefaultAdapter response has incorrect parameters: " | |
137 << response->ToString(); | |
138 } else { | |
139 success = true; | |
140 LOG(INFO) << "OnDefaultAdapter: " << adapter; | |
141 } | |
142 } else { | |
143 LOG(ERROR) << "Failed to get default adapter."; | |
144 } | |
145 | |
146 // Notify client. | |
147 callback.Run(adapter, success); | |
148 } | |
149 | |
150 // Weak pointer factory for generating 'this' pointers that might live longer | |
151 // than we do. | |
152 base::WeakPtrFactory<BluetoothManagerClientImpl> weak_ptr_factory_; | |
153 | |
154 // D-Bus proxy for BlueZ Manager interface. | |
155 dbus::ObjectProxy* bluetooth_manager_proxy_; | |
156 | |
157 // List of observers interested in event notifications from us. | |
158 ObserverList<Observer> observers_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(BluetoothManagerClientImpl); | |
161 }; | |
162 | |
163 // The BluetoothManagerClient implementation used on Linux desktop, which does | |
164 // nothing. | |
165 class BluetoothManagerClientStubImpl : public BluetoothManagerClient { | |
166 public: | |
167 // BluetoothManagerClient override. | |
168 virtual void AddObserver(Observer* observer) { | |
169 } | |
170 | |
171 // BluetoothManagerClient override. | |
172 virtual void RemoveObserver(Observer* observer) { | |
173 } | |
174 | |
175 // BluetoothManagerClient override. | |
176 virtual void DefaultAdapter(const DefaultAdapterCallback& callback) { | |
177 VLOG(1) << "Requested default adapter."; | |
178 } | |
179 }; | |
180 | |
181 BluetoothManagerClient::BluetoothManagerClient() { | |
182 } | |
183 | |
184 BluetoothManagerClient::~BluetoothManagerClient() { | |
185 } | |
186 | |
187 BluetoothManagerClient* BluetoothManagerClient::Create(dbus::Bus* bus) { | |
188 if (system::runtime_environment::IsRunningOnChromeOS()) { | |
189 return new BluetoothManagerClientImpl(bus); | |
190 } else { | |
191 return new BluetoothManagerClientStubImpl(); | |
192 } | |
193 } | |
194 | |
195 } // namespace chromeos | |
OLD | NEW |