device_description.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_util.h"
10#include "chrome/browser/local_discovery/privet_constants.h"
11#include "chrome/common/local_discovery/service_discovery_client.h"
12
13namespace local_discovery {
14
15namespace {
16
17DeviceDescription::ConnectionState
18ConnectionStateFromString(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;
27  }
28
29  return DeviceDescription::UNKNOWN;
30}
31
32}  // namespace
33
34void 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;
39
40  for (std::vector<std::string>::const_iterator i =
41           service_description.metadata.begin();
42       i != service_description.metadata.end();
43       i++) {
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
47
48    std::string key = i->substr(0, equals_pos);
49    std::string value = i->substr(equals_pos + 1);
50
51    if (LowerCaseEqualsASCII(key, kPrivetTxtKeyName)) {
52      name = value;
53    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyDescription)) {
54      description = value;
55    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyURL)) {
56      url = value;
57    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyType)) {
58      type = value;
59    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyID)) {
60      id = value;
61    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyConnectionState)) {
62      connection_state = ConnectionStateFromString(value);
63    }
64  }
65}
66
67
68}  // namespace local_discovery
69