network_connection.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2015 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 NET_QUIC_NETWORK_CONNECTION_H_
  5. #define NET_QUIC_NETWORK_CONNECTION_H_
  6. #include "net/base/net_export.h"
  7. #include "net/base/network_change_notifier.h"
  8. namespace net {
  9. // This class stores information about the current network type and
  10. // provides a textual description of it.
  11. class NET_EXPORT NetworkConnection
  12. : public NetworkChangeNotifier::IPAddressObserver,
  13. public NetworkChangeNotifier::ConnectionTypeObserver {
  14. public:
  15. NetworkConnection();
  16. NetworkConnection(const NetworkConnection&) = delete;
  17. NetworkConnection& operator=(const NetworkConnection&) = delete;
  18. ~NetworkConnection() override;
  19. // Returns the underlying connection type.
  20. NetworkChangeNotifier::ConnectionType connection_type() {
  21. return connection_type_;
  22. }
  23. // Return a string equivalent of current connection type. Callers don't need
  24. // to make a copy of the returned C-string value. If the connection type is
  25. // CONNECTION_WIFI, then we'll tease out some details when we are on WiFi, and
  26. // hopefully leave only ethernet (with no WiFi available) in the
  27. // CONNECTION_UNKNOWN category. This *might* err if there is both ethernet,
  28. // as well as WiFi, where WiFi was not being used that much. Most platforms
  29. // don't distinguish Wifi vs Etherenet, and call everything CONNECTION_UNKNOWN
  30. // :-(. Fo non CONNECTIION_WIFI, this returns the C-string returned by
  31. // NetworkChangeNotifier::ConnectionTypeToString.
  32. const char* connection_description() { return connection_description_; }
  33. // NetworkChangeNotifier::IPAddressObserver methods:
  34. void OnIPAddressChanged() override;
  35. // NetworkChangeNotifier::ConnectionTypeObserver methods:
  36. void OnConnectionTypeChanged(
  37. NetworkChangeNotifier::ConnectionType type) override;
  38. private:
  39. // Cache the connection type to avoid calling the potentially expensive
  40. // NetworkChangeNotifier::GetConnectionType() function.
  41. NetworkChangeNotifier::ConnectionType connection_type_ =
  42. NetworkChangeNotifier::CONNECTION_UNKNOWN;
  43. // Cache the connection description string to avoid calling the expensive
  44. // GetWifiPHYLayerProtocol() function.
  45. const char* connection_description_ = nullptr;
  46. };
  47. } // namespace net
  48. #endif // NET_QUIC_NETWORK_CONNECTION_H_