network_change_notifier_mac.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
  5. #define NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
  6. #include <SystemConfiguration/SystemConfiguration.h>
  7. #include <memory>
  8. #include "base/compiler_specific.h"
  9. #include "base/mac/scoped_cftyperef.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/synchronization/condition_variable.h"
  13. #include "base/synchronization/lock.h"
  14. #include "build/build_config.h"
  15. #include "net/base/network_change_notifier.h"
  16. #include "net/base/network_config_watcher_mac.h"
  17. namespace net {
  18. class NetworkChangeNotifierMac: public NetworkChangeNotifier {
  19. public:
  20. NetworkChangeNotifierMac();
  21. NetworkChangeNotifierMac(const NetworkChangeNotifierMac&) = delete;
  22. NetworkChangeNotifierMac& operator=(const NetworkChangeNotifierMac&) = delete;
  23. ~NetworkChangeNotifierMac() override;
  24. // NetworkChangeNotifier implementation:
  25. ConnectionType GetCurrentConnectionType() const override;
  26. // Forwarder just exists to keep the NetworkConfigWatcherMac API out of
  27. // NetworkChangeNotifierMac's public API.
  28. class Forwarder : public NetworkConfigWatcherMac::Delegate {
  29. public:
  30. explicit Forwarder(NetworkChangeNotifierMac* net_config_watcher)
  31. : net_config_watcher_(net_config_watcher) {}
  32. Forwarder(const Forwarder&) = delete;
  33. Forwarder& operator=(const Forwarder&) = delete;
  34. // NetworkConfigWatcherMac::Delegate implementation:
  35. void Init() override;
  36. void StartReachabilityNotifications() override;
  37. void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store) override;
  38. void OnNetworkConfigChange(CFArrayRef changed_keys) override;
  39. private:
  40. const raw_ptr<NetworkChangeNotifierMac> net_config_watcher_;
  41. };
  42. private:
  43. // Called on the main thread on startup, afterwards on the notifier thread.
  44. static ConnectionType CalculateConnectionType(SCNetworkConnectionFlags flags);
  45. // Methods directly called by the NetworkConfigWatcherMac::Delegate:
  46. void StartReachabilityNotifications();
  47. void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store);
  48. void OnNetworkConfigChange(CFArrayRef changed_keys);
  49. void SetInitialConnectionType();
  50. static void ReachabilityCallback(SCNetworkReachabilityRef target,
  51. SCNetworkConnectionFlags flags,
  52. void* notifier);
  53. static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsMac();
  54. // These must be constructed before config_watcher_ to ensure
  55. // the lock is in a valid state when Forwarder::Init is called.
  56. ConnectionType connection_type_ = CONNECTION_UNKNOWN;
  57. bool connection_type_initialized_ = false;
  58. mutable base::Lock connection_type_lock_;
  59. mutable base::ConditionVariable initial_connection_type_cv_;
  60. base::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability_;
  61. base::ScopedCFTypeRef<CFRunLoopRef> run_loop_;
  62. Forwarder forwarder_;
  63. std::unique_ptr<const NetworkConfigWatcherMac> config_watcher_;
  64. };
  65. } // namespace net
  66. #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_