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

Side by Side Diff: components/pairing/bluetooth_host_pairing_controller.cc

Issue 652743003: Fix update and enrollment flow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: indent Created 6 years, 2 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
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 "components/pairing/bluetooth_host_pairing_controller.h" 5 #include "components/pairing/bluetooth_host_pairing_controller.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/hash.h" 8 #include "base/hash.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "components/pairing/bluetooth_pairing_constants.h" 11 #include "components/pairing/bluetooth_pairing_constants.h"
12 #include "components/pairing/pairing_api.pb.h" 12 #include "components/pairing/pairing_api.pb.h"
13 #include "components/pairing/proto_decoder.h" 13 #include "components/pairing/proto_decoder.h"
14 #include "device/bluetooth/bluetooth_adapter_factory.h" 14 #include "device/bluetooth/bluetooth_adapter_factory.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 16
17 namespace pairing_chromeos {
18
17 namespace { 19 namespace {
18 const int kReceiveSize = 16384; 20 const int kReceiveSize = 16384;
21
22 pairing_api::HostStatusParameters::UpdateStatus PairingApiUpdateStatus(
23 HostPairingController::UpdateStatus update_status) {
24 switch(update_status) {
25 case HostPairingController::UPDATE_STATUS_UNKNOWN:
26 return pairing_api::HostStatusParameters::UPDATE_STATUS_UNKNOWN;
27 case HostPairingController::UPDATE_STATUS_UPDATING:
28 return pairing_api::HostStatusParameters::UPDATE_STATUS_UPDATING;
29 case HostPairingController::UPDATE_STATUS_REBOOTING:
30 return pairing_api::HostStatusParameters::UPDATE_STATUS_REBOOTING;
31 case HostPairingController::UPDATE_STATUS_UPDATED:
32 return pairing_api::HostStatusParameters::UPDATE_STATUS_UPDATED;
33 default:
34 NOTREACHED();
35 return pairing_api::HostStatusParameters::UPDATE_STATUS_UNKNOWN;
36 }
19 } 37 }
20 38
21 namespace pairing_chromeos { 39 pairing_api::HostStatusParameters::EnrollmentStatus PairingApiEnrollmentStatus(
40 HostPairingController::EnrollmentStatus enrollment_status) {
41 switch(enrollment_status) {
42 case HostPairingController::ENROLLMENT_STATUS_UNKNOWN:
43 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_UNKNOWN;
44 case HostPairingController::ENROLLMENT_STATUS_ENROLLING:
45 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_ENROLLING;
46 case HostPairingController::ENROLLMENT_STATUS_FAILURE:
47 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_FAILURE;
48 case HostPairingController::ENROLLMENT_STATUS_SUCCESS:
49 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_SUCCESS;
50 default:
51 NOTREACHED();
52 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_UNKNOWN;
53 }
54 }
55
56 } // namespace
22 57
23 BluetoothHostPairingController::BluetoothHostPairingController() 58 BluetoothHostPairingController::BluetoothHostPairingController()
24 : current_stage_(STAGE_NONE), 59 : current_stage_(STAGE_NONE),
60 update_status_(UPDATE_STATUS_UNKNOWN),
61 enrollment_status_(ENROLLMENT_STATUS_UNKNOWN),
25 device_(NULL), 62 device_(NULL),
26 proto_decoder_(new ProtoDecoder(this)), 63 proto_decoder_(new ProtoDecoder(this)),
27 ptr_factory_(this) { 64 ptr_factory_(this) {
28 } 65 }
29 66
30 BluetoothHostPairingController::~BluetoothHostPairingController() { 67 BluetoothHostPairingController::~BluetoothHostPairingController() {
31 if (adapter_.get()) 68 if (adapter_.get())
32 adapter_->RemoveObserver(this); 69 adapter_->RemoveObserver(this);
33 } 70 }
34 71
35 void BluetoothHostPairingController::ChangeStage(Stage new_stage) { 72 void BluetoothHostPairingController::ChangeStage(Stage new_stage) {
36 if (current_stage_ == new_stage) 73 if (current_stage_ == new_stage)
37 return; 74 return;
38 VLOG(1) << "ChangeStage " << new_stage; 75 VLOG(1) << "ChangeStage " << new_stage;
39 current_stage_ = new_stage; 76 current_stage_ = new_stage;
40 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage)); 77 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage));
41 } 78 }
42 79
43 void BluetoothHostPairingController::SendHostStatus() { 80 void BluetoothHostPairingController::SendHostStatus() {
44 pairing_api::HostStatus host_status; 81 pairing_api::HostStatus host_status;
45 82
46 host_status.set_api_version(kPairingAPIVersion); 83 host_status.set_api_version(kPairingAPIVersion);
47 if (!enrollment_domain_.empty()) 84 if (!enrollment_domain_.empty())
48 host_status.mutable_parameters()->set_domain(enrollment_domain_); 85 host_status.mutable_parameters()->set_domain(enrollment_domain_);
49 86
50 // TODO(zork): Get these values from the UI. (http://crbug.com/405744) 87 // TODO(zork): Get these values from the UI. (http://crbug.com/405744)
51 host_status.mutable_parameters()->set_connectivity( 88 host_status.mutable_parameters()->set_connectivity(
52 pairing_api::HostStatusParameters::CONNECTIVITY_CONNECTED); 89 pairing_api::HostStatusParameters::CONNECTIVITY_CONNECTED);
53 host_status.mutable_parameters()->set_update_status( 90 host_status.mutable_parameters()->set_update_status(
54 pairing_api::HostStatusParameters::UPDATE_STATUS_UPDATED); 91 PairingApiUpdateStatus(update_status_));
92 host_status.mutable_parameters()->set_enrollment_status(
93 PairingApiEnrollmentStatus(enrollment_status_));
55 94
56 // TODO(zork): Get a list of other paired controllers. 95 // TODO(zork): Get a list of other paired controllers.
57 // (http://crbug.com/405757) 96 // (http://crbug.com/405757)
58 97
59 int size = 0; 98 int size = 0;
60 scoped_refptr<net::IOBuffer> io_buffer( 99 scoped_refptr<net::IOBuffer> io_buffer(
61 ProtoDecoder::SendHostStatus(host_status, &size)); 100 ProtoDecoder::SendHostStatus(host_status, &size));
62 101
63 controller_socket_->Send( 102 controller_socket_->Send(
64 io_buffer, size, 103 io_buffer, size,
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // TODO: Update Host. (http://crbug.com/405754) 238 // TODO: Update Host. (http://crbug.com/405754)
200 SendHostStatus(); 239 SendHostStatus();
201 240
202 controller_socket_->Receive( 241 controller_socket_->Receive(
203 kReceiveSize, 242 kReceiveSize,
204 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, 243 base::Bind(&BluetoothHostPairingController::OnReceiveComplete,
205 ptr_factory_.GetWeakPtr()), 244 ptr_factory_.GetWeakPtr()),
206 base::Bind(&BluetoothHostPairingController::OnReceiveError, 245 base::Bind(&BluetoothHostPairingController::OnReceiveError,
207 ptr_factory_.GetWeakPtr())); 246 ptr_factory_.GetWeakPtr()));
208 247
209 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS); 248 ChangeStage(STAGE_UPDATING);
210 } 249 }
211 250
212 void BluetoothHostPairingController::OnSetDiscoverable(bool change_stage) { 251 void BluetoothHostPairingController::OnSetDiscoverable(bool change_stage) {
213 DCHECK(thread_checker_.CalledOnValidThread()); 252 DCHECK(thread_checker_.CalledOnValidThread());
214 if (change_stage) { 253 if (change_stage) {
215 DCHECK_EQ(current_stage_, STAGE_NONE); 254 DCHECK_EQ(current_stage_, STAGE_NONE);
216 ChangeStage(STAGE_WAITING_FOR_CONTROLLER); 255 ChangeStage(STAGE_WAITING_FOR_CONTROLLER);
217 } 256 }
218 } 257 }
219 258
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 ConfigureHost(message.parameters().accepted_eula(), 311 ConfigureHost(message.parameters().accepted_eula(),
273 message.parameters().lang(), 312 message.parameters().lang(),
274 message.parameters().timezone(), 313 message.parameters().timezone(),
275 message.parameters().send_reports(), 314 message.parameters().send_reports(),
276 message.parameters().keyboard_layout())); 315 message.parameters().keyboard_layout()));
277 } 316 }
278 317
279 void BluetoothHostPairingController::OnPairDevicesMessage( 318 void BluetoothHostPairingController::OnPairDevicesMessage(
280 const pairing_api::PairDevices& message) { 319 const pairing_api::PairDevices& message) {
281 DCHECK(thread_checker_.CalledOnValidThread()); 320 DCHECK(thread_checker_.CalledOnValidThread());
282 if (current_stage_ != STAGE_WAITING_FOR_CREDENTIALS) {
283 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol);
284 return;
285 }
286
287 ChangeStage(STAGE_ENROLLING); 321 ChangeStage(STAGE_ENROLLING);
288 FOR_EACH_OBSERVER(Observer, observers_, 322 FOR_EACH_OBSERVER(Observer, observers_,
289 EnrollHost(message.parameters().admin_access_token())); 323 EnrollHost(message.parameters().admin_access_token()));
290 } 324 }
291 325
292 void BluetoothHostPairingController::SetEnrollmentComplete(bool success) {
293 DCHECK_EQ(current_stage_, STAGE_ENROLLING);
294 DCHECK(thread_checker_.CalledOnValidThread());
295 if (success) {
296 ChangeStage(STAGE_PAIRING_DONE);
297 SendHostStatus();
298 } else {
299 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorEnrollmentFailed);
300 }
301 }
302
303 void BluetoothHostPairingController::OnCompleteSetupMessage( 326 void BluetoothHostPairingController::OnCompleteSetupMessage(
304 const pairing_api::CompleteSetup& message) { 327 const pairing_api::CompleteSetup& message) {
305 DCHECK(thread_checker_.CalledOnValidThread()); 328 DCHECK(thread_checker_.CalledOnValidThread());
306 if (current_stage_ != STAGE_PAIRING_DONE) { 329 if (current_stage_ != STAGE_PAIRING_DONE) {
307 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol); 330 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT, kErrorInvalidProtocol);
308 return; 331 return;
309 } 332 }
310 333
311 // TODO(zork): Handle adding another controller. (http://crbug.com/405757) 334 // TODO(zork): Handle adding another controller. (http://crbug.com/405757)
312 ChangeStage(STAGE_FINISHED); 335 ChangeStage(STAGE_FINISHED);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 DCHECK_EQ(current_stage_, STAGE_WAITING_FOR_CODE_CONFIRMATION); 384 DCHECK_EQ(current_stage_, STAGE_WAITING_FOR_CODE_CONFIRMATION);
362 return confirmation_code_; 385 return confirmation_code_;
363 } 386 }
364 387
365 std::string BluetoothHostPairingController::GetEnrollmentDomain() { 388 std::string BluetoothHostPairingController::GetEnrollmentDomain() {
366 return enrollment_domain_; 389 return enrollment_domain_;
367 } 390 }
368 391
369 void BluetoothHostPairingController::OnUpdateStatusChanged( 392 void BluetoothHostPairingController::OnUpdateStatusChanged(
370 UpdateStatus update_status) { 393 UpdateStatus update_status) {
371 // TODO(zork): Handling updating stages (http://crbug.com/405754). 394 update_status_ = update_status;
395 if (update_status == UPDATE_STATUS_UPDATED)
396 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS);
397 SendHostStatus();
398 }
399
400 void BluetoothHostPairingController::OnEnrollmentStatusChanged(
401 EnrollmentStatus enrollment_status) {
402 DCHECK_EQ(current_stage_, STAGE_ENROLLING);
403 DCHECK(thread_checker_.CalledOnValidThread());
404
405 enrollment_status_ = enrollment_status;
406 if (enrollment_status == ENROLLMENT_STATUS_SUCCESS) {
407 ChangeStage(STAGE_PAIRING_DONE);
408 } else if (enrollment_status == ENROLLMENT_STATUS_FAILURE) {
409 AbortWithError(PAIRING_ERROR_PAIRING_OR_ENROLLMENT,
410 kErrorEnrollmentFailed);
411 }
412 SendHostStatus();
372 } 413 }
373 414
374 void BluetoothHostPairingController::RequestPinCode( 415 void BluetoothHostPairingController::RequestPinCode(
375 device::BluetoothDevice* device) { 416 device::BluetoothDevice* device) {
376 // Disallow unknown device. 417 // Disallow unknown device.
377 device->RejectPairing(); 418 device->RejectPairing();
378 } 419 }
379 420
380 void BluetoothHostPairingController::RequestPasskey( 421 void BluetoothHostPairingController::RequestPasskey(
381 device::BluetoothDevice* device) { 422 device::BluetoothDevice* device) {
(...skipping 30 matching lines...) Expand all
412 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); 453 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION);
413 } 454 }
414 455
415 void BluetoothHostPairingController::AuthorizePairing( 456 void BluetoothHostPairingController::AuthorizePairing(
416 device::BluetoothDevice* device) { 457 device::BluetoothDevice* device) {
417 // Disallow unknown device. 458 // Disallow unknown device.
418 device->RejectPairing(); 459 device->RejectPairing();
419 } 460 }
420 461
421 } // namespace pairing_chromeos 462 } // namespace pairing_chromeos
OLDNEW
« no previous file with comments | « components/pairing/bluetooth_host_pairing_controller.h ('k') | components/pairing/fake_host_pairing_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698