PM: Make the connection type an enum class.
This improves type safety and encapsulation, but otherwise a superficial
syntactic substitution.
BUG=None
TEST=Unit tests.
Change-Id: I0ec880833704227cf6277ff37e37b51f8a8c5ce7
Reviewed-on: https://chromium-review.googlesource.com/189852
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/fake_shill_provider.h b/policy_manager/fake_shill_provider.h
index acc14f3..8919d16 100644
--- a/policy_manager/fake_shill_provider.h
+++ b/policy_manager/fake_shill_provider.h
@@ -20,7 +20,7 @@
set_var_is_connected(
new FakeVariable<bool>("is_connected", kVariableModePoll));
set_var_conn_type(
- new FakeVariable<ShillConnType>("conn_type", kVariableModePoll));
+ new FakeVariable<ConnectionType>("conn_type", kVariableModePoll));
set_var_conn_last_changed(
new FakeVariable<base::Time>("conn_last_changed", kVariableModePoll));
return true;
diff --git a/policy_manager/real_shill_provider.cc b/policy_manager/real_shill_provider.cc
index 244a059..48d01e6 100644
--- a/policy_manager/real_shill_provider.cc
+++ b/policy_manager/real_shill_provider.cc
@@ -32,11 +32,11 @@
// ShillConnector methods.
const ShillConnector::ConnStrToType ShillConnector::shill_conn_str_to_type[] = {
- {shill::kTypeEthernet, kShillConnTypeEthernet},
- {shill::kTypeWifi, kShillConnTypeWifi},
- {shill::kTypeWimax, kShillConnTypeWimax},
- {shill::kTypeBluetooth, kShillConnTypeBluetooth},
- {shill::kTypeCellular, kShillConnTypeCellular},
+ {shill::kTypeEthernet, ConnectionType::kEthernet},
+ {shill::kTypeWifi, ConnectionType::kWifi},
+ {shill::kTypeWimax, ConnectionType::kWimax},
+ {shill::kTypeBluetooth, ConnectionType::kBluetooth},
+ {shill::kTypeCellular, ConnectionType::kCellular},
};
ShillConnector::~ShillConnector() {
@@ -76,7 +76,7 @@
}
bool ShillConnector::GetConnectionType(const string& service_path,
- ShillConnType* conn_type_p) {
+ ConnectionType* conn_type_p) {
// Obtain a proxy for the service path.
DBusGProxy* service_proxy = GetProxy(service_path.c_str(),
shill::kFlimflamServiceInterface);
@@ -112,12 +112,12 @@
path, interface);
}
-ShillConnType ShillConnector::ParseConnType(const char* str) {
+ConnectionType ShillConnector::ParseConnType(const char* str) {
for (unsigned i = 0; i < arraysize(shill_conn_str_to_type); i++)
if (!strcmp(str, shill_conn_str_to_type[i].str))
return shill_conn_str_to_type[i].type;
- return kShillConnTypeUnknown;
+ return ConnectionType::kUnknown;
}
bool ShillConnector::GetProperties(DBusGProxy* proxy, GHashTable** result_p) {
@@ -133,24 +133,24 @@
// A variable returning the curent connection type.
-class ConnTypeVariable : public Variable<ShillConnType> {
+class ConnTypeVariable : public Variable<ConnectionType> {
public:
ConnTypeVariable(const string& name, ShillConnector* connector,
RealShillProvider* provider)
- : Variable<ShillConnType>(name, kVariableModePoll),
+ : Variable<ConnectionType>(name, kVariableModePoll),
connector_(connector), provider_(provider) {}
protected:
// TODO(garnold) Shift to a non-blocking version, respect the timeout.
- virtual const ShillConnType* GetValue(base::TimeDelta /* timeout */,
- string* errmsg) {
+ virtual const ConnectionType* GetValue(base::TimeDelta /* timeout */,
+ string* errmsg) {
if (!(provider_->is_connected_)) {
if (errmsg)
*errmsg = "No connection detected";
return NULL;
}
- ShillConnType conn_type;
+ ConnectionType conn_type;
if (provider_->is_conn_type_valid_) {
conn_type = provider_->conn_type_;
} else {
@@ -165,7 +165,7 @@
provider_->is_conn_type_valid_ = true;
}
- return new ShillConnType(conn_type);
+ return new ConnectionType(conn_type);
}
private:
diff --git a/policy_manager/real_shill_provider.h b/policy_manager/real_shill_provider.h
index ba24234..55125bc 100644
--- a/policy_manager/real_shill_provider.h
+++ b/policy_manager/real_shill_provider.h
@@ -43,7 +43,7 @@
// storing it to |*conn_type_p|. Returns |true| on success; |false| on
// failure, in which case no value is written.
bool GetConnectionType(const std::string& service_path,
- ShillConnType* conn_type_p);
+ ConnectionType* conn_type_p);
// Issues a GetProperties call to shill's manager interface, storing the
// result to |*result_p|. Returns |true| on success.
@@ -58,7 +58,7 @@
typedef struct {
const char *str;
- ShillConnType type;
+ ConnectionType type;
} ConnStrToType;
// A mapping from shill connection type strings to enum values.
@@ -80,7 +80,7 @@
DBusGProxy* GetProxy(const char* path, const char* interface);
// Converts a shill connection type string into a symbolic value.
- ShillConnType ParseConnType(const char* str);
+ ConnectionType ParseConnType(const char* str);
DISALLOW_COPY_AND_ASSIGN(ShillConnector);
};
@@ -115,7 +115,7 @@
std::string default_service_path_;
// The last known type of the default connection.
- ShillConnType conn_type_ = kShillConnTypeUnknown;
+ ConnectionType conn_type_ = ConnectionType::kUnknown;
// Whether the last known connection type is valid.
bool is_conn_type_valid_ = false;
diff --git a/policy_manager/real_shill_provider_unittest.cc b/policy_manager/real_shill_provider_unittest.cc
index 364be1a..855c154 100644
--- a/policy_manager/real_shill_provider_unittest.cc
+++ b/policy_manager/real_shill_provider_unittest.cc
@@ -146,7 +146,7 @@
void SetConnectionAndTestType(const char* service_path,
DBusGProxy* service_proxy,
const char* shill_type_str,
- ShillConnType expected_conn_type) {
+ ConnectionType expected_conn_type) {
// Send a signal about a new default service.
auto callback = reinterpret_cast<ShillConnector::PropertyChangedHandler>(
signal_handler_);
@@ -185,7 +185,7 @@
arraysize(service_pairs), service_pairs);
// Query the connection type, ensure last change time did not change.
- scoped_ptr<const ShillConnType> conn_type(
+ scoped_ptr<const ConnectionType> conn_type(
provider_->var_conn_type()->GetValue(default_timeout_, NULL));
PMTEST_ASSERT_NOT_NULL(conn_type.get());
EXPECT_EQ(expected_conn_type, *conn_type);
@@ -218,7 +218,7 @@
PMTEST_ASSERT_NOT_NULL(is_connected.get());
EXPECT_FALSE(*is_connected);
- scoped_ptr<const ShillConnType> conn_type(
+ scoped_ptr<const ConnectionType> conn_type(
provider_->var_conn_type()->GetValue(default_timeout_, NULL));
PMTEST_ASSERT_NULL(conn_type.get());
@@ -233,7 +233,7 @@
SetConnectionAndTestType(kFakeEthernetServicePath,
kFakeEthernetServiceProxy,
shill::kTypeEthernet,
- kShillConnTypeEthernet);
+ ConnectionType::kEthernet);
}
// Test that Wifi connection is identified correctly.
@@ -241,7 +241,7 @@
SetConnectionAndTestType(kFakeWifiServicePath,
kFakeWifiServiceProxy,
shill::kTypeWifi,
- kShillConnTypeWifi);
+ ConnectionType::kWifi);
}
// Test that Wimax connection is identified correctly.
@@ -249,7 +249,7 @@
SetConnectionAndTestType(kFakeWimaxServicePath,
kFakeWimaxServiceProxy,
shill::kTypeWimax,
- kShillConnTypeWimax);
+ ConnectionType::kWimax);
}
// Test that Bluetooth connection is identified correctly.
@@ -257,7 +257,7 @@
SetConnectionAndTestType(kFakeBluetoothServicePath,
kFakeBluetoothServiceProxy,
shill::kTypeBluetooth,
- kShillConnTypeBluetooth);
+ ConnectionType::kBluetooth);
}
// Test that Cellular connection is identified correctly.
@@ -265,7 +265,7 @@
SetConnectionAndTestType(kFakeCellularServicePath,
kFakeCellularServiceProxy,
shill::kTypeCellular,
- kShillConnTypeCellular);
+ ConnectionType::kCellular);
}
// Test that an unknown connection is identified as such.
@@ -273,7 +273,7 @@
SetConnectionAndTestType(kFakeUnknownServicePath,
kFakeUnknownServiceProxy,
"FooConnectionType",
- kShillConnTypeUnknown);
+ ConnectionType::kUnknown);
}
// Tests that VPN connection is identified correctly.
@@ -303,10 +303,10 @@
kFakeVpnServiceProxy, false, arraysize(service_pairs), service_pairs);
// Query the connection type, ensure last change time reported correctly.
- scoped_ptr<const ShillConnType> conn_type(
+ scoped_ptr<const ConnectionType> conn_type(
provider_->var_conn_type()->GetValue(default_timeout_, NULL));
PMTEST_ASSERT_NOT_NULL(conn_type.get());
- EXPECT_EQ(kShillConnTypeWifi, *conn_type);
+ EXPECT_EQ(ConnectionType::kWifi, *conn_type);
scoped_ptr<const Time> conn_last_changed(
provider_->var_conn_last_changed()->GetValue(default_timeout_, NULL));
diff --git a/policy_manager/shill_provider.h b/policy_manager/shill_provider.h
index 3530916..82405b6 100644
--- a/policy_manager/shill_provider.h
+++ b/policy_manager/shill_provider.h
@@ -13,14 +13,13 @@
namespace chromeos_policy_manager {
-// TODO(garnold) Adapted from connection_manager.h.
-enum ShillConnType {
- kShillConnTypeEthernet = 0,
- kShillConnTypeWifi,
- kShillConnTypeWimax,
- kShillConnTypeBluetooth,
- kShillConnTypeCellular,
- kShillConnTypeUnknown
+enum class ConnectionType {
+ kEthernet,
+ kWifi,
+ kWimax,
+ kBluetooth,
+ kCellular,
+ kUnknown
};
// Provider for networking related information.
@@ -32,7 +31,7 @@
}
// Returns the current network connection type. Unknown if not connected.
- Variable<ShillConnType>* var_conn_type() const {
+ Variable<ConnectionType>* var_conn_type() const {
return var_conn_type_.get();
}
@@ -49,7 +48,7 @@
var_is_connected_.reset(var_is_connected);
}
- void set_var_conn_type(Variable<ShillConnType>* var_conn_type) {
+ void set_var_conn_type(Variable<ConnectionType>* var_conn_type) {
var_conn_type_.reset(var_conn_type);
}
@@ -59,7 +58,7 @@
private:
scoped_ptr<Variable<bool>> var_is_connected_;
- scoped_ptr<Variable<ShillConnType>> var_conn_type_;
+ scoped_ptr<Variable<ConnectionType>> var_conn_type_;
scoped_ptr<Variable<base::Time>> var_conn_last_changed_;
DISALLOW_COPY_AND_ASSIGN(ShillProvider);