network_status_listener.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2017 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 COMPONENTS_DOWNLOAD_NETWORK_NETWORK_STATUS_LISTENER_H_
  5. #define COMPONENTS_DOWNLOAD_NETWORK_NETWORK_STATUS_LISTENER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "services/network/public/mojom/network_change_manager.mojom.h"
  8. namespace download {
  9. // Monitor and propagate network status change events.
  10. // Base class only manages the observer pointer, derived class should override
  11. // to provide actual network hook to monitor the changes, and call base class
  12. // virtual functions.
  13. class NetworkStatusListener {
  14. public:
  15. // Observer to receive network connection type change notifications.
  16. class Observer {
  17. public:
  18. // Called after the NetworkStatusListener is initialized and ready to use.
  19. virtual void OnNetworkStatusReady(network::mojom::ConnectionType type) = 0;
  20. // Called when the network type is changed.
  21. virtual void OnNetworkChanged(network::mojom::ConnectionType type) = 0;
  22. Observer() = default;
  23. Observer(const Observer&) = delete;
  24. Observer& operator=(const Observer&) = delete;
  25. protected:
  26. virtual ~Observer() {}
  27. };
  28. NetworkStatusListener(const NetworkStatusListener&) = delete;
  29. NetworkStatusListener& operator=(const NetworkStatusListener&) = delete;
  30. virtual ~NetworkStatusListener();
  31. // Starts to listen to network changes.
  32. virtual void Start(Observer* observer) = 0;
  33. // Stops to listen to network changes.
  34. virtual void Stop() = 0;
  35. // Gets the current connection type.
  36. virtual network::mojom::ConnectionType GetConnectionType() = 0;
  37. protected:
  38. NetworkStatusListener();
  39. // The only observer that listens to connection type change. Must outlive this
  40. // class.
  41. raw_ptr<Observer> observer_ = nullptr;
  42. // The current network status.
  43. network::mojom::ConnectionType connection_type_ =
  44. network::mojom::ConnectionType::CONNECTION_UNKNOWN;
  45. };
  46. } // namespace download
  47. #endif // COMPONENTS_DOWNLOAD_NETWORK_NETWORK_STATUS_LISTENER_H_