device_description.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#include "chrome/browser/local_discovery/device_description.h"
6
7#include <vector>
8
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"
13
14namespace local_discovery {
15
16namespace {
17
18DeviceDescription::ConnectionState
19ConnectionStateFromString(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;
28  }
29
30  return DeviceDescription::UNKNOWN;
31}
32
33}  // namespace
34
35DeviceDescription::DeviceDescription()
36    : version(0),
37      connection_state(UNKNOWN) {
38}
39
40DeviceDescription::~DeviceDescription() {
41}
42
43void 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;
48
49  for (std::vector<std::string>::const_iterator i =
50           service_description.metadata.begin();
51       i != service_description.metadata.end();
52       i++) {
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
56
57    std::string key = i->substr(0, equals_pos);
58    std::string value = i->substr(equals_pos + 1);
59
60    if (LowerCaseEqualsASCII(key, kPrivetTxtKeyVersion)) {
61      if (!base::StringToInt(value, &version))
62        continue;  // Unknown version.
63    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyName)) {
64      name = value;
65    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyDescription)) {
66      description = value;
67    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyURL)) {
68      url = value;
69    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyType)) {
70      type = value;
71    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyID)) {
72      id = value;
73    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyConnectionState)) {
74      connection_state = ConnectionStateFromString(value);
75    }
76  }
77}
78
79
80}  // namespace local_discovery
81