xref: /aosp_15_r20/external/cronet/net/base/network_change_notifier_linux.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_CHANGE_NOTIFIER_LINUX_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_LINUX_H_
7 
8 #include <memory>
9 #include <unordered_set>
10 
11 #include "base/compiler_specific.h"
12 #include "base/files/scoped_file.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "base/types/pass_key.h"
15 #include "net/base/net_export.h"
16 #include "net/base/network_change_notifier.h"
17 
18 namespace base {
19 class SequencedTaskRunner;
20 struct OnTaskRunnerDeleter;
21 }  // namespace base
22 
23 namespace net {
24 
25 class NET_EXPORT_PRIVATE NetworkChangeNotifierLinux
26     : public NetworkChangeNotifier {
27  public:
28   // Creates the object mostly like normal, but the AddressTrackerLinux will use
29   // |netlink_fd| instead of creating and binding its own netlink socket.
30   static std::unique_ptr<NetworkChangeNotifierLinux> CreateWithSocketForTesting(
31       const std::unordered_set<std::string>& ignored_interfaces,
32       base::ScopedFD netlink_fd);
33 
34   // Creates NetworkChangeNotifierLinux with a list of ignored interfaces.
35   // |ignored_interfaces| is the list of interfaces to ignore. An ignored
36   // interface will not trigger IP address or connection type notifications.
37   // NOTE: Only ignore interfaces not used to connect to the internet. Adding
38   // interfaces used to connect to the internet can cause critical network
39   // changed signals to be lost allowing incorrect stale state to persist.
40   explicit NetworkChangeNotifierLinux(
41       const std::unordered_set<std::string>& ignored_interfaces);
42 
43   // This constructor can leave the BlockingThreadObjects uninitialized. This
44   // is useful in tests that want to mock the netlink dependency of
45   // AddressTrackerLinux. The PassKey makes this essentially a private
46   // constructor.
47   NetworkChangeNotifierLinux(
48       const std::unordered_set<std::string>& ignored_interfaces,
49       bool initialize_blocking_thread_objects,
50       base::PassKey<NetworkChangeNotifierLinux>);
51 
52   NetworkChangeNotifierLinux(const NetworkChangeNotifierLinux&) = delete;
53   NetworkChangeNotifierLinux& operator=(const NetworkChangeNotifierLinux&) =
54       delete;
55 
56   ~NetworkChangeNotifierLinux() override;
57 
58   static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsLinux();
59 
60  private:
61   class BlockingThreadObjects;
62 
63   // Initializes BlockingThreadObjects, but AddressTrackerLinux will listen to
64   // |netlink_fd| rather than the kernel.
65   void InitBlockingThreadObjectsForTesting(base::ScopedFD netlink_fd);
66 
67   // NetworkChangeNotifier:
68   ConnectionType GetCurrentConnectionType() const override;
69 
70   AddressMapOwnerLinux* GetAddressMapOwnerInternal() override;
71 
72   // |blocking_thread_objects_| will live on this runner.
73   scoped_refptr<base::SequencedTaskRunner> blocking_thread_runner_;
74   // A collection of objects that must live on blocking sequences. These objects
75   // listen for notifications and relay the notifications to the registered
76   // observers without posting back to the thread the object was created on.
77   std::unique_ptr<BlockingThreadObjects, base::OnTaskRunnerDeleter>
78       blocking_thread_objects_;
79 };
80 
81 }  // namespace net
82 
83 #endif  // NET_BASE_NETWORK_CHANGE_NOTIFIER_LINUX_H_
84