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

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

Issue 2937253003: cros: Replace BrowserThread::FILE in InputServiceProxy (Closed)
Patch Set: for #2 Created 3 years, 6 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/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/task_runner.h"
13 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
14 #include "chromeos/system/devicetype.h" 14 #include "chromeos/system/devicetype.h"
15 #include "components/pairing/bluetooth_pairing_constants.h" 15 #include "components/pairing/bluetooth_pairing_constants.h"
16 #include "components/pairing/pairing_api.pb.h" 16 #include "components/pairing/pairing_api.pb.h"
17 #include "components/pairing/proto_decoder.h" 17 #include "components/pairing/proto_decoder.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h" 18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "net/base/io_buffer.h" 19 #include "net/base/io_buffer.h"
20 20
21 namespace pairing_chromeos { 21 namespace pairing_chromeos {
22 22
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 std::vector<BluetoothHostPairingController::InputDeviceInfo> GetDevices() { 79 std::vector<BluetoothHostPairingController::InputDeviceInfo> GetDevices() {
80 std::vector<BluetoothHostPairingController::InputDeviceInfo> devices; 80 std::vector<BluetoothHostPairingController::InputDeviceInfo> devices;
81 if (device::InputServiceLinux::HasInstance()) 81 if (device::InputServiceLinux::HasInstance())
82 device::InputServiceLinux::GetInstance()->GetDevices(&devices); 82 device::InputServiceLinux::GetInstance()->GetDevices(&devices);
83 return devices; 83 return devices;
84 } 84 }
85 85
86 } // namespace 86 } // namespace
87 87
88 BluetoothHostPairingController::BluetoothHostPairingController( 88 BluetoothHostPairingController::BluetoothHostPairingController(
89 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) 89 const scoped_refptr<base::TaskRunner>& input_service_task_runner)
90 : current_stage_(STAGE_NONE), 90 : proto_decoder_(new ProtoDecoder(this)),
achuithb 2017/06/20 08:36:48 Does base::MakeUnique not work here?
xiyuan 2017/06/20 15:26:55 Done.
91 connectivity_status_(CONNECTIVITY_UNTESTED), 91 input_service_task_runner_(input_service_task_runner) {}
92 update_status_(UPDATE_STATUS_UNKNOWN),
93 enrollment_status_(ENROLLMENT_STATUS_UNKNOWN),
94 proto_decoder_(new ProtoDecoder(this)),
95 file_task_runner_(file_task_runner),
96 ptr_factory_(this) {}
97 92
98 BluetoothHostPairingController::~BluetoothHostPairingController() { 93 BluetoothHostPairingController::~BluetoothHostPairingController() {
99 Reset(); 94 Reset();
100 } 95 }
101 96
102 void BluetoothHostPairingController::SetDelegateForTesting( 97 void BluetoothHostPairingController::SetDelegateForTesting(
103 TestDelegate* delegate) { 98 TestDelegate* delegate) {
104 delegate_ = delegate; 99 delegate_ = delegate;
105 } 100 }
106 101
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 base::Bind(&BluetoothHostPairingController::OnForget, 155 base::Bind(&BluetoothHostPairingController::OnForget,
161 ptr_factory_.GetWeakPtr())); 156 ptr_factory_.GetWeakPtr()));
162 return; 157 return;
163 } 158 }
164 } 159 }
165 OnForget(); 160 OnForget();
166 } 161 }
167 162
168 void BluetoothHostPairingController::OnGetAdapter( 163 void BluetoothHostPairingController::OnGetAdapter(
169 scoped_refptr<device::BluetoothAdapter> adapter) { 164 scoped_refptr<device::BluetoothAdapter> adapter) {
170 DCHECK(thread_checker_.CalledOnValidThread()); 165 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
171 DCHECK(!adapter_.get()); 166 DCHECK(!adapter_.get());
172 adapter_ = adapter; 167 adapter_ = adapter;
173 168
174 if (adapter_->IsPresent()) { 169 if (adapter_->IsPresent()) {
175 SetPowered(); 170 SetPowered();
176 } else { 171 } else {
177 // Set the name once the adapter is present. 172 // Set the name once the adapter is present.
178 adapter_->AddObserver(this); 173 adapter_->AddObserver(this);
179 } 174 }
180 } 175 }
181 176
182 void BluetoothHostPairingController::SetPowered() { 177 void BluetoothHostPairingController::SetPowered() {
183 DCHECK(thread_checker_.CalledOnValidThread()); 178 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
184 if (adapter_->IsPowered()) { 179 if (adapter_->IsPowered()) {
185 was_powered_ = true; 180 was_powered_ = true;
186 OnSetPowered(); 181 OnSetPowered();
187 } else { 182 } else {
188 adapter_->SetPowered( 183 adapter_->SetPowered(
189 true, 184 true,
190 base::Bind(&BluetoothHostPairingController::OnSetPowered, 185 base::Bind(&BluetoothHostPairingController::OnSetPowered,
191 ptr_factory_.GetWeakPtr()), 186 ptr_factory_.GetWeakPtr()),
192 base::Bind(&BluetoothHostPairingController::OnSetError, 187 base::Bind(&BluetoothHostPairingController::OnSetError,
193 ptr_factory_.GetWeakPtr())); 188 ptr_factory_.GetWeakPtr()));
194 } 189 }
195 } 190 }
196 191
197 void BluetoothHostPairingController::OnSetPowered() { 192 void BluetoothHostPairingController::OnSetPowered() {
198 DCHECK(thread_checker_.CalledOnValidThread()); 193 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
199 adapter_->AddPairingDelegate( 194 adapter_->AddPairingDelegate(
200 this, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); 195 this, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
201 196
202 device::BluetoothAdapter::ServiceOptions options; 197 device::BluetoothAdapter::ServiceOptions options;
203 options.name.reset(new std::string(kPairingServiceName)); 198 options.name.reset(new std::string(kPairingServiceName));
204 199
205 adapter_->CreateRfcommService( 200 adapter_->CreateRfcommService(
206 device::BluetoothUUID(kPairingServiceUUID), options, 201 device::BluetoothUUID(kPairingServiceUUID), options,
207 base::Bind(&BluetoothHostPairingController::OnCreateService, 202 base::Bind(&BluetoothHostPairingController::OnCreateService,
208 ptr_factory_.GetWeakPtr()), 203 ptr_factory_.GetWeakPtr()),
209 base::Bind(&BluetoothHostPairingController::OnCreateServiceError, 204 base::Bind(&BluetoothHostPairingController::OnCreateServiceError,
210 ptr_factory_.GetWeakPtr())); 205 ptr_factory_.GetWeakPtr()));
211 } 206 }
212 207
213 void BluetoothHostPairingController::OnCreateService( 208 void BluetoothHostPairingController::OnCreateService(
214 scoped_refptr<device::BluetoothSocket> socket) { 209 scoped_refptr<device::BluetoothSocket> socket) {
215 DCHECK(thread_checker_.CalledOnValidThread()); 210 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
216 service_socket_ = socket; 211 service_socket_ = socket;
217 212
218 service_socket_->Accept( 213 service_socket_->Accept(
219 base::Bind(&BluetoothHostPairingController::OnAccept, 214 base::Bind(&BluetoothHostPairingController::OnAccept,
220 ptr_factory_.GetWeakPtr()), 215 ptr_factory_.GetWeakPtr()),
221 base::Bind(&BluetoothHostPairingController::OnAcceptError, 216 base::Bind(&BluetoothHostPairingController::OnAcceptError,
222 ptr_factory_.GetWeakPtr())); 217 ptr_factory_.GetWeakPtr()));
223 218
224 adapter_->SetDiscoverable( 219 adapter_->SetDiscoverable(
225 true, 220 true,
226 base::Bind(&BluetoothHostPairingController::OnSetDiscoverable, 221 base::Bind(&BluetoothHostPairingController::OnSetDiscoverable,
227 ptr_factory_.GetWeakPtr(), true), 222 ptr_factory_.GetWeakPtr(), true),
228 base::Bind(&BluetoothHostPairingController::OnSetError, 223 base::Bind(&BluetoothHostPairingController::OnSetError,
229 ptr_factory_.GetWeakPtr())); 224 ptr_factory_.GetWeakPtr()));
230 } 225 }
231 226
232 void BluetoothHostPairingController::OnAccept( 227 void BluetoothHostPairingController::OnAccept(
233 const device::BluetoothDevice* device, 228 const device::BluetoothDevice* device,
234 scoped_refptr<device::BluetoothSocket> socket) { 229 scoped_refptr<device::BluetoothSocket> socket) {
235 controller_device_address_ = device->GetAddress(); 230 controller_device_address_ = device->GetAddress();
236 231
237 DCHECK(thread_checker_.CalledOnValidThread()); 232 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
238 adapter_->SetDiscoverable( 233 adapter_->SetDiscoverable(
239 false, 234 false,
240 base::Bind(&BluetoothHostPairingController::OnSetDiscoverable, 235 base::Bind(&BluetoothHostPairingController::OnSetDiscoverable,
241 ptr_factory_.GetWeakPtr(), false), 236 ptr_factory_.GetWeakPtr(), false),
242 base::Bind(&BluetoothHostPairingController::OnSetError, 237 base::Bind(&BluetoothHostPairingController::OnSetError,
243 ptr_factory_.GetWeakPtr())); 238 ptr_factory_.GetWeakPtr()));
244 controller_socket_ = socket; 239 controller_socket_ = socket;
245 240
246 SendHostStatus(); 241 SendHostStatus();
247 242
248 controller_socket_->Receive( 243 controller_socket_->Receive(
249 kReceiveSize, 244 kReceiveSize,
250 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, 245 base::Bind(&BluetoothHostPairingController::OnReceiveComplete,
251 ptr_factory_.GetWeakPtr()), 246 ptr_factory_.GetWeakPtr()),
252 base::Bind(&BluetoothHostPairingController::OnReceiveError, 247 base::Bind(&BluetoothHostPairingController::OnReceiveError,
253 ptr_factory_.GetWeakPtr())); 248 ptr_factory_.GetWeakPtr()));
254 } 249 }
255 250
256 void BluetoothHostPairingController::OnSetDiscoverable(bool change_stage) { 251 void BluetoothHostPairingController::OnSetDiscoverable(bool change_stage) {
257 DCHECK(thread_checker_.CalledOnValidThread()); 252 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
258 if (change_stage) { 253 if (change_stage) {
259 DCHECK_EQ(current_stage_, STAGE_NONE); 254 DCHECK_EQ(current_stage_, STAGE_NONE);
260 ChangeStage(STAGE_WAITING_FOR_CONTROLLER); 255 ChangeStage(STAGE_WAITING_FOR_CONTROLLER);
261 } 256 }
262 } 257 }
263 258
264 void BluetoothHostPairingController::OnSendComplete(int bytes_sent) {} 259 void BluetoothHostPairingController::OnSendComplete(int bytes_sent) {}
265 260
266 void BluetoothHostPairingController::OnReceiveComplete( 261 void BluetoothHostPairingController::OnReceiveComplete(
267 int bytes, scoped_refptr<net::IOBuffer> io_buffer) { 262 int bytes, scoped_refptr<net::IOBuffer> io_buffer) {
268 DCHECK(thread_checker_.CalledOnValidThread()); 263 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
269 proto_decoder_->DecodeIOBuffer(bytes, io_buffer); 264 proto_decoder_->DecodeIOBuffer(bytes, io_buffer);
270 265
271 if (controller_socket_.get()) { 266 if (controller_socket_.get()) {
272 controller_socket_->Receive( 267 controller_socket_->Receive(
273 kReceiveSize, 268 kReceiveSize,
274 base::Bind(&BluetoothHostPairingController::OnReceiveComplete, 269 base::Bind(&BluetoothHostPairingController::OnReceiveComplete,
275 ptr_factory_.GetWeakPtr()), 270 ptr_factory_.GetWeakPtr()),
276 base::Bind(&BluetoothHostPairingController::OnReceiveError, 271 base::Bind(&BluetoothHostPairingController::OnReceiveError,
277 ptr_factory_.GetWeakPtr())); 272 ptr_factory_.GetWeakPtr()));
278 } 273 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 service_socket_ = nullptr; 337 service_socket_ = nullptr;
343 } 338 }
344 339
345 if (adapter_.get()) { 340 if (adapter_.get()) {
346 if (adapter_->IsDiscoverable()) { 341 if (adapter_->IsDiscoverable()) {
347 adapter_->SetDiscoverable(false, base::Bind(&base::DoNothing), 342 adapter_->SetDiscoverable(false, base::Bind(&base::DoNothing),
348 base::Bind(&base::DoNothing)); 343 base::Bind(&base::DoNothing));
349 } 344 }
350 345
351 base::PostTaskAndReplyWithResult( 346 base::PostTaskAndReplyWithResult(
352 file_task_runner_.get(), FROM_HERE, base::Bind(&GetDevices), 347 input_service_task_runner_.get(), FROM_HERE, base::Bind(&GetDevices),
353 base::Bind(&BluetoothHostPairingController::PowerOffAdapterIfApplicable, 348 base::Bind(&BluetoothHostPairingController::PowerOffAdapterIfApplicable,
354 ptr_factory_.GetWeakPtr())); 349 ptr_factory_.GetWeakPtr()));
355 } 350 }
356 ChangeStage(STAGE_NONE); 351 ChangeStage(STAGE_NONE);
357 } 352 }
358 353
359 void BluetoothHostPairingController::SetControllerDeviceAddressForTesting( 354 void BluetoothHostPairingController::SetControllerDeviceAddressForTesting(
360 const std::string& address) { 355 const std::string& address) {
361 controller_device_address_ = address; 356 controller_device_address_ = address;
362 } 357 }
(...skipping 16 matching lines...) Expand all
379 for (Observer& observer : observers_) { 374 for (Observer& observer : observers_) {
380 observer.ConfigureHostRequested( 375 observer.ConfigureHostRequested(
381 message.parameters().accepted_eula(), message.parameters().lang(), 376 message.parameters().accepted_eula(), message.parameters().lang(),
382 message.parameters().timezone(), message.parameters().send_reports(), 377 message.parameters().timezone(), message.parameters().send_reports(),
383 message.parameters().keyboard_layout()); 378 message.parameters().keyboard_layout());
384 } 379 }
385 } 380 }
386 381
387 void BluetoothHostPairingController::OnPairDevicesMessage( 382 void BluetoothHostPairingController::OnPairDevicesMessage(
388 const pairing_api::PairDevices& message) { 383 const pairing_api::PairDevices& message) {
389 DCHECK(thread_checker_.CalledOnValidThread()); 384 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
390 enrollment_domain_ = message.parameters().enrolling_domain(); 385 enrollment_domain_ = message.parameters().enrolling_domain();
391 ChangeStage(STAGE_ENROLLING); 386 ChangeStage(STAGE_ENROLLING);
392 for (Observer& observer : observers_) 387 for (Observer& observer : observers_)
393 observer.EnrollHostRequested(message.parameters().admin_access_token()); 388 observer.EnrollHostRequested(message.parameters().admin_access_token());
394 } 389 }
395 390
396 void BluetoothHostPairingController::OnCompleteSetupMessage( 391 void BluetoothHostPairingController::OnCompleteSetupMessage(
397 const pairing_api::CompleteSetup& message) { 392 const pairing_api::CompleteSetup& message) {
398 DCHECK(thread_checker_.CalledOnValidThread()); 393 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
399 if (current_stage_ != STAGE_ENROLLMENT_SUCCESS) { 394 if (current_stage_ != STAGE_ENROLLMENT_SUCCESS) {
400 ChangeStage(STAGE_ENROLLMENT_ERROR); 395 ChangeStage(STAGE_ENROLLMENT_ERROR);
401 } else { 396 } else {
402 // TODO(zork): Handle adding another controller. (http://crbug.com/405757) 397 // TODO(zork): Handle adding another controller. (http://crbug.com/405757)
403 ChangeStage(STAGE_FINISHED); 398 ChangeStage(STAGE_FINISHED);
404 } 399 }
405 Reset(); 400 Reset();
406 } 401 }
407 402
408 void BluetoothHostPairingController::OnErrorMessage( 403 void BluetoothHostPairingController::OnErrorMessage(
409 const pairing_api::Error& message) { 404 const pairing_api::Error& message) {
410 NOTREACHED(); 405 NOTREACHED();
411 } 406 }
412 407
413 void BluetoothHostPairingController::OnRebootMessage( 408 void BluetoothHostPairingController::OnRebootMessage(
414 const pairing_api::Reboot& message) { 409 const pairing_api::Reboot& message) {
415 DCHECK(thread_checker_.CalledOnValidThread()); 410 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
416 for (Observer& observer : observers_) 411 for (Observer& observer : observers_)
417 observer.RebootHostRequested(); 412 observer.RebootHostRequested();
418 } 413 }
419 414
420 void BluetoothHostPairingController::OnAddNetworkMessage( 415 void BluetoothHostPairingController::OnAddNetworkMessage(
421 const pairing_api::AddNetwork& message) { 416 const pairing_api::AddNetwork& message) {
422 DCHECK(thread_checker_.CalledOnValidThread()); 417 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
423 for (Observer& observer : observers_) 418 for (Observer& observer : observers_)
424 observer.AddNetworkRequested(message.parameters().onc_spec()); 419 observer.AddNetworkRequested(message.parameters().onc_spec());
425 } 420 }
426 421
427 void BluetoothHostPairingController::AdapterPresentChanged( 422 void BluetoothHostPairingController::AdapterPresentChanged(
428 device::BluetoothAdapter* adapter, 423 device::BluetoothAdapter* adapter,
429 bool present) { 424 bool present) {
430 DCHECK_EQ(adapter, adapter_.get()); 425 DCHECK_EQ(adapter, adapter_.get());
431 if (present) { 426 if (present) {
432 adapter_->RemoveObserver(this); 427 adapter_->RemoveObserver(this);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 UpdateStatus update_status) { 480 UpdateStatus update_status) {
486 update_status_ = update_status; 481 update_status_ = update_status;
487 if (update_status == UPDATE_STATUS_UPDATED) 482 if (update_status == UPDATE_STATUS_UPDATED)
488 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS); 483 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS);
489 SendHostStatus(); 484 SendHostStatus();
490 } 485 }
491 486
492 void BluetoothHostPairingController::OnEnrollmentStatusChanged( 487 void BluetoothHostPairingController::OnEnrollmentStatusChanged(
493 EnrollmentStatus enrollment_status) { 488 EnrollmentStatus enrollment_status) {
494 DCHECK_EQ(current_stage_, STAGE_ENROLLING); 489 DCHECK_EQ(current_stage_, STAGE_ENROLLING);
495 DCHECK(thread_checker_.CalledOnValidThread()); 490 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
496 491
497 enrollment_status_ = enrollment_status; 492 enrollment_status_ = enrollment_status;
498 if (enrollment_status == ENROLLMENT_STATUS_SUCCESS) { 493 if (enrollment_status == ENROLLMENT_STATUS_SUCCESS) {
499 ChangeStage(STAGE_ENROLLMENT_SUCCESS); 494 ChangeStage(STAGE_ENROLLMENT_SUCCESS);
500 } else if (enrollment_status == ENROLLMENT_STATUS_FAILURE) { 495 } else if (enrollment_status == ENROLLMENT_STATUS_FAILURE) {
501 ChangeStage(STAGE_ENROLLMENT_ERROR); 496 ChangeStage(STAGE_ENROLLMENT_ERROR);
502 } 497 }
503 SendHostStatus(); 498 SendHostStatus();
504 } 499 }
505 500
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); 549 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION);
555 } 550 }
556 551
557 void BluetoothHostPairingController::AuthorizePairing( 552 void BluetoothHostPairingController::AuthorizePairing(
558 device::BluetoothDevice* device) { 553 device::BluetoothDevice* device) {
559 // Disallow unknown device. 554 // Disallow unknown device.
560 device->RejectPairing(); 555 device->RejectPairing();
561 } 556 }
562 557
563 } // namespace pairing_chromeos 558 } // namespace pairing_chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698