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_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "chrome/browser/local_discovery/privet_constants.h" | 11 #include "chrome/browser/local_discovery/privet_constants.h" |
12 #include "chrome/common/local_discovery/service_discovery_client.h" | 12 #include "chrome/common/local_discovery/service_discovery_client.h" |
13 | 13 |
14 namespace local_discovery { | 14 namespace local_discovery { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 DeviceDescription::ConnectionState | 18 std::string GetValueByName(const std::vector<std::string>& metadata, |
19 ConnectionStateFromString(const std::string& str) { | 19 const std::string& name) { |
20 if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOnline)) { | 20 std::string prefix = name + "="; |
Aleksey Shlyapnikov
2015/01/26 21:04:47
const std::string prefix(name + "=");
Vitaly Buka (NO REVIEWS)
2015/01/26 21:27:49
Done.
| |
21 return DeviceDescription::ONLINE; | 21 for (const std::string& record : metadata) |
22 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusOffline)) { | 22 if (StartsWithASCII(record, prefix, false)) |
23 return DeviceDescription::OFFLINE; | 23 return record.substr(prefix.size()); |
Aleksey Shlyapnikov
2015/01/26 21:04:47
Add {} around for and if bodies
Vitaly Buka (NO REVIEWS)
2015/01/26 21:27:49
Done.
| |
24 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusConnecting)) { | 24 return std::string(); |
25 return DeviceDescription::CONNECTING; | |
26 } else if (LowerCaseEqualsASCII(str, kPrivetConnectionStatusNotConfigured)) { | |
27 return DeviceDescription::NOT_CONFIGURED; | |
28 } | |
29 | |
30 return DeviceDescription::UNKNOWN; | |
31 } | 25 } |
32 | 26 |
33 } // namespace | 27 } // namespace |
34 | 28 |
35 DeviceDescription::DeviceDescription() | 29 DeviceDescription::DeviceDescription() : version(0) { |
36 : version(0), | 30 } |
37 connection_state(UNKNOWN) { | 31 |
32 DeviceDescription::DeviceDescription( | |
33 const ServiceDescription& service_description) { | |
34 address = service_description.address; | |
35 | |
36 const std::vector<std::string>& metadata = service_description.metadata; | |
37 if (!base::StringToInt(GetValueByName(metadata, kPrivetTxtKeyVersion), | |
38 &version)) { | |
39 version = 0; | |
Aleksey Shlyapnikov
2015/01/26 21:04:47
Why assign it again here?
Vitaly Buka (NO REVIEWS)
2015/01/26 21:27:49
for consistent result.
StringToInt implementation
| |
40 } | |
41 name = GetValueByName(metadata, kPrivetTxtKeyName); | |
42 description = GetValueByName(metadata, kPrivetTxtKeyDescription); | |
43 if (version >= 3) { | |
44 type = GetValueByName(metadata, kPrivetTxtKeyDevicesClass); | |
45 id = GetValueByName(metadata, kPrivetTxtKeyGcdID); | |
46 } else { | |
47 type = GetValueByName(metadata, kPrivetTxtKeyType); | |
48 id = GetValueByName(metadata, kPrivetTxtKeyID); | |
49 } | |
38 } | 50 } |
39 | 51 |
40 DeviceDescription::~DeviceDescription() { | 52 DeviceDescription::~DeviceDescription() { |
41 } | 53 } |
42 | 54 |
43 void 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 | 55 } // namespace local_discovery |
OLD | NEW |