1// Copyright (c) 2011 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_BASE_MAPPED_HOST_RESOLVER_H_
6#define NET_BASE_MAPPED_HOST_RESOLVER_H_
7#pragma once
8
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "net/base/host_mapping_rules.h"
13#include "net/base/host_resolver.h"
14
15namespace net {
16
17// This class wraps an existing HostResolver instance, but modifies the
18// request before passing it off to |impl|. This is different from
19// MockHostResolver which does the remapping at the HostResolverProc
20// layer, so it is able to preserve the effectiveness of the cache.
21class MappedHostResolver : public HostResolver {
22 public:
23  // Creates a MappedHostResolver that forwards all of its requests through
24  // |impl|.  It takes ownership of |impl|.
25  explicit MappedHostResolver(HostResolver* impl);
26  virtual ~MappedHostResolver();
27
28  // Adds a rule to this mapper. The format of the rule can be one of:
29  //
30  //   "MAP" <hostname_pattern> <replacement_host> [":" <replacement_port>]
31  //   "EXCLUDE" <hostname_pattern>
32  //
33  // The <replacement_host> can be either a hostname, or an IP address literal.
34  //
35  // Returns true if the rule was successfully parsed and added.
36  bool AddRuleFromString(const std::string& rule_string) {
37    return rules_.AddRuleFromString(rule_string);
38  }
39
40  // Takes a comma separated list of rules, and assigns them to this resolver.
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                      AddressList* addresses,
48                      CompletionCallback* callback,
49                      RequestHandle* out_req,
50                      const BoundNetLog& net_log);
51  virtual void CancelRequest(RequestHandle req);
52  virtual void AddObserver(Observer* observer);
53  virtual void RemoveObserver(Observer* observer);
54  virtual HostResolverImpl* GetAsHostResolverImpl();
55
56 private:
57  scoped_ptr<HostResolver> impl_;
58
59  HostMappingRules rules_;
60};
61
62}  // namespace net
63
64#endif  // NET_BASE_MAPPED_HOST_RESOLVER_H_
65