wifi_toggle_notification_controller.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/network/wifi_toggle_notification_controller.h"
  5. #include "ash/shell.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/system/model/system_tray_model.h"
  8. #include "ash/system/network/network_icon.h"
  9. #include "ash/system/network/tray_network_state_model.h"
  10. #include "ash/system/tray/system_tray_notifier.h"
  11. #include "base/metrics/user_metrics.h"
  12. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  13. #include "ui/base/l10n/l10n_util.h"
  14. #include "ui/message_center/message_center.h"
  15. #include "ui/message_center/public/cpp/notification.h"
  16. using chromeos::network_config::mojom::DeviceStateProperties;
  17. using chromeos::network_config::mojom::DeviceStateType;
  18. using chromeos::network_config::mojom::NetworkType;
  19. using message_center::Notification;
  20. namespace ash {
  21. namespace {
  22. constexpr char kWifiToggleNotificationId[] = "wifi-toggle";
  23. constexpr char kNotifierWifiToggle[] = "ash.wifi-toggle";
  24. std::unique_ptr<Notification> CreateNotification(bool wifi_enabled) {
  25. const int string_id = wifi_enabled
  26. ? IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED
  27. : IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED;
  28. std::unique_ptr<Notification> notification = std::make_unique<Notification>(
  29. message_center::NOTIFICATION_TYPE_SIMPLE, kWifiToggleNotificationId,
  30. std::u16string(), l10n_util::GetStringUTF16(string_id),
  31. ui::ImageModel::FromImageSkia(
  32. network_icon::GetImageForWiFiEnabledState(wifi_enabled)),
  33. std::u16string() /* display_source */, GURL(),
  34. message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
  35. kNotifierWifiToggle,
  36. NotificationCatalogName::kWifiToggle),
  37. message_center::RichNotificationData(), nullptr);
  38. return notification;
  39. }
  40. } // namespace
  41. WifiToggleNotificationController::WifiToggleNotificationController() {
  42. Shell::Get()->system_tray_notifier()->AddNetworkObserver(this);
  43. }
  44. WifiToggleNotificationController::~WifiToggleNotificationController() {
  45. Shell::Get()->system_tray_notifier()->RemoveNetworkObserver(this);
  46. }
  47. void WifiToggleNotificationController::RequestToggleWifi() {
  48. message_center::MessageCenter* message_center =
  49. message_center::MessageCenter::Get();
  50. // Remove any existing notification.
  51. if (message_center->FindVisibleNotificationById(kWifiToggleNotificationId))
  52. message_center->RemoveNotification(kWifiToggleNotificationId, false);
  53. TrayNetworkStateModel* model =
  54. Shell::Get()->system_tray_model()->network_state_model();
  55. const DeviceStateProperties* wifi = model->GetDevice(NetworkType::kWiFi);
  56. // A WiFi device should always exist, but the model is not part of Shell
  57. // so just return to handle the edge case.
  58. if (!wifi)
  59. return;
  60. bool enabled = wifi->device_state == DeviceStateType::kEnabled;
  61. base::RecordAction(
  62. enabled ? base::UserMetricsAction("StatusArea_Network_WifiDisabled")
  63. : base::UserMetricsAction("StatusArea_Network_WifiEnabled"));
  64. model->SetNetworkTypeEnabledState(NetworkType::kWiFi, !enabled);
  65. // Create a new notification with the new state.
  66. message_center->AddNotification(CreateNotification(!enabled));
  67. }
  68. } // namespace ash