Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: chromeos/network/shill_property_util.cc

Issue 23712002: Cleanup network type matching. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Generalized network type matching. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "chromeos/network/shill_property_util.h" 5 #include "chromeos/network/shill_property_util.h"
6 6
7 #include "base/i18n/icu_encoding_detection.h" 7 #include "base/i18n/icu_encoding_detection.h"
8 #include "base/i18n/icu_string_conversions.h" 8 #include "base/i18n/icu_string_conversions.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 // Ethernet and EthernetEAP don't have any additional identifying 212 // Ethernet and EthernetEAP don't have any additional identifying
213 // properties. 213 // properties.
214 } else { 214 } else {
215 NOTREACHED() << "Unsupported network type " << type; 215 NOTREACHED() << "Unsupported network type " << type;
216 } 216 }
217 return true; 217 return true;
218 } 218 }
219 219
220 } // namespace shill_property_util 220 } // namespace shill_property_util
221 221
222 namespace {
223
224 const char kPatternDefault[] = "PatternDefault";
225 const char kPatternEthernet[] = "PatternEthernet";
226 const char kPatternWireless[] = "PatternWireless";
227 const char kPatternMobile[] = "PatternMobile";
228 const char kPatternNonVirtual[] = "PatternNonVirtual";
229
230 enum NetworkTypeBitFlag {
231 kNetworkTypeNone = 0,
232 kNetworkTypeEthernet = 1 << 0,
233 kNetworkTypeWifi = 1 << 1,
234 kNetworkTypeWimax = 1 << 2,
235 kNetworkTypeBluetooth = 1 << 3,
stevenjb 2013/09/03 16:22:43 We don't handle Bluetooth anywhere in the current
pneubeck (no reviews) 2013/09/04 12:57:29 Done.
236 kNetworkTypeCellular = 1 << 4,
237 kNetworkTypeVPN = 1 << 5,
238 kNetworkTypeEthernetEap = 1 << 6
239 };
240
241 struct ShillToBitFlagEntry {
242 const char* shill_network_type;
243 NetworkTypeBitFlag bit_flag;
244 } shill_type_to_flag[] = {
245 { flimflam::kTypeEthernet, kNetworkTypeEthernet },
246 { shill::kTypeEthernetEap, kNetworkTypeEthernetEap },
247 { flimflam::kTypeWifi, kNetworkTypeWifi },
248 { flimflam::kTypeWimax, kNetworkTypeWimax },
249 { flimflam::kTypeBluetooth, kNetworkTypeBluetooth },
250 { flimflam::kTypeCellular, kNetworkTypeCellular },
251 { flimflam::kTypeVPN, kNetworkTypeVPN }
252 };
253
254 NetworkTypeBitFlag ShillNetworkTypeToFlag(const std::string& shill_type) {
255 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
256 if (shill_type_to_flag[i].shill_network_type == shill_type)
257 return shill_type_to_flag[i].bit_flag;
258 }
259 NOTREACHED();
260 return kNetworkTypeNone;
261 }
262
263 std::string FlagToShillNetworkType(const NetworkTypeBitFlag flag) {
264 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
265 if (shill_type_to_flag[i].bit_flag == flag)
266 return shill_type_to_flag[i].shill_network_type;
267 }
268 NOTREACHED();
269 return std::string();
270 }
271
272 } // namespace
273
274 // static
275 NetworkTypePattern NetworkTypePattern::Default() {
276 return NetworkTypePattern(~0);
277 }
278
279 // static
280 NetworkTypePattern NetworkTypePattern::Wireless() {
281 return NetworkTypePattern(kNetworkTypeWifi | kNetworkTypeWimax |
282 kNetworkTypeBluetooth | kNetworkTypeCellular);
283 }
284
285 // static
286 NetworkTypePattern NetworkTypePattern::Mobile() {
287 return NetworkTypePattern(kNetworkTypeCellular | kNetworkTypeWimax);
288 }
289
290 // static
291 NetworkTypePattern NetworkTypePattern::NonVirtual() {
292 return NetworkTypePattern(~kNetworkTypeVPN);
293 }
294
295 // static
296 NetworkTypePattern NetworkTypePattern::Ethernet() {
297 return NetworkTypePattern(kNetworkTypeEthernet | kNetworkTypeEthernetEap);
298 }
299
300 // static
301 NetworkTypePattern NetworkTypePattern::WiFi() {
302 return NetworkTypePattern(kNetworkTypeWifi);
303 }
304
305 // static
306 NetworkTypePattern NetworkTypePattern::Cellular() {
307 return NetworkTypePattern(kNetworkTypeCellular);
308 }
309
310 // static
311 NetworkTypePattern NetworkTypePattern::VPN() {
312 return NetworkTypePattern(kNetworkTypeVPN);
313 }
314
315 // static
316 NetworkTypePattern NetworkTypePattern::Wimax() {
317 return NetworkTypePattern(kNetworkTypeWimax);
318 }
319
320 // static
321 NetworkTypePattern NetworkTypePattern::Primitive(
322 const std::string& shill_network_type) {
323 return NetworkTypePattern(ShillNetworkTypeToFlag(shill_network_type));
324 }
325
326 bool NetworkTypePattern::Equals(const NetworkTypePattern& other) const {
327 return pattern_ == other.pattern_;
328 }
329
330 bool NetworkTypePattern::Matches(const std::string& shill_network_type) const {
331 return ContainsPattern(Primitive(shill_network_type));
332 }
333
334 bool NetworkTypePattern::ContainsPattern(
335 const NetworkTypePattern& subpattern) const {
336 if (Equals(subpattern))
337 return true;
338
339 return (~pattern_ & subpattern.pattern_) == 0;
stevenjb 2013/09/03 16:22:43 I don't think this is the behavior I would expect.
pneubeck (no reviews) 2013/09/04 12:57:29 Done.
340 }
341
342 std::string NetworkTypePattern::ToDebugString() const {
343 if (Equals(Default()))
344 return kPatternDefault;
345 if (Equals(Ethernet()))
346 return kPatternEthernet;
347 if (Equals(Wireless()))
348 return kPatternWireless;
349 if (Equals(Mobile()))
350 return kPatternMobile;
351 if (Equals(NonVirtual()))
352 return kPatternNonVirtual;
353
354 std::string str;
355 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
356 if (!(pattern_ | shill_type_to_flag[i].bit_flag))
357 continue;
358 if (!str.empty())
359 str += "|";
360 str += shill_type_to_flag[i].shill_network_type;
361 }
362 return str;
363 }
364
365 NetworkTypePattern::NetworkTypePattern(int pattern) : pattern_(pattern) {}
366
222 } // namespace chromeos 367 } // namespace chromeos
OLDNEW
« chrome/browser/extensions/api/dial/dial_service.cc ('K') | « chromeos/network/shill_property_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698