| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/local_discovery/device_description.h" | 5 #include "chrome/browser/local_discovery/device_description.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/local_discovery/privet_constants.h" | 11 #include "chrome/browser/local_discovery/privet_constants.h" |
| 11 #include "chrome/common/local_discovery/service_discovery_client.h" | 12 #include "chrome/common/local_discovery/service_discovery_client.h" |
| 12 | 13 |
| 13 namespace local_discovery { | 14 namespace local_discovery { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 DeviceDescription::ConnectionState | 18 DeviceDescription::ConnectionState |
| 18 ConnectionStateFromString(const std::string& str) { | 19 ConnectionStateFromString(const std::string& str) { |
| 19 if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOnline)) { | 20 if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOnline)) { |
| 20 return DeviceDescription::ONLINE; | 21 return DeviceDescription::ONLINE; |
| 21 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOffline)) { | 22 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOffline)) { |
| 22 return DeviceDescription::OFFLINE; | 23 return DeviceDescription::OFFLINE; |
| 23 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusConnecting)) { | 24 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusConnecting)) { |
| 24 return DeviceDescription::CONNECTING; | 25 return DeviceDescription::CONNECTING; |
| 25 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusNotConfigured)) { | 26 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusNotConfigured)) { |
| 26 return DeviceDescription::NOT_CONFIGURED; | 27 return DeviceDescription::NOT_CONFIGURED; |
| 27 } | 28 } |
| 28 | 29 |
| 29 return DeviceDescription::UNKNOWN; | 30 return DeviceDescription::UNKNOWN; |
| 30 } | 31 } |
| 31 | 32 |
| 32 } // namespace | 33 } // namespace |
| 33 | 34 |
| 35 DeviceDescription::DeviceDescription() |
| 36 : version(0), |
| 37 connection_state(UNKNOWN) { |
| 38 } |
| 39 |
| 40 DeviceDescription::~DeviceDescription() { |
| 41 } |
| 42 |
| 34 void DeviceDescription::FillFromServiceDescription( | 43 void DeviceDescription::FillFromServiceDescription( |
| 35 const ServiceDescription& service_description) { | 44 const ServiceDescription& service_description) { |
| 36 address = service_description.address; | 45 address = service_description.address; |
| 37 ip_address = service_description.ip_address; | 46 ip_address = service_description.ip_address; |
| 38 last_seen = service_description.last_seen; | 47 last_seen = service_description.last_seen; |
| 39 | 48 |
| 40 for (std::vector<std::string>::const_iterator i = | 49 for (std::vector<std::string>::const_iterator i = |
| 41 service_description.metadata.begin(); | 50 service_description.metadata.begin(); |
| 42 i != service_description.metadata.end(); | 51 i != service_description.metadata.end(); |
| 43 i++) { | 52 i++) { |
| 44 size_t equals_pos = i->find_first_of('='); | 53 size_t equals_pos = i->find_first_of('='); |
| 45 if (equals_pos == std::string::npos) | 54 if (equals_pos == std::string::npos) |
| 46 continue; // We do not parse non key-value TXT records | 55 continue; // We do not parse non key-value TXT records |
| 47 | 56 |
| 48 std::string key = i->substr(0, equals_pos); | 57 std::string key = i->substr(0, equals_pos); |
| 49 std::string value = i->substr(equals_pos + 1); | 58 std::string value = i->substr(equals_pos + 1); |
| 50 | 59 |
| 51 if (LowerCaseEqualsASCII(key, kPrivetTxtKeyName)) { | 60 if (LowerCaseEqualsASCII(key, kPrivetTxtKeyVersion)) { |
| 61 if (!base::StringToInt(value, &version)) |
| 62 continue; // Unknown version. |
| 63 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyName)) { |
| 52 name = value; | 64 name = value; |
| 53 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyDescription)) { | 65 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyDescription)) { |
| 54 description = value; | 66 description = value; |
| 55 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyURL)) { | 67 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyURL)) { |
| 56 url = value; | 68 url = value; |
| 57 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyType)) { | 69 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyType)) { |
| 58 type = value; | 70 type = value; |
| 59 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyID)) { | 71 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyID)) { |
| 60 id = value; | 72 id = value; |
| 61 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyConnectionState)) { | 73 } else if (LowerCaseEqualsASCII(key, kPrivetTxtKeyConnectionState)) { |
| 62 connection_state = ConnectionStateFromString(value); | 74 connection_state = ConnectionStateFromString(value); |
| 63 } | 75 } |
| 64 } | 76 } |
| 65 } | 77 } |
| 66 | 78 |
| 67 | 79 |
| 68 } // namespace local_discovery | 80 } // namespace local_discovery |
| OLD | NEW |