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_util.h"
10 #include "chrome/browser/local_discovery/privet_constants.h"
11 #include "chrome/common/local_discovery/service_discovery_client.h"
13 namespace local_discovery
{
17 DeviceDescription::ConnectionState
18 ConnectionStateFromString(const std::string
& str
) {
19 if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusOnline
)) {
20 return DeviceDescription::ONLINE
;
21 } else if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusOffline
)) {
22 return DeviceDescription::OFFLINE
;
23 } else if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusConnecting
)) {
24 return DeviceDescription::CONNECTING
;
25 } else if (LowerCaseEqualsASCII(str
, kPrivetConnectionStatusNotConfigured
)) {
26 return DeviceDescription::NOT_CONFIGURED
;
29 return DeviceDescription::UNKNOWN
;
34 void DeviceDescription::FillFromServiceDescription(
35 const ServiceDescription
& service_description
) {
36 address
= service_description
.address
;
37 ip_address
= service_description
.ip_address
;
38 last_seen
= service_description
.last_seen
;
40 for (std::vector
<std::string
>::const_iterator i
=
41 service_description
.metadata
.begin();
42 i
!= service_description
.metadata
.end();
44 size_t equals_pos
= i
->find_first_of('=');
45 if (equals_pos
== std::string::npos
)
46 continue; // We do not parse non key-value TXT records
48 std::string key
= i
->substr(0, equals_pos
);
49 std::string value
= i
->substr(equals_pos
+ 1);
51 if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyName
)) {
53 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyDescription
)) {
55 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyURL
)) {
57 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyType
)) {
59 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyID
)) {
61 } else if (LowerCaseEqualsASCII(key
, kPrivetTxtKeyConnectionState
)) {
62 connection_state
= ConnectionStateFromString(value
);
68 } // namespace local_discovery