xref: /aosp_15_r20/external/cronet/net/dns/public/dns_over_https_server_config.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 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_DNS_PUBLIC_DNS_OVER_HTTPS_SERVER_CONFIG_H_
6 #define NET_DNS_PUBLIC_DNS_OVER_HTTPS_SERVER_CONFIG_H_
7 
8 #include <optional>
9 #include <string>
10 #include <string_view>
11 
12 #include "base/values.h"
13 #include "net/base/ip_address.h"
14 #include "net/base/net_export.h"
15 
16 namespace net {
17 
18 // Simple representation of a DoH server for use in configurations.
19 class NET_EXPORT DnsOverHttpsServerConfig {
20  public:
21   // TODO(crbug.com/1200908): Generalize endpoints to enable other capabilities
22   // of HTTPS records, such as extended metadata and aliases.
23   using Endpoints = std::vector<IPAddressList>;
24 
25   // A default constructor is required by Mojo.
26   DnsOverHttpsServerConfig();
27   DnsOverHttpsServerConfig(const DnsOverHttpsServerConfig& other);
28   DnsOverHttpsServerConfig& operator=(const DnsOverHttpsServerConfig& other);
29   DnsOverHttpsServerConfig(DnsOverHttpsServerConfig&& other);
30   DnsOverHttpsServerConfig& operator=(DnsOverHttpsServerConfig&& other);
31   ~DnsOverHttpsServerConfig();
32 
33   // Returns nullopt if |doh_template| is invalid.
34   static std::optional<DnsOverHttpsServerConfig> FromString(
35       std::string doh_template,
36       Endpoints endpoints = {});
37 
38   static std::optional<DnsOverHttpsServerConfig> FromValue(
39       base::Value::Dict value);
40 
41   bool operator==(const DnsOverHttpsServerConfig& other) const;
42   bool operator<(const DnsOverHttpsServerConfig& other) const;
43 
44   const std::string& server_template() const;
45   std::string_view server_template_piece() const;
46   bool use_post() const;
47   const Endpoints& endpoints() const;
48 
49   // Returns true if this server config can be represented as just a template.
50   bool IsSimple() const;
51 
52   base::Value::Dict ToValue() const;
53 
54  private:
55   DnsOverHttpsServerConfig(std::string server_template,
56                            bool use_post,
57                            Endpoints endpoints);
58 
59   std::string server_template_;
60   bool use_post_;
61   Endpoints endpoints_;
62 };
63 
64 }  // namespace net
65 
66 #endif  // NET_DNS_PUBLIC_DNS_OVER_HTTPS_SERVER_CONFIG_H_
67