xref: /aosp_15_r20/external/cronet/net/base/network_config_watcher_apple.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_CONFIG_WATCHER_APPLE_H_
6 #define NET_BASE_NETWORK_CONFIG_WATCHER_APPLE_H_
7 
8 #include <SystemConfiguration/SystemConfiguration.h>
9 
10 #include <memory>
11 
12 #include "base/apple/scoped_cftyperef.h"
13 #include "net/base/net_export.h"
14 
15 namespace base {
16 class Thread;
17 }
18 
19 namespace net {
20 
21 // Helper class for watching the Mac OS system network settings.
22 class NET_EXPORT_PRIVATE NetworkConfigWatcherApple {
23  public:
24   // NOTE: The lifetime of Delegate is expected to exceed the lifetime of
25   // NetworkConfigWatcherApple.
26   class Delegate {
27    public:
28     virtual ~Delegate() = default;
29 
30     // Called to let the delegate do any setup work the must be run on the
31     // notifier thread immediately after it starts.
Init()32     virtual void Init() {}
33 
34     // Called to start receiving notifications from the SCNetworkReachability
35     // API.
36     // Will be called on the notifier thread.
37     virtual void StartReachabilityNotifications() = 0;
38 
39     // Called to register the notification keys on |store|.
40     // Implementors are expected to call SCDynamicStoreSetNotificationKeys().
41     // Will be called on the notifier thread.
42     virtual void SetDynamicStoreNotificationKeys(
43         base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store) = 0;
44 
45     // Called when one of the notification keys has changed.
46     // Will be called on the notifier thread.
47     virtual void OnNetworkConfigChange(CFArrayRef changed_keys) = 0;
48 
49     // Called when `this` is being destructed.
50     // Will be called on the notifier thread.
51     virtual void CleanUpOnNotifierThread() = 0;
52   };
53 
54   explicit NetworkConfigWatcherApple(Delegate* delegate);
55   NetworkConfigWatcherApple(const NetworkConfigWatcherApple&) = delete;
56   NetworkConfigWatcherApple& operator=(const NetworkConfigWatcherApple&) = delete;
57   ~NetworkConfigWatcherApple();
58 
59   base::Thread* GetNotifierThreadForTest();
60 
61  private:
62   // The thread used to listen for notifications.  This relays the notification
63   // to the registered observers without posting back to the thread the object
64   // was created on.
65   std::unique_ptr<base::Thread> notifier_thread_;
66 };
67 
68 }  // namespace net
69 
70 #endif  // NET_BASE_NETWORK_CONFIG_WATCHER_APPLE_H_
71