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

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

Issue 1942883003: [Chrome OS Bootstrapping]: Do not always power off the Bluetooth adapter. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address achuith@'s comments. Created 4 years, 7 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 "base/task_runner_util.h"
11 #include "chromeos/system/devicetype.h" 12 #include "chromeos/system/devicetype.h"
12 #include "components/pairing/bluetooth_pairing_constants.h" 13 #include "components/pairing/bluetooth_pairing_constants.h"
13 #include "components/pairing/pairing_api.pb.h" 14 #include "components/pairing/pairing_api.pb.h"
14 #include "components/pairing/proto_decoder.h" 15 #include "components/pairing/proto_decoder.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h" 16 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
17 18
18 namespace pairing_chromeos { 19 namespace pairing_chromeos {
19 20
20 namespace { 21 namespace {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 case HostPairingController::ENROLLMENT_STATUS_FAILURE: 82 case HostPairingController::ENROLLMENT_STATUS_FAILURE:
82 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_FAILURE; 83 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_FAILURE;
83 case HostPairingController::ENROLLMENT_STATUS_SUCCESS: 84 case HostPairingController::ENROLLMENT_STATUS_SUCCESS:
84 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_SUCCESS; 85 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_SUCCESS;
85 default: 86 default:
86 NOTREACHED(); 87 NOTREACHED();
87 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_UNKNOWN; 88 return pairing_api::HostStatusParameters::ENROLLMENT_STATUS_UNKNOWN;
88 } 89 }
89 } 90 }
90 91
92 std::vector<BluetoothHostPairingController::InputDeviceInfo> GetDevices() {
93 std::vector<BluetoothHostPairingController::InputDeviceInfo> devices;
94 if (device::InputServiceLinux::HasInstance())
95 device::InputServiceLinux::GetInstance()->GetDevices(&devices);
96 return devices;
97 }
98
91 } // namespace 99 } // namespace
92 100
93 BluetoothHostPairingController::BluetoothHostPairingController() 101 BluetoothHostPairingController::BluetoothHostPairingController(
102 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
94 : current_stage_(STAGE_NONE), 103 : current_stage_(STAGE_NONE),
95 connectivity_status_(CONNECTIVITY_UNTESTED), 104 connectivity_status_(CONNECTIVITY_UNTESTED),
96 update_status_(UPDATE_STATUS_UNKNOWN), 105 update_status_(UPDATE_STATUS_UNKNOWN),
97 enrollment_status_(ENROLLMENT_STATUS_UNKNOWN), 106 enrollment_status_(ENROLLMENT_STATUS_UNKNOWN),
98 proto_decoder_(new ProtoDecoder(this)), 107 proto_decoder_(new ProtoDecoder(this)),
108 file_task_runner_(file_task_runner),
99 ptr_factory_(this) {} 109 ptr_factory_(this) {}
100 110
101 BluetoothHostPairingController::~BluetoothHostPairingController() { 111 BluetoothHostPairingController::~BluetoothHostPairingController() {
102 Reset(); 112 Reset();
103 } 113 }
104 114
105 void BluetoothHostPairingController::ChangeStage(Stage new_stage) { 115 void BluetoothHostPairingController::ChangeStage(Stage new_stage) {
106 if (current_stage_ == new_stage) 116 if (current_stage_ == new_stage)
107 return; 117 return;
108 VLOG(1) << "ChangeStage " << new_stage; 118 VLOG(1) << "ChangeStage " << new_stage;
(...skipping 29 matching lines...) Expand all
138 io_buffer, size, 148 io_buffer, size,
139 base::Bind(&BluetoothHostPairingController::OnSendComplete, 149 base::Bind(&BluetoothHostPairingController::OnSendComplete,
140 ptr_factory_.GetWeakPtr()), 150 ptr_factory_.GetWeakPtr()),
141 base::Bind(&BluetoothHostPairingController::OnSendError, 151 base::Bind(&BluetoothHostPairingController::OnSendError,
142 ptr_factory_.GetWeakPtr())); 152 ptr_factory_.GetWeakPtr()));
143 } 153 }
144 154
145 void BluetoothHostPairingController::Reset() { 155 void BluetoothHostPairingController::Reset() {
146 if (controller_socket_.get()) { 156 if (controller_socket_.get()) {
147 controller_socket_->Close(); 157 controller_socket_->Close();
148 controller_socket_ = NULL; 158 controller_socket_ = nullptr;
149 } 159 }
150 160
151 if (service_socket_.get()) { 161 if (service_socket_.get()) {
152 service_socket_->Close(); 162 service_socket_->Close();
153 service_socket_ = NULL; 163 service_socket_ = nullptr;
154 } 164 }
155 165
156 if (adapter_.get()) { 166 if (adapter_.get()) {
157 if (adapter_->IsDiscoverable()) { 167 if (adapter_->IsDiscoverable()) {
158 adapter_->SetDiscoverable(false, base::Bind(&base::DoNothing), 168 adapter_->SetDiscoverable(false, base::Bind(&base::DoNothing),
159 base::Bind(&base::DoNothing)); 169 base::Bind(&base::DoNothing));
160 } 170 }
161 if (!was_powered_) { 171
162 adapter_->SetPowered(false, base::Bind(&base::DoNothing), 172 base::PostTaskAndReplyWithResult(
163 base::Bind(&base::DoNothing)); 173 file_task_runner_.get(), FROM_HERE, base::Bind(&GetDevices),
164 } 174 base::Bind(&BluetoothHostPairingController::PowerOffAdapterIfApplicable,
165 adapter_->RemoveObserver(this); 175 ptr_factory_.GetWeakPtr()));
166 adapter_ = NULL;
167 } 176 }
168 ChangeStage(STAGE_NONE); 177 ChangeStage(STAGE_NONE);
169 } 178 }
170 179
171 void BluetoothHostPairingController::OnGetAdapter( 180 void BluetoothHostPairingController::OnGetAdapter(
172 scoped_refptr<device::BluetoothAdapter> adapter) { 181 scoped_refptr<device::BluetoothAdapter> adapter) {
173 DCHECK(thread_checker_.CalledOnValidThread()); 182 DCHECK(thread_checker_.CalledOnValidThread());
174 DCHECK(!adapter_.get()); 183 DCHECK(!adapter_.get());
175 adapter_ = adapter; 184 adapter_ = adapter;
176 185
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 scoped_refptr<device::BluetoothSocket> socket) { 261 scoped_refptr<device::BluetoothSocket> socket) {
253 DCHECK(thread_checker_.CalledOnValidThread()); 262 DCHECK(thread_checker_.CalledOnValidThread());
254 adapter_->SetDiscoverable( 263 adapter_->SetDiscoverable(
255 false, 264 false,
256 base::Bind(&BluetoothHostPairingController::OnSetDiscoverable, 265 base::Bind(&BluetoothHostPairingController::OnSetDiscoverable,
257 ptr_factory_.GetWeakPtr(), false), 266 ptr_factory_.GetWeakPtr(), false),
258 base::Bind(&BluetoothHostPairingController::OnSetError, 267 base::Bind(&BluetoothHostPairingController::OnSetError,
259 ptr_factory_.GetWeakPtr())); 268 ptr_factory_.GetWeakPtr()));
260 269
261 controller_socket_ = socket; 270 controller_socket_ = socket;
262 service_socket_ = NULL; 271 service_socket_ = nullptr;
263 272
264 SendHostStatus(); 273 SendHostStatus();
265 274
266 controller_socket_->Receive( 275 controller_socket_->Receive(
267 kReceiveSize, 276 kReceiveSize,
268 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, 277 base::Bind(&BluetoothHostPairingController::OnReceiveComplete,
269 ptr_factory_.GetWeakPtr()), 278 ptr_factory_.GetWeakPtr()),
270 base::Bind(&BluetoothHostPairingController::OnReceiveError, 279 base::Bind(&BluetoothHostPairingController::OnReceiveError,
271 ptr_factory_.GetWeakPtr())); 280 ptr_factory_.GetWeakPtr()));
272 } 281 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 324
316 void BluetoothHostPairingController::OnSendError( 325 void BluetoothHostPairingController::OnSendError(
317 const std::string& error_message) { 326 const std::string& error_message) {
318 LOG(ERROR) << error_message; 327 LOG(ERROR) << error_message;
319 if (enrollment_status_ != ENROLLMENT_STATUS_ENROLLING && 328 if (enrollment_status_ != ENROLLMENT_STATUS_ENROLLING &&
320 enrollment_status_ != ENROLLMENT_STATUS_SUCCESS) { 329 enrollment_status_ != ENROLLMENT_STATUS_SUCCESS) {
321 ChangeStage(STAGE_CONTROLLER_CONNECTION_ERROR); 330 ChangeStage(STAGE_CONTROLLER_CONNECTION_ERROR);
322 } 331 }
323 } 332 }
324 333
334 void BluetoothHostPairingController::PowerOffAdapterIfApplicable(
335 const std::vector<InputDeviceInfo>& devices) {
336 bool use_bluetooth = false;
337 for (const auto& device : devices) {
338 if (device.type == InputDeviceInfo::TYPE_BLUETOOTH) {
339 use_bluetooth = true;
340 break;
341 }
342 }
343 if (!was_powered_ && !use_bluetooth) {
344 adapter_->SetPowered(false, base::Bind(&base::DoNothing),
345 base::Bind(&base::DoNothing));
346 }
347 adapter_->RemoveObserver(this);
348 adapter_ = nullptr;
349 }
350
325 void BluetoothHostPairingController::OnReceiveError( 351 void BluetoothHostPairingController::OnReceiveError(
326 device::BluetoothSocket::ErrorReason reason, 352 device::BluetoothSocket::ErrorReason reason,
327 const std::string& error_message) { 353 const std::string& error_message) {
328 LOG(ERROR) << reason << ", " << error_message; 354 LOG(ERROR) << reason << ", " << error_message;
329 ChangeStage(STAGE_CONTROLLER_CONNECTION_ERROR); 355 ChangeStage(STAGE_CONTROLLER_CONNECTION_ERROR);
330 } 356 }
331 357
332 void BluetoothHostPairingController::OnHostStatusMessage( 358 void BluetoothHostPairingController::OnHostStatusMessage(
333 const pairing_api::HostStatus& message) { 359 const pairing_api::HostStatus& message) {
334 NOTREACHED(); 360 NOTREACHED();
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); 536 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION);
511 } 537 }
512 538
513 void BluetoothHostPairingController::AuthorizePairing( 539 void BluetoothHostPairingController::AuthorizePairing(
514 device::BluetoothDevice* device) { 540 device::BluetoothDevice* device) {
515 // Disallow unknown device. 541 // Disallow unknown device.
516 device->RejectPairing(); 542 device->RejectPairing();
517 } 543 }
518 544
519 } // namespace pairing_chromeos 545 } // namespace pairing_chromeos
OLDNEW
« no previous file with comments | « components/pairing/bluetooth_host_pairing_controller.h ('k') | components/pairing/shark_connection_listener.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698