[Merge to M65]Update wifi network immediately when it's disabled

TBR=stevenjb@chromium.org
(cherry picked from commit 50ef07e85110f261574c300e8c236274ea05797f)

Bug: 798325
Change-Id: Icc0e4a8e6e61b01e5736d52e65632b3e47a1f0d6
Reviewed-on: https://chromium-review.googlesource.com/936003
Commit-Queue: Xiaoyin Hu <xiaoyinh@chromium.org>
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#539290}
Reviewed-on: https://chromium-review.googlesource.com/940767
Reviewed-by: Xiaoyin Hu <xiaoyinh@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#621}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/ash/system/network/network_list.cc b/ash/system/network/network_list.cc
index 9ad3ede..2aee2ef 100644
--- a/ash/system/network/network_list.cc
+++ b/ash/system/network/network_list.cc
@@ -700,14 +700,17 @@
       &wifi_header_view_, &wifi_separator_view_);
 
   // "Wifi Enabled / Disabled".
-  int wifi_message_id = 0;
-  if (!handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()))
-    wifi_message_id = IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED;
-  else if (!handler->FirstNetworkByType(NetworkTypePattern::WiFi()))
-    wifi_message_id = IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED;
-  UpdateInfoLabel(wifi_message_id, index, &no_wifi_networks_view_);
-  if (wifi_message_id)
+  if (!handler->IsTechnologyEnabled(NetworkTypePattern::WiFi())) {
+    UpdateInfoLabel(IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED, index,
+                    &no_wifi_networks_view_);
+    return new_guids;
+  }
+
+  if (!handler->FirstNetworkByType(NetworkTypePattern::WiFi())) {
+    UpdateInfoLabel(IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED, index,
+                    &no_wifi_networks_view_);
     ++index;
+  }
 
   // Add Wi-Fi networks.
   std::unique_ptr<std::set<std::string>> new_wifi_guids =
diff --git a/ash/system/network/tray_network_state_observer.cc b/ash/system/network/tray_network_state_observer.cc
index 044de08..270ec12 100644
--- a/ash/system/network/tray_network_state_observer.cc
+++ b/ash/system/network/tray_network_state_observer.cc
@@ -21,6 +21,11 @@
 
 const int kUpdateFrequencyMs = 1000;
 
+bool IsWifiEnabled() {
+  return NetworkHandler::Get()->network_state_handler()->IsTechnologyEnabled(
+      chromeos::NetworkTypePattern::WiFi());
+}
+
 }  // namespace
 
 namespace ash {
@@ -36,6 +41,7 @@
   if (NetworkHandler::IsInitialized()) {
     NetworkHandler::Get()->network_state_handler()->AddObserver(this,
                                                                 FROM_HERE);
+    wifi_enabled_ = IsWifiEnabled();
   }
 }
 
@@ -84,6 +90,15 @@
 }
 
 void TrayNetworkStateObserver::SignalUpdate(bool notify_a11y) {
+  bool old_state = wifi_enabled_;
+  wifi_enabled_ = IsWifiEnabled();
+
+  // Update immediately when wifi network changed from enabled->disabled.
+  if (old_state && !wifi_enabled_) {
+    SendNetworkStateChanged(notify_a11y);
+    return;
+  }
+
   if (timer_.IsRunning())
     return;
   timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(update_frequency_),
diff --git a/ash/system/network/tray_network_state_observer.h b/ash/system/network/tray_network_state_observer.h
index 1991281..8810399 100644
--- a/ash/system/network/tray_network_state_observer.h
+++ b/ash/system/network/tray_network_state_observer.h
@@ -54,6 +54,10 @@
   // Timer used to limit the frequency of NetworkStateChanged updates.
   base::OneShotTimer timer_;
 
+  // The previous state of the wifi network, used to immediately send
+  // NetworkStateChanged update when wifi changed from enabled->disabled.
+  bool wifi_enabled_ = false;
+
   DISALLOW_COPY_AND_ASSIGN(TrayNetworkStateObserver);
 };