OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/values.h" |
| 9 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 10 |
| 11 namespace chromeos { |
| 12 |
| 13 class BluetoothDeviceImpl : public BluetoothDevice { |
| 14 public: |
| 15 BluetoothDeviceImpl(const std::string& address, |
| 16 uint32 bluetooth_class, |
| 17 const std::string& icon, |
| 18 const std::string& name, |
| 19 bool paired, |
| 20 const base::DictionaryValue& properties) |
| 21 : address_(address), |
| 22 bluetooth_class_(bluetooth_class), |
| 23 icon_(icon), |
| 24 name_(name), |
| 25 paired_(paired), |
| 26 dictionary_rep_(properties.DeepCopy()) { |
| 27 DCHECK(dictionary_rep_); |
| 28 } |
| 29 |
| 30 virtual ~BluetoothDeviceImpl() { |
| 31 delete dictionary_rep_; |
| 32 } |
| 33 |
| 34 virtual const std::string& GetAddress() const { |
| 35 return address_; |
| 36 } |
| 37 |
| 38 virtual uint32 GetBluetoothClass() const { |
| 39 return bluetooth_class_; |
| 40 } |
| 41 |
| 42 virtual const std::string& GetIcon() const { |
| 43 return icon_; |
| 44 } |
| 45 |
| 46 virtual const std::string& GetName() const { |
| 47 return name_; |
| 48 } |
| 49 |
| 50 virtual bool IsPaired() const { |
| 51 return paired_; |
| 52 } |
| 53 |
| 54 virtual const base::DictionaryValue& AsDictionary() const { |
| 55 DCHECK(dictionary_rep_); |
| 56 return *dictionary_rep_; |
| 57 } |
| 58 |
| 59 private: |
| 60 const std::string address_; |
| 61 uint32 bluetooth_class_; |
| 62 const std::string icon_; |
| 63 const std::string name_; |
| 64 bool paired_; |
| 65 base::DictionaryValue* dictionary_rep_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceImpl); |
| 68 }; |
| 69 |
| 70 BluetoothDevice::BluetoothDevice() { |
| 71 } |
| 72 |
| 73 BluetoothDevice::~BluetoothDevice() { |
| 74 } |
| 75 |
| 76 // static |
| 77 BluetoothDevice* BluetoothDevice::Create( |
| 78 const base::DictionaryValue& properties) { |
| 79 std::string address; |
| 80 uint32 bluetooth_class = 0; |
| 81 std::string icon; |
| 82 std::string name; |
| 83 bool paired = false; |
| 84 |
| 85 if (!properties.GetString(bluetooth_device::kAddressProperty, &address)) { |
| 86 LOG(ERROR) << "No address property"; |
| 87 return NULL; // mandatory |
| 88 } |
| 89 VLOG(1) << "address = " << address; |
| 90 |
| 91 // Note: DictionaryValue is Javascript-oriented and doesn't support unsigned |
| 92 // integers. |
| 93 if (!properties.GetInteger(bluetooth_device::kClassProperty, |
| 94 reinterpret_cast<int*>(&bluetooth_class))) { |
| 95 LOG(ERROR) << "No bluetooth_class property"; |
| 96 return NULL; // mandatory |
| 97 } |
| 98 VLOG(1) << "bluetooth_class = 0x" << std::hex << bluetooth_class; |
| 99 |
| 100 if (!properties.GetString(bluetooth_device::kIconProperty, &icon)) { |
| 101 LOG(WARNING) << "No icon property"; |
| 102 } |
| 103 VLOG(1) << "icon = " << icon; |
| 104 |
| 105 if (!properties.GetString(bluetooth_device::kNameProperty, &name)) { |
| 106 LOG(WARNING) << "No name property. Substituting MAC address."; |
| 107 // Populate name with mac address so that UI doesn't display a blank name. |
| 108 name = address; |
| 109 } |
| 110 VLOG(1) << "name = " << name; |
| 111 |
| 112 if (!properties.GetBoolean(bluetooth_device::kPairedProperty, &paired)) { |
| 113 LOG(ERROR) << "No paired property"; |
| 114 return NULL; // mandatory |
| 115 } |
| 116 |
| 117 return new BluetoothDeviceImpl(address, bluetooth_class, icon, name, paired, |
| 118 properties); |
| 119 } |
| 120 |
| 121 } // namespace chromeos |
OLD | NEW |