File: network_change_notifier_apple.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 4,818 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_APPLE_H_
#define NET_BASE_NETWORK_CHANGE_NOTIFIER_APPLE_H_

#include <SystemConfiguration/SystemConfiguration.h>

#include <memory>
#include <optional>

#include "base/apple/scoped_cftyperef.h"
#include "base/compiler_specific.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "build/build_config.h"
#include "net/base/net_export.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_config_watcher_apple.h"
#include "net/base/network_interfaces.h"
#include "net/log/net_log_with_source.h"

namespace net {

class NET_EXPORT_PRIVATE NetworkChangeNotifierApple
    : public NetworkChangeNotifier {
 public:
  NetworkChangeNotifierApple();
  NetworkChangeNotifierApple(const NetworkChangeNotifierApple&) = delete;
  NetworkChangeNotifierApple& operator=(const NetworkChangeNotifierApple&) = delete;
  ~NetworkChangeNotifierApple() override;

  // NetworkChangeNotifier implementation:
  ConnectionType GetCurrentConnectionType() const override;

  // Forwarder just exists to keep the NetworkConfigWatcherApple API out of
  // NetworkChangeNotifierApple's public API.
  class Forwarder : public NetworkConfigWatcherApple::Delegate {
   public:
    explicit Forwarder(NetworkChangeNotifierApple* net_config_watcher)
        : net_config_watcher_(net_config_watcher) {}
    Forwarder(const Forwarder&) = delete;
    Forwarder& operator=(const Forwarder&) = delete;

    // NetworkConfigWatcherApple::Delegate implementation:
    void Init() override;
    void StartReachabilityNotifications() override;
    void SetDynamicStoreNotificationKeys(
        base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store) override;
    void OnNetworkConfigChange(CFArrayRef changed_keys) override;
    void CleanUpOnNotifierThread() override;

   private:
    const raw_ptr<NetworkChangeNotifierApple> net_config_watcher_;
  };

 private:
  friend class NetworkChangeNotifierAppleTest;

  // Called on the main thread on startup, afterwards on the notifier thread.
  static ConnectionType CalculateConnectionType(SCNetworkConnectionFlags flags);

  // Methods directly called by the NetworkConfigWatcherApple::Delegate:
  void StartReachabilityNotifications();
  void SetDynamicStoreNotificationKeys(
      base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store);
  void OnNetworkConfigChange(CFArrayRef changed_keys);
  void CleanUpOnNotifierThread();

  void SetInitialConnectionType();

  static void ReachabilityCallback(SCNetworkReachabilityRef target,
                                   SCNetworkConnectionFlags flags,
                                   void* notifier);

  static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsMac();

#if BUILDFLAG(IS_MAC)
  void SetCallbacksForTest(
      base::OnceClosure initialized_callback,
      base::RepeatingCallback<bool(NetworkInterfaceList*, int)>
          get_network_list_callback,
      base::RepeatingCallback<std::string(SCDynamicStoreRef)>
          get_ipv4_primary_interface_name_callback,
      base::RepeatingCallback<std::string(SCDynamicStoreRef)>
          get_ipv6_primary_interface_name_callback);
#endif  // BUILDFLAG(IS_MAC)

  // These must be constructed before config_watcher_ to ensure
  // the lock is in a valid state when Forwarder::Init is called.
  ConnectionType connection_type_ = CONNECTION_UNKNOWN;
  bool connection_type_initialized_ = false;
  mutable base::Lock connection_type_lock_;
  mutable base::ConditionVariable initial_connection_type_cv_;
  base::apple::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability_;
  base::apple::ScopedCFTypeRef<CFRunLoopRef> run_loop_;

  Forwarder forwarder_;
  std::unique_ptr<NetworkConfigWatcherApple> config_watcher_;

#if BUILDFLAG(IS_MAC)
  const bool reduce_ip_address_change_notification_;
  base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store_;
  std::optional<NetworkInterfaceList> interfaces_for_network_change_check_;
  std::string ipv4_primary_interface_name_;
  std::string ipv6_primary_interface_name_;

  base::OnceClosure initialized_callback_for_test_;
  base::RepeatingCallback<bool(NetworkInterfaceList*, int)>
      get_network_list_callback_;
  base::RepeatingCallback<std::string(SCDynamicStoreRef)>
      get_ipv4_primary_interface_name_callback_;
  base::RepeatingCallback<std::string(SCDynamicStoreRef)>
      get_ipv6_primary_interface_name_callback_;
#endif  // BUILDFLAG(IS_MAC)
  NetLogWithSource net_log_;
};

}  // namespace net

#endif  // NET_BASE_NETWORK_CHANGE_NOTIFIER_APPLE_H_