Commit 20743072 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Convert WifiToggleNotificationController to network_config.mojom

Bug: 862420
Change-Id: I9efda6eb3732ee36a82589e680b0455a4797d9e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1628811
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664446}
parent a02bfd46
......@@ -77,6 +77,9 @@ class ASH_EXPORT TrayNetworkStateModel
}
private:
// For BindCrosNetworkConfig() calls from tests to override the connector:
friend class WifiToggleNotificationControllerTest;
// CrosNetworkConfigObserver
void OnActiveNetworksChanged(
std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>)
......@@ -84,7 +87,10 @@ class ASH_EXPORT TrayNetworkStateModel
void OnNetworkStateListChanged() override;
void OnDeviceStateListChanged() override;
// Binds the network_config interface to |connector| and sets up the observer.
// Also requests the initial state to set the cached properties.
void BindCrosNetworkConfig(service_manager::Connector* connector);
void GetDeviceStateList();
void OnGetDeviceStateList(
std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
......
......@@ -6,16 +6,17 @@
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/network/network_icon.h"
#include "ash/system/network/tray_network_state_model.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "chromeos/network/network_state_handler.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
using chromeos::NetworkHandler;
using chromeos::NetworkStateHandler;
using chromeos::NetworkTypePattern;
using chromeos::network_config::mojom::DeviceStateProperties;
using chromeos::network_config::mojom::DeviceStateType;
using chromeos::network_config::mojom::NetworkType;
using message_center::Notification;
namespace ash {
......@@ -51,17 +52,25 @@ WifiToggleNotificationController::~WifiToggleNotificationController() {
}
void WifiToggleNotificationController::RequestToggleWifi() {
// This will always be triggered by a user action (e.g. keyboard shortcut)
NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
bool enabled = handler->IsTechnologyEnabled(NetworkTypePattern::WiFi());
Shell::Get()->metrics()->RecordUserMetricsAction(
enabled ? UMA_STATUS_AREA_DISABLE_WIFI : UMA_STATUS_AREA_ENABLE_WIFI);
handler->SetTechnologyEnabled(NetworkTypePattern::WiFi(), !enabled,
chromeos::network_handler::ErrorCallback());
message_center::MessageCenter* message_center =
message_center::MessageCenter::Get();
// Remove any existing notification.
if (message_center->FindVisibleNotificationById(kWifiToggleNotificationId))
message_center->RemoveNotification(kWifiToggleNotificationId, false);
TrayNetworkStateModel* model =
Shell::Get()->system_tray_model()->network_state_model();
const DeviceStateProperties* wifi = model->GetDevice(NetworkType::kWiFi);
// A WiFi device should always exist, but the model is not part of Shell
// so just return to handle the edge case.
if (!wifi)
return;
bool enabled = wifi->state == DeviceStateType::kEnabled;
Shell::Get()->metrics()->RecordUserMetricsAction(
enabled ? UMA_STATUS_AREA_DISABLE_WIFI : UMA_STATUS_AREA_ENABLE_WIFI);
model->SetNetworkTypeEnabledState(NetworkType::kWiFi, !enabled);
// Create a new notification with the new state.
message_center->AddNotification(CreateNotification(!enabled));
}
......
......@@ -5,10 +5,13 @@
#include "ash/system/network/wifi_toggle_notification_controller.h"
#include "ash/shell.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/network/tray_network_state_model.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/test/ash_test_base.h"
#include "chromeos/dbus/shill/shill_clients.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "components/prefs/testing_pref_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
......@@ -24,25 +27,18 @@ class WifiToggleNotificationControllerTest : public AshTestBase {
// testing::Test:
void SetUp() override {
chromeos::shill_clients::InitializeFakes();
// Initializing NetworkHandler before ash is more like production.
chromeos::NetworkHandler::Initialize();
AshTestBase::SetUp();
chromeos::NetworkHandler::Get()->InitializePrefServices(&profile_prefs_,
&local_state_);
// Networking stubs may have asynchronous initialization.
// Override the TrayNetworkStateModel connector with the one in
// network_config_helper_.
Shell::Get()
->system_tray_model()
->network_state_model()
->BindCrosNetworkConfig(network_config_helper_.connector());
base::RunLoop().RunUntilIdle();
}
void TearDown() override {
// This roughly matches production shutdown order.
chromeos::NetworkHandler::Get()->ShutdownPrefServices();
AshTestBase::TearDown();
chromeos::NetworkHandler::Shutdown();
chromeos::shill_clients::Shutdown();
}
private:
chromeos::network_config::CrosNetworkConfigTestHelper network_config_helper_;
TestingPrefServiceSimple profile_prefs_;
TestingPrefServiceSimple local_state_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment