privet_device_lister_impl.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
1// Copyright 2013 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/privet_device_lister_impl.h"
6
7#include <utility>
8#include <vector>
9
10#include "base/strings/string_util.h"
11#include "base/strings/stringprintf.h"
12#include "chrome/browser/local_discovery/privet_constants.h"
13
14namespace local_discovery {
15
16namespace {
17
18
19DeviceDescription::ConnectionState
20ConnectionStateFromString(const std::string& str) {
21  if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOnline)) {
22    return DeviceDescription::ONLINE;
23  } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOffline)) {
24    return DeviceDescription::OFFLINE;
25  } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusConnecting)) {
26    return DeviceDescription::CONNECTING;
27  } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusNotConfigured)) {
28    return DeviceDescription::NOT_CONFIGURED;
29  }
30
31  return DeviceDescription::UNKNOWN;
32}
33
34void FillDeviceDescription(const ServiceDescription& service_description,
35                           DeviceDescription* device_description) {
36  device_description->address = service_description.address;
37  device_description->ip_address = service_description.ip_address;
38  device_description->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      device_description->name = value;
53    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyDescription)) {
54      device_description->description = value;
55    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyURL)) {
56      device_description->url = value;
57    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyType)) {
58      device_description->type = value;
59    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyID)) {
60      device_description->id = value;
61    } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyConnectionState)) {
62      device_description->connection_state = ConnectionStateFromString(value);
63    }
64  }
65}
66
67}  // namespace
68
69PrivetDeviceListerImpl::PrivetDeviceListerImpl(
70    ServiceDiscoveryClient* service_discovery_client,
71    PrivetDeviceLister::Delegate* delegate)
72    : delegate_(delegate),
73      device_lister_(this, service_discovery_client, kPrivetDefaultDeviceType) {
74}
75
76PrivetDeviceListerImpl::PrivetDeviceListerImpl(
77    ServiceDiscoveryClient* service_discovery_client,
78    PrivetDeviceLister::Delegate* delegate,
79    const std::string& subtype)
80        : delegate_(delegate),
81          device_lister_(
82              this,
83              service_discovery_client,
84              base::StringPrintf(kPrivetSubtypeTemplate, subtype.c_str())) {
85}
86
87PrivetDeviceListerImpl::~PrivetDeviceListerImpl() {
88}
89
90void PrivetDeviceListerImpl::Start() {
91  device_lister_.Start();
92}
93
94void PrivetDeviceListerImpl::DiscoverNewDevices(bool force_update) {
95  device_lister_.DiscoverNewDevices(force_update);
96}
97
98void PrivetDeviceListerImpl::OnDeviceChanged(
99    bool added, const ServiceDescription& service_description) {
100  if (!delegate_)
101    return;
102
103  DeviceDescription device_description;
104  FillDeviceDescription(service_description, &device_description);
105
106  delegate_->DeviceChanged(
107      added, service_description.service_name, device_description);
108}
109
110void PrivetDeviceListerImpl::OnDeviceRemoved(const std::string& service_name) {
111  if (delegate_)
112    delegate_->DeviceRemoved(service_name);
113}
114
115void PrivetDeviceListerImpl::OnDeviceCacheFlushed() {
116  if (delegate_)
117    delegate_->DeviceCacheFlushed();
118}
119
120}  // namespace local_discovery
121