tray_network_state_model.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2012 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_TRAY_NETWORK_STATE_MODEL_H_
  5. #define ASH_SYSTEM_NETWORK_TRAY_NETWORK_STATE_MODEL_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "ash/system/network/tray_network_state_observer.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/observer_list.h"
  12. #include "base/timer/timer.h"
  13. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom-forward.h"
  14. #include "chromeos/services/network_config/public/mojom/network_types.mojom-forward.h"
  15. #include "mojo/public/cpp/bindings/struct_ptr.h"
  16. namespace ash {
  17. class VpnList;
  18. // TrayNetworkStateModel observes the mojo interface and tracks the devices
  19. // and active networks. It has UI observers that are informed when important
  20. // changes occur.
  21. class ASH_EXPORT TrayNetworkStateModel {
  22. public:
  23. TrayNetworkStateModel();
  24. TrayNetworkStateModel(const TrayNetworkStateModel&) = delete;
  25. TrayNetworkStateModel& operator=(const TrayNetworkStateModel&) = delete;
  26. ~TrayNetworkStateModel();
  27. void AddObserver(TrayNetworkStateObserver* observer);
  28. void RemoveObserver(TrayNetworkStateObserver* observer);
  29. // Returns DeviceStateProperties for |type| if it exists or null.
  30. const chromeos::network_config::mojom::DeviceStateProperties* GetDevice(
  31. chromeos::network_config::mojom::NetworkType type) const;
  32. // Returns the DeviceStateType for |type| if a device exists or kUnavailable.
  33. chromeos::network_config::mojom::DeviceStateType GetDeviceState(
  34. chromeos::network_config::mojom::NetworkType type) const;
  35. // Convenience method to call the |remote_cros_network_config_| method.
  36. void SetNetworkTypeEnabledState(
  37. chromeos::network_config::mojom::NetworkType type,
  38. bool enabled);
  39. // Returns true if built-in VPN is prohibited.
  40. // Note: Currently only built-in VPNs can be prohibited by policy.
  41. bool IsBuiltinVpnProhibited() const;
  42. // Used in tests, updates currently cached global policies.
  43. void FlushGlobalPolicyForTesting();
  44. // This used to be inlined but now requires details from the Impl class.
  45. chromeos::network_config::mojom::CrosNetworkConfig* cros_network_config();
  46. const chromeos::network_config::mojom::NetworkStateProperties*
  47. default_network() const {
  48. return default_network_.get();
  49. }
  50. const chromeos::network_config::mojom::NetworkStateProperties*
  51. active_non_cellular() const {
  52. return active_non_cellular_.get();
  53. }
  54. const chromeos::network_config::mojom::NetworkStateProperties*
  55. active_cellular() const {
  56. return active_cellular_.get();
  57. }
  58. const chromeos::network_config::mojom::NetworkStateProperties* active_vpn()
  59. const {
  60. return active_vpn_.get();
  61. }
  62. bool has_vpn() const { return has_vpn_; }
  63. VpnList* vpn_list() { return vpn_list_.get(); }
  64. const chromeos::network_config::mojom::GlobalPolicy* global_policy() {
  65. return global_policy_.get();
  66. }
  67. private:
  68. void OnGetDeviceStateList(
  69. std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
  70. devices);
  71. void UpdateActiveNetworks(
  72. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  73. networks);
  74. void OnGetVirtualNetworks(
  75. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  76. networks);
  77. void OnGetGlobalPolicy(
  78. chromeos::network_config::mojom::GlobalPolicyPtr global_policy);
  79. void NotifyNetworkListChanged();
  80. void NotifyGlobalPolicyChanged();
  81. void NotifyVpnProvidersChanged();
  82. void SendActiveNetworkStateChanged();
  83. void SendNetworkListChanged();
  84. void SendDeviceStateListChanged();
  85. class Impl;
  86. std::unique_ptr<Impl> impl_;
  87. base::ObserverList<TrayNetworkStateObserver> observer_list_;
  88. // Frequency at which to push NetworkListChanged updates. This avoids
  89. // unnecessarily frequent UI updates (which can be expensive). We set this
  90. // to 0 for tests to eliminate timing variance.
  91. int update_frequency_;
  92. // Timer used to limit the frequency of NetworkListChanged updates.
  93. base::OneShotTimer timer_;
  94. base::flat_map<chromeos::network_config::mojom::NetworkType,
  95. chromeos::network_config::mojom::DeviceStatePropertiesPtr>
  96. devices_;
  97. chromeos::network_config::mojom::NetworkStatePropertiesPtr default_network_;
  98. chromeos::network_config::mojom::NetworkStatePropertiesPtr
  99. active_non_cellular_;
  100. chromeos::network_config::mojom::NetworkStatePropertiesPtr active_cellular_;
  101. chromeos::network_config::mojom::NetworkStatePropertiesPtr active_vpn_;
  102. chromeos::network_config::mojom::GlobalPolicyPtr global_policy_;
  103. bool has_vpn_ = false;
  104. std::unique_ptr<VpnList> vpn_list_;
  105. };
  106. } // namespace ash
  107. #endif // ASH_SYSTEM_NETWORK_TRAY_NETWORK_STATE_MODEL_H_