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

Side by Side Diff: device/bluetooth/bluetooth_adapter_chromeos.cc

Issue 13416005: Bluetooth: clean up BluetoothDevice (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More win visible fixes Created 7 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_adapter_chromeos.h" 5 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 DCHECK(observer); 58 DCHECK(observer);
59 observers_.AddObserver(observer); 59 observers_.AddObserver(observer);
60 } 60 }
61 61
62 void BluetoothAdapterChromeOS::RemoveObserver( 62 void BluetoothAdapterChromeOS::RemoveObserver(
63 BluetoothAdapter::Observer* observer) { 63 BluetoothAdapter::Observer* observer) {
64 DCHECK(observer); 64 DCHECK(observer);
65 observers_.RemoveObserver(observer); 65 observers_.RemoveObserver(observer);
66 } 66 }
67 67
68 std::string BluetoothAdapterChromeOS::address() const { 68 std::string BluetoothAdapterChromeOS::GetAddress() const {
69 return address_; 69 return address_;
70 } 70 }
71 71
72 std::string BluetoothAdapterChromeOS::name() const { 72 std::string BluetoothAdapterChromeOS::GetName() const {
73 return name_; 73 return name_;
74 } 74 }
75 75
76 // TODO(youngki) Return true when object path and properties of the adapter are 76 // TODO(youngki) Return true when object path and properties of the adapter are
77 // initialized. 77 // initialized.
78 bool BluetoothAdapterChromeOS::IsInitialized() const { 78 bool BluetoothAdapterChromeOS::IsInitialized() const {
79 return true; 79 return true;
80 } 80 }
81 81
82 bool BluetoothAdapterChromeOS::IsPresent() const { 82 bool BluetoothAdapterChromeOS::IsPresent() const {
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 DevicesMap::iterator iter = devices_.find(address); 352 DevicesMap::iterator iter = devices_.find(address);
353 BluetoothDeviceChromeOS* device; 353 BluetoothDeviceChromeOS* device;
354 const bool update_device = (iter != devices_.end()); 354 const bool update_device = (iter != devices_.end());
355 if (update_device) { 355 if (update_device) {
356 device = static_cast<BluetoothDeviceChromeOS*>(iter->second); 356 device = static_cast<BluetoothDeviceChromeOS*>(iter->second);
357 } else { 357 } else {
358 device = BluetoothDeviceChromeOS::Create(this); 358 device = BluetoothDeviceChromeOS::Create(this);
359 devices_[address] = device; 359 devices_[address] = device;
360 } 360 }
361 361
362 const bool was_paired = device->IsPaired(); 362 if (!device->HasObjectPath()) {
363 if (!was_paired) {
364 VLOG(1) << "Assigned object path " << device_path.value() << " to device " 363 VLOG(1) << "Assigned object path " << device_path.value() << " to device "
365 << address; 364 << address;
366 device->SetObjectPath(device_path); 365 device->SetObjectPath(device_path);
367 } 366 }
368 device->Update(properties, true); 367 device->Update(properties, true);
369 368
370 if (update_device) { 369 if (update_device) {
371 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 370 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
372 DeviceChanged(this, device)); 371 DeviceChanged(this, device));
373 } else { 372 } else {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 static_cast<BluetoothDeviceChromeOS*>(iter->second); 409 static_cast<BluetoothDeviceChromeOS*>(iter->second);
411 DevicesMap::iterator temp = iter; 410 DevicesMap::iterator temp = iter;
412 ++iter; 411 ++iter;
413 412
414 if (device->object_path_ != device_path) 413 if (device->object_path_ != device_path)
415 continue; 414 continue;
416 415
417 // DeviceRemoved can also be called to indicate a device that is visible 416 // DeviceRemoved can also be called to indicate a device that is visible
418 // during discovery has disconnected, but it is still visible to the 417 // during discovery has disconnected, but it is still visible to the
419 // adapter, so don't remove in that case and only clear the object path. 418 // adapter, so don't remove in that case and only clear the object path.
420 if (!device->IsVisible()) { 419 if (!device->WasDiscovered()) {
421 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 420 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
422 DeviceRemoved(this, device)); 421 DeviceRemoved(this, device));
423 422
424 VLOG(1) << "Removed device " << device->address(); 423 VLOG(1) << "Removed device " << device->GetAddress();
425 424
426 delete device; 425 delete device;
427 devices_.erase(temp); 426 devices_.erase(temp);
428 } else { 427 } else {
429 VLOG(1) << "Removed object path from device " << device->address(); 428 VLOG(1) << "Removed object path from device " << device->GetAddress();
430 device->RemoveObjectPath(); 429 device->RemoveObjectPath();
431 430
432 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 431 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
433 DeviceChanged(this, device)); 432 DeviceChanged(this, device));
434 } 433 }
435 } 434 }
436 } 435 }
437 436
438 void BluetoothAdapterChromeOS::DevicesChanged( 437 void BluetoothAdapterChromeOS::DevicesChanged(
439 const std::vector<dbus::ObjectPath>& devices) { 438 const std::vector<dbus::ObjectPath>& devices) {
440 for (std::vector<dbus::ObjectPath>::const_iterator iter = 439 for (std::vector<dbus::ObjectPath>::const_iterator iter =
441 devices.begin(); iter != devices.end(); ++iter) 440 devices.begin(); iter != devices.end(); ++iter)
442 UpdateDevice(*iter); 441 UpdateDevice(*iter);
443 } 442 }
444 443
445 void BluetoothAdapterChromeOS::ClearDiscoveredDevices() { 444 void BluetoothAdapterChromeOS::ClearDiscoveredDevices() {
446 DevicesMap::iterator iter = devices_.begin(); 445 DevicesMap::iterator iter = devices_.begin();
447 while (iter != devices_.end()) { 446 while (iter != devices_.end()) {
448 BluetoothDevice* device = iter->second; 447 BluetoothDeviceChromeOS* device =
448 static_cast<BluetoothDeviceChromeOS*>(iter->second);
449 DevicesMap::iterator temp = iter; 449 DevicesMap::iterator temp = iter;
450 ++iter; 450 ++iter;
451 451
452 if (!device->IsPaired()) { 452 if (!device->HasObjectPath()) {
453 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 453 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
454 DeviceRemoved(this, device)); 454 DeviceRemoved(this, device));
455 455
456 delete device; 456 delete device;
457 devices_.erase(temp); 457 devices_.erase(temp);
458 } 458
459 } else
460 device->SetDiscovered(false);
459 } 461 }
460 } 462 }
461 463
462 void BluetoothAdapterChromeOS::DeviceFound( 464 void BluetoothAdapterChromeOS::DeviceFound(
463 const dbus::ObjectPath& adapter_path, 465 const dbus::ObjectPath& adapter_path,
464 const std::string& address, 466 const std::string& address,
465 const BluetoothDeviceClient::Properties& properties) { 467 const BluetoothDeviceClient::Properties& properties) {
466 if (adapter_path != object_path_) 468 if (adapter_path != object_path_)
467 return; 469 return;
468 470
469 // DeviceFound can also be called to indicate that a device we've 471 // DeviceFound can also be called to indicate that a device we've
470 // paired with is now visible to the adapter during discovery, in which 472 // paired with is now visible to the adapter during discovery, in which
471 // case we want to update the existing object, not create a new one. 473 // case we want to update the existing object, not create a new one.
472 BluetoothDeviceChromeOS* device; 474 BluetoothDeviceChromeOS* device;
473 DevicesMap::iterator iter = devices_.find(address); 475 DevicesMap::iterator iter = devices_.find(address);
474 const bool update_device = (iter != devices_.end()); 476 const bool update_device = (iter != devices_.end());
475 if (update_device) { 477 if (update_device) {
476 device = static_cast<BluetoothDeviceChromeOS*>(iter->second); 478 device = static_cast<BluetoothDeviceChromeOS*>(iter->second);
477 } else { 479 } else {
478 device = BluetoothDeviceChromeOS::Create(this); 480 device = BluetoothDeviceChromeOS::Create(this);
479 devices_[address] = device; 481 devices_[address] = device;
480 } 482 }
481 483
482 VLOG(1) << "Device " << address << " is visible to the adapter"; 484 VLOG(1) << "Device " << address << " is visible to the adapter";
483 device->SetVisible(true); 485 device->SetDiscovered(true);
484 device->Update(&properties, false); 486 device->Update(&properties, false);
485 487
486 if (update_device) { 488 if (update_device) {
487 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 489 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
488 DeviceChanged(this, device)); 490 DeviceChanged(this, device));
489 } else { 491 } else {
490 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 492 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
491 DeviceAdded(this, device)); 493 DeviceAdded(this, device));
492 } 494 }
493 } 495 }
494 496
495 void BluetoothAdapterChromeOS::DeviceDisappeared( 497 void BluetoothAdapterChromeOS::DeviceDisappeared(
496 const dbus::ObjectPath& adapter_path, 498 const dbus::ObjectPath& adapter_path,
497 const std::string& address) { 499 const std::string& address) {
498 if (adapter_path != object_path_) 500 if (adapter_path != object_path_)
499 return; 501 return;
500 502
501 DevicesMap::iterator iter = devices_.find(address); 503 DevicesMap::iterator iter = devices_.find(address);
502 if (iter == devices_.end()) 504 if (iter == devices_.end())
503 return; 505 return;
504 506
505 BluetoothDeviceChromeOS* device = 507 BluetoothDeviceChromeOS* device =
506 static_cast<BluetoothDeviceChromeOS*>(iter->second); 508 static_cast<BluetoothDeviceChromeOS*>(iter->second);
507 509
508 // DeviceDisappeared can also be called to indicate that a device we've 510 // DeviceDisappeared can also be called to indicate that a device we've
509 // paired with is no longer visible to the adapter, so don't remove 511 // paired with is no longer visible to the adapter, so don't remove
510 // in that case and only clear the visible flag. 512 // in that case and only clear the visible flag.
511 if (!device->IsPaired()) { 513 if (!device->HasObjectPath()) {
512 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 514 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
513 DeviceRemoved(this, device)); 515 DeviceRemoved(this, device));
514 516
515 VLOG(1) << "Discovered device " << device->address() 517 VLOG(1) << "Discovered device " << device->GetAddress()
516 << " is no longer visible to the adapter"; 518 << " is no longer visible to the adapter";
517 519
518 delete device; 520 delete device;
519 devices_.erase(iter); 521 devices_.erase(iter);
520 } else { 522 } else {
521 VLOG(1) << "Paired device " << device->address() 523 VLOG(1) << "Paired device " << device->GetAddress()
522 << " is no longer visible to the adapter"; 524 << " is no longer visible to the adapter";
523 device->SetVisible(false); 525 device->SetDiscovered(false);
524 526
525 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 527 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
526 DeviceChanged(this, device)); 528 DeviceChanged(this, device));
527 } 529 }
528 } 530 }
529 531
530 } // namespace chromeos 532 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_chromeos.h ('k') | device/bluetooth/bluetooth_adapter_chromeos_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698