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
3 // found in the LICENSE file.
5 #include "chrome/browser/local_discovery/device_description.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "chrome/browser/local_discovery/privet_constants.h"
12 #include "chrome/common/local_discovery/service_discovery_client.h"
14 namespace local_discovery
{
18 DeviceDescription::ConnectionState
19 ConnectionStateFromString(const std::string
& str
) {
20 if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusOnline
)) {
21 return DeviceDescription::ONLINE
;
22 } else if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusOffline
)) {
23 return DeviceDescription::OFFLINE
;
24 } else if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusConnecting
)) {
25 return DeviceDescription::CONNECTING
;
26 } else if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusNotConfigured
)) {
27 return DeviceDescription::NOT_CONFIGURED
;
30 return DeviceDescription::UNKNOWN
;
35 DeviceDescription::DeviceDescription()
37 connection_state(UNKNOWN
) {
40 DeviceDescription::~DeviceDescription() {
43 void DeviceDescription::FillFromServiceDescription(
44 const ServiceDescription
& service_description
) {
45 address
= service_description
.address
;
46 ip_address
= service_description
.ip_address
;
47 last_seen
= service_description
.last_seen
;
49 for (std::vector
<std::string
>::const_iterator i
=
50 service_description
.metadata
.begin();
51 i
!= service_description
.metadata
.end();
53 size_t equals_pos
= i
->find_first_of('=');
54 if (equals_pos
== std::string::npos
)
55 continue; // We do not parse non key-value TXT records
57 std::string key
= i
->substr(0, equals_pos
);
58 std::string value
= i
->substr(equals_pos
+ 1);
60 if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyVersion
)) {
61 if (!base::StringToInt(value
, &version
))
62 continue; // Unknown version.
63 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyName
)) {
65 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyDescription
)) {
67 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyURL
)) {
69 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyType
)) {
71 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyID
)) {
73 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyConnectionState
)) {
74 connection_state
= ConnectionStateFromString(value
);
80 } // namespace local_discovery