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

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

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

Powered by Google App Engine
This is Rietveld 408576698