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