Add default implementations for AppWindowRegistry::Observer notifications.
[chromium-blink-merge.git] / chromeos / network / network_util.cc
blob1acf6e584c5b296a37dc6d6a8742a26aa4496e3b
1 // Copyright (c) 2012 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.
5 #include "chromeos/network/network_util.h"
7 #include "base/strings/string_tokenizer.h"
8 #include "base/strings/stringprintf.h"
9 #include "third_party/cros_system_api/dbus/service_constants.h"
11 namespace chromeos {
13 WifiAccessPoint::WifiAccessPoint()
14 : signal_strength(0),
15 signal_to_noise(0),
16 channel(0) {
19 WifiAccessPoint::~WifiAccessPoint() {
22 CellularScanResult::CellularScanResult() {
25 CellularScanResult::~CellularScanResult() {
28 namespace network_util {
30 std::string PrefixLengthToNetmask(int32 prefix_length) {
31 std::string netmask;
32 // Return the empty string for invalid inputs.
33 if (prefix_length < 0 || prefix_length > 32)
34 return netmask;
35 for (int i = 0; i < 4; i++) {
36 int remainder = 8;
37 if (prefix_length >= 8) {
38 prefix_length -= 8;
39 } else {
40 remainder = prefix_length;
41 prefix_length = 0;
43 if (i > 0)
44 netmask += ".";
45 int value = remainder == 0 ? 0 :
46 ((2L << (remainder - 1)) - 1) << (8 - remainder);
47 netmask += base::StringPrintf("%d", value);
49 return netmask;
52 int32 NetmaskToPrefixLength(const std::string& netmask) {
53 int count = 0;
54 int prefix_length = 0;
55 base::StringTokenizer t(netmask, ".");
56 while (t.GetNext()) {
57 // If there are more than 4 numbers, then it's invalid.
58 if (count == 4)
59 return -1;
61 std::string token = t.token();
62 // If we already found the last mask and the current one is not
63 // "0" then the netmask is invalid. For example, 255.224.255.0
64 if (prefix_length / 8 != count) {
65 if (token != "0")
66 return -1;
67 } else if (token == "255") {
68 prefix_length += 8;
69 } else if (token == "254") {
70 prefix_length += 7;
71 } else if (token == "252") {
72 prefix_length += 6;
73 } else if (token == "248") {
74 prefix_length += 5;
75 } else if (token == "240") {
76 prefix_length += 4;
77 } else if (token == "224") {
78 prefix_length += 3;
79 } else if (token == "192") {
80 prefix_length += 2;
81 } else if (token == "128") {
82 prefix_length += 1;
83 } else if (token == "0") {
84 prefix_length += 0;
85 } else {
86 // mask is not a valid number.
87 return -1;
89 count++;
91 if (count < 4)
92 return -1;
93 return prefix_length;
96 bool ParseCellularScanResults(const base::ListValue& list,
97 std::vector<CellularScanResult>* scan_results) {
98 scan_results->clear();
99 scan_results->reserve(list.GetSize());
100 for (base::ListValue::const_iterator it = list.begin();
101 it != list.end(); ++it) {
102 if (!(*it)->IsType(base::Value::TYPE_DICTIONARY))
103 return false;
104 CellularScanResult scan_result;
105 const base::DictionaryValue* dict =
106 static_cast<const base::DictionaryValue*>(*it);
107 // If the network id property is not present then this network cannot be
108 // connected to so don't include it in the results.
109 if (!dict->GetStringWithoutPathExpansion(shill::kNetworkIdProperty,
110 &scan_result.network_id))
111 continue;
112 dict->GetStringWithoutPathExpansion(shill::kStatusProperty,
113 &scan_result.status);
114 dict->GetStringWithoutPathExpansion(shill::kLongNameProperty,
115 &scan_result.long_name);
116 dict->GetStringWithoutPathExpansion(shill::kShortNameProperty,
117 &scan_result.short_name);
118 dict->GetStringWithoutPathExpansion(shill::kTechnologyProperty,
119 &scan_result.technology);
120 scan_results->push_back(scan_result);
122 return true;
125 } // namespace network_util
126 } // namespace chromeos