• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
7 
8 #include <SystemConfiguration/SystemConfiguration.h>
9 
10 #include <memory>
11 
12 #include "base/compiler_specific.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/synchronization/condition_variable.h"
17 #include "base/synchronization/lock.h"
18 #include "build/build_config.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/base/network_config_watcher_mac.h"
21 
22 namespace net {
23 
24 class NetworkChangeNotifierMac: public NetworkChangeNotifier {
25  public:
26   NetworkChangeNotifierMac();
27   NetworkChangeNotifierMac(const NetworkChangeNotifierMac&) = delete;
28   NetworkChangeNotifierMac& operator=(const NetworkChangeNotifierMac&) = delete;
29   ~NetworkChangeNotifierMac() override;
30 
31   // NetworkChangeNotifier implementation:
32   ConnectionType GetCurrentConnectionType() const override;
33 
34   // Forwarder just exists to keep the NetworkConfigWatcherMac API out of
35   // NetworkChangeNotifierMac's public API.
36   class Forwarder : public NetworkConfigWatcherMac::Delegate {
37    public:
Forwarder(NetworkChangeNotifierMac * net_config_watcher)38     explicit Forwarder(NetworkChangeNotifierMac* net_config_watcher)
39         : net_config_watcher_(net_config_watcher) {}
40     Forwarder(const Forwarder&) = delete;
41     Forwarder& operator=(const Forwarder&) = delete;
42 
43     // NetworkConfigWatcherMac::Delegate implementation:
44     void Init() override;
45     void StartReachabilityNotifications() override;
46     void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store) override;
47     void OnNetworkConfigChange(CFArrayRef changed_keys) override;
48 
49    private:
50     const raw_ptr<NetworkChangeNotifierMac> net_config_watcher_;
51   };
52 
53  private:
54   // Called on the main thread on startup, afterwards on the notifier thread.
55   static ConnectionType CalculateConnectionType(SCNetworkConnectionFlags flags);
56 
57   // Methods directly called by the NetworkConfigWatcherMac::Delegate:
58   void StartReachabilityNotifications();
59   void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store);
60   void OnNetworkConfigChange(CFArrayRef changed_keys);
61 
62   void SetInitialConnectionType();
63 
64   static void ReachabilityCallback(SCNetworkReachabilityRef target,
65                                    SCNetworkConnectionFlags flags,
66                                    void* notifier);
67 
68   static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsMac();
69 
70   // These must be constructed before config_watcher_ to ensure
71   // the lock is in a valid state when Forwarder::Init is called.
72   ConnectionType connection_type_ = CONNECTION_UNKNOWN;
73   bool connection_type_initialized_ = false;
74   mutable base::Lock connection_type_lock_;
75   mutable base::ConditionVariable initial_connection_type_cv_;
76   base::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability_;
77   base::ScopedCFTypeRef<CFRunLoopRef> run_loop_;
78 
79   Forwarder forwarder_;
80   std::unique_ptr<const NetworkConfigWatcherMac> config_watcher_;
81 };
82 
83 }  // namespace net
84 
85 #endif  // NET_BASE_NETWORK_CHANGE_NOTIFIER_MAC_H_
86