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