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

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

Issue 300623008: Move NetworkTypePattern to its own file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 6 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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 } 340 }
341 LOG(WARNING) 341 LOG(WARNING)
342 << "Provider name and country not defined, using code instead: " 342 << "Provider name and country not defined, using code instead: "
343 << *home_provider_id; 343 << *home_provider_id;
344 } 344 }
345 return true; 345 return true;
346 } 346 }
347 347
348 } // namespace shill_property_util 348 } // namespace shill_property_util
349 349
350 namespace {
351
352 const char kPatternDefault[] = "PatternDefault";
353 const char kPatternEthernet[] = "PatternEthernet";
354 const char kPatternWireless[] = "PatternWireless";
355 const char kPatternMobile[] = "PatternMobile";
356 const char kPatternNonVirtual[] = "PatternNonVirtual";
357
358 enum NetworkTypeBitFlag {
359 kNetworkTypeNone = 0,
360 kNetworkTypeEthernet = 1 << 0,
361 kNetworkTypeWifi = 1 << 1,
362 kNetworkTypeWimax = 1 << 2,
363 kNetworkTypeCellular = 1 << 3,
364 kNetworkTypeVPN = 1 << 4,
365 kNetworkTypeEthernetEap = 1 << 5,
366 kNetworkTypeBluetooth = 1 << 6
367 };
368
369 struct ShillToBitFlagEntry {
370 const char* shill_network_type;
371 NetworkTypeBitFlag bit_flag;
372 } shill_type_to_flag[] = {
373 { shill::kTypeEthernet, kNetworkTypeEthernet },
374 { shill::kTypeEthernetEap, kNetworkTypeEthernetEap },
375 { shill::kTypeWifi, kNetworkTypeWifi },
376 { shill::kTypeWimax, kNetworkTypeWimax },
377 { shill::kTypeCellular, kNetworkTypeCellular },
378 { shill::kTypeVPN, kNetworkTypeVPN },
379 { shill::kTypeBluetooth, kNetworkTypeBluetooth }
380 };
381
382 NetworkTypeBitFlag ShillNetworkTypeToFlag(const std::string& shill_type) {
383 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
384 if (shill_type_to_flag[i].shill_network_type == shill_type)
385 return shill_type_to_flag[i].bit_flag;
386 }
387 NET_LOG_ERROR("ShillNetworkTypeToFlag", "Unknown type: " + shill_type);
388 return kNetworkTypeNone;
389 }
390
391 } // namespace
392
393 // static
394 NetworkTypePattern NetworkTypePattern::Default() {
395 return NetworkTypePattern(~0);
396 }
397
398 // static
399 NetworkTypePattern NetworkTypePattern::Wireless() {
400 return NetworkTypePattern(kNetworkTypeWifi | kNetworkTypeWimax |
401 kNetworkTypeCellular);
402 }
403
404 // static
405 NetworkTypePattern NetworkTypePattern::Mobile() {
406 return NetworkTypePattern(kNetworkTypeCellular | kNetworkTypeWimax);
407 }
408
409 // static
410 NetworkTypePattern NetworkTypePattern::NonVirtual() {
411 return NetworkTypePattern(~kNetworkTypeVPN);
412 }
413
414 // static
415 NetworkTypePattern NetworkTypePattern::Ethernet() {
416 return NetworkTypePattern(kNetworkTypeEthernet);
417 }
418
419 // static
420 NetworkTypePattern NetworkTypePattern::WiFi() {
421 return NetworkTypePattern(kNetworkTypeWifi);
422 }
423
424 // static
425 NetworkTypePattern NetworkTypePattern::Cellular() {
426 return NetworkTypePattern(kNetworkTypeCellular);
427 }
428
429 // static
430 NetworkTypePattern NetworkTypePattern::VPN() {
431 return NetworkTypePattern(kNetworkTypeVPN);
432 }
433
434 // static
435 NetworkTypePattern NetworkTypePattern::Wimax() {
436 return NetworkTypePattern(kNetworkTypeWimax);
437 }
438
439 // static
440 NetworkTypePattern NetworkTypePattern::Primitive(
441 const std::string& shill_network_type) {
442 return NetworkTypePattern(ShillNetworkTypeToFlag(shill_network_type));
443 }
444
445 bool NetworkTypePattern::Equals(const NetworkTypePattern& other) const {
446 return pattern_ == other.pattern_;
447 }
448
449 bool NetworkTypePattern::MatchesType(
450 const std::string& shill_network_type) const {
451 return MatchesPattern(Primitive(shill_network_type));
452 }
453
454 bool NetworkTypePattern::MatchesPattern(
455 const NetworkTypePattern& other_pattern) const {
456 if (Equals(other_pattern))
457 return true;
458
459 return pattern_ & other_pattern.pattern_;
460 }
461
462 std::string NetworkTypePattern::ToDebugString() const {
463 if (Equals(Default()))
464 return kPatternDefault;
465 if (Equals(Ethernet()))
466 return kPatternEthernet;
467 if (Equals(Wireless()))
468 return kPatternWireless;
469 if (Equals(Mobile()))
470 return kPatternMobile;
471 if (Equals(NonVirtual()))
472 return kPatternNonVirtual;
473
474 std::string str;
475 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
476 if (!(pattern_ & shill_type_to_flag[i].bit_flag))
477 continue;
478 if (!str.empty())
479 str += "|";
480 str += shill_type_to_flag[i].shill_network_type;
481 }
482 return str;
483 }
484
485 NetworkTypePattern::NetworkTypePattern(int pattern) : pattern_(pattern) {}
486
487 } // namespace chromeos 350 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/shill_property_util.h ('k') | chromeos/network/shill_property_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698