OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef NET_DNS_MAPPED_IP_RESOLVER_H_ | |
6 #define NET_DNS_MAPPED_IP_RESOLVER_H_ | |
7 | |
8 #include <string> | |
9 | |
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" | |
15 | |
16 namespace net { | |
17 | |
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 { | |
23 public: | |
24 // Creates a MappedIPResolver that forwards all of its requests through | |
25 // |impl|. | |
26 explicit MappedIPResolver(scoped_ptr<HostResolver> impl); | |
27 virtual ~MappedIPResolver(); | |
28 | |
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); | |
37 } | |
38 | |
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); | |
43 } | |
44 | |
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; | |
59 | |
60 private: | |
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, | |
65 int rv) const; | |
66 | |
67 scoped_ptr<HostResolver> impl_; | |
68 IPMappingRules rules_; | |
69 | |
70 base::WeakPtrFactory<MappedIPResolver> weak_factory_; | |
71 DISALLOW_COPY_AND_ASSIGN(MappedIPResolver); | |
72 }; | |
73 | |
74 } // namespace net | |
75 | |
76 #endif // NET_DNS_MAPPED_IP_RESOLVER_H_ | |
OLD | NEW |