summaryrefslogtreecommitdiffstats
path: root/chromeos
diff options
context:
space:
mode:
Diffstat (limited to 'chromeos')
-rw-r--r--chromeos/chromeos.gyp2
-rw-r--r--chromeos/dbus/mock_dbus_thread_manager.cc2
-rw-r--r--chromeos/network/cert_loader.cc40
-rw-r--r--chromeos/network/cert_loader.h9
-rw-r--r--chromeos/network/geolocation_handler.cc23
-rw-r--r--chromeos/network/geolocation_handler.h8
-rw-r--r--chromeos/network/managed_network_configuration_handler.cc91
-rw-r--r--chromeos/network/managed_network_configuration_handler.h38
-rw-r--r--chromeos/network/managed_network_configuration_handler_unittest.cc60
-rw-r--r--chromeos/network/network_change_notifier_chromeos.cc9
-rw-r--r--chromeos/network/network_configuration_handler.cc70
-rw-r--r--chromeos/network/network_configuration_handler.h35
-rw-r--r--chromeos/network/network_configuration_handler_unittest.cc29
-rw-r--r--chromeos/network/network_connection_handler.cc43
-rw-r--r--chromeos/network/network_connection_handler.h21
-rw-r--r--chromeos/network/network_connection_handler_unittest.cc23
-rw-r--r--chromeos/network/network_handler.cc109
-rw-r--r--chromeos/network/network_handler.h71
-rw-r--r--chromeos/network/network_profile_handler.cc55
-rw-r--r--chromeos/network/network_profile_handler.h19
-rw-r--r--chromeos/network/network_profile_handler_stub.h22
-rw-r--r--chromeos/network/network_state_handler.cc29
-rw-r--r--chromeos/network/network_state_handler.h19
23 files changed, 445 insertions, 382 deletions
diff --git a/chromeos/chromeos.gyp b/chromeos/chromeos.gyp
index 475f04b..cca9e02 100644
--- a/chromeos/chromeos.gyp
+++ b/chromeos/chromeos.gyp
@@ -240,6 +240,8 @@
'network/network_connection_handler.h',
'network/network_event_log.cc',
'network/network_event_log.h',
+ 'network/network_handler.cc',
+ 'network/network_handler.h',
'network/network_handler_callbacks.cc',
'network/network_handler_callbacks.h',
'network/network_ip_config.cc',
diff --git a/chromeos/dbus/mock_dbus_thread_manager.cc b/chromeos/dbus/mock_dbus_thread_manager.cc
index 518e745..2265a08 100644
--- a/chromeos/dbus/mock_dbus_thread_manager.cc
+++ b/chromeos/dbus/mock_dbus_thread_manager.cc
@@ -114,6 +114,8 @@ MockDBusThreadManager::MockDBusThreadManager()
EXPECT_CALL(*mock_cryptohome_client_.get(), GetSystemSalt(_))
.WillRepeatedly(DoAll(SetArgumentPointee<0>(*GetMockSystemSalt()),
Return(true)));
+ EXPECT_CALL(*mock_cryptohome_client_.get(), TpmIsEnabled(_))
+ .Times(AnyNumber());
// Called from GeolocationHandler::Init().
EXPECT_CALL(*mock_shill_manager_client_.get(), GetProperties(_))
diff --git a/chromeos/network/cert_loader.cc b/chromeos/network/cert_loader.cc
index 9f05604..4a31586 100644
--- a/chromeos/network/cert_loader.cc
+++ b/chromeos/network/cert_loader.cc
@@ -48,32 +48,6 @@ void LoadNSSCertificates(net::CertificateList* cert_list) {
} // namespace
-static CertLoader* g_cert_loader = NULL;
-
-// static
-void CertLoader::Initialize() {
- CHECK(!g_cert_loader);
- g_cert_loader = new CertLoader();
-}
-
-// static
-void CertLoader::Shutdown() {
- CHECK(g_cert_loader);
- delete g_cert_loader;
- g_cert_loader = NULL;
-}
-
-// static
-CertLoader* CertLoader::Get() {
- CHECK(g_cert_loader) << "CertLoader::Get() called before Initialize()";
- return g_cert_loader;
-}
-
-// static
-bool CertLoader::IsInitialized() {
- return g_cert_loader;
-}
-
CertLoader::CertLoader()
: certificates_requested_(false),
certificates_loaded_(false),
@@ -85,13 +59,15 @@ CertLoader::CertLoader()
initialize_token_factory_(this),
update_certificates_factory_(this) {
net::CertDatabase::GetInstance()->AddObserver(this);
- LoginState::Get()->AddObserver(this);
+ if (LoginState::IsInitialized())
+ LoginState::Get()->AddObserver(this);
RequestCertificates();
}
CertLoader::~CertLoader() {
net::CertDatabase::GetInstance()->RemoveObserver(this);
- LoginState::Get()->RemoveObserver(this);
+ if (LoginState::IsInitialized())
+ LoginState::Get()->RemoveObserver(this);
}
void CertLoader::AddObserver(CertLoader::Observer* observer) {
@@ -112,8 +88,10 @@ bool CertLoader::IsHardwareBacked() const {
void CertLoader::RequestCertificates() {
CHECK(thread_checker_.CalledOnValidThread());
- VLOG(1) << "RequestCertificates: " << LoginState::Get()->IsUserLoggedIn();
- if (certificates_requested_ || !LoginState::Get()->IsUserLoggedIn())
+ const bool logged_in = LoginState::IsInitialized() ?
+ LoginState::Get()->IsUserLoggedIn() : false;
+ VLOG(1) << "RequestCertificates: " << logged_in;
+ if (certificates_requested_ || !logged_in)
return;
certificates_requested_ = true;
@@ -133,7 +111,7 @@ void CertLoader::InitializeTokenAndLoadCertificates() {
CHECK(thread_checker_.CalledOnValidThread());
VLOG(1) << "InitializeTokenAndLoadCertificates";
- switch(tpm_token_state_) {
+ switch (tpm_token_state_) {
case TPM_STATE_UNKNOWN: {
DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsEnabled(
base::Bind(&CertLoader::OnTpmIsEnabled,
diff --git a/chromeos/network/cert_loader.h b/chromeos/network/cert_loader.h
index a87f5c0..a57d29a 100644
--- a/chromeos/network/cert_loader.h
+++ b/chromeos/network/cert_loader.h
@@ -15,6 +15,7 @@
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/login/login_state.h"
+#include "chromeos/network/network_handler.h"
#include "net/cert/cert_database.h"
#include "net/cert/x509_certificate.h"
@@ -49,11 +50,7 @@ class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer,
DISALLOW_COPY_AND_ASSIGN(Observer);
};
- // Manage the global instance.
- static void Initialize();
- static void Shutdown();
- static CertLoader* Get();
- static bool IsInitialized();
+ virtual ~CertLoader();
void AddObserver(CertLoader::Observer* observer);
void RemoveObserver(CertLoader::Observer* observer);
@@ -78,8 +75,8 @@ class CHROMEOS_EXPORT CertLoader : public net::CertDatabase::Observer,
const net::CertificateList& cert_list() const { return cert_list_; }
private:
+ friend class NetworkHandler;
CertLoader();
- virtual ~CertLoader();
void RequestCertificates();
diff --git a/chromeos/network/geolocation_handler.cc b/chromeos/network/geolocation_handler.cc
index ed9aeb5..dfccd6d 100644
--- a/chromeos/network/geolocation_handler.cc
+++ b/chromeos/network/geolocation_handler.cc
@@ -13,8 +13,6 @@
namespace chromeos {
-static GeolocationHandler* g_geolocation_handler = NULL;
-
GeolocationHandler::GeolocationHandler()
: wifi_enabled_(false),
weak_ptr_factory_(this) {
@@ -36,27 +34,6 @@ void GeolocationHandler::Init() {
manager_client->AddPropertyChangedObserver(this);
}
-// static
-void GeolocationHandler::Initialize() {
- CHECK(!g_geolocation_handler);
- g_geolocation_handler = new GeolocationHandler();
- g_geolocation_handler->Init();
-}
-
-// static
-void GeolocationHandler::Shutdown() {
- CHECK(g_geolocation_handler);
- delete g_geolocation_handler;
- g_geolocation_handler = NULL;
-}
-
-// static
-GeolocationHandler* GeolocationHandler::Get() {
- CHECK(g_geolocation_handler)
- << "GeolocationHandler::Get() called before Initialize()";
- return g_geolocation_handler;
-}
-
bool GeolocationHandler::GetWifiAccessPoints(
WifiAccessPointVector* access_points, int64* age_ms) {
if (!wifi_enabled_)
diff --git a/chromeos/network/geolocation_handler.h b/chromeos/network/geolocation_handler.h
index 9c3e533..68766d3 100644
--- a/chromeos/network/geolocation_handler.h
+++ b/chromeos/network/geolocation_handler.h
@@ -9,6 +9,7 @@
#include "base/time.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/shill_property_changed_observer.h"
+#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_util.h"
namespace base {
@@ -33,11 +34,6 @@ class CHROMEOS_EXPORT GeolocationHandler : public ShillPropertyChangedObserver {
public:
virtual ~GeolocationHandler();
- // Manage the global instance. Must be initialized before any calls to Get().
- static void Initialize();
- static void Shutdown();
- static GeolocationHandler* Get();
-
// This sends a request for wifi access point data. If data is already
// available, returns |true|, fills |access_points| with the latest access
// point data, and sets |age_ms| to the time since the last update in MS.
@@ -50,8 +46,10 @@ class CHROMEOS_EXPORT GeolocationHandler : public ShillPropertyChangedObserver {
const base::Value& value) OVERRIDE;
private:
+ friend class NetworkHandler;
friend class GeolocationHandlerTest;
GeolocationHandler();
+
void Init();
// ShillManagerClient callback
diff --git a/chromeos/network/managed_network_configuration_handler.cc b/chromeos/network/managed_network_configuration_handler.cc
index bf44b9f..8025034 100644
--- a/chromeos/network/managed_network_configuration_handler.cc
+++ b/chromeos/network/managed_network_configuration_handler.cc
@@ -294,34 +294,6 @@ void TranslatePropertiesToOncAndRunCallback(
} // namespace
-static ManagedNetworkConfigurationHandler*
-g_configuration_handler_instance = NULL;
-
-// static
-void ManagedNetworkConfigurationHandler::Initialize(
- NetworkProfileHandler* profile_handler) {
- CHECK(!g_configuration_handler_instance);
- g_configuration_handler_instance =
- new ManagedNetworkConfigurationHandler(profile_handler);
-}
-
-// static
-bool ManagedNetworkConfigurationHandler::IsInitialized() {
- return g_configuration_handler_instance;
-}
-
-// static
-void ManagedNetworkConfigurationHandler::Shutdown() {
- CHECK(g_configuration_handler_instance);
- delete g_configuration_handler_instance;
- g_configuration_handler_instance = NULL;
-}
-
-// static
-ManagedNetworkConfigurationHandler* ManagedNetworkConfigurationHandler::Get() {
- CHECK(g_configuration_handler_instance);
- return g_configuration_handler_instance;
-}
// static
scoped_ptr<NetworkUIData> ManagedNetworkConfigurationHandler::GetUIData(
@@ -354,7 +326,7 @@ void ManagedNetworkConfigurationHandler::GetManagedProperties(
error_callback);
return;
}
- NetworkConfigurationHandler::Get()->GetProperties(
+ network_configuration_handler_->GetProperties(
service_path,
base::Bind(
&ManagedNetworkConfigurationHandler::GetManagedPropertiesCallback,
@@ -372,10 +344,11 @@ void ManagedNetworkConfigurationHandler::GetManagedPropertiesCallback(
std::string profile_path;
shill_properties.GetStringWithoutPathExpansion(flimflam::kProfileProperty,
&profile_path);
+ LOG(ERROR) << "Profile: " << profile_path;
const NetworkProfile* profile =
- profile_handler_->GetProfileForPath(profile_path);
+ network_profile_handler_->GetProfileForPath(profile_path);
if (!profile) {
- VLOG(1) << "No or no known profile received for service "
+ LOG(ERROR) << "No or no known profile received for service "
<< service_path << ".";
}
@@ -444,7 +417,7 @@ void ManagedNetworkConfigurationHandler::GetProperties(
const std::string& service_path,
const network_handler::DictionaryResultCallback& callback,
const network_handler::ErrorCallback& error_callback) const {
- NetworkConfigurationHandler::Get()->GetProperties(
+ network_configuration_handler_->GetProperties(
service_path,
base::Bind(&TranslatePropertiesToOncAndRunCallback, callback),
error_callback);
@@ -456,7 +429,7 @@ void ManagedNetworkConfigurationHandler::SetProperties(
const base::Closure& callback,
const network_handler::ErrorCallback& error_callback) const {
const NetworkState* state =
- NetworkStateHandler::Get()->GetNetworkState(service_path);
+ network_state_handler_->GetNetworkState(service_path);
if (!state) {
RunErrorCallback(service_path,
@@ -480,7 +453,7 @@ void ManagedNetworkConfigurationHandler::SetProperties(
const std::string& profile_path = state->profile_path();
const NetworkProfile *profile =
- profile_handler_->GetProfileForPath(profile_path);
+ network_profile_handler_->GetProfileForPath(profile_path);
if (!profile) {
RunErrorCallback(service_path,
kUnknownProfilePath,
@@ -531,10 +504,8 @@ void ManagedNetworkConfigurationHandler::SetProperties(
scoped_ptr<base::DictionaryValue> shill_dictionary(
CreateShillConfiguration(*profile, guid, policy, &user_settings));
- NetworkConfigurationHandler::Get()->SetProperties(service_path,
- *shill_dictionary,
- callback,
- error_callback);
+ network_configuration_handler_->SetProperties(
+ service_path, *shill_dictionary, callback, error_callback);
}
void ManagedNetworkConfigurationHandler::CreateConfiguration(
@@ -559,7 +530,7 @@ void ManagedNetworkConfigurationHandler::CreateConfiguration(
}
const NetworkProfile* profile =
- profile_handler_->GetProfileForUserhash(userhash);
+ network_profile_handler_->GetProfileForUserhash(userhash);
if (!profile) {
RunErrorCallback("",
kProfileNotInitialized,
@@ -578,18 +549,16 @@ void ManagedNetworkConfigurationHandler::CreateConfiguration(
CreateShillConfiguration(*profile, guid, NULL /*no policy*/,
&properties));
- NetworkConfigurationHandler::Get()->CreateConfiguration(*shill_dictionary,
- callback,
- error_callback);
+ network_configuration_handler_->CreateConfiguration(
+ *shill_dictionary, callback, error_callback);
}
void ManagedNetworkConfigurationHandler::RemoveConfiguration(
const std::string& service_path,
const base::Closure& callback,
const network_handler::ErrorCallback& error_callback) const {
- NetworkConfigurationHandler::Get()->RemoveConfiguration(service_path,
- callback,
- error_callback);
+ network_configuration_handler_->RemoveConfiguration(
+ service_path, callback, error_callback);
}
// This class compares (entry point is Run()) |modified_policies| with the
@@ -736,10 +705,10 @@ class ManagedNetworkConfigurationHandler::PolicyApplicator
scoped_ptr<base::DictionaryValue> shill_dictionary =
CreateShillConfiguration(profile_, new_guid, new_policy,
ui_data->user_settings());
- NetworkConfigurationHandler::Get()->CreateConfiguration(
- *shill_dictionary,
- base::Bind(&IgnoreString),
- base::Bind(&LogErrorWithDict, FROM_HERE));
+ handler_->network_configuration_handler()->
+ CreateConfiguration(*shill_dictionary,
+ base::Bind(&IgnoreString),
+ base::Bind(&LogErrorWithDict, FROM_HERE));
remaining_policies_.erase(new_guid);
}
} else if (was_managed) {
@@ -801,7 +770,7 @@ class ManagedNetworkConfigurationHandler::PolicyApplicator
scoped_ptr<base::DictionaryValue> shill_dictionary =
CreateShillConfiguration(profile_, *it, policy,
NULL /* no user settings */);
- NetworkConfigurationHandler::Get()->CreateConfiguration(
+ handler_->network_configuration_handler()->CreateConfiguration(
*shill_dictionary,
base::Bind(&IgnoreString),
base::Bind(&LogErrorWithDict, FROM_HERE));
@@ -860,7 +829,7 @@ void ManagedNetworkConfigurationHandler::SetPolicy(
STLDeleteValues(&old_policies);
const NetworkProfile* profile =
- profile_handler_->GetProfileForUserhash(userhash);
+ network_profile_handler_->GetProfileForUserhash(userhash);
if (!profile) {
VLOG(1) << "The relevant Shill profile isn't initialized yet, postponing "
<< "policy application.";
@@ -920,19 +889,29 @@ ManagedNetworkConfigurationHandler::GetPoliciesForProfile(
return GetPoliciesForUser(profile.userhash);
}
-ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler(
- NetworkProfileHandler* profile_handler)
- : profile_handler_(profile_handler),
+ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler()
+ : network_state_handler_(NULL),
+ network_profile_handler_(NULL),
+ network_configuration_handler_(NULL),
weak_ptr_factory_(this) {
- profile_handler_->AddObserver(this);
}
ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() {
- profile_handler_->RemoveObserver(this);
+ network_profile_handler_->RemoveObserver(this);
for (UserToPoliciesMap::iterator it = policies_by_user_.begin();
it != policies_by_user_.end(); ++it) {
STLDeleteValues(&it->second);
}
}
+void ManagedNetworkConfigurationHandler::Init(
+ NetworkStateHandler* network_state_handler,
+ NetworkProfileHandler* network_profile_handler,
+ NetworkConfigurationHandler* network_configuration_handler) {
+ network_state_handler_ = network_state_handler;
+ network_profile_handler_ = network_profile_handler;
+ network_configuration_handler_ = network_configuration_handler;
+ network_profile_handler_->AddObserver(this);
+}
+
} // namespace chromeos
diff --git a/chromeos/network/managed_network_configuration_handler.h b/chromeos/network/managed_network_configuration_handler.h
index 295fd1e..fb8fc73 100644
--- a/chromeos/network/managed_network_configuration_handler.h
+++ b/chromeos/network/managed_network_configuration_handler.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+ // Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -13,6 +13,7 @@
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/chromeos_export.h"
+#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_handler_callbacks.h"
#include "chromeos/network/network_profile_observer.h"
#include "chromeos/network/onc/onc_constants.h"
@@ -24,7 +25,9 @@ class ListValue;
namespace chromeos {
+class NetworkConfigurationHandler;
class NetworkProfileHandler;
+class NetworkStateHandler;
class NetworkUIData;
// The ManagedNetworkConfigurationHandler class is used to create and configure
@@ -58,17 +61,7 @@ class CHROMEOS_EXPORT ManagedNetworkConfigurationHandler
typedef std::map<std::string, const base::DictionaryValue*> GuidToPolicyMap;
typedef std::map<std::string, GuidToPolicyMap> UserToPoliciesMap;
- // Initializes the singleton.
- static void Initialize(NetworkProfileHandler* profile_handler);
-
- // Returns if the singleton is initialized.
- static bool IsInitialized();
-
- // Destroys the singleton.
- static void Shutdown();
-
- // Initialize() must be called before this.
- static ManagedNetworkConfigurationHandler* Get();
+ virtual ~ManagedNetworkConfigurationHandler();
// Returns the NetworkUIData parsed from the UIData property of
// |shill_dictionary|. If parsing fails or the field doesn't exist, returns
@@ -135,12 +128,21 @@ class CHROMEOS_EXPORT ManagedNetworkConfigurationHandler
virtual void OnProfileAdded(const NetworkProfile& profile) OVERRIDE;
virtual void OnProfileRemoved(const NetworkProfile& profile) OVERRIDE;
+ NetworkConfigurationHandler* network_configuration_handler() {
+ return network_configuration_handler_;
+ }
+
private:
+ friend class NetworkHandler;
+ friend class ManagedNetworkConfigurationHandlerTest;
class PolicyApplicator;
- explicit ManagedNetworkConfigurationHandler(
- NetworkProfileHandler* profile_handler);
- virtual ~ManagedNetworkConfigurationHandler();
+ ManagedNetworkConfigurationHandler();
+
+ void Init(NetworkStateHandler* network_state_handler,
+ NetworkProfileHandler* network_profile_handler,
+ NetworkConfigurationHandler* network_configuration_handler);
+
void GetManagedPropertiesCallback(
const network_handler::DictionaryResultCallback& callback,
@@ -157,8 +159,10 @@ class CHROMEOS_EXPORT ManagedNetworkConfigurationHandler
// the device policy.
UserToPoliciesMap policies_by_user_;
- // A local reference to the policy handler singleton.
- NetworkProfileHandler* profile_handler_;
+ // Local references to the associated handler instances.
+ NetworkStateHandler* network_state_handler_;
+ NetworkProfileHandler* network_profile_handler_;
+ NetworkConfigurationHandler* network_configuration_handler_;
// For Shill client callbacks
base::WeakPtrFactory<ManagedNetworkConfigurationHandler> weak_ptr_factory_;
diff --git a/chromeos/network/managed_network_configuration_handler_unittest.cc b/chromeos/network/managed_network_configuration_handler_unittest.cc
index 3c17809..3ea8cec 100644
--- a/chromeos/network/managed_network_configuration_handler_unittest.cc
+++ b/chromeos/network/managed_network_configuration_handler_unittest.cc
@@ -15,7 +15,7 @@
#include "chromeos/dbus/mock_shill_service_client.h"
#include "chromeos/dbus/shill_profile_client_stub.h"
#include "chromeos/network/network_configuration_handler.h"
-#include "chromeos/network/network_profile_handler_stub.h"
+#include "chromeos/network/network_profile_handler.h"
#include "chromeos/network/onc/onc_test_utils.h"
#include "chromeos/network/onc/onc_utils.h"
#include "dbus/object_path.h"
@@ -23,6 +23,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
+using ::testing::AnyNumber;
using ::testing::Invoke;
using ::testing::Mock;
using ::testing::Pointee;
@@ -129,6 +130,19 @@ class ShillProfileTestClient {
std::map<std::string, std::string> profile_to_user_;
};
+class TestNetworkProfileHandler : public NetworkProfileHandler {
+ public:
+ TestNetworkProfileHandler() {}
+ virtual ~TestNetworkProfileHandler() {}
+
+ void AddProfileForTest(const NetworkProfile& profile) {
+ AddProfile(profile);
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
+};
+
} // namespace
class ManagedNetworkConfigurationHandlerTest : public testing::Test {
@@ -145,6 +159,8 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
.WillRepeatedly(Return(static_cast<dbus::Bus*>(NULL)));
DBusThreadManager::InitializeForTesting(dbus_thread_manager);
+ SetNetworkConfigurationHandlerExpectations();
+
EXPECT_CALL(*dbus_thread_manager, GetShillManagerClient())
.WillRepeatedly(Return(&mock_manager_client_));
EXPECT_CALL(*dbus_thread_manager, GetShillServiceClient())
@@ -160,14 +176,24 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
.WillByDefault(Invoke(&profiles_stub_,
&ShillProfileTestClient::GetEntry));
- NetworkConfigurationHandler::Initialize();
- ManagedNetworkConfigurationHandler::Initialize(&stub_profile_handler_);
+ network_profile_handler_.reset(new TestNetworkProfileHandler());
+ network_configuration_handler_.reset(
+ NetworkConfigurationHandler::InitializeForTest(
+ NULL /* no NetworkStateHandler */));
+ managed_network_configuration_handler_.reset(
+ new ManagedNetworkConfigurationHandler());
+ managed_network_configuration_handler_->Init(
+ NULL /* no NetworkStateHandler */,
+ network_profile_handler_.get(),
+ network_configuration_handler_.get());
+
message_loop_.RunUntilIdle();
}
virtual void TearDown() OVERRIDE {
- ManagedNetworkConfigurationHandler::Shutdown();
- NetworkConfigurationHandler::Shutdown();
+ managed_network_configuration_handler_.reset();
+ network_configuration_handler_.reset();
+ network_profile_handler_.reset();
DBusThreadManager::Shutdown();
}
@@ -175,13 +201,15 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
Mock::VerifyAndClearExpectations(&mock_manager_client_);
Mock::VerifyAndClearExpectations(&mock_service_client_);
Mock::VerifyAndClearExpectations(&mock_profile_client_);
+ SetNetworkConfigurationHandlerExpectations();
}
void InitializeStandardProfiles() {
profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
- stub_profile_handler_.AddProfile(NetworkProfile(kUser1ProfilePath, kUser1));
- stub_profile_handler_.AddProfile(
- NetworkProfile(kSharedProfilePath, std::string()));
+ network_profile_handler_->
+ AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
+ network_profile_handler_->
+ AddProfileForTest(NetworkProfile(kSharedProfilePath, std::string()));
}
void SetUpEntry(const std::string& path_to_shill_json,
@@ -209,16 +237,28 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
onc::ONC_SOURCE_USER_POLICY, userhash, *network_configs);
}
+ void SetNetworkConfigurationHandlerExpectations() {
+ // These calls occur in NetworkConfigurationHandler.
+ EXPECT_CALL(mock_manager_client_, GetProperties(_)).Times(AnyNumber());
+ EXPECT_CALL(mock_manager_client_,
+ AddPropertyChangedObserver(_)).Times(AnyNumber());
+ EXPECT_CALL(mock_manager_client_,
+ RemovePropertyChangedObserver(_)).Times(AnyNumber());
+ }
+
ManagedNetworkConfigurationHandler* managed_handler() {
- return ManagedNetworkConfigurationHandler::Get();
+ return managed_network_configuration_handler_.get();
}
protected:
StrictMock<MockShillManagerClient> mock_manager_client_;
StrictMock<MockShillServiceClient> mock_service_client_;
StrictMock<MockShillProfileClient> mock_profile_client_;
- NetworkProfileHandlerStub stub_profile_handler_;
ShillProfileTestClient profiles_stub_;
+ scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
+ scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
+ scoped_ptr<ManagedNetworkConfigurationHandler>
+ managed_network_configuration_handler_;
MessageLoop message_loop_;
private:
diff --git a/chromeos/network/network_change_notifier_chromeos.cc b/chromeos/network/network_change_notifier_chromeos.cc
index 224930c..8afc7de 100644
--- a/chromeos/network/network_change_notifier_chromeos.cc
+++ b/chromeos/network/network_change_notifier_chromeos.cc
@@ -58,20 +58,20 @@ NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
void NetworkChangeNotifierChromeos::Initialize() {
DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
- NetworkStateHandler::Get()->AddObserver(this);
+ NetworkHandler::Get()->network_state_handler()->AddObserver(this);
dns_config_service_.reset(new DnsConfigService());
dns_config_service_->WatchConfig(
base::Bind(net::NetworkChangeNotifier::SetDnsConfig));
// Update initial connection state.
- DefaultNetworkChanged(NetworkStateHandler::Get()->DefaultNetwork());
+ DefaultNetworkChanged(
+ NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
}
void NetworkChangeNotifierChromeos::Shutdown() {
dns_config_service_.reset();
- if (NetworkStateHandler::Get())
- NetworkStateHandler::Get()->RemoveObserver(this);
+ NetworkHandler::Get()->network_state_handler()->RemoveObserver(this);
DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
}
@@ -210,4 +210,3 @@ NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
}
} // namespace chromeos
-
diff --git a/chromeos/network/network_configuration_handler.cc b/chromeos/network/network_configuration_handler.cc
index 91c87a0..903e606 100644
--- a/chromeos/network/network_configuration_handler.cc
+++ b/chromeos/network/network_configuration_handler.cc
@@ -24,8 +24,6 @@ namespace chromeos {
namespace {
-NetworkConfigurationHandler* g_configuration_handler_instance = NULL;
-
// None of these error messages are user-facing: they should only appear in
// logs.
const char kErrorsListTag[] = "errors";
@@ -93,17 +91,6 @@ void RunCallbackWithDictionaryValue(
}
}
-void RunCreateNetworkCallback(
- const network_handler::StringResultCallback& callback,
- const dbus::ObjectPath& service_path) {
- callback.Run(service_path.value());
- // This may also get called when CreateConfiguration is used to update an
- // existing configuration, so request a service update just in case.
- // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger
- // this on an update.
- NetworkStateHandler::Get()->RequestUpdateForNetwork(service_path.value());
-}
-
void IgnoreObjectPathCallback(const base::Closure& callback,
const dbus::ObjectPath& object_path) {
callback.Run();
@@ -111,26 +98,6 @@ void IgnoreObjectPathCallback(const base::Closure& callback,
} // namespace
-// static
-void NetworkConfigurationHandler::Initialize() {
- CHECK(!g_configuration_handler_instance);
- g_configuration_handler_instance = new NetworkConfigurationHandler;
-}
-
-// static
-void NetworkConfigurationHandler::Shutdown() {
- CHECK(g_configuration_handler_instance);
- delete g_configuration_handler_instance;
- g_configuration_handler_instance = NULL;
-}
-
-// static
-NetworkConfigurationHandler* NetworkConfigurationHandler::Get() {
- CHECK(g_configuration_handler_instance)
- << "NetworkConfigurationHandler::Get() called before Initialize()";
- return g_configuration_handler_instance;
-}
-
void NetworkConfigurationHandler::GetProperties(
const std::string& service_path,
const network_handler::DictionaryResultCallback& callback,
@@ -153,7 +120,7 @@ void NetworkConfigurationHandler::SetProperties(
base::Bind(&IgnoreObjectPathCallback, callback),
base::Bind(&network_handler::ShillErrorCallbackFunction,
service_path, error_callback));
- NetworkStateHandler::Get()->RequestUpdateForNetwork(service_path);
+ network_state_handler_->RequestUpdateForNetwork(service_path);
}
void NetworkConfigurationHandler::ClearProperties(
@@ -176,7 +143,7 @@ void NetworkConfigurationHandler::ClearProperties(
void NetworkConfigurationHandler::CreateConfiguration(
const base::DictionaryValue& properties,
const network_handler::StringResultCallback& callback,
- const network_handler::ErrorCallback& error_callback) const {
+ const network_handler::ErrorCallback& error_callback) {
ShillManagerClient* manager =
DBusThreadManager::Get()->GetShillManagerClient();
@@ -193,13 +160,15 @@ void NetworkConfigurationHandler::CreateConfiguration(
manager->ConfigureServiceForProfile(
dbus::ObjectPath(profile),
properties,
- base::Bind(&RunCreateNetworkCallback, callback),
+ base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback,
+ AsWeakPtr(), callback),
base::Bind(&network_handler::ShillErrorCallbackFunction,
"", error_callback));
} else {
manager->GetService(
properties,
- base::Bind(&RunCreateNetworkCallback, callback),
+ base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback,
+ AsWeakPtr(), callback),
base::Bind(&network_handler::ShillErrorCallbackFunction,
"", error_callback));
}
@@ -216,10 +185,35 @@ void NetworkConfigurationHandler::RemoveConfiguration(
service_path, error_callback));
}
-NetworkConfigurationHandler::NetworkConfigurationHandler() {
+NetworkConfigurationHandler::NetworkConfigurationHandler()
+ : network_state_handler_(NULL) {
}
NetworkConfigurationHandler::~NetworkConfigurationHandler() {
}
+void NetworkConfigurationHandler::Init(
+ NetworkStateHandler* network_state_handler) {
+ network_state_handler_ = network_state_handler;
+}
+
+void NetworkConfigurationHandler::RunCreateNetworkCallback(
+ const network_handler::StringResultCallback& callback,
+ const dbus::ObjectPath& service_path) {
+ callback.Run(service_path.value());
+ // This may also get called when CreateConfiguration is used to update an
+ // existing configuration, so request a service update just in case.
+ // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger
+ // this on an update.
+ network_state_handler_->RequestUpdateForNetwork(service_path.value());
+}
+
+// static
+NetworkConfigurationHandler* NetworkConfigurationHandler::InitializeForTest(
+ NetworkStateHandler* network_state_handler) {
+ NetworkConfigurationHandler* handler = new NetworkConfigurationHandler();
+ handler->Init(network_state_handler);
+ return handler;
+}
+
} // namespace chromeos
diff --git a/chromeos/network/network_configuration_handler.h b/chromeos/network/network_configuration_handler.h
index 802798a..213314f 100644
--- a/chromeos/network/network_configuration_handler.h
+++ b/chromeos/network/network_configuration_handler.h
@@ -11,13 +11,19 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/gtest_prod_util.h"
+#include "base/memory/weak_ptr.h"
#include "chromeos/chromeos_export.h"
+#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_handler_callbacks.h"
namespace base {
class DictionaryValue;
}
+namespace dbus {
+class ObjectPath;
+}
+
namespace chromeos {
// The NetworkConfigurationHandler class is used to create and configure
@@ -39,19 +45,11 @@ namespace chromeos {
// that is suitable for logging. None of the error message text is meant for
// user consumption.
-class CHROMEOS_EXPORT NetworkConfigurationHandler {
+class CHROMEOS_EXPORT NetworkConfigurationHandler
+ : public base::SupportsWeakPtr<NetworkConfigurationHandler> {
public:
~NetworkConfigurationHandler();
- // Sets the global instance. Must be called before any calls to Get().
- static void Initialize();
-
- // Destroys the global instance.
- static void Shutdown();
-
- // Gets the global instance. Initialize() must be called first.
- static NetworkConfigurationHandler* Get();
-
// Gets the properties of the network with id |service_path|. See note on
// |callback| and |error_callback|, in class description above.
void GetProperties(
@@ -88,7 +86,7 @@ class CHROMEOS_EXPORT NetworkConfigurationHandler {
void CreateConfiguration(
const base::DictionaryValue& properties,
const network_handler::StringResultCallback& callback,
- const network_handler::ErrorCallback& error_callback) const;
+ const network_handler::ErrorCallback& error_callback);
// Removes the network |service_path| from the remembered network list in the
// active Shill profile. The network may still show up in the visible networks
@@ -99,9 +97,22 @@ class CHROMEOS_EXPORT NetworkConfigurationHandler {
const base::Closure& callback,
const network_handler::ErrorCallback& error_callback) const;
- private:
+ // Construct and initialize an instance for testing.
+ static NetworkConfigurationHandler* InitializeForTest(
+ NetworkStateHandler* network_state_handler);
+
+ protected:
+ friend class NetworkHandler;
friend class NetworkConfigurationHandlerTest;
+
NetworkConfigurationHandler();
+ void Init(NetworkStateHandler* network_state_handler);
+
+ void RunCreateNetworkCallback(
+ const network_handler::StringResultCallback& callback,
+ const dbus::ObjectPath& service_path);
+
+ NetworkStateHandler* network_state_handler_;
DISALLOW_COPY_AND_ASSIGN(NetworkConfigurationHandler);
};
diff --git a/chromeos/network/network_configuration_handler_unittest.cc b/chromeos/network/network_configuration_handler_unittest.cc
index 0dad618..04fa4ad 100644
--- a/chromeos/network/network_configuration_handler_unittest.cc
+++ b/chromeos/network/network_configuration_handler_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+ // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -88,14 +88,15 @@ class NetworkConfigurationHandlerTest : public testing::Test {
mock_service_client_ =
mock_dbus_thread_manager->mock_shill_service_client();
- NetworkStateHandler::Initialize();
- NetworkConfigurationHandler::Initialize();
+ network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
+ network_configuration_handler_.reset(new NetworkConfigurationHandler());
+ network_configuration_handler_->Init(network_state_handler_.get());
message_loop_.RunUntilIdle();
}
virtual void TearDown() OVERRIDE {
- NetworkConfigurationHandler::Shutdown();
- NetworkStateHandler::Shutdown();
+ network_configuration_handler_.reset();
+ network_state_handler_.reset();
DBusThreadManager::Shutdown();
}
@@ -151,6 +152,8 @@ class NetworkConfigurationHandlerTest : public testing::Test {
protected:
MockShillManagerClient* mock_manager_client_;
MockShillServiceClient* mock_service_client_;
+ scoped_ptr<NetworkStateHandler> network_state_handler_;
+ scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
MessageLoop message_loop_;
base::DictionaryValue* dictionary_value_result_;
};
@@ -180,7 +183,7 @@ TEST_F(NetworkConfigurationHandlerTest, GetProperties) {
GetProperties(_, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnGetProperties));
- NetworkConfigurationHandler::Get()->GetProperties(
+ network_configuration_handler_->GetProperties(
service_path,
base::Bind(&DictionaryValueCallback,
service_path,
@@ -203,7 +206,7 @@ TEST_F(NetworkConfigurationHandlerTest, SetProperties) {
ConfigureService(_, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnSetProperties));
- NetworkConfigurationHandler::Get()->SetProperties(
+ network_configuration_handler_->SetProperties(
service_path,
value,
base::Bind(&base::DoNothing),
@@ -226,7 +229,7 @@ TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
ConfigureService(_, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnSetProperties));
- NetworkConfigurationHandler::Get()->SetProperties(
+ network_configuration_handler_->SetProperties(
service_path,
value,
base::Bind(&base::DoNothing),
@@ -240,7 +243,7 @@ TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
ClearProperties(_, _, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnClearProperties));
- NetworkConfigurationHandler::Get()->ClearProperties(
+ network_configuration_handler_->ClearProperties(
service_path,
values_to_clear,
base::Bind(&base::DoNothing),
@@ -263,7 +266,7 @@ TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
ConfigureService(_, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnSetProperties));
- NetworkConfigurationHandler::Get()->SetProperties(
+ network_configuration_handler_->SetProperties(
service_path,
value,
base::Bind(&base::DoNothing),
@@ -278,7 +281,7 @@ TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
ClearProperties(_, _, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnClearPropertiesError));
- NetworkConfigurationHandler::Get()->ClearProperties(
+ network_configuration_handler_->ClearProperties(
service_path,
values_to_clear,
base::Bind(&base::DoNothing),
@@ -300,7 +303,7 @@ TEST_F(NetworkConfigurationHandlerTest, CreateConfiguration) {
GetService(_, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnGetService));
- NetworkConfigurationHandler::Get()->CreateConfiguration(
+ network_configuration_handler_->CreateConfiguration(
value,
base::Bind(&StringResultCallback, std::string("/service/2")),
base::Bind(&ErrorCallback, false, std::string("")));
@@ -315,7 +318,7 @@ TEST_F(NetworkConfigurationHandlerTest, RemoveConfiguration) {
Remove(_, _, _)).WillOnce(
Invoke(this,
&NetworkConfigurationHandlerTest::OnRemove));
- NetworkConfigurationHandler::Get()->RemoveConfiguration(
+ network_configuration_handler_->RemoveConfiguration(
service_path,
base::Bind(&base::DoNothing),
base::Bind(&ErrorCallback, false, service_path));
diff --git a/chromeos/network/network_connection_handler.cc b/chromeos/network/network_connection_handler.cc
index 4b7bdff..d63688e 100644
--- a/chromeos/network/network_connection_handler.cc
+++ b/chromeos/network/network_connection_handler.cc
@@ -111,8 +111,6 @@ bool CertificateIsConfigured(NetworkUIData* ui_data) {
} // namespace
-static NetworkConnectionHandler* g_connection_handler_instance = NULL;
-
const char NetworkConnectionHandler::kErrorNotFound[] = "not-found";
const char NetworkConnectionHandler::kErrorConnected[] = "connected";
const char NetworkConnectionHandler::kErrorConnecting[] = "connecting";
@@ -127,27 +125,9 @@ const char NetworkConnectionHandler::kErrorConfigurationRequired[] =
"configuration-required";
const char NetworkConnectionHandler::kErrorShillError[] = "shill-error";
-// static
-void NetworkConnectionHandler::Initialize() {
- CHECK(!g_connection_handler_instance);
- g_connection_handler_instance = new NetworkConnectionHandler;
-}
-
-// static
-void NetworkConnectionHandler::Shutdown() {
- CHECK(g_connection_handler_instance);
- delete g_connection_handler_instance;
- g_connection_handler_instance = NULL;
-}
-
-// static
-NetworkConnectionHandler* NetworkConnectionHandler::Get() {
- CHECK(g_connection_handler_instance)
- << "NetworkConnectionHandler::Get() called before Initialize()";
- return g_connection_handler_instance;
-}
-
-NetworkConnectionHandler::NetworkConnectionHandler() {
+NetworkConnectionHandler::NetworkConnectionHandler()
+ : network_state_handler_(NULL),
+ network_configuration_handler_(NULL) {
const char* new_handlers_enabled =
CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kUseNewNetworkConfigurationHandlers) ?
@@ -158,13 +138,20 @@ NetworkConnectionHandler::NetworkConnectionHandler() {
NetworkConnectionHandler::~NetworkConnectionHandler() {
}
+void NetworkConnectionHandler::Init(
+ NetworkStateHandler* network_state_handler,
+ NetworkConfigurationHandler* network_configuration_handler) {
+ network_state_handler_ = network_state_handler;
+ network_configuration_handler_ = network_configuration_handler;
+}
+
void NetworkConnectionHandler::ConnectToNetwork(
const std::string& service_path,
const base::Closure& success_callback,
const network_handler::ErrorCallback& error_callback,
bool ignore_error_state) {
const NetworkState* network =
- NetworkStateHandler::Get()->GetNetworkState(service_path);
+ network_state_handler_->GetNetworkState(service_path);
if (!network) {
InvokeErrorCallback(service_path, error_callback, kErrorNotFound);
return;
@@ -210,7 +197,7 @@ void NetworkConnectionHandler::ConnectToNetwork(
if (!network->connectable() && NetworkMayNeedCredentials(network)) {
// Request additional properties to check.
- NetworkConfigurationHandler::Get()->GetProperties(
+ network_configuration_handler_->GetProperties(
network->path(),
base::Bind(&NetworkConnectionHandler::VerifyConfiguredAndConnect,
AsWeakPtr(), success_callback, error_callback),
@@ -227,7 +214,7 @@ void NetworkConnectionHandler::DisconnectNetwork(
const base::Closure& success_callback,
const network_handler::ErrorCallback& error_callback) {
const NetworkState* network =
- NetworkStateHandler::Get()->GetNetworkState(service_path);
+ network_state_handler_->GetNetworkState(service_path);
if (!network) {
InvokeErrorCallback(service_path, error_callback, kErrorNotFound);
return;
@@ -245,7 +232,7 @@ void NetworkConnectionHandler::CallShillConnect(
const network_handler::ErrorCallback& error_callback) {
// TODO(stevenjb): Remove SetConnectingNetwork and use this class to maintain
// the connecting network(s) once NetworkLibrary path is eliminated.
- NetworkStateHandler::Get()->SetConnectingNetwork(service_path);
+ network_state_handler_->SetConnectingNetwork(service_path);
NET_LOG_EVENT("Connect Request", service_path);
DBusThreadManager::Get()->GetShillServiceClient()->Connect(
dbus::ObjectPath(service_path),
@@ -274,7 +261,7 @@ void NetworkConnectionHandler::VerifyConfiguredAndConnect(
const std::string& service_path,
const base::DictionaryValue& properties) {
const NetworkState* network =
- NetworkStateHandler::Get()->GetNetworkState(service_path);
+ network_state_handler_->GetNetworkState(service_path);
if (!network) {
InvokeErrorCallback(service_path, error_callback, kErrorNotFound);
return;
diff --git a/chromeos/network/network_connection_handler.h b/chromeos/network/network_connection_handler.h
index cc147be..a4d233de 100644
--- a/chromeos/network/network_connection_handler.h
+++ b/chromeos/network/network_connection_handler.h
@@ -14,6 +14,7 @@
#include "base/values.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_method_call_status.h"
+#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_handler_callbacks.h"
namespace chromeos {
@@ -49,14 +50,7 @@ class CHROMEOS_EXPORT NetworkConnectionHandler
static const char kErrorShillError[];
static const char kErrorPreviousConnectFailed[];
- // Sets the global instance. Must be called before any calls to Get().
- static void Initialize();
-
- // Destroys the global instance.
- static void Shutdown();
-
- // Gets the global instance. Initialize() must be called first.
- static NetworkConnectionHandler* Get();
+ ~NetworkConnectionHandler();
// ConnectToNetwork() will start an asynchronous connection attempt.
// On success, |success_callback| will be called.
@@ -89,8 +83,12 @@ class CHROMEOS_EXPORT NetworkConnectionHandler
const network_handler::ErrorCallback& error_callback);
private:
+ friend class NetworkHandler;
+ friend class NetworkConnectionHandlerTest;
NetworkConnectionHandler();
- ~NetworkConnectionHandler();
+
+ void Init(NetworkStateHandler* network_state_handler,
+ NetworkConfigurationHandler* network_configuration_handler);
// Calls Shill.Manager.Connect asynchronously.
void CallShillConnect(
@@ -133,6 +131,11 @@ class CHROMEOS_EXPORT NetworkConnectionHandler
const std::string& error_name,
const std::string& error_message);
+ // Local references to the associated handler instances.
+ NetworkStateHandler* network_state_handler_;
+ NetworkProfileHandler* network_profile_handler_;
+ NetworkConfigurationHandler* network_configuration_handler_;
+
// Set of pending connect requests, used to prevent repeat attempts while
// waiting for Shill.
std::set<std::string> pending_requests_;
diff --git a/chromeos/network/network_connection_handler_unittest.cc b/chromeos/network/network_connection_handler_unittest.cc
index bd3f5a2..e5d02bf 100644
--- a/chromeos/network/network_connection_handler_unittest.cc
+++ b/chromeos/network/network_connection_handler_unittest.cc
@@ -38,15 +38,19 @@ class NetworkConnectionHandlerTest : public testing::Test {
DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()
->ClearServices();
message_loop_.RunUntilIdle();
- NetworkStateHandler::Initialize();
- NetworkConfigurationHandler::Initialize();
- NetworkConnectionHandler::Initialize();
+ network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
+ network_configuration_handler_.reset(
+ NetworkConfigurationHandler::InitializeForTest(
+ network_state_handler_.get()));
+ network_connection_handler_.reset(new NetworkConnectionHandler);
+ network_connection_handler_->Init(network_state_handler_.get(),
+ network_configuration_handler_.get());
}
virtual void TearDown() OVERRIDE {
- NetworkConnectionHandler::Shutdown();
- NetworkConfigurationHandler::Shutdown();
- NetworkStateHandler::Shutdown();
+ network_connection_handler_.reset();
+ network_configuration_handler_.reset();
+ network_state_handler_.reset();
DBusThreadManager::Shutdown();
}
@@ -67,7 +71,7 @@ class NetworkConnectionHandlerTest : public testing::Test {
void Connect(const std::string& service_path) {
const bool ignore_error_state = false;
- NetworkConnectionHandler::Get()->ConnectToNetwork(
+ network_connection_handler_->ConnectToNetwork(
service_path,
base::Bind(&NetworkConnectionHandlerTest::SuccessCallback,
base::Unretained(this)),
@@ -78,7 +82,7 @@ class NetworkConnectionHandlerTest : public testing::Test {
}
void Disconnect(const std::string& service_path) {
- NetworkConnectionHandler::Get()->DisconnectNetwork(
+ network_connection_handler_->DisconnectNetwork(
service_path,
base::Bind(&NetworkConnectionHandlerTest::SuccessCallback,
base::Unretained(this)),
@@ -113,6 +117,9 @@ class NetworkConnectionHandlerTest : public testing::Test {
return result;
}
+ scoped_ptr<NetworkStateHandler> network_state_handler_;
+ scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
+ scoped_ptr<NetworkConnectionHandler> network_connection_handler_;
MessageLoopForUI message_loop_;
std::string result_;
diff --git a/chromeos/network/network_handler.cc b/chromeos/network/network_handler.cc
new file mode 100644
index 0000000..5eb2a87
--- /dev/null
+++ b/chromeos/network/network_handler.cc
@@ -0,0 +1,109 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromeos/network/network_handler.h"
+
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/network/cert_loader.h"
+#include "chromeos/network/geolocation_handler.h"
+#include "chromeos/network/managed_network_configuration_handler.h"
+#include "chromeos/network/network_configuration_handler.h"
+#include "chromeos/network/network_connection_handler.h"
+#include "chromeos/network/network_event_log.h"
+#include "chromeos/network/network_profile_handler.h"
+#include "chromeos/network/network_profile_observer.h"
+#include "chromeos/network/network_state_handler.h"
+#include "chromeos/network/network_state_handler_observer.h"
+
+namespace chromeos {
+
+static NetworkHandler* g_network_handler = NULL;
+
+NetworkHandler::NetworkHandler() {
+ CHECK(DBusThreadManager::IsInitialized());
+
+ network_event_log::Initialize();
+
+ cert_loader_.reset(new CertLoader);
+ network_state_handler_.reset(new NetworkStateHandler());
+ network_profile_handler_.reset(new NetworkProfileHandler());
+ network_configuration_handler_.reset(new NetworkConfigurationHandler());
+ managed_network_configuration_handler_.reset(
+ new ManagedNetworkConfigurationHandler());
+ network_connection_handler_.reset(new NetworkConnectionHandler());
+ geolocation_handler_.reset(new GeolocationHandler());
+}
+
+NetworkHandler::~NetworkHandler() {
+ network_event_log::Shutdown();
+}
+
+void NetworkHandler::Init() {
+ network_state_handler_->InitShillPropertyHandler();
+ network_configuration_handler_->Init(network_state_handler_.get());
+ managed_network_configuration_handler_->Init(
+ network_state_handler_.get(),
+ network_profile_handler_.get(),
+ network_configuration_handler_.get());
+ network_connection_handler_->Init(network_state_handler_.get(),
+ network_configuration_handler_.get());
+ geolocation_handler_->Init();
+}
+
+// static
+void NetworkHandler::Initialize() {
+ CHECK(!g_network_handler);
+ g_network_handler = new NetworkHandler();
+ g_network_handler->Init();
+}
+
+// static
+void NetworkHandler::Shutdown() {
+ CHECK(g_network_handler);
+ delete g_network_handler;
+ g_network_handler = NULL;
+}
+
+// static
+NetworkHandler* NetworkHandler::Get() {
+ CHECK(g_network_handler)
+ << "NetworkHandler::Get() called before Initialize()";
+ return g_network_handler;
+}
+
+// static
+bool NetworkHandler::IsInitialized() {
+ return g_network_handler;
+}
+
+CertLoader* NetworkHandler::cert_loader() {
+ return cert_loader_.get();
+}
+
+NetworkStateHandler* NetworkHandler::network_state_handler() {
+ return network_state_handler_.get();
+}
+
+NetworkProfileHandler* NetworkHandler::network_profile_handler() {
+ return network_profile_handler_.get();
+}
+
+NetworkConfigurationHandler* NetworkHandler::network_configuration_handler() {
+ return network_configuration_handler_.get();
+}
+
+ManagedNetworkConfigurationHandler*
+NetworkHandler::managed_network_configuration_handler() {
+ return managed_network_configuration_handler_.get();
+}
+
+NetworkConnectionHandler* NetworkHandler::network_connection_handler() {
+ return network_connection_handler_.get();
+}
+
+GeolocationHandler* NetworkHandler::geolocation_handler() {
+ return geolocation_handler_.get();
+}
+
+} // namespace chromeos
diff --git a/chromeos/network/network_handler.h b/chromeos/network/network_handler.h
new file mode 100644
index 0000000..e3df697
--- /dev/null
+++ b/chromeos/network/network_handler.h
@@ -0,0 +1,71 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_NETWORK_NETWORK_HANDLER_H_
+#define CHROMEOS_NETWORK_NETWORK_HANDLER_H_
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "chromeos/chromeos_export.h"
+
+namespace chromeos {
+
+class CertLoader;
+class GeolocationHandler;
+class ManagedNetworkConfigurationHandler;
+class NetworkConfigurationHandler;
+class NetworkConnectionHandler;
+class NetworkProfileHandler;
+class NetworkStateHandler;
+
+// Class for handling initialization and access to chromeos network handlers.
+// This class should NOT be used in unit tests. Instead, construct individual
+// classes independently.
+class CHROMEOS_EXPORT NetworkHandler {
+ public:
+ // Sets the global instance. Must be called before any calls to Get().
+ static void Initialize();
+
+ // Destroys the global instance.
+ static void Shutdown();
+
+ // Gets the global instance. Initialize() must be called first.
+ static NetworkHandler* Get();
+
+ // Returns true if the global instance has been initialized.
+ static bool IsInitialized();
+
+ // Do not use these accessors within this module; all dependencies should be
+ // explicit so that classes can be constructed explicitly in tests without
+ // NetworkHandler.
+ CertLoader* cert_loader();
+ NetworkStateHandler* network_state_handler();
+ NetworkProfileHandler* network_profile_handler();
+ NetworkConfigurationHandler* network_configuration_handler();
+ ManagedNetworkConfigurationHandler* managed_network_configuration_handler();
+ NetworkConnectionHandler* network_connection_handler();
+ GeolocationHandler* geolocation_handler();
+
+ private:
+ NetworkHandler();
+ virtual ~NetworkHandler();
+
+ void Init();
+
+ // The order of these determines the (inverse) destruction order.
+ scoped_ptr<CertLoader> cert_loader_;
+ scoped_ptr<NetworkStateHandler> network_state_handler_;
+ scoped_ptr<NetworkProfileHandler> network_profile_handler_;
+ scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
+ scoped_ptr<ManagedNetworkConfigurationHandler>
+ managed_network_configuration_handler_;
+ scoped_ptr<NetworkConnectionHandler> network_connection_handler_;
+ scoped_ptr<GeolocationHandler> geolocation_handler_;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkHandler);
+};
+
+} // namespace chromeos
+
+#endif // CHROMEOS_NETWORK_NETWORK_HANDLER_H_
diff --git a/chromeos/network/network_profile_handler.cc b/chromeos/network/network_profile_handler.cc
index 30c8fde..cd6a8c6 100644
--- a/chromeos/network/network_profile_handler.cc
+++ b/chromeos/network/network_profile_handler.cc
@@ -18,8 +18,6 @@
namespace chromeos {
-static NetworkProfileHandler* g_profile_handler_instance = NULL;
-
namespace {
bool ConvertListValueToStringVector(const base::ListValue& string_list,
@@ -54,51 +52,8 @@ class ProfilePathEquals {
std::string path_;
};
-class NetworkProfileHandlerImpl : public NetworkProfileHandler {
- public:
- NetworkProfileHandlerImpl() {
- DBusThreadManager::Get()->GetShillManagerClient()->
- AddPropertyChangedObserver(this);
-
- // Request the initial profile list.
- DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(
- base::Bind(&NetworkProfileHandler::GetManagerPropertiesCallback,
- weak_ptr_factory_.GetWeakPtr()));
- }
-
- virtual ~NetworkProfileHandlerImpl() {
- DBusThreadManager::Get()->GetShillManagerClient()->
- RemovePropertyChangedObserver(this);
- }
-};
-
} // namespace
-// static
-NetworkProfileHandler* NetworkProfileHandler::Initialize() {
- CHECK(!g_profile_handler_instance);
- g_profile_handler_instance = new NetworkProfileHandlerImpl();
- return g_profile_handler_instance;
-}
-
-// static
-bool NetworkProfileHandler::IsInitialized() {
- return g_profile_handler_instance;
-}
-
-// static
-void NetworkProfileHandler::Shutdown() {
- CHECK(g_profile_handler_instance);
- delete g_profile_handler_instance;
- g_profile_handler_instance = NULL;
-}
-
-// static
-NetworkProfileHandler* NetworkProfileHandler::Get() {
- CHECK(g_profile_handler_instance);
- return g_profile_handler_instance;
-}
-
void NetworkProfileHandler::AddObserver(NetworkProfileObserver* observer) {
observers_.AddObserver(observer);
}
@@ -139,6 +94,7 @@ void NetworkProfileHandler::OnPropertyChanged(const std::string& name,
&new_profile_paths);
DCHECK(result);
+ VLOG(2) << "Profiles: " << profiles_.size();
// Search for removed profiles.
std::vector<std::string> removed_profile_paths;
for (ProfileList::const_iterator it = profiles_.begin();
@@ -225,9 +181,18 @@ const NetworkProfile* NetworkProfileHandler::GetProfileForUserhash(
NetworkProfileHandler::NetworkProfileHandler()
: weak_ptr_factory_(this) {
+ DBusThreadManager::Get()->GetShillManagerClient()->
+ AddPropertyChangedObserver(this);
+
+ // Request the initial profile list.
+ DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(
+ base::Bind(&NetworkProfileHandler::GetManagerPropertiesCallback,
+ weak_ptr_factory_.GetWeakPtr()));
}
NetworkProfileHandler::~NetworkProfileHandler() {
+ DBusThreadManager::Get()->GetShillManagerClient()->
+ RemovePropertyChangedObserver(this);
}
} // namespace chromeos
diff --git a/chromeos/network/network_profile_handler.h b/chromeos/network/network_profile_handler.h
index 9dbc5d4..f175876 100644
--- a/chromeos/network/network_profile_handler.h
+++ b/chromeos/network/network_profile_handler.h
@@ -15,6 +15,7 @@
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/shill_property_changed_observer.h"
+#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_profile.h"
namespace base {
@@ -30,22 +31,11 @@ class CHROMEOS_EXPORT NetworkProfileHandler
public:
typedef std::vector<NetworkProfile> ProfileList;
- // Initializes the singleton and registers it for DBus events.
- static NetworkProfileHandler* Initialize();
-
- // Returns if the singleton is initialized.
- static bool IsInitialized();
-
- // Unregisters the singleton from DBus events and destroys it.
- static void Shutdown();
-
- // Initialize() must be called before this.
- static NetworkProfileHandler* Get();
+ virtual ~NetworkProfileHandler();
void AddObserver(NetworkProfileObserver* observer);
void RemoveObserver(NetworkProfileObserver* observer);
- void RequestInitialProfileList();
void GetManagerPropertiesCallback(DBusMethodCallStatus call_status,
const base::DictionaryValue& properties);
@@ -62,11 +52,8 @@ class CHROMEOS_EXPORT NetworkProfileHandler
const std::string& userhash) const;
protected:
- // We make the de-/constructor protected to prevent their usage except in
- // tests by deriving a stub (see NetworkProfileHandlerStub). Outside of tests,
- // the singleton should be retrieved with the static Get() function.
+ friend class NetworkHandler;
NetworkProfileHandler();
- virtual ~NetworkProfileHandler();
void AddProfile(const NetworkProfile& profile);
void RemoveProfile(const std::string& profile_path);
diff --git a/chromeos/network/network_profile_handler_stub.h b/chromeos/network/network_profile_handler_stub.h
deleted file mode 100644
index b136028..0000000
--- a/chromeos/network/network_profile_handler_stub.h
+++ /dev/null
@@ -1,22 +0,0 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROMEOS_NETWORK_NETWORK_PROFILE_HANDLER_STUB_H_
-#define CHROMEOS_NETWORK_NETWORK_PROFILE_HANDLER_STUB_H_
-
-#include "chromeos/chromeos_export.h"
-#include "chromeos/network/network_profile_handler.h"
-
-namespace chromeos {
-
-class CHROMEOS_EXPORT NetworkProfileHandlerStub
- : public NetworkProfileHandler {
- public:
- using NetworkProfileHandler::AddProfile;
- using NetworkProfileHandler::RemoveProfile;
-};
-
-} // namespace chromeos
-
-#endif // CHROMEOS_NETWORK_NETWORK_PROFILE_HANDLER_STUB_H_
diff --git a/chromeos/network/network_state_handler.cc b/chromeos/network/network_state_handler.cc
index db73348..053a932e 100644
--- a/chromeos/network/network_state_handler.cc
+++ b/chromeos/network/network_state_handler.cc
@@ -71,8 +71,6 @@ const char NetworkStateHandler::kMatchTypeWireless[] = "wireless";
const char NetworkStateHandler::kMatchTypeMobile[] = "mobile";
const char NetworkStateHandler::kMatchTypeNonVirtual[] = "non-virtual";
-static NetworkStateHandler* g_network_state_handler = NULL;
-
NetworkStateHandler::NetworkStateHandler() {
}
@@ -87,29 +85,10 @@ void NetworkStateHandler::InitShillPropertyHandler() {
}
// static
-void NetworkStateHandler::Initialize() {
- CHECK(!g_network_state_handler);
- g_network_state_handler = new NetworkStateHandler();
- g_network_state_handler->InitShillPropertyHandler();
-}
-
-// static
-bool NetworkStateHandler::IsInitialized() {
- return g_network_state_handler != NULL;
-}
-
-// static
-void NetworkStateHandler::Shutdown() {
- CHECK(g_network_state_handler);
- delete g_network_state_handler;
- g_network_state_handler = NULL;
-}
-
-// static
-NetworkStateHandler* NetworkStateHandler::Get() {
- CHECK(g_network_state_handler)
- << "NetworkStateHandler::Get() called before Initialize()";
- return g_network_state_handler;
+NetworkStateHandler* NetworkStateHandler::InitializeForTest() {
+ NetworkStateHandler* handler = new NetworkStateHandler();
+ handler->InitShillPropertyHandler();
+ return handler;
}
void NetworkStateHandler::AddObserver(NetworkStateHandlerObserver* observer) {
diff --git a/chromeos/network/network_state_handler.h b/chromeos/network/network_state_handler.h
index 072258d..a361e25 100644
--- a/chromeos/network/network_state_handler.h
+++ b/chromeos/network/network_state_handler.h
@@ -16,6 +16,7 @@
#include "base/observer_list.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/network/managed_state.h"
+#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_handler_callbacks.h"
#include "chromeos/network/shill_property_handler.h"
@@ -67,18 +68,6 @@ class CHROMEOS_EXPORT NetworkStateHandler
virtual ~NetworkStateHandler();
- // Sets the global instance. Must be called before any calls to Get().
- static void Initialize();
-
- // Returns true if the global instance has been initialized.
- static bool IsInitialized();
-
- // Destroys the global instance.
- static void Shutdown();
-
- // Gets the global instance. Initialize() must be called first.
- static NetworkStateHandler* Get();
-
// Add/remove observers.
void AddObserver(NetworkStateHandlerObserver* observer);
void RemoveObserver(NetworkStateHandlerObserver* observer);
@@ -180,12 +169,16 @@ class CHROMEOS_EXPORT NetworkStateHandler
void GetNetworkStatePropertiesForTest(
base::DictionaryValue* dictionary) const;
+ // Construct and initialize an instance for testing.
+ static NetworkStateHandler* InitializeForTest();
+
static const char kMatchTypeDefault[];
static const char kMatchTypeWireless[];
static const char kMatchTypeMobile[];
static const char kMatchTypeNonVirtual[];
protected:
+ friend class NetworkHandler;
NetworkStateHandler();
// ShillPropertyHandler::Listener overrides.
@@ -228,7 +221,7 @@ class CHROMEOS_EXPORT NetworkStateHandler
virtual void ManagedStateListChanged(
ManagedState::ManagedType type) OVERRIDE;
- // Called in Initialize(). Called explicitly by tests after adding
+ // Called after construction. Called explicitly by tests after adding
// test observers.
void InitShillPropertyHandler();