diff options
author | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-16 03:38:38 +0000 |
---|---|---|
committer | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-16 03:38:38 +0000 |
commit | 658ce05d1f2ec2717f3c00c9b21934373010ca35 (patch) | |
tree | 80f638faae01c9792125c05bff0790e2eb0334f4 /device | |
parent | 8c9311c3b938c389b6292eff3b830408e662c261 (diff) | |
download | chromium_src-658ce05d1f2ec2717f3c00c9b21934373010ca35.zip chromium_src-658ce05d1f2ec2717f3c00c9b21934373010ca35.tar.gz chromium_src-658ce05d1f2ec2717f3c00c9b21934373010ca35.tar.bz2 |
Separated BluetoothServiceRecord interface from BluetoothServiceRecordChromeOs.
BluetoothServiceRecordWin will take the windows-specific object to populate the
service records instead of xml data; hence we need to separate the interface so
that the implementations can be done separately in different platforms.
BUG=135470
Review URL: https://chromiumcodereview.appspot.com/11884027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@177072 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/bluetooth_device_chromeos.cc | 3 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_service_record.cc | 111 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_service_record.h | 36 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_service_record_chromeos.cc | 122 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_service_record_chromeos.h | 33 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_service_record_chromeos_unittest.cc (renamed from device/bluetooth/bluetooth_service_record_unittest.cc) | 24 | ||||
-rw-r--r-- | device/device.gyp | 4 |
7 files changed, 190 insertions, 143 deletions
diff --git a/device/bluetooth/bluetooth_device_chromeos.cc b/device/bluetooth/bluetooth_device_chromeos.cc index c8bb700..b452ff0 100644 --- a/device/bluetooth/bluetooth_device_chromeos.cc +++ b/device/bluetooth/bluetooth_device_chromeos.cc @@ -28,6 +28,7 @@ #include "device/bluetooth/bluetooth_adapter_chromeos.h" #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" #include "device/bluetooth/bluetooth_service_record.h" +#include "device/bluetooth/bluetooth_service_record_chromeos.h" #include "device/bluetooth/bluetooth_socket_chromeos.h" #include "device/bluetooth/bluetooth_utils.h" #include "third_party/cros_system_api/dbus/service_constants.h" @@ -404,7 +405,7 @@ void BluetoothDeviceChromeOs::CollectServiceRecordsCallback( for (BluetoothDeviceClient::ServiceMap::const_iterator i = service_map.begin(); i != service_map.end(); ++i) { records.push_back( - new BluetoothServiceRecord(address(), i->second)); + new BluetoothServiceRecordChromeOS(address(), i->second)); } callback.Run(records); } diff --git a/device/bluetooth/bluetooth_service_record.cc b/device/bluetooth/bluetooth_service_record.cc index 0ea18d7..aa97373 100644 --- a/device/bluetooth/bluetooth_service_record.cc +++ b/device/bluetooth/bluetooth_service_record.cc @@ -4,119 +4,12 @@ #include "device/bluetooth/bluetooth_service_record.h" -#include <string> -#include <vector> - -#include "base/logging.h" -#include "base/string_number_conversions.h" -#include "device/bluetooth/bluetooth_utils.h" -#include "third_party/libxml/chromium/libxml_utils.h" - -namespace { - -static const char* kAttributeNode = "attribute"; -static const char* kIdAttribute = "id"; -static const char* kProtocolDescriptorListId = "0x0004"; -static const char* kRfcommUuid = "0x0003"; -static const char* kSdpNameId = "0x0100"; -static const char* kSequenceNode = "sequence"; -static const char* kTextNode = "text"; -static const char* kUint8Node = "uint8"; -static const char* kUuidId = "0x0001"; -static const char* kUuidNode = "uuid"; -static const char* kValueAttribute = "value"; - -bool AdvanceToTag(XmlReader* reader, const char* node_type) { - do { - if (!reader->Read()) - return false; - } while (reader->NodeName() != node_type); - return true; -} - -bool ExtractTextValue(XmlReader* reader, std::string* value_out) { - if (AdvanceToTag(reader, kTextNode)) { - reader->NodeAttribute(kValueAttribute, value_out); - return true; - } - return false; -} - -} // namespace - namespace device { -BluetoothServiceRecord::BluetoothServiceRecord( - const std::string& address, - const std::string& xml_data) - : address_(address), - supports_rfcomm_(false) { - - XmlReader reader; - if (!reader.Load(xml_data)) - return; - - while (AdvanceToTag(&reader, kAttributeNode)) { - std::string id; - if (reader.NodeAttribute(kIdAttribute, &id)) { - if (id == kSdpNameId) { - ExtractTextValue(&reader, &name_); - } else if (id == kProtocolDescriptorListId) { - if (AdvanceToTag(&reader, kSequenceNode)) { - ExtractChannels(&reader); - } - } else if (id == kUuidId) { - if (AdvanceToTag(&reader, kSequenceNode)) { - ExtractUuid(&reader); - } - } - } - // We don't care about anything else here, so find the closing tag - AdvanceToTag(&reader, kAttributeNode); - } +BluetoothServiceRecord::BluetoothServiceRecord() { } -void BluetoothServiceRecord::ExtractChannels(XmlReader* reader) { - const int start_depth = reader->Depth(); - do { - if (reader->NodeName() == kSequenceNode) { - if (AdvanceToTag(reader, kUuidNode)) { - std::string type; - if (reader->NodeAttribute(kValueAttribute, &type) && - type == kRfcommUuid) { - if (AdvanceToTag(reader, kUint8Node)) { - std::string channel_string; - if (reader->NodeAttribute(kValueAttribute, &channel_string)) { - std::vector<uint8> channel_bytes; - if (base::HexStringToBytes(channel_string.substr(2), - &channel_bytes)) { - if (channel_bytes.size() == 1) { - rfcomm_channel_ = channel_bytes[0]; - supports_rfcomm_ = true; - } - } - } - } - } - } - } - } while (AdvanceToTag(reader, kSequenceNode) && - reader->Depth() != start_depth); -} - -void BluetoothServiceRecord::ExtractUuid(XmlReader* reader) { - const int start_depth = reader->Depth(); - do { - if (reader->NodeName() == kSequenceNode) { - if (AdvanceToTag(reader, kUuidNode)) { - if (!reader->NodeAttribute(kValueAttribute, &uuid_)) - uuid_.clear(); - } - } - } while (AdvanceToTag(reader, kSequenceNode) && - reader->Depth() != start_depth); - - uuid_ = device::bluetooth_utils::CanonicalUuid(uuid_); +BluetoothServiceRecord::~BluetoothServiceRecord() { } } // namespace device diff --git a/device/bluetooth/bluetooth_service_record.h b/device/bluetooth/bluetooth_service_record.h index e04d73a..5059776 100644 --- a/device/bluetooth/bluetooth_service_record.h +++ b/device/bluetooth/bluetooth_service_record.h @@ -9,8 +9,6 @@ #include "base/basictypes.h" -class XmlReader; - namespace device { // BluetoothServiceRecord represents an SDP service record. @@ -19,30 +17,27 @@ namespace device { // that have been necessary so far. class BluetoothServiceRecord { public: - BluetoothServiceRecord( - const std::string& address, - const std::string& xml_data); + virtual ~BluetoothServiceRecord(); - // The human-readable name of this service. - const std::string& name() const { return name_; } + // The human-readable name of this service. + const std::string& name() const { return name_; } - // The address of the BluetoothDevice providing this service. - const std::string& address() const { return address_; } + // The address of the BluetoothDevice providing this service. + const std::string& address() const { return address_; } - // The UUID of the service. This field may be empty if no UUID was - // specified in the service record. - const std::string& uuid() const { return uuid_; } + // The UUID of the service. This field may be empty if no UUID was + // specified in the service record. + const std::string& uuid() const { return uuid_; } - // Indicates if this service supports RFCOMM communication. - bool SupportsRfcomm() const { return supports_rfcomm_; } + // Indicates if this service supports RFCOMM communication. + bool SupportsRfcomm() const { return supports_rfcomm_; } - // The RFCOMM channel to use, if this service supports RFCOMM communication. - // The return value is undefined if SupportsRfcomm() returns false. - uint8 rfcomm_channel() const { return rfcomm_channel_; } + // The RFCOMM channel to use, if this service supports RFCOMM communication. + // The return value is undefined if SupportsRfcomm() returns false. + uint8 rfcomm_channel() const { return rfcomm_channel_; } - private: - void ExtractChannels(XmlReader* reader); - void ExtractUuid(XmlReader* reader); + protected: + BluetoothServiceRecord(); std::string address_; std::string name_; @@ -51,6 +46,7 @@ class BluetoothServiceRecord { bool supports_rfcomm_; uint8 rfcomm_channel_; + private: DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecord); }; diff --git a/device/bluetooth/bluetooth_service_record_chromeos.cc b/device/bluetooth/bluetooth_service_record_chromeos.cc new file mode 100644 index 0000000..a5f3818 --- /dev/null +++ b/device/bluetooth/bluetooth_service_record_chromeos.cc @@ -0,0 +1,122 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device/bluetooth/bluetooth_service_record_chromeos.h" + +#include <string> +#include <vector> + +#include "base/logging.h" +#include "base/string_number_conversions.h" +#include "device/bluetooth/bluetooth_utils.h" +#include "third_party/libxml/chromium/libxml_utils.h" + +namespace { + +static const char* kAttributeNode = "attribute"; +static const char* kIdAttribute = "id"; +static const char* kProtocolDescriptorListId = "0x0004"; +static const char* kRfcommUuid = "0x0003"; +static const char* kSdpNameId = "0x0100"; +static const char* kSequenceNode = "sequence"; +static const char* kTextNode = "text"; +static const char* kUint8Node = "uint8"; +static const char* kUuidId = "0x0001"; +static const char* kUuidNode = "uuid"; +static const char* kValueAttribute = "value"; + +bool AdvanceToTag(XmlReader* reader, const char* node_type) { + do { + if (!reader->Read()) + return false; + } while (reader->NodeName() != node_type); + return true; +} + +bool ExtractTextValue(XmlReader* reader, std::string* value_out) { + if (AdvanceToTag(reader, kTextNode)) { + reader->NodeAttribute(kValueAttribute, value_out); + return true; + } + return false; +} + +} // namespace + +namespace chromeos { + +BluetoothServiceRecordChromeOS::BluetoothServiceRecordChromeOS( + const std::string& address, + const std::string& xml_data) { + address_ = address; + supports_rfcomm_ = false; + + XmlReader reader; + if (!reader.Load(xml_data)) + return; + + while (AdvanceToTag(&reader, kAttributeNode)) { + std::string id; + if (reader.NodeAttribute(kIdAttribute, &id)) { + if (id == kSdpNameId) { + ExtractTextValue(&reader, &name_); + } else if (id == kProtocolDescriptorListId) { + if (AdvanceToTag(&reader, kSequenceNode)) { + ExtractChannels(&reader); + } + } else if (id == kUuidId) { + if (AdvanceToTag(&reader, kSequenceNode)) { + ExtractUuid(&reader); + } + } + } + // We don't care about anything else here, so find the closing tag + AdvanceToTag(&reader, kAttributeNode); + } +} + +void BluetoothServiceRecordChromeOS::ExtractChannels(XmlReader* reader) { + const int start_depth = reader->Depth(); + do { + if (reader->NodeName() == kSequenceNode) { + if (AdvanceToTag(reader, kUuidNode)) { + std::string type; + if (reader->NodeAttribute(kValueAttribute, &type) && + type == kRfcommUuid) { + if (AdvanceToTag(reader, kUint8Node)) { + std::string channel_string; + if (reader->NodeAttribute(kValueAttribute, &channel_string)) { + std::vector<uint8> channel_bytes; + if (base::HexStringToBytes(channel_string.substr(2), + &channel_bytes)) { + if (channel_bytes.size() == 1) { + rfcomm_channel_ = channel_bytes[0]; + supports_rfcomm_ = true; + } + } + } + } + } + } + } + } while (AdvanceToTag(reader, kSequenceNode) && + reader->Depth() != start_depth); +} + +void BluetoothServiceRecordChromeOS::ExtractUuid(XmlReader* reader) { + const int start_depth = reader->Depth(); + do { + if (reader->NodeName() == kSequenceNode) { + if (AdvanceToTag(reader, kUuidNode)) { + if (!reader->NodeAttribute(kValueAttribute, &uuid_)) + uuid_.clear(); + } + } + } while (AdvanceToTag(reader, kSequenceNode) && + reader->Depth() != start_depth); + + uuid_ = device::bluetooth_utils::CanonicalUuid(uuid_); +} + +} // namespace chromeos diff --git a/device/bluetooth/bluetooth_service_record_chromeos.h b/device/bluetooth/bluetooth_service_record_chromeos.h new file mode 100644 index 0000000..b3adc25 --- /dev/null +++ b/device/bluetooth/bluetooth_service_record_chromeos.h @@ -0,0 +1,33 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef DEVICE_BLUETOOTH_BLUETOOTH_SERVICE_RECORD_CHROMEOS_H_ +#define DEVICE_BLUETOOTH_BLUETOOTH_SERVICE_RECORD_CHROMEOS_H_ + +#include <string> + +#include "base/basictypes.h" +#include "device/bluetooth/bluetooth_service_record.h" + +class XmlReader; + +namespace chromeos { + +// BluetoothServiceRecordChromeOS is an implementation of BluetoothServiceRecord +// for the ChromeOS platform. +class BluetoothServiceRecordChromeOS : public device::BluetoothServiceRecord { + public: + BluetoothServiceRecordChromeOS(const std::string& address, + const std::string& xml_data); + + private: + void ExtractChannels(XmlReader* reader); + void ExtractUuid(XmlReader* reader); + + DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordChromeOS); +}; + +} // namespace chromeos + +#endif // DEVICE_BLUETOOTH_BLUETOOTH_SERVICE_RECORD_CHROMEOS_H_ diff --git a/device/bluetooth/bluetooth_service_record_unittest.cc b/device/bluetooth/bluetooth_service_record_chromeos_unittest.cc index b59474f5..2dbebab 100644 --- a/device/bluetooth/bluetooth_service_record_unittest.cc +++ b/device/bluetooth/bluetooth_service_record_chromeos_unittest.cc @@ -9,7 +9,7 @@ #include "base/file_path.h" #include "base/file_util.h" #include "base/path_service.h" -#include "device/bluetooth/bluetooth_service_record.h" +#include "device/bluetooth/bluetooth_service_record_chromeos.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -20,9 +20,9 @@ static const char* kSerialUuid = "00001101-0000-1000-8000-00805f9b34fb"; } // namespace -namespace device { +namespace chromeos { -class BluetoothServiceRecordTest : public testing::Test { +class BluetoothServiceRecordChromeOSTest : public testing::Test { public: FilePath GetTestDataFilePath(const char* file) { FilePath path; @@ -36,11 +36,11 @@ class BluetoothServiceRecordTest : public testing::Test { } }; -TEST_F(BluetoothServiceRecordTest, RfcommService) { +TEST_F(BluetoothServiceRecordChromeOSTest, RfcommService) { std::string xml_data; file_util::ReadFileToString(GetTestDataFilePath("rfcomm.xml"), &xml_data); - BluetoothServiceRecord service_record(kAddress, xml_data); + BluetoothServiceRecordChromeOS service_record(kAddress, xml_data); EXPECT_EQ(kAddress, service_record.address()); EXPECT_EQ("Headset Audio Gateway", service_record.name()); EXPECT_TRUE(service_record.SupportsRfcomm()); @@ -48,31 +48,31 @@ TEST_F(BluetoothServiceRecordTest, RfcommService) { EXPECT_EQ(kCustomUuid, service_record.uuid()); } -TEST_F(BluetoothServiceRecordTest, ShortUuid) { +TEST_F(BluetoothServiceRecordChromeOSTest, ShortUuid) { std::string xml_data; file_util::ReadFileToString(GetTestDataFilePath("short_uuid.xml"), &xml_data); - BluetoothServiceRecord short_uuid_service_record(kAddress, xml_data); + BluetoothServiceRecordChromeOS short_uuid_service_record(kAddress, xml_data); EXPECT_EQ(kSerialUuid, short_uuid_service_record.uuid()); xml_data.clear(); file_util::ReadFileToString( GetTestDataFilePath("medium_uuid.xml"), &xml_data); - BluetoothServiceRecord medium_uuid_service_record(kAddress, xml_data); + BluetoothServiceRecordChromeOS medium_uuid_service_record(kAddress, xml_data); EXPECT_EQ(kSerialUuid, medium_uuid_service_record.uuid()); } -TEST_F(BluetoothServiceRecordTest, CleanUuid) { +TEST_F(BluetoothServiceRecordChromeOSTest, CleanUuid) { std::string xml_data; file_util::ReadFileToString(GetTestDataFilePath("uppercase_uuid.xml"), &xml_data); - BluetoothServiceRecord service_record(kAddress, xml_data); + BluetoothServiceRecordChromeOS service_record(kAddress, xml_data); EXPECT_EQ(kCustomUuid, service_record.uuid()); xml_data.clear(); file_util::ReadFileToString(GetTestDataFilePath("invalid_uuid.xml"), &xml_data); - BluetoothServiceRecord invalid_service_record(kAddress, xml_data); + BluetoothServiceRecordChromeOS invalid_service_record(kAddress, xml_data); EXPECT_EQ("", invalid_service_record.uuid()); } -} // namespace device +} // namespace chromeos diff --git a/device/device.gyp b/device/device.gyp index 0f06eea..bc54b76 100644 --- a/device/device.gyp +++ b/device/device.gyp @@ -33,6 +33,8 @@ 'bluetooth/bluetooth_out_of_band_pairing_data.h', 'bluetooth/bluetooth_service_record.cc', 'bluetooth/bluetooth_service_record.h', + 'bluetooth/bluetooth_service_record_chromeos.cc', + 'bluetooth/bluetooth_service_record_chromeos.h', 'bluetooth/bluetooth_socket.h', 'bluetooth/bluetooth_socket_chromeos.cc', 'bluetooth/bluetooth_socket_chromeos.h', @@ -135,7 +137,7 @@ 'bluetooth/bluetooth_adapter_chromeos_unittest.cc', 'bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc', 'bluetooth/bluetooth_adapter_win_unittest.cc', - 'bluetooth/bluetooth_service_record_unittest.cc', + 'bluetooth/bluetooth_service_record_chromeos_unittest.cc', 'bluetooth/bluetooth_task_manager_win_unittest.cc', 'bluetooth/bluetooth_utils_unittest.cc', 'test/device_test_suite.cc', |