network_change_manager.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 SERVICES_NETWORK_NETWORK_CHANGE_MANAGER_H_
  5. #define SERVICES_NETWORK_NETWORK_CHANGE_MANAGER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "build/build_config.h"
  10. #include "build/chromeos_buildflags.h"
  11. #include "mojo/public/cpp/bindings/receiver_set.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "net/base/network_change_notifier.h"
  14. #include "services/network/public/mojom/network_change_manager.mojom.h"
  15. namespace network {
  16. // Implementation of mojom::NetworkChangeManager. All accesses to this class are
  17. // done through mojo on the main thread. This registers itself to receive
  18. // broadcasts from net::NetworkChangeNotifier and rebroadcasts the notifications
  19. // to mojom::NetworkChangeManagerClients through mojo pipes.
  20. class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkChangeManager
  21. : public mojom::NetworkChangeManager,
  22. public net::NetworkChangeNotifier::NetworkChangeObserver {
  23. public:
  24. // If |network_change_notifier| is not null, |this| will take ownership of it.
  25. // Otherwise, the global net::NetworkChangeNotifier will be used.
  26. explicit NetworkChangeManager(
  27. std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier);
  28. NetworkChangeManager(const NetworkChangeManager&) = delete;
  29. NetworkChangeManager& operator=(const NetworkChangeManager&) = delete;
  30. ~NetworkChangeManager() override;
  31. // Binds a NetworkChangeManager receiver to this object. Mojo messages
  32. // coming through the associated pipe will be served by this object.
  33. void AddReceiver(mojo::PendingReceiver<mojom::NetworkChangeManager> receiver);
  34. // mojom::NetworkChangeManager implementation:
  35. void RequestNotifications(
  36. mojo::PendingRemote<mojom::NetworkChangeManagerClient> client_remote)
  37. override;
  38. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  39. void OnNetworkChanged(
  40. bool dns_changed,
  41. bool ip_address_changed,
  42. bool connection_type_changed,
  43. mojom::ConnectionType new_connection_type,
  44. bool connection_subtype_changed,
  45. mojom::ConnectionSubtype new_connection_subtype) override;
  46. #endif
  47. size_t GetNumClientsForTesting() const;
  48. private:
  49. // Handles connection errors on notification pipes.
  50. void NotificationPipeBroken(mojom::NetworkChangeManagerClient* client);
  51. // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
  52. void OnNetworkChanged(
  53. net::NetworkChangeNotifier::ConnectionType type) override;
  54. std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
  55. mojo::ReceiverSet<mojom::NetworkChangeManager> receivers_;
  56. std::vector<mojo::Remote<mojom::NetworkChangeManagerClient>> clients_;
  57. mojom::ConnectionType connection_type_;
  58. };
  59. } // namespace network
  60. #endif // SERVICES_NETWORK_NETWORK_CHANGE_MANAGER_H_