OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 COMPONENTS_CRONET_STALE_HOST_RESOLVER_H_ | |
6 #define COMPONENTS_CRONET_STALE_HOST_RESOLVER_H_ | |
7 | |
8 #include <unordered_set> | |
9 | |
10 #include "net/dns/host_resolver.h" | |
11 #include "net/dns/host_resolver_impl.h" | |
12 | |
13 namespace cronet { | |
14 | |
15 // A HostResolver that wraps a HostResolverImpl and uses it to make requests, | |
16 // but "impatiently" returns stale data (if available and usable) after a delay, | |
17 // to reduce DNS latency at the expense of accuracy. | |
18 class NET_EXPORT StaleHostResolver : public net::HostResolver { | |
19 public: | |
20 struct NET_EXPORT StaleOptions { | |
21 StaleOptions(); | |
22 | |
23 // How long to wait before returning stale data, if available. | |
24 base::TimeDelta delay; | |
25 | |
26 // If positive, how long stale data can be past the expiration time before | |
27 // it's considered unusable. If zero or negative, stale data can be used | |
28 // indefinitely. | |
29 base::TimeDelta max_expired_time; | |
30 | |
31 // If set, stale data from previous networks is usable; if clear, it's not. | |
32 // | |
33 // If the other network had a working, correct DNS setup, this can increase | |
34 // the availability of useful stale results. | |
35 // | |
36 // If the other network had a broken (e.g. hijacked for captive portal) DNS | |
37 // setup, this will instead end up returning useless results. | |
38 bool allow_other_network; | |
39 | |
40 // If positive, the maximum number of times a stale entry can be used. If | |
41 // zero, there is no limit. | |
42 int max_stale_uses; | |
43 }; | |
44 | |
45 // Creates a StaleHostResolver that uses |inner_resolver| for actual | |
46 // resolution, but potentially returns stale data according to | |
47 // |stale_options|. | |
48 StaleHostResolver(std::unique_ptr<net::HostResolverImpl> inner_resolver, | |
49 const StaleOptions& stale_options); | |
50 | |
51 // Cancels all pending calls to Resolve, and does not call their callbacks. | |
52 ~StaleHostResolver() override; | |
53 | |
54 // HostResolver implementation: | |
55 | |
56 // Resolves as a regular HostResolver, but if stale data is available and | |
57 // usable (according to the options passed to the constructor), and fresh data | |
58 // is not returned before the specified delay, returns the stale data instead. | |
59 // | |
60 // If stale data is returned, the StaleHostResolver allows the underlying | |
61 // request to continue in order to repopulate the cache. | |
62 int Resolve(const RequestInfo& info, | |
63 net::RequestPriority priority, | |
64 net::AddressList* addresses, | |
65 const net::CompletionCallback& callback, | |
66 RequestHandle* out_req, | |
67 const net::BoundNetLog& net_log) override; | |
68 | |
69 void CancelRequest(RequestHandle req) override; | |
70 | |
71 // The remaining public methods pass through to the inner resolver: | |
72 | |
73 int ResolveFromCache(const RequestInfo& info, | |
74 net::AddressList* addresses, | |
75 const net::BoundNetLog& net_log) override; | |
76 void SetDnsClientEnabled(bool enabled) override; | |
77 net::HostCache* GetHostCache() override; | |
78 std::unique_ptr<base::Value> GetDnsConfigAsValue() const override; | |
79 | |
80 private: | |
81 class Request; | |
82 | |
83 // Called from |Request| when a request is complete and can be destroyed. | |
84 void OnRequestComplete(Request* request); | |
85 | |
86 // The underlying HostResolverImpl that will be used to make cache and network | |
87 // requests. | |
88 std::unique_ptr<net::HostResolverImpl> inner_resolver_; | |
89 | |
90 // Options that govern when a stale response can or can't be returned. | |
91 StaleOptions options_; | |
92 | |
93 // The set of requests that have not completed (or been canceled) yet. They | |
94 // will be removed when completed, canceled, or when the StaleHostResolver is | |
95 // destroyed. | |
96 std::unordered_set<Request*> pending_requests_; | |
97 }; | |
xunjieli
2016/06/20 18:50:20
DISALLOW_COPY_AND_ASSIGN?
Julia Tuttle
2016/07/18 18:43:32
Done.
| |
98 | |
99 } // namespace cronet | |
100 | |
101 #endif // COMPONENTS_CRONET_STALE_HOST_RESOLVER_H_ | |
OLD | NEW |