xref: /aosp_15_r20/external/cronet/net/base/network_change_notifier_fuchsia.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 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_FUCHSIA_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_FUCHSIA_H_
7 
8 #include <fuchsia/net/interfaces/cpp/fidl.h>
9 
10 #include <optional>
11 #include <vector>
12 
13 #include "base/threading/thread_checker.h"
14 #include "base/types/expected.h"
15 #include "net/base/fuchsia/network_interface_cache.h"
16 #include "net/base/net_export.h"
17 #include "net/base/network_change_notifier.h"
18 
19 namespace net {
20 
21 class SystemDnsConfigChangeNotifier;
22 
23 class NET_EXPORT_PRIVATE NetworkChangeNotifierFuchsia
24     : public NetworkChangeNotifier {
25  public:
26   // Registers for asynchronous notifications of changes to network interfaces.
27   // Only WLAN interfaces are observed if |require_wlan| is requested.
28   explicit NetworkChangeNotifierFuchsia(bool require_wlan);
29   NetworkChangeNotifierFuchsia(const NetworkChangeNotifierFuchsia&) = delete;
30   NetworkChangeNotifierFuchsia& operator=(const NetworkChangeNotifierFuchsia&) =
31       delete;
32   ~NetworkChangeNotifierFuchsia() override;
33 
34   // NetworkChangeNotifier implementation.
35   ConnectionType GetCurrentConnectionType() const override;
36 
37  private:
38   friend class NetworkChangeNotifierFuchsiaTest;
39 
40   const internal::NetworkInterfaceCache* GetNetworkInterfaceCacheInternal()
41       const override;
42 
43   NetworkChangeNotifierFuchsia(
44       fuchsia::net::interfaces::WatcherHandle watcher,
45       bool require_wlan,
46       SystemDnsConfigChangeNotifier* system_dns_config_notifier);
47 
48   // Processes events from the watcher for interface addition, change, or
49   // removal. Listeners are notified of changes that affect them. `watcher_` is
50   // unbound if an event is malformed in some way.
51   void OnInterfacesEvent(fuchsia::net::interfaces::Event event);
52 
53   // Notifies observers of changes. Unbinds `watcher_` if there was an error.
54   void HandleCacheStatus(
55       std::optional<internal::NetworkInterfaceCache::ChangeBits> change_bits);
56 
57   fuchsia::net::interfaces::WatcherPtr watcher_;
58 
59   // Keeps an updated cache of network interfaces and connection type.
60   internal::NetworkInterfaceCache cache_;
61 
62   THREAD_CHECKER(thread_checker_);
63 };
64 
65 namespace internal {
66 
67 // Connects to the service via the process' ComponentContext, and connects the
68 // Watcher to the service.
69 fuchsia::net::interfaces::WatcherHandle ConnectInterfacesWatcher();
70 
71 // Reads existing network interfaces from `watcher_handle`, appending them to
72 // `interfaces`. If successful, returns an unbound WatcherHandle that can be
73 // used to watch for subsequent changes.
74 //
75 // `watcher_handle` must be a newly created fuchsia.net.interfaces.Watcher. Can
76 // be used as the first part of the hanging-get pattern.
77 base::expected<fuchsia::net::interfaces::WatcherHandle, zx_status_t>
78 ReadExistingNetworkInterfacesFromNewWatcher(
79     fuchsia::net::interfaces::WatcherHandle watcher_handle,
80     std::vector<fuchsia::net::interfaces::Properties>& interfaces);
81 
82 }  // namespace internal
83 }  // namespace net
84 
85 #endif  // NET_BASE_NETWORK_CHANGE_NOTIFIER_FUCHSIA_H_
86