1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_DNS_MAPPED_IP_RESOLVER_H_
6 #define NET_DNS_MAPPED_IP_RESOLVER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "net/base/ip_mapping_rules.h"
13 #include "net/base/net_export.h"
14 #include "net/dns/host_resolver.h"
18 // This class wraps an existing HostResolver instance, but modifies the
19 // resolution response by inserting or replacing IP addresses before returning
20 // it. Currently, the only directive suported is a "PREFACE" directive which
21 // (when a match exists) inserts a single IP address at the start of a list.
22 class NET_EXPORT MappedIPResolver
: public HostResolver
{
24 // Creates a MappedIPResolver that forwards all of its requests through
26 explicit MappedIPResolver(scoped_ptr
<HostResolver
> impl
);
27 virtual ~MappedIPResolver();
29 // Adds a rule to our IP mapper rules_.
30 // The most recently added rule "has priority" and will be used first (in
31 // preference to) any previous rules. Once one rule is found that matches,
32 // no other rules will be considered.
33 // See ip_mapping_rules.h for syntax and semantics.
34 // Returns true if the rule was successfully parsed and added.
35 bool AddRuleFromString(const std::string
& rule_string
) {
36 return rules_
.AddRuleFromString(rule_string
);
39 // Takes a semicolon separated list of rules, and assigns them to this
40 // resolver, discarding any previously added or set rules.
41 void SetRulesFromString(const std::string
& rules_string
) {
42 rules_
.SetRulesFromString(rules_string
);
45 // HostResolver methods:
46 virtual int Resolve(const RequestInfo
& info
,
47 RequestPriority priority
,
48 AddressList
* addresses
,
49 const CompletionCallback
& callback
,
50 RequestHandle
* out_req
,
51 const BoundNetLog
& net_log
) OVERRIDE
;
52 virtual int ResolveFromCache(const RequestInfo
& info
,
53 AddressList
* addresses
,
54 const BoundNetLog
& net_log
) OVERRIDE
;
55 virtual void CancelRequest(RequestHandle req
) OVERRIDE
;
56 virtual void SetDnsClientEnabled(bool enabled
) OVERRIDE
;
57 virtual HostCache
* GetHostCache() OVERRIDE
;
58 virtual base::Value
* GetDnsConfigAsValue() const OVERRIDE
;
61 // Modify the list of resolution |addresses| according to |rules_|, and then
62 // calls the |original_callback| with network error code |rv|.
63 void ApplyRules(const CompletionCallback
& original_callback
,
64 AddressList
* addresses
,
67 scoped_ptr
<HostResolver
> impl_
;
68 IPMappingRules rules_
;
70 base::WeakPtrFactory
<MappedIPResolver
> weak_factory_
;
71 DISALLOW_COPY_AND_ASSIGN(MappedIPResolver
);
76 #endif // NET_DNS_MAPPED_IP_RESOLVER_H_