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

Side by Side Diff: device/bluetooth/bluetooth_pairing_bluez.cc

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_pairing_chromeos.h" 5 #include "device/bluetooth/bluetooth_pairing_bluez.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "device/bluetooth/bluetooth_device.h" 9 #include "device/bluetooth/bluetooth_device.h"
10 #include "device/bluetooth/bluetooth_device_chromeos.h" 10 #include "device/bluetooth/bluetooth_device_bluez.h"
11 11
12 using device::BluetoothDevice; 12 using device::BluetoothDevice;
13 13
14 namespace { 14 namespace {
15 15
16 // Histogram enumerations for pairing methods. 16 // Histogram enumerations for pairing methods.
17 enum UMAPairingMethod { 17 enum UMAPairingMethod {
18 UMA_PAIRING_METHOD_NONE, 18 UMA_PAIRING_METHOD_NONE,
19 UMA_PAIRING_METHOD_REQUEST_PINCODE, 19 UMA_PAIRING_METHOD_REQUEST_PINCODE,
20 UMA_PAIRING_METHOD_REQUEST_PASSKEY, 20 UMA_PAIRING_METHOD_REQUEST_PASSKEY,
21 UMA_PAIRING_METHOD_DISPLAY_PINCODE, 21 UMA_PAIRING_METHOD_DISPLAY_PINCODE,
22 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, 22 UMA_PAIRING_METHOD_DISPLAY_PASSKEY,
23 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, 23 UMA_PAIRING_METHOD_CONFIRM_PASSKEY,
24 // NOTE: Add new pairing methods immediately above this line. Make sure to 24 // NOTE: Add new pairing methods immediately above this line. Make sure to
25 // update the enum list in tools/histogram/histograms.xml accordingly. 25 // update the enum list in tools/histogram/histograms.xml accordingly.
26 UMA_PAIRING_METHOD_COUNT 26 UMA_PAIRING_METHOD_COUNT
27 }; 27 };
28 28
29 // Number of keys that will be entered for a passkey, six digits plus the 29 // Number of keys that will be entered for a passkey, six digits plus the
30 // final enter. 30 // final enter.
31 const uint16 kPasskeyMaxKeysEntered = 7; 31 const uint16 kPasskeyMaxKeysEntered = 7;
32 32
33 } // namespace 33 } // namespace
34 34
35 namespace chromeos { 35 namespace bluez {
36 36
37 BluetoothPairingChromeOS::BluetoothPairingChromeOS( 37 BluetoothPairingBlueZ::BluetoothPairingBlueZ(
38 BluetoothDeviceChromeOS* device, 38 BluetoothDeviceBlueZ* device,
39 BluetoothDevice::PairingDelegate* pairing_delegate) 39 BluetoothDevice::PairingDelegate* pairing_delegate)
40 : device_(device), 40 : device_(device),
41 pairing_delegate_(pairing_delegate), 41 pairing_delegate_(pairing_delegate),
42 pairing_delegate_used_(false) { 42 pairing_delegate_used_(false) {
43 VLOG(1) << "Created BluetoothPairingChromeOS for " 43 VLOG(1) << "Created BluetoothPairingBlueZ for " << device_->GetAddress();
44 << device_->GetAddress();
45 } 44 }
46 45
47 BluetoothPairingChromeOS::~BluetoothPairingChromeOS() { 46 BluetoothPairingBlueZ::~BluetoothPairingBlueZ() {
48 VLOG(1) << "Destroying BluetoothPairingChromeOS for " 47 VLOG(1) << "Destroying BluetoothPairingBlueZ for " << device_->GetAddress();
49 << device_->GetAddress();
50 48
51 if (!pairing_delegate_used_) { 49 if (!pairing_delegate_used_) {
52 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 50 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
53 UMA_PAIRING_METHOD_NONE, 51 UMA_PAIRING_METHOD_NONE,
54 UMA_PAIRING_METHOD_COUNT); 52 UMA_PAIRING_METHOD_COUNT);
55 } 53 }
56 54
57 if (!pincode_callback_.is_null()) { 55 if (!pincode_callback_.is_null()) {
58 pincode_callback_.Run( 56 pincode_callback_.Run(
59 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, ""); 57 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, "");
60 } 58 }
61 59
62 if (!passkey_callback_.is_null()) { 60 if (!passkey_callback_.is_null()) {
63 passkey_callback_.Run( 61 passkey_callback_.Run(
64 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, 0); 62 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, 0);
65 } 63 }
66 64
67 if (!confirmation_callback_.is_null()) { 65 if (!confirmation_callback_.is_null()) {
68 confirmation_callback_.Run( 66 confirmation_callback_.Run(
69 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED); 67 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED);
70 } 68 }
71 69
72 pairing_delegate_ = NULL; 70 pairing_delegate_ = NULL;
73 } 71 }
74 72
75 void BluetoothPairingChromeOS::RequestPinCode( 73 void BluetoothPairingBlueZ::RequestPinCode(
76 const bluez::BluetoothAgentServiceProvider::Delegate::PinCodeCallback& 74 const bluez::BluetoothAgentServiceProvider::Delegate::PinCodeCallback&
77 callback) { 75 callback) {
78 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 76 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
79 UMA_PAIRING_METHOD_REQUEST_PINCODE, 77 UMA_PAIRING_METHOD_REQUEST_PINCODE,
80 UMA_PAIRING_METHOD_COUNT); 78 UMA_PAIRING_METHOD_COUNT);
81 79
82 ResetCallbacks(); 80 ResetCallbacks();
83 pincode_callback_ = callback; 81 pincode_callback_ = callback;
84 pairing_delegate_used_ = true; 82 pairing_delegate_used_ = true;
85 pairing_delegate_->RequestPinCode(device_); 83 pairing_delegate_->RequestPinCode(device_);
86 } 84 }
87 85
88 bool BluetoothPairingChromeOS::ExpectingPinCode() const { 86 bool BluetoothPairingBlueZ::ExpectingPinCode() const {
89 return !pincode_callback_.is_null(); 87 return !pincode_callback_.is_null();
90 } 88 }
91 89
92 void BluetoothPairingChromeOS::SetPinCode(const std::string& pincode) { 90 void BluetoothPairingBlueZ::SetPinCode(const std::string& pincode) {
93 if (pincode_callback_.is_null()) 91 if (pincode_callback_.is_null())
94 return; 92 return;
95 93
96 pincode_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS, 94 pincode_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS,
97 pincode); 95 pincode);
98 pincode_callback_.Reset(); 96 pincode_callback_.Reset();
99 97
100 // If this is not an outgoing connection to the device, clean up the pairing 98 // If this is not an outgoing connection to the device, clean up the pairing
101 // context since the pairing is done. The outgoing connection case is cleaned 99 // context since the pairing is done. The outgoing connection case is cleaned
102 // up in the callback for the underlying Pair() call. 100 // up in the callback for the underlying Pair() call.
103 if (!device_->IsConnecting()) 101 if (!device_->IsConnecting())
104 device_->EndPairing(); 102 device_->EndPairing();
105 } 103 }
106 104
107 void BluetoothPairingChromeOS::DisplayPinCode(const std::string& pincode) { 105 void BluetoothPairingBlueZ::DisplayPinCode(const std::string& pincode) {
108 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 106 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
109 UMA_PAIRING_METHOD_DISPLAY_PINCODE, 107 UMA_PAIRING_METHOD_DISPLAY_PINCODE,
110 UMA_PAIRING_METHOD_COUNT); 108 UMA_PAIRING_METHOD_COUNT);
111 109
112 ResetCallbacks(); 110 ResetCallbacks();
113 pairing_delegate_used_ = true; 111 pairing_delegate_used_ = true;
114 pairing_delegate_->DisplayPinCode(device_, pincode); 112 pairing_delegate_->DisplayPinCode(device_, pincode);
115 113
116 // If this is not an outgoing connection to the device, the pairing context 114 // If this is not an outgoing connection to the device, the pairing context
117 // needs to be cleaned up again as there's no reliable indication of 115 // needs to be cleaned up again as there's no reliable indication of
118 // completion of incoming pairing. 116 // completion of incoming pairing.
119 if (!device_->IsConnecting()) 117 if (!device_->IsConnecting())
120 device_->EndPairing(); 118 device_->EndPairing();
121 } 119 }
122 120
123 void BluetoothPairingChromeOS::RequestPasskey( 121 void BluetoothPairingBlueZ::RequestPasskey(
124 const bluez::BluetoothAgentServiceProvider::Delegate::PasskeyCallback& 122 const bluez::BluetoothAgentServiceProvider::Delegate::PasskeyCallback&
125 callback) { 123 callback) {
126 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 124 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
127 UMA_PAIRING_METHOD_REQUEST_PASSKEY, 125 UMA_PAIRING_METHOD_REQUEST_PASSKEY,
128 UMA_PAIRING_METHOD_COUNT); 126 UMA_PAIRING_METHOD_COUNT);
129 127
130 ResetCallbacks(); 128 ResetCallbacks();
131 passkey_callback_ = callback; 129 passkey_callback_ = callback;
132 pairing_delegate_used_ = true; 130 pairing_delegate_used_ = true;
133 pairing_delegate_->RequestPasskey(device_); 131 pairing_delegate_->RequestPasskey(device_);
134 } 132 }
135 133
136 bool BluetoothPairingChromeOS::ExpectingPasskey() const { 134 bool BluetoothPairingBlueZ::ExpectingPasskey() const {
137 return !passkey_callback_.is_null(); 135 return !passkey_callback_.is_null();
138 } 136 }
139 137
140 void BluetoothPairingChromeOS::SetPasskey(uint32 passkey) { 138 void BluetoothPairingBlueZ::SetPasskey(uint32 passkey) {
141 if (passkey_callback_.is_null()) 139 if (passkey_callback_.is_null())
142 return; 140 return;
143 141
144 passkey_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS, 142 passkey_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS,
145 passkey); 143 passkey);
146 passkey_callback_.Reset(); 144 passkey_callback_.Reset();
147 145
148 // If this is not an outgoing connection to the device, clean up the pairing 146 // If this is not an outgoing connection to the device, clean up the pairing
149 // context since the pairing is done. The outgoing connection case is cleaned 147 // context since the pairing is done. The outgoing connection case is cleaned
150 // up in the callback for the underlying Pair() call. 148 // up in the callback for the underlying Pair() call.
151 if (!device_->IsConnecting()) 149 if (!device_->IsConnecting())
152 device_->EndPairing(); 150 device_->EndPairing();
153 } 151 }
154 152
155 void BluetoothPairingChromeOS::DisplayPasskey(uint32 passkey) { 153 void BluetoothPairingBlueZ::DisplayPasskey(uint32 passkey) {
156 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 154 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
157 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, 155 UMA_PAIRING_METHOD_DISPLAY_PASSKEY,
158 UMA_PAIRING_METHOD_COUNT); 156 UMA_PAIRING_METHOD_COUNT);
159 157
160 ResetCallbacks(); 158 ResetCallbacks();
161 pairing_delegate_used_ = true; 159 pairing_delegate_used_ = true;
162 pairing_delegate_->DisplayPasskey(device_, passkey); 160 pairing_delegate_->DisplayPasskey(device_, passkey);
163 } 161 }
164 162
165 void BluetoothPairingChromeOS::KeysEntered(uint16 entered) { 163 void BluetoothPairingBlueZ::KeysEntered(uint16 entered) {
166 pairing_delegate_used_ = true; 164 pairing_delegate_used_ = true;
167 pairing_delegate_->KeysEntered(device_, entered); 165 pairing_delegate_->KeysEntered(device_, entered);
168 166
169 // If this is not an outgoing connection to the device, the pairing context 167 // If this is not an outgoing connection to the device, the pairing context
170 // needs to be cleaned up again as there's no reliable indication of 168 // needs to be cleaned up again as there's no reliable indication of
171 // completion of incoming pairing. 169 // completion of incoming pairing.
172 if (entered >= kPasskeyMaxKeysEntered && !device_->IsConnecting()) 170 if (entered >= kPasskeyMaxKeysEntered && !device_->IsConnecting())
173 device_->EndPairing(); 171 device_->EndPairing();
174 } 172 }
175 173
176 void BluetoothPairingChromeOS::RequestConfirmation( 174 void BluetoothPairingBlueZ::RequestConfirmation(
177 uint32 passkey, 175 uint32 passkey,
178 const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& 176 const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
179 callback) { 177 callback) {
180 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 178 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
181 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, 179 UMA_PAIRING_METHOD_CONFIRM_PASSKEY,
182 UMA_PAIRING_METHOD_COUNT); 180 UMA_PAIRING_METHOD_COUNT);
183 181
184 ResetCallbacks(); 182 ResetCallbacks();
185 confirmation_callback_ = callback; 183 confirmation_callback_ = callback;
186 pairing_delegate_used_ = true; 184 pairing_delegate_used_ = true;
187 pairing_delegate_->ConfirmPasskey(device_, passkey); 185 pairing_delegate_->ConfirmPasskey(device_, passkey);
188 } 186 }
189 187
190 void BluetoothPairingChromeOS::RequestAuthorization( 188 void BluetoothPairingBlueZ::RequestAuthorization(
191 const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& 189 const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
192 callback) { 190 callback) {
193 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", 191 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", UMA_PAIRING_METHOD_NONE,
194 UMA_PAIRING_METHOD_NONE,
195 UMA_PAIRING_METHOD_COUNT); 192 UMA_PAIRING_METHOD_COUNT);
196 193
197 ResetCallbacks(); 194 ResetCallbacks();
198 confirmation_callback_ = callback; 195 confirmation_callback_ = callback;
199 pairing_delegate_used_ = true; 196 pairing_delegate_used_ = true;
200 pairing_delegate_->AuthorizePairing(device_); 197 pairing_delegate_->AuthorizePairing(device_);
201 } 198 }
202 199
203 bool BluetoothPairingChromeOS::ExpectingConfirmation() const { 200 bool BluetoothPairingBlueZ::ExpectingConfirmation() const {
204 return !confirmation_callback_.is_null(); 201 return !confirmation_callback_.is_null();
205 } 202 }
206 203
207 void BluetoothPairingChromeOS::ConfirmPairing() { 204 void BluetoothPairingBlueZ::ConfirmPairing() {
208 if (confirmation_callback_.is_null()) 205 if (confirmation_callback_.is_null())
209 return; 206 return;
210 207
211 confirmation_callback_.Run( 208 confirmation_callback_.Run(
212 bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS); 209 bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS);
213 confirmation_callback_.Reset(); 210 confirmation_callback_.Reset();
214 211
215 // If this is not an outgoing connection to the device, clean up the pairing 212 // If this is not an outgoing connection to the device, clean up the pairing
216 // context since the pairing is done. The outgoing connection case is cleaned 213 // context since the pairing is done. The outgoing connection case is cleaned
217 // up in the callback for the underlying Pair() call. 214 // up in the callback for the underlying Pair() call.
218 if (!device_->IsConnecting()) 215 if (!device_->IsConnecting())
219 device_->EndPairing(); 216 device_->EndPairing();
220 } 217 }
221 218
222 bool BluetoothPairingChromeOS::RejectPairing() { 219 bool BluetoothPairingBlueZ::RejectPairing() {
223 return RunPairingCallbacks( 220 return RunPairingCallbacks(
224 bluez::BluetoothAgentServiceProvider::Delegate::REJECTED); 221 bluez::BluetoothAgentServiceProvider::Delegate::REJECTED);
225 } 222 }
226 223
227 bool BluetoothPairingChromeOS::CancelPairing() { 224 bool BluetoothPairingBlueZ::CancelPairing() {
228 return RunPairingCallbacks( 225 return RunPairingCallbacks(
229 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED); 226 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED);
230 } 227 }
231 228
232 BluetoothDevice::PairingDelegate* 229 BluetoothDevice::PairingDelegate* BluetoothPairingBlueZ::GetPairingDelegate()
233 BluetoothPairingChromeOS::GetPairingDelegate() const { 230 const {
234 return pairing_delegate_; 231 return pairing_delegate_;
235 } 232 }
236 233
237 void BluetoothPairingChromeOS::ResetCallbacks() { 234 void BluetoothPairingBlueZ::ResetCallbacks() {
238 pincode_callback_.Reset(); 235 pincode_callback_.Reset();
239 passkey_callback_.Reset(); 236 passkey_callback_.Reset();
240 confirmation_callback_.Reset(); 237 confirmation_callback_.Reset();
241 } 238 }
242 239
243 bool BluetoothPairingChromeOS::RunPairingCallbacks( 240 bool BluetoothPairingBlueZ::RunPairingCallbacks(
244 bluez::BluetoothAgentServiceProvider::Delegate::Status status) { 241 bluez::BluetoothAgentServiceProvider::Delegate::Status status) {
245 pairing_delegate_used_ = true; 242 pairing_delegate_used_ = true;
246 243
247 bool callback_run = false; 244 bool callback_run = false;
248 if (!pincode_callback_.is_null()) { 245 if (!pincode_callback_.is_null()) {
249 pincode_callback_.Run(status, ""); 246 pincode_callback_.Run(status, "");
250 pincode_callback_.Reset(); 247 pincode_callback_.Reset();
251 callback_run = true; 248 callback_run = true;
252 } 249 }
253 250
(...skipping 11 matching lines...) Expand all
265 262
266 // If this is not an outgoing connection to the device, clean up the pairing 263 // If this is not an outgoing connection to the device, clean up the pairing
267 // context since the pairing is done. The outgoing connection case is cleaned 264 // context since the pairing is done. The outgoing connection case is cleaned
268 // up in the callback for the underlying Pair() call. 265 // up in the callback for the underlying Pair() call.
269 if (!device_->IsConnecting()) 266 if (!device_->IsConnecting())
270 device_->EndPairing(); 267 device_->EndPairing();
271 268
272 return callback_run; 269 return callback_run;
273 } 270 }
274 271
275 } // namespace chromeos 272 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698