summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-10 00:36:56 +0000
committeroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-10 00:36:56 +0000
commit24e8fd5d90ec644f24e0edb5aab359aeb46a80da (patch)
tree4477fd4971fd8dc0bd735c0b704457c15e0c2781
parent088b3d85fcbd72b1743b767452abc41fa5c01764 (diff)
downloadchromium_src-24e8fd5d90ec644f24e0edb5aab359aeb46a80da.zip
chromium_src-24e8fd5d90ec644f24e0edb5aab359aeb46a80da.tar.gz
chromium_src-24e8fd5d90ec644f24e0edb5aab359aeb46a80da.tar.bz2
NetworkStateNotifier that sends notification when network stat has changed.
This will be used to implement 3605. I created separate notification as we're planning to port to other platfrom. BUG=chromium-os:3605 TEST=added browser test Review URL: http://codereview.chromium.org/2913001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52028 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/browser_init.cc4
-rw-r--r--chrome/browser/chromeos/network_state_notifier.cc64
-rw-r--r--chrome/browser/chromeos/network_state_notifier.h83
-rw-r--r--chrome/browser/chromeos/network_state_notifier_browsertest.cc118
-rw-r--r--chrome/chrome_browser.gypi2
-rw-r--r--chrome/chrome_tests.gypi1
-rw-r--r--chrome/common/notification_type.h6
7 files changed, 278 insertions, 0 deletions
diff --git a/chrome/browser/browser_init.cc b/chrome/browser/browser_init.cc
index d75b0e2..7a651e5 100644
--- a/chrome/browser/browser_init.cc
+++ b/chrome/browser/browser_init.cc
@@ -76,6 +76,7 @@
#include "chrome/browser/chromeos/gview_request_interceptor.h"
#include "chrome/browser/chromeos/low_battery_observer.h"
#include "chrome/browser/chromeos/network_message_observer.h"
+#include "chrome/browser/chromeos/network_state_notifier.h"
#include "chrome/browser/chromeos/system_key_event_listener.h"
#include "chrome/browser/chromeos/usb_mount_observer.h"
#include "chrome/browser/chromeos/wm_message_listener.h"
@@ -436,6 +437,9 @@ bool BrowserInit::LaunchBrowser(
chromeos::CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(
network_message_observer);
+ chromeos::CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(
+ chromeos::NetworkStateNotifier::Get());
+
// Creates the SystemKeyEventListener to listen for keypress messages
// regardless of what window has focus.
chromeos::SystemKeyEventListener::instance();
diff --git a/chrome/browser/chromeos/network_state_notifier.cc b/chrome/browser/chromeos/network_state_notifier.cc
new file mode 100644
index 0000000..2d13030
--- /dev/null
+++ b/chrome/browser/chromeos/network_state_notifier.cc
@@ -0,0 +1,64 @@
+// Copyright (c) 2010 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 "chrome/browser/chrome_thread.h"
+#include "chrome/browser/chromeos/cros/cros_library.h"
+#include "chrome/browser/chromeos/network_state_notifier.h"
+#include "chrome/common/notification_service.h"
+#include "chrome/common/notification_type.h"
+
+#include "base/message_loop.h"
+
+namespace chromeos {
+
+NetworkStateNotifier* NetworkStateNotifier::Get() {
+ return Singleton<NetworkStateNotifier>::get();
+}
+
+NetworkStateNotifier::NetworkStateNotifier()
+ : task_factory_(this) {
+ state_ = RetrieveState();
+}
+
+void NetworkStateNotifier::NetworkChanged(NetworkLibrary* cros) {
+ DCHECK(CrosLibrary::Get()->EnsureLoaded());
+ // Update the state 1 seconds later using UI thread.
+ // See http://crosbug.com/4558
+ ChromeThread::PostDelayedTask(
+ ChromeThread::UI, FROM_HERE,
+ task_factory_.NewRunnableMethod(
+ &NetworkStateNotifier::UpdateNetworkState,
+ RetrieveState()),
+ 1000);
+}
+
+void NetworkStateNotifier::UpdateNetworkState(
+ NetworkStateDetails::State new_state) {
+ DLOG(INFO) << "UpdateNetworkState: new="
+ << new_state << ", old=" << state_;
+ state_ = new_state;
+ NetworkStateDetails details(state_);
+ NotificationService::current()->Notify(
+ NotificationType::NETWORK_STATE_CHANGED,
+ NotificationService::AllSources(),
+ Details<NetworkStateDetails>(&details));
+};
+
+// static
+NetworkStateDetails::State NetworkStateNotifier::RetrieveState() {
+ // Running on desktop means always connected, for now.
+ if (!CrosLibrary::Get()->EnsureLoaded())
+ return NetworkStateDetails::CONNECTED;
+ NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
+ if (cros->Connected()) {
+ return NetworkStateDetails::CONNECTED;
+ } else if (cros->Connecting()) {
+ return NetworkStateDetails::CONNECTING;
+ } else {
+ return NetworkStateDetails::DISCONNECTED;
+ }
+}
+
+
+} // namespace chromeos
diff --git a/chrome/browser/chromeos/network_state_notifier.h b/chrome/browser/chromeos/network_state_notifier.h
new file mode 100644
index 0000000..a48d60e
--- /dev/null
+++ b/chrome/browser/chromeos/network_state_notifier.h
@@ -0,0 +1,83 @@
+// Copyright (c) 2010 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 CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_
+#define CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_
+
+#include "chrome/browser/chromeos/cros/network_library.h"
+
+#include "base/singleton.h"
+#include "base/task.h"
+
+namespace chromeos {
+
+// NetworkStateDetails contains the information about
+// network status.
+class NetworkStateDetails {
+ public:
+ enum State {
+ UNKNOWN = 0,
+ DISCONNECTED,
+ CONNECTING,
+ CONNECTED,
+ };
+
+ State state() const {
+ return state_;
+ }
+
+ private:
+ friend class NetworkStateNotifier;
+
+ explicit NetworkStateDetails(State state)
+ : state_(state) {
+ }
+
+ State state_;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkStateDetails);
+};
+
+// NetworkStateNotifier sends notification when network state has
+// chagned. Notificatio is sent in UI thread.
+// TODO(oshima): port this to other platform. merge with
+// NetworkChangeNotifier if possible.
+class NetworkStateNotifier : public NetworkLibrary::Observer {
+ public:
+ // Returns the singleton instance of the network state notifier;
+ static NetworkStateNotifier* Get();
+
+ // NetworkLibrary::Observer implementation.
+ virtual void NetworkChanged(NetworkLibrary* cros);
+ virtual void NetworkTraffic(NetworkLibrary* cros, int traffic_type) {}
+
+ // Returns true if the network is connected.
+ static bool is_connected() {
+ return Get()->state_ == NetworkStateDetails::CONNECTED;
+ }
+
+ private:
+ friend struct DefaultSingletonTraits<NetworkStateNotifier>;
+
+ // Retrieve the current state from libcros.
+ static NetworkStateDetails::State RetrieveState();
+
+ NetworkStateNotifier();
+ virtual ~NetworkStateNotifier() {}
+
+ // Update the current state and sends notification to observers.
+ // This should be invoked in UI thread.
+ void UpdateNetworkState(NetworkStateDetails::State new_state);
+
+ // The current network state.
+ NetworkStateDetails::State state_;
+
+ ScopedRunnableMethodFactory<NetworkStateNotifier> task_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifier);
+};
+
+} // chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_
diff --git a/chrome/browser/chromeos/network_state_notifier_browsertest.cc b/chrome/browser/chromeos/network_state_notifier_browsertest.cc
new file mode 100644
index 0000000..bf2019c
--- /dev/null
+++ b/chrome/browser/chromeos/network_state_notifier_browsertest.cc
@@ -0,0 +1,118 @@
+// Copyright (c) 2009 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 "chrome/browser/chromeos/network_state_notifier.h"
+
+#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h"
+#include "chrome/browser/chromeos/cros/mock_network_library.h"
+#include "chrome/common/notification_service.h"
+#include "chrome/common/notification_registrar.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "chrome/test/ui_test_utils.h"
+
+namespace chromeos {
+
+using ::testing::Return;
+using ::testing::_;
+
+class NetworkStateNotifierTest : public CrosInProcessBrowserTest,
+ public NotificationObserver {
+ public:
+ NetworkStateNotifierTest()
+ : notification_received_(false) {
+ }
+
+ protected:
+ virtual void SetUpInProcessBrowserTestFixture() {
+ InitStatusAreaMocks();
+ SetStatusAreaMocksExpectations();
+ // Initialize network state notifier.
+ EXPECT_CALL(*mock_network_library_, Connected())
+ .Times(1)
+ .WillRepeatedly((Return(true)))
+ .RetiresOnSaturation();
+ NetworkStateNotifier::Get();
+ }
+
+ // NotificationObserver overrides.
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ EXPECT_TRUE(ChromeThread::CurrentlyOn(ChromeThread::UI));
+ EXPECT_TRUE(NotificationType::NETWORK_STATE_CHANGED == type);
+ chromeos::NetworkStateDetails* state_details =
+ Details<chromeos::NetworkStateDetails>(details).ptr();
+ state_ = state_details->state();
+ notification_received_ = true;
+ }
+
+ void WaitForNotification() {
+ notification_received_ = false;
+ while (!notification_received_) {
+ ui_test_utils::RunAllPendingInMessageLoop();
+ }
+ }
+
+ protected:
+ NetworkStateDetails::State state_;
+ bool notification_received_;
+};
+
+IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnected) {
+ // NETWORK_STATE_CHAGNED has to be registered in UI thread.
+ NotificationRegistrar registrar;
+ registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED,
+ NotificationService::AllSources());
+ EXPECT_CALL(*mock_network_library_, Connected())
+ .Times(1)
+ .WillRepeatedly((Return(true)))
+ .RetiresOnSaturation();
+ NetworkStateNotifier* notifier = NetworkStateNotifier::Get();
+ DCHECK(CrosLibrary::Get()->EnsureLoaded());
+ notifier->NetworkChanged(mock_network_library_);
+ WaitForNotification();
+ EXPECT_EQ(chromeos::NetworkStateDetails::CONNECTED, state_);
+}
+
+IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnecting) {
+ NotificationRegistrar registrar;
+ registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED,
+ NotificationService::AllSources());
+ EXPECT_CALL(*mock_network_library_, Connected())
+ .Times(1)
+ .WillOnce((Return(false)))
+ .RetiresOnSaturation();
+ EXPECT_CALL(*mock_network_library_, Connecting())
+ .Times(1)
+ .WillOnce((Return(true)))
+ .RetiresOnSaturation();
+ NetworkStateNotifier* notifier = NetworkStateNotifier::Get();
+ DCHECK(CrosLibrary::Get()->EnsureLoaded());
+ notifier->NetworkChanged(mock_network_library_);
+ WaitForNotification();
+ EXPECT_EQ(chromeos::NetworkStateDetails::CONNECTING, state_);
+}
+
+IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestDisconnected) {
+ NotificationRegistrar registrar;
+ registrar.Add(this, NotificationType::NETWORK_STATE_CHANGED,
+ NotificationService::AllSources());
+ EXPECT_CALL(*mock_network_library_, Connected())
+ .Times(1)
+ .WillOnce((Return(false)))
+ .RetiresOnSaturation();
+ EXPECT_CALL(*mock_network_library_, Connecting())
+ .Times(1)
+ .WillOnce((Return(false)))
+ .RetiresOnSaturation();
+ NetworkStateNotifier* notifier = NetworkStateNotifier::Get();
+ DCHECK(CrosLibrary::Get()->EnsureLoaded());
+ notifier->NetworkChanged(mock_network_library_);
+ WaitForNotification();
+ EXPECT_EQ(chromeos::NetworkStateDetails::DISCONNECTED, state_);
+}
+
+} // namespace chromeos
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index d75ab5a..809a79e 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -501,6 +501,8 @@
'browser/chromeos/native_dialog_window.h',
'browser/chromeos/network_list.cc',
'browser/chromeos/network_list.h',
+ 'browser/chromeos/network_state_notifier.cc',
+ 'browser/chromeos/network_state_notifier.h',
'browser/chromeos/network_message_observer.cc',
'browser/chromeos/network_message_observer.h',
'browser/chromeos/options/internet_page_view.cc',
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index c95fecc..72a39af 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -1366,6 +1366,7 @@
'browser/chromeos/login/wizard_controller_browsertest.cc',
'browser/chromeos/login/wizard_in_process_browser_test.cc',
'browser/chromeos/login/wizard_in_process_browser_test.h',
+ 'browser/chromeos/network_state_notifier_browsertest.cc',
'browser/chromeos/notifications/notification_browsertest.cc',
'browser/chromeos/options/wifi_config_view_browsertest.cc',
'browser/chromeos/panels/panel_browsertest.cc',
diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h
index 1e49b97..cd97990 100644
--- a/chrome/common/notification_type.h
+++ b/chrome/common/notification_type.h
@@ -1037,6 +1037,12 @@ class NotificationType {
// is being deleted, so the receiver shouldn't use the screen locker
// object.
SCREEN_LOCK_STATE_CHANGED,
+
+ // Sent when the network state has changed on UI thread.
+ // The source is AllSources and the details is NetworkStateDetails defined
+ // in chrome/browser/chromeos/network_state_notifier.h.
+ // TODO(oshima): Port this to all platforms.
+ NETWORK_STATE_CHANGED,
#endif
// Sent before the repost form warning is brought up.