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

Unified Diff: chromeos/network/network_state_handler.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 side-by-side diff with in-line comments
Download patch
Index: chromeos/network/network_state_handler.cc
diff --git a/chromeos/network/network_state_handler.cc b/chromeos/network/network_state_handler.cc
index eadc2caf10c158240a3ca80f7eef1519223a951a..65ccbfd0c99d495666610f316a0fe5e5446beaea 100644
--- a/chromeos/network/network_state_handler.cc
+++ b/chromeos/network/network_state_handler.cc
@@ -19,40 +19,13 @@
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "chromeos/network/shill_property_handler.h"
+#include "chromeos/network/shill_property_util.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace {
-// Returns true if |network->type()| == |match_type|, or it matches one of the
-// following special match types:
-// * kMatchTypeDefault matches any network (i.e. the first instance)
-// * kMatchTypeNonVirtual matches non virtual networks
-// * kMatchTypeWireless matches wireless networks
-// * kMatchTypeMobile matches cellular or wimax networks
-bool ManagedStateMatchesType(const ManagedState* managed,
- const std::string& match_type) {
- const std::string& type = managed->type();
- if (match_type == NetworkStateHandler::kMatchTypeDefault)
- return true;
- if (match_type == type)
- return true;
- if (match_type == NetworkStateHandler::kMatchTypeNonVirtual &&
- type != flimflam::kTypeVPN) {
- return true;
- }
- if (match_type == NetworkStateHandler::kMatchTypeWireless &&
- type != flimflam::kTypeEthernet && type != flimflam::kTypeVPN) {
- return true;
- }
- if (match_type == NetworkStateHandler::kMatchTypeMobile &&
- (type == flimflam::kTypeCellular || type == flimflam::kTypeWimax)) {
- return true;
- }
- return false;
-}
-
bool ConnectionStateChanged(NetworkState* network,
const std::string& prev_connection_state) {
return (network->connection_state() != prev_connection_state) &&
@@ -82,10 +55,6 @@ std::string GetManagedStateLogName(const ManagedState* state) {
} // namespace
-const char NetworkStateHandler::kMatchTypeDefault[] = "default";
-const char NetworkStateHandler::kMatchTypeWireless[] = "wireless";
-const char NetworkStateHandler::kMatchTypeMobile[] = "mobile";
-const char NetworkStateHandler::kMatchTypeNonVirtual[] = "non-virtual";
const char NetworkStateHandler::kDefaultCheckPortalList[] =
"ethernet,wifi,cellular";
@@ -136,7 +105,7 @@ void NetworkStateHandler::UpdateManagerProperties() {
}
NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
std::string technology = GetTechnologyForType(type);
TechnologyState state;
if (shill_property_handler_->IsTechnologyEnabled(technology))
@@ -149,12 +118,12 @@ NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState(
state = TECHNOLOGY_AVAILABLE;
else
state = TECHNOLOGY_UNAVAILABLE;
- VLOG(2) << "GetTechnologyState: " << type << " = " << state;
+ VLOG(2) << "GetTechnologyState: " << type.ToDebugString() << " = " << state;
return state;
}
void NetworkStateHandler::SetTechnologyEnabled(
- const std::string& type,
+ const NetworkTypePattern& type,
bool enabled,
const network_handler::ErrorCallback& error_callback) {
std::string technology = GetTechnologyForType(type);
@@ -172,22 +141,23 @@ const DeviceState* NetworkStateHandler::GetDeviceState(
}
const DeviceState* NetworkStateHandler::GetDeviceStateByType(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
for (ManagedStateList::const_iterator iter = device_list_.begin();
iter != device_list_.end(); ++iter) {
ManagedState* device = *iter;
- if (ManagedStateMatchesType(device, type))
+ if (device->Matches(type))
return device->AsDeviceState();
}
return NULL;
}
-bool NetworkStateHandler::GetScanningByType(const std::string& type) const {
+bool NetworkStateHandler::GetScanningByType(
+ const NetworkTypePattern& type) const {
for (ManagedStateList::const_iterator iter = device_list_.begin();
iter != device_list_.end(); ++iter) {
const DeviceState* device = (*iter)->AsDeviceState();
DCHECK(device);
- if (ManagedStateMatchesType(device, type) && device->scanning())
+ if (device->Matches(type) && device->scanning())
return true;
}
return false;
@@ -209,21 +179,21 @@ const NetworkState* NetworkStateHandler::DefaultNetwork() const {
}
const NetworkState* NetworkStateHandler::ConnectedNetworkByType(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
for (ManagedStateList::const_iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
if (!network->IsConnectedState())
break; // Connected networks are listed first.
- if (ManagedStateMatchesType(network, type))
+ if (network->Matches(type))
return network;
}
return NULL;
}
const NetworkState* NetworkStateHandler::ConnectingNetworkByType(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
for (ManagedStateList::const_iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
@@ -232,26 +202,26 @@ const NetworkState* NetworkStateHandler::ConnectingNetworkByType(
continue;
if (!network->IsConnectingState())
break; // Connected and connecting networks are listed first.
- if (ManagedStateMatchesType(network, type))
+ if (network->Matches(type))
return network;
}
return NULL;
}
const NetworkState* NetworkStateHandler::FirstNetworkByType(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
for (ManagedStateList::const_iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
- if (ManagedStateMatchesType(network, type))
+ if (network->Matches(type))
return network;
}
return NULL;
}
std::string NetworkStateHandler::HardwareAddressForType(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
std::string result;
const NetworkState* network = ConnectedNetworkByType(type);
if (network) {
@@ -264,7 +234,7 @@ std::string NetworkStateHandler::HardwareAddressForType(
}
std::string NetworkStateHandler::FormattedHardwareAddressForType(
- const std::string& type) const {
+ const NetworkTypePattern& type) const {
std::string address = HardwareAddressForType(type);
if (address.size() % 2 != 0)
return address;
@@ -278,10 +248,10 @@ std::string NetworkStateHandler::FormattedHardwareAddressForType(
}
void NetworkStateHandler::GetNetworkList(NetworkStateList* list) const {
- GetNetworkListByType(kMatchTypeDefault, list);
+ GetNetworkListByType(NetworkTypePattern::Default(), list);
}
-void NetworkStateHandler::GetNetworkListByType(const std::string& type,
+void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type,
NetworkStateList* list) const {
DCHECK(list);
list->clear();
@@ -291,7 +261,7 @@ void NetworkStateHandler::GetNetworkListByType(const std::string& type,
continue;
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
- if (ManagedStateMatchesType(network, type))
+ if (network->Matches(type))
list->push_back(network);
}
}
@@ -341,7 +311,7 @@ void NetworkStateHandler::RequestScan() const {
void NetworkStateHandler::WaitForScan(const std::string& type,
const base::Closure& callback) {
scan_complete_callbacks_[type].push_back(callback);
- if (!GetScanningByType(type))
+ if (!GetScanningByType(NetworkTypePattern::Primitive(type)))
RequestScan();
}
@@ -732,19 +702,27 @@ void NetworkStateHandler::ScanCompleted(const std::string& type) {
}
std::string NetworkStateHandler::GetTechnologyForType(
- const std::string& type) const {
- if (type == kMatchTypeMobile) {
- if (shill_property_handler_->IsTechnologyAvailable(flimflam::kTypeWimax))
- return flimflam::kTypeWimax;
- else
- return flimflam::kTypeCellular;
- }
- if (type == kMatchTypeDefault || type == kMatchTypeNonVirtual ||
- type == kMatchTypeWireless) {
- NOTREACHED();
+ const NetworkTypePattern& type) const {
+ if (type.Matches(flimflam::kTypeEthernet))
+ return flimflam::kTypeEthernet;
+
+ if (type.Matches(flimflam::kTypeWifi))
return flimflam::kTypeWifi;
+
+ if (type.Equals(NetworkTypePattern::Primitive(flimflam::kTypeWimax)))
+ return flimflam::kTypeWimax;
+
+ // Prefer Wimax over Cellular only if it's available.
+ if (type.Matches(flimflam::kTypeWimax) &&
+ shill_property_handler_->IsTechnologyAvailable(flimflam::kTypeWimax)) {
+ return flimflam::kTypeWimax;
}
- return type;
+
+ if (type.Matches(flimflam::kTypeCellular))
+ return flimflam::kTypeCellular;
+
+ NOTREACHED();
+ return std::string();
}
} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698