File: network_cost_change_notifier_win.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 (88 lines) | stat: -rw-r--r-- 3,397 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
// Copyright 2024 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_COST_CHANGE_NOTIFIER_WIN_H_
#define NET_BASE_NETWORK_COST_CHANGE_NOTIFIER_WIN_H_

#include <netlistmgr.h>
#include <wrl/client.h>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/threading/sequence_bound.h"
#include "base/win/windows_version.h"
#include "net/base/net_export.h"
#include "net/base/network_change_notifier.h"

namespace net {
class NetworkCostManagerEventSinkWin;

// Uses the `INetworkCostManager` Windows OS API to monitor the cost of the
// current connection.  `INetworkCostManager` performs blocking IO and
// synchronous RPC, which must be accessed through a thread pool worker thread.
// `NetworkCostChangeNotifierWin` uses `base::SequenceBound` to prevent these
// expensive operations from happening on the UI thread.
class NET_EXPORT_PRIVATE NetworkCostChangeNotifierWin final {
 public:
  // `INetworkCostManager` requires Windows Build 19041 or higher.  On prior
  // builds, calls to the Windows OS API `IConnectionPoint::Advise()` may hang.
  static constexpr base::win::Version kSupportedOsVersion =
      base::win::Version::WIN10_20H1;

  using CostChangedCallback =
      base::RepeatingCallback<void(NetworkChangeNotifier::ConnectionCost)>;

  // Constructs a new instance using a COM STA single threaded task runner.
  // Posts the task that subscribes to cost change events using Windows OS APIs.
  static base::SequenceBound<NetworkCostChangeNotifierWin> CreateInstance(
      CostChangedCallback cost_changed_callback);

  // Use `CreateInstance()` above.  This constructor is public for use by
  // `base::SequenceBound` only.
  explicit NetworkCostChangeNotifierWin(
      CostChangedCallback cost_changed_callback);
  ~NetworkCostChangeNotifierWin();

  // Tests use this hook to provide a fake implementation of the OS APIs.
  // The fake implementation enables tests to simulate different network
  // conditions.
  using CoCreateInstanceCallback = base::RepeatingCallback<
      HRESULT(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID*)>;
  static void OverrideCoCreateInstanceForTesting(
      CoCreateInstanceCallback callback_for_testing);

  NetworkCostChangeNotifierWin(const NetworkCostChangeNotifierWin&) = delete;
  NetworkCostChangeNotifierWin& operator=(const NetworkCostChangeNotifierWin&) =
      delete;

 private:
  // Creates `INetworkCostManager` for `cost_manager_` and subscribe to cost
  // change events.
  void StartWatching();

  // Stops monitoring the cost of the current connection by unsubscribing to
  // `INetworkCostManager` events and releasing all members.
  void StopWatching();

  // Gets the current cost from `cost_manager_` and then runs
  // `cost_changed_callback_`.
  void HandleCostChanged();

  // All members must be accessed on the sequence from `sequence_checker_`
  SEQUENCE_CHECKER(sequence_checker_);

  CostChangedCallback cost_changed_callback_;

  Microsoft::WRL::ComPtr<INetworkCostManager> cost_manager_;

  Microsoft::WRL::ComPtr<NetworkCostManagerEventSinkWin>
      cost_manager_event_sink_;

  base::WeakPtrFactory<NetworkCostChangeNotifierWin> weak_ptr_factory_{this};
};

}  // namespace net

#endif  // NET_BASE_NETWORK_COST_CHANGE_NOTIFIER_WIN_H_