network_list_view_controller_impl.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2022 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. #ifndef ASH_SYSTEM_NETWORK_NETWORK_LIST_VIEW_CONTROLLER_IMPL_H_
  5. #define ASH_SYSTEM_NETWORK_NETWORK_LIST_VIEW_CONTROLLER_IMPL_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/system/network/network_detailed_network_view_impl.h"
  8. #include "ash/system/network/network_list_mobile_header_view.h"
  9. #include "ash/system/network/network_list_network_header_view.h"
  10. #include "ash/system/network/network_list_network_item_view.h"
  11. #include "ash/system/network/network_list_view_controller.h"
  12. #include "ash/system/network/network_list_wifi_header_view.h"
  13. #include "ash/system/network/tray_network_state_observer.h"
  14. #include "ash/system/tray/tray_info_label.h"
  15. #include "ash/system/tray/tray_popup_utils.h"
  16. #include "ash/system/tray/tray_utils.h"
  17. #include "ash/system/tray/tri_view.h"
  18. #include "base/timer/timer.h"
  19. #include "chromeos/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
  20. #include "mojo/public/cpp/bindings/receiver.h"
  21. #include "mojo/public/cpp/bindings/remote.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. #include "ui/views/controls/separator.h"
  24. namespace ash {
  25. class NetworkDetailedNetworkView;
  26. // Implementation of NetworkListViewController.
  27. class ASH_EXPORT NetworkListViewControllerImpl
  28. : public TrayNetworkStateObserver,
  29. public NetworkListViewController,
  30. public chromeos::bluetooth_config::mojom::SystemPropertiesObserver {
  31. public:
  32. NetworkListViewControllerImpl(
  33. NetworkDetailedNetworkView* network_detailed_network_view);
  34. NetworkListViewControllerImpl(const NetworkListViewController&) = delete;
  35. NetworkListViewControllerImpl& operator=(
  36. const NetworkListViewControllerImpl&) = delete;
  37. ~NetworkListViewControllerImpl() override;
  38. protected:
  39. TrayNetworkStateModel* model() { return model_; }
  40. NetworkDetailedNetworkView* network_detailed_network_view() {
  41. return network_detailed_network_view_;
  42. }
  43. private:
  44. friend class NetworkListViewControllerTest;
  45. friend class FakeNetworkDetailedNetworkView;
  46. // Used for testing. Starts at 11 to avoid collision with header view
  47. // child elements.
  48. enum class NetworkListViewControllerViewChildId {
  49. kConnectionWarning = 11,
  50. kConnectionWarningLabel = 12,
  51. kMobileSeperator = 13,
  52. kMobileStatusMessage = 14,
  53. kMobileSectionHeader = 15,
  54. kWifiSeperator = 16,
  55. kWifiSectionHeader = 17,
  56. kWifiStatusMessage = 18,
  57. };
  58. // Map of network guids and their corresponding list item views.
  59. using NetworkIdToViewMap =
  60. base::flat_map<std::string, NetworkListNetworkItemView*>;
  61. // TrayNetworkStateObserver:
  62. void ActiveNetworkStateChanged() override;
  63. void NetworkListChanged() override;
  64. void GlobalPolicyChanged() override;
  65. // chromeos::bluetooth_config::mojom::SystemPropertiesObserver:
  66. void OnPropertiesUpdated(
  67. chromeos::bluetooth_config::mojom::BluetoothSystemPropertiesPtr
  68. properties) override;
  69. // Called to initialize views and when network list is recently updated.
  70. void GetNetworkStateList();
  71. void OnGetNetworkStateList(
  72. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  73. networks);
  74. // Checks |networks| and caches whether Mobile network, WiFi networks and vpn
  75. // networks exist in the list of |networks|. Also caches if a Mobile and
  76. // WiFi networks are enabled.
  77. void UpdateNetworkTypeExistence(
  78. const std::vector<
  79. chromeos::network_config::mojom::NetworkStatePropertiesPtr>&
  80. networks);
  81. // Adds a warning indicator if connected to a VPN or if the default network
  82. // has a proxy installed.
  83. size_t ShowConnectionWarningIfVpnOrProxy(size_t index);
  84. // Returns true if mobile data section should be added to view.
  85. bool ShouldMobileDataSectionBeShown();
  86. // Creates if missing and adds a Mobile or Wifi separator to the view.
  87. // Also reorders separator view in network list. A reference to the
  88. // separator is captured in |*separator_view|.
  89. size_t CreateSeparatorIfMissingAndReorder(size_t index,
  90. views::Separator** separator_view);
  91. // Updates Mobile data section, updates add eSIM button states and
  92. // calls UpdateMobileToggleAndSetStatusMessage().
  93. void UpdateMobileSection();
  94. // Updates the WiFi data section. This method creates a new header if one does
  95. // not exist, and will update both the WiFi toggle and "add network" button.
  96. // If there are no WiFi networks or WiFi is disabled, this method will also
  97. // add an info message.
  98. void UpdateWifiSection();
  99. // Updated mobile data toggle states and sets mobile data status message.
  100. void UpdateMobileToggleAndSetStatusMessage();
  101. // Creates an info label if missing and updates info label message.
  102. void CreateInfoLabelIfMissingAndUpdate(int message_id,
  103. TrayInfoLabel** info_label_ptr);
  104. // Creates a NetworkListNetworkItem if it does not exist else uses the
  105. // existing view, also reorders it in NetworkDetailedNetworkView scroll list.
  106. size_t CreateItemViewsIfMissingAndReorder(
  107. chromeos::network_config::mojom::NetworkType type,
  108. size_t index,
  109. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>&
  110. networks,
  111. NetworkIdToViewMap* previous_views);
  112. // Creates a view that indicates connections might be monitored if
  113. // connected to a VPN or if the default network has a proxy installed.
  114. void ShowConnectionWarning();
  115. // Determines whether a scan for WiFi and Tether networks should be requested
  116. // and updates the scanning bar accordingly.
  117. void UpdateScanningBarAndTimer();
  118. // Calls RequestScan() and starts a timer that will repeatedly call
  119. // RequestScan() after a delay.
  120. void ScanAndStartTimer();
  121. // Immediately request a WiFi and Tether network scan.
  122. void RequestScan();
  123. // Focuses on last selected view in NetworkDetailedNetworkView scroll list.
  124. void FocusLastSelectedView();
  125. TrayNetworkStateModel* model_;
  126. mojo::Remote<chromeos::bluetooth_config::mojom::CrosBluetoothConfig>
  127. remote_cros_bluetooth_config_;
  128. mojo::Receiver<chromeos::bluetooth_config::mojom::SystemPropertiesObserver>
  129. cros_system_properties_observer_receiver_{this};
  130. chromeos::bluetooth_config::mojom::BluetoothSystemState
  131. bluetooth_system_state_ =
  132. chromeos::bluetooth_config::mojom::BluetoothSystemState::kUnavailable;
  133. TrayInfoLabel* mobile_status_message_ = nullptr;
  134. NetworkListMobileHeaderView* mobile_header_view_ = nullptr;
  135. views::Separator* mobile_separator_view_ = nullptr;
  136. TriView* connection_warning_ = nullptr;
  137. NetworkListWifiHeaderView* wifi_header_view_ = nullptr;
  138. views::Separator* wifi_separator_view_ = nullptr;
  139. TrayInfoLabel* wifi_status_message_ = nullptr;
  140. bool has_mobile_networks_;
  141. bool has_wifi_networks_;
  142. bool is_vpn_connected_;
  143. bool is_mobile_network_enabled_;
  144. bool is_wifi_enabled_;
  145. NetworkDetailedNetworkView* network_detailed_network_view_;
  146. NetworkIdToViewMap network_id_to_view_map_;
  147. // Timer for repeatedly requesting network scans with a delay between
  148. // requests.
  149. base::RepeatingTimer network_scan_repeating_timer_;
  150. base::WeakPtrFactory<NetworkListViewControllerImpl> weak_ptr_factory_{this};
  151. };
  152. } // namespace ash
  153. #endif // ASH_SYSTEM_NETWORK_NETWORK_LIST_VIEW_CONTROLLER_IMPL_H_