blob: 209e07efae8eed84adf9598d8ed0fb38898b4eb8 [file] [log] [blame]
Kaido Kert25902c62024-06-17 17:10:28 -07001// Copyright 2012 The Chromium Authors
David Ghandehari9e5b5872016-07-28 09:50:04 -07002// 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_DNS_ADDRESS_SORTER_H_
6#define NET_DNS_ADDRESS_SORTER_H_
7
Andrew Top0d1858f2019-05-15 22:01:47 -07008#include <memory>
Kaido Kert25902c62024-06-17 17:10:28 -07009#include <vector>
Andrew Top0d1858f2019-05-15 22:01:47 -070010
Kaido Kert25902c62024-06-17 17:10:28 -070011#include "base/functional/callback.h"
12#include "net/base/ip_endpoint.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070013#include "net/base/net_export.h"
14
15namespace net {
16
17class AddressList;
18
19// Sorts AddressList according to RFC3484, by likelihood of successful
20// connection. Depending on the platform, the sort could be performed
21// asynchronously by the OS, or synchronously by local implementation.
22// AddressSorter does not necessarily preserve port numbers on the sorted list.
23class NET_EXPORT AddressSorter {
24 public:
Andrew Top0d1858f2019-05-15 22:01:47 -070025 using CallbackType =
Kaido Kert25902c62024-06-17 17:10:28 -070026 base::OnceCallback<void(bool success, std::vector<IPEndPoint> sorted)>;
David Ghandehari9e5b5872016-07-28 09:50:04 -070027
Kaido Kert25902c62024-06-17 17:10:28 -070028 AddressSorter(const AddressSorter&) = delete;
29 AddressSorter& operator=(const AddressSorter&) = delete;
David Ghandehari9e5b5872016-07-28 09:50:04 -070030
Kaido Kert25902c62024-06-17 17:10:28 -070031 virtual ~AddressSorter() = default;
32
33 // Sorts `endpoints`, which must include at least one IPv6 address.
34 // Calls `callback` upon completion. Could complete synchronously. Could
David Ghandehari9e5b5872016-07-28 09:50:04 -070035 // complete after this AddressSorter is destroyed.
Kaido Kert25902c62024-06-17 17:10:28 -070036 virtual void Sort(const std::vector<IPEndPoint>& endpoints,
37 CallbackType callback) const = 0;
David Ghandehari9e5b5872016-07-28 09:50:04 -070038
39 // Creates platform-dependent AddressSorter.
Andrew Top0d1858f2019-05-15 22:01:47 -070040 static std::unique_ptr<AddressSorter> CreateAddressSorter();
David Ghandehari9e5b5872016-07-28 09:50:04 -070041
42 protected:
Kaido Kert25902c62024-06-17 17:10:28 -070043 AddressSorter() = default;
David Ghandehari9e5b5872016-07-28 09:50:04 -070044};
45
46} // namespace net
47
48#endif // NET_DNS_ADDRESS_SORTER_H_