network_change_manager.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "services/network/network_change_manager.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "build/build_config.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "mojo/public/cpp/bindings/pending_remote.h"
  11. #include "net/base/network_change_notifier.h"
  12. #include "net/base/network_change_notifier_posix.h"
  13. namespace network {
  14. NetworkChangeManager::NetworkChangeManager(
  15. std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier)
  16. : network_change_notifier_(std::move(network_change_notifier)) {
  17. net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
  18. connection_type_ =
  19. mojom::ConnectionType(net::NetworkChangeNotifier::GetConnectionType());
  20. }
  21. NetworkChangeManager::~NetworkChangeManager() {
  22. net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
  23. }
  24. void NetworkChangeManager::AddReceiver(
  25. mojo::PendingReceiver<mojom::NetworkChangeManager> receiver) {
  26. receivers_.Add(this, std::move(receiver));
  27. }
  28. void NetworkChangeManager::RequestNotifications(
  29. mojo::PendingRemote<mojom::NetworkChangeManagerClient>
  30. client_pending_remote) {
  31. mojo::Remote<mojom::NetworkChangeManagerClient> client_remote(
  32. std::move(client_pending_remote));
  33. client_remote.set_disconnect_handler(base::BindOnce(
  34. &NetworkChangeManager::NotificationPipeBroken,
  35. // base::Unretained is safe as destruction of the
  36. // NetworkChangeManager will also destroy the
  37. // |clients_| list (which this object will be
  38. // inserted into, below), which will destroy the
  39. // client_remote, rendering this callback moot.
  40. base::Unretained(this), base::Unretained(client_remote.get())));
  41. client_remote->OnInitialConnectionType(connection_type_);
  42. clients_.push_back(std::move(client_remote));
  43. }
  44. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  45. void NetworkChangeManager::OnNetworkChanged(
  46. bool dns_changed,
  47. bool ip_address_changed,
  48. bool connection_type_changed,
  49. mojom::ConnectionType new_connection_type,
  50. bool connection_subtype_changed,
  51. mojom::ConnectionSubtype new_connection_subtype) {
  52. // network_change_notifier_ can be null in unit tests.
  53. if (!network_change_notifier_)
  54. return;
  55. net::NetworkChangeNotifierPosix* notifier =
  56. static_cast<net::NetworkChangeNotifierPosix*>(
  57. network_change_notifier_.get());
  58. if (dns_changed)
  59. notifier->OnDNSChanged();
  60. if (ip_address_changed)
  61. notifier->OnIPAddressChanged();
  62. if (connection_type_changed) {
  63. notifier->OnConnectionChanged(
  64. net::NetworkChangeNotifier::ConnectionType(new_connection_type));
  65. }
  66. if (connection_type_changed || connection_subtype_changed) {
  67. notifier->OnConnectionSubtypeChanged(
  68. net::NetworkChangeNotifier::ConnectionType(new_connection_type),
  69. net::NetworkChangeNotifier::ConnectionSubtype(new_connection_subtype));
  70. }
  71. }
  72. #endif
  73. size_t NetworkChangeManager::GetNumClientsForTesting() const {
  74. return clients_.size();
  75. }
  76. void NetworkChangeManager::NotificationPipeBroken(
  77. mojom::NetworkChangeManagerClient* client) {
  78. clients_.erase(std::find_if(
  79. clients_.begin(), clients_.end(),
  80. [client](mojo::Remote<mojom::NetworkChangeManagerClient>& remote) {
  81. return remote.get() == client;
  82. }));
  83. }
  84. void NetworkChangeManager::OnNetworkChanged(
  85. net::NetworkChangeNotifier::ConnectionType type) {
  86. connection_type_ = mojom::ConnectionType(type);
  87. for (const auto& client : clients_) {
  88. client->OnNetworkChanged(connection_type_);
  89. }
  90. }
  91. } // namespace network