OLD | NEW |
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_chromeos.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_chromeos.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 |
| 30 // final enter. |
| 31 const uint16 kPasskeyMaxKeysEntered = 7; |
| 32 |
29 } // namespace | 33 } // namespace |
30 | 34 |
31 namespace chromeos { | 35 namespace chromeos { |
32 | 36 |
33 BluetoothPairingChromeOS::BluetoothPairingChromeOS( | 37 BluetoothPairingChromeOS::BluetoothPairingChromeOS( |
34 BluetoothDeviceChromeOS* device, | 38 BluetoothDeviceChromeOS* device, |
35 BluetoothDevice::PairingDelegate* pairing_delegate) | 39 BluetoothDevice::PairingDelegate* pairing_delegate) |
36 : device_(device), | 40 : device_(device), |
37 pairing_delegate_(pairing_delegate), | 41 pairing_delegate_(pairing_delegate), |
38 pairing_delegate_used_(false) { | 42 pairing_delegate_used_(false) { |
(...skipping 28 matching lines...) Expand all Loading... |
67 | 71 |
68 pairing_delegate_ = NULL; | 72 pairing_delegate_ = NULL; |
69 } | 73 } |
70 | 74 |
71 void BluetoothPairingChromeOS::RequestPinCode( | 75 void BluetoothPairingChromeOS::RequestPinCode( |
72 const BluetoothAgentServiceProvider::Delegate::PinCodeCallback& callback) { | 76 const BluetoothAgentServiceProvider::Delegate::PinCodeCallback& callback) { |
73 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 77 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", |
74 UMA_PAIRING_METHOD_REQUEST_PINCODE, | 78 UMA_PAIRING_METHOD_REQUEST_PINCODE, |
75 UMA_PAIRING_METHOD_COUNT); | 79 UMA_PAIRING_METHOD_COUNT); |
76 | 80 |
77 DCHECK(pincode_callback_.is_null()); | 81 ResetCallbacks(); |
78 pincode_callback_ = callback; | 82 pincode_callback_ = callback; |
| 83 pairing_delegate_used_ = true; |
79 pairing_delegate_->RequestPinCode(device_); | 84 pairing_delegate_->RequestPinCode(device_); |
80 pairing_delegate_used_ = true; | |
81 } | 85 } |
82 | 86 |
83 bool BluetoothPairingChromeOS::ExpectingPinCode() const { | 87 bool BluetoothPairingChromeOS::ExpectingPinCode() const { |
84 return !pincode_callback_.is_null(); | 88 return !pincode_callback_.is_null(); |
85 } | 89 } |
86 | 90 |
87 void BluetoothPairingChromeOS::SetPinCode(const std::string& pincode) { | 91 void BluetoothPairingChromeOS::SetPinCode(const std::string& pincode) { |
88 if (pincode_callback_.is_null()) | 92 if (pincode_callback_.is_null()) |
89 return; | 93 return; |
90 | 94 |
91 pincode_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS, | 95 pincode_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS, |
92 pincode); | 96 pincode); |
93 pincode_callback_.Reset(); | 97 pincode_callback_.Reset(); |
94 | 98 |
95 // If this is not an outgoing connection to the device, clean up the pairing | 99 // If this is not an outgoing connection to the device, clean up the pairing |
96 // context since the pairing is done. The outgoing connection case is cleaned | 100 // context since the pairing is done. The outgoing connection case is cleaned |
97 // up in the callback for the underlying Pair() call. | 101 // up in the callback for the underlying Pair() call. |
98 if (!device_->IsConnecting()) | 102 if (!device_->IsConnecting()) |
99 device_->EndPairing(); | 103 device_->EndPairing(); |
100 } | 104 } |
101 | 105 |
102 void BluetoothPairingChromeOS::DisplayPinCode(const std::string& pincode) { | 106 void BluetoothPairingChromeOS::DisplayPinCode(const std::string& pincode) { |
103 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 107 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", |
104 UMA_PAIRING_METHOD_DISPLAY_PINCODE, | 108 UMA_PAIRING_METHOD_DISPLAY_PINCODE, |
105 UMA_PAIRING_METHOD_COUNT); | 109 UMA_PAIRING_METHOD_COUNT); |
106 | 110 |
| 111 ResetCallbacks(); |
| 112 pairing_delegate_used_ = true; |
107 pairing_delegate_->DisplayPinCode(device_, pincode); | 113 pairing_delegate_->DisplayPinCode(device_, pincode); |
108 pairing_delegate_used_ = true; | |
109 | 114 |
110 // If this is not an outgoing connection to the device, the pairing context | 115 // If this is not an outgoing connection to the device, the pairing context |
111 // needs to be cleaned up again as there's no reliable indication of | 116 // needs to be cleaned up again as there's no reliable indication of |
112 // completion of incoming pairing. | 117 // completion of incoming pairing. |
113 if (!device_->IsConnecting()) | 118 if (!device_->IsConnecting()) |
114 device_->EndPairing(); | 119 device_->EndPairing(); |
115 } | 120 } |
116 | 121 |
117 void BluetoothPairingChromeOS::RequestPasskey( | 122 void BluetoothPairingChromeOS::RequestPasskey( |
118 const BluetoothAgentServiceProvider::Delegate::PasskeyCallback& callback) { | 123 const BluetoothAgentServiceProvider::Delegate::PasskeyCallback& callback) { |
119 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 124 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", |
120 UMA_PAIRING_METHOD_REQUEST_PASSKEY, | 125 UMA_PAIRING_METHOD_REQUEST_PASSKEY, |
121 UMA_PAIRING_METHOD_COUNT); | 126 UMA_PAIRING_METHOD_COUNT); |
122 | 127 |
123 DCHECK(passkey_callback_.is_null()); | 128 ResetCallbacks(); |
124 passkey_callback_ = callback; | 129 passkey_callback_ = callback; |
| 130 pairing_delegate_used_ = true; |
125 pairing_delegate_->RequestPasskey(device_); | 131 pairing_delegate_->RequestPasskey(device_); |
126 pairing_delegate_used_ = true; | |
127 } | 132 } |
128 | 133 |
129 bool BluetoothPairingChromeOS::ExpectingPasskey() const { | 134 bool BluetoothPairingChromeOS::ExpectingPasskey() const { |
130 return !passkey_callback_.is_null(); | 135 return !passkey_callback_.is_null(); |
131 } | 136 } |
132 | 137 |
133 void BluetoothPairingChromeOS::SetPasskey(uint32 passkey) { | 138 void BluetoothPairingChromeOS::SetPasskey(uint32 passkey) { |
134 if (passkey_callback_.is_null()) | 139 if (passkey_callback_.is_null()) |
135 return; | 140 return; |
136 | 141 |
137 passkey_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS, | 142 passkey_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS, |
138 passkey); | 143 passkey); |
139 passkey_callback_.Reset(); | 144 passkey_callback_.Reset(); |
140 | 145 |
141 // 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 |
142 // 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 |
143 // up in the callback for the underlying Pair() call. | 148 // up in the callback for the underlying Pair() call. |
144 if (!device_->IsConnecting()) | 149 if (!device_->IsConnecting()) |
145 device_->EndPairing(); | 150 device_->EndPairing(); |
146 } | 151 } |
147 | 152 |
148 void BluetoothPairingChromeOS::DisplayPasskey(uint32 passkey) { | 153 void BluetoothPairingChromeOS::DisplayPasskey(uint32 passkey) { |
149 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 154 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", |
150 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, | 155 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, |
151 UMA_PAIRING_METHOD_COUNT); | 156 UMA_PAIRING_METHOD_COUNT); |
152 | 157 |
| 158 ResetCallbacks(); |
| 159 pairing_delegate_used_ = true; |
| 160 pairing_delegate_->DisplayPasskey(device_, passkey); |
153 | 161 |
154 pairing_delegate_->DisplayPasskey(device_, passkey); | 162 } |
| 163 |
| 164 void BluetoothPairingChromeOS::KeysEntered(uint16 entered) { |
155 pairing_delegate_used_ = true; | 165 pairing_delegate_used_ = true; |
| 166 pairing_delegate_->KeysEntered(device_, entered); |
156 | 167 |
157 // If this is not an outgoing connection to the device, the pairing context | 168 // If this is not an outgoing connection to the device, the pairing context |
158 // needs to be cleaned up again as there's no reliable indication of | 169 // needs to be cleaned up again as there's no reliable indication of |
159 // completion of incoming pairing. | 170 // completion of incoming pairing. |
160 if (!device_->IsConnecting()) | 171 if (entered >= kPasskeyMaxKeysEntered && !device_->IsConnecting()) |
161 device_->EndPairing(); | 172 device_->EndPairing(); |
162 } | 173 } |
163 | 174 |
164 void BluetoothPairingChromeOS::KeysEntered(uint16 entered) { | |
165 pairing_delegate_->KeysEntered(device_, entered); | |
166 pairing_delegate_used_ = true; | |
167 } | |
168 | |
169 void BluetoothPairingChromeOS::RequestConfirmation( | 175 void BluetoothPairingChromeOS::RequestConfirmation( |
170 uint32 passkey, | 176 uint32 passkey, |
171 const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& | 177 const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& |
172 callback) { | 178 callback) { |
173 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 179 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", |
174 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, | 180 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, |
175 UMA_PAIRING_METHOD_COUNT); | 181 UMA_PAIRING_METHOD_COUNT); |
176 | 182 |
177 DCHECK(confirmation_callback_.is_null()); | 183 ResetCallbacks(); |
178 confirmation_callback_ = callback; | 184 confirmation_callback_ = callback; |
| 185 pairing_delegate_used_ = true; |
179 pairing_delegate_->ConfirmPasskey(device_, passkey); | 186 pairing_delegate_->ConfirmPasskey(device_, passkey); |
180 pairing_delegate_used_ = true; | |
181 } | 187 } |
182 | 188 |
183 void BluetoothPairingChromeOS::RequestAuthorization( | 189 void BluetoothPairingChromeOS::RequestAuthorization( |
184 const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& | 190 const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& |
185 callback) { | 191 callback) { |
186 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 192 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", |
187 UMA_PAIRING_METHOD_NONE, | 193 UMA_PAIRING_METHOD_NONE, |
188 UMA_PAIRING_METHOD_COUNT); | 194 UMA_PAIRING_METHOD_COUNT); |
189 | 195 |
190 DCHECK(confirmation_callback_.is_null()); | 196 ResetCallbacks(); |
191 confirmation_callback_ = callback; | 197 confirmation_callback_ = callback; |
| 198 pairing_delegate_used_ = true; |
192 pairing_delegate_->AuthorizePairing(device_); | 199 pairing_delegate_->AuthorizePairing(device_); |
193 pairing_delegate_used_ = true; | |
194 } | 200 } |
195 | 201 |
196 bool BluetoothPairingChromeOS::ExpectingConfirmation() const { | 202 bool BluetoothPairingChromeOS::ExpectingConfirmation() const { |
197 return !confirmation_callback_.is_null(); | 203 return !confirmation_callback_.is_null(); |
198 } | 204 } |
199 | 205 |
200 void BluetoothPairingChromeOS::ConfirmPairing() { | 206 void BluetoothPairingChromeOS::ConfirmPairing() { |
201 if (confirmation_callback_.is_null()) | 207 if (confirmation_callback_.is_null()) |
202 return; | 208 return; |
203 | 209 |
(...skipping 15 matching lines...) Expand all Loading... |
219 bool BluetoothPairingChromeOS::CancelPairing() { | 225 bool BluetoothPairingChromeOS::CancelPairing() { |
220 return RunPairingCallbacks( | 226 return RunPairingCallbacks( |
221 BluetoothAgentServiceProvider::Delegate::CANCELLED); | 227 BluetoothAgentServiceProvider::Delegate::CANCELLED); |
222 } | 228 } |
223 | 229 |
224 BluetoothDevice::PairingDelegate* | 230 BluetoothDevice::PairingDelegate* |
225 BluetoothPairingChromeOS::GetPairingDelegate() const { | 231 BluetoothPairingChromeOS::GetPairingDelegate() const { |
226 return pairing_delegate_; | 232 return pairing_delegate_; |
227 } | 233 } |
228 | 234 |
| 235 void BluetoothPairingChromeOS::ResetCallbacks() { |
| 236 pincode_callback_.Reset(); |
| 237 passkey_callback_.Reset(); |
| 238 confirmation_callback_.Reset(); |
| 239 } |
| 240 |
229 bool BluetoothPairingChromeOS::RunPairingCallbacks( | 241 bool BluetoothPairingChromeOS::RunPairingCallbacks( |
230 BluetoothAgentServiceProvider::Delegate::Status status) { | 242 BluetoothAgentServiceProvider::Delegate::Status status) { |
231 pairing_delegate_used_ = true; | 243 pairing_delegate_used_ = true; |
232 | 244 |
233 bool callback_run = false; | 245 bool callback_run = false; |
234 if (!pincode_callback_.is_null()) { | 246 if (!pincode_callback_.is_null()) { |
235 pincode_callback_.Run(status, ""); | 247 pincode_callback_.Run(status, ""); |
236 pincode_callback_.Reset(); | 248 pincode_callback_.Reset(); |
237 callback_run = true; | 249 callback_run = true; |
238 } | 250 } |
(...skipping 13 matching lines...) Expand all Loading... |
252 // If this is not an outgoing connection to the device, clean up the pairing | 264 // If this is not an outgoing connection to the device, clean up the pairing |
253 // context since the pairing is done. The outgoing connection case is cleaned | 265 // context since the pairing is done. The outgoing connection case is cleaned |
254 // up in the callback for the underlying Pair() call. | 266 // up in the callback for the underlying Pair() call. |
255 if (!device_->IsConnecting()) | 267 if (!device_->IsConnecting()) |
256 device_->EndPairing(); | 268 device_->EndPairing(); |
257 | 269 |
258 return callback_run; | 270 return callback_run; |
259 } | 271 } |
260 | 272 |
261 } // namespace chromeos | 273 } // namespace chromeos |
OLD | NEW |