OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_MOCK_HOST_RESOLVER_H_ | 5 #ifndef NET_BASE_MOCK_HOST_RESOLVER_H_ |
6 #define NET_BASE_MOCK_HOST_RESOLVER_H_ | 6 #define NET_BASE_MOCK_HOST_RESOLVER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 virtual ~MockHostResolverBase(); | 58 virtual ~MockHostResolverBase(); |
59 | 59 |
60 RuleBasedHostResolverProc* rules() { return rules_; } | 60 RuleBasedHostResolverProc* rules() { return rules_; } |
61 void set_rules(RuleBasedHostResolverProc* rules) { rules_ = rules; } | 61 void set_rules(RuleBasedHostResolverProc* rules) { rules_ = rules; } |
62 | 62 |
63 // Controls whether resolutions complete synchronously or asynchronously. | 63 // Controls whether resolutions complete synchronously or asynchronously. |
64 void set_synchronous_mode(bool is_synchronous) { | 64 void set_synchronous_mode(bool is_synchronous) { |
65 synchronous_mode_ = is_synchronous; | 65 synchronous_mode_ = is_synchronous; |
66 } | 66 } |
67 | 67 |
| 68 // Asynchronous requests are automatically resolved by default. |
| 69 // If set_ondemand_mode() is set then Resolve() returns IO_PENDING and |
| 70 // ResolveAllPending() must be explicitly invoked to resolve all requests |
| 71 // that are pending. |
| 72 void set_ondemand_mode(bool is_ondemand) { |
| 73 ondemand_mode_ = is_ondemand; |
| 74 } |
| 75 |
68 // HostResolver methods: | 76 // HostResolver methods: |
69 virtual int Resolve(const RequestInfo& info, | 77 virtual int Resolve(const RequestInfo& info, |
70 AddressList* addresses, | 78 AddressList* addresses, |
71 const CompletionCallback& callback, | 79 const CompletionCallback& callback, |
72 RequestHandle* out_req, | 80 RequestHandle* out_req, |
73 const BoundNetLog& net_log) OVERRIDE; | 81 const BoundNetLog& net_log) OVERRIDE; |
74 virtual int ResolveFromCache(const RequestInfo& info, | 82 virtual int ResolveFromCache(const RequestInfo& info, |
75 AddressList* addresses, | 83 AddressList* addresses, |
76 const BoundNetLog& net_log) OVERRIDE; | 84 const BoundNetLog& net_log) OVERRIDE; |
77 virtual void CancelRequest(RequestHandle req) OVERRIDE; | 85 virtual void CancelRequest(RequestHandle req) OVERRIDE; |
78 virtual HostCache* GetHostCache() OVERRIDE; | 86 virtual HostCache* GetHostCache() OVERRIDE; |
79 | 87 |
| 88 // Resolves all pending requests. It is only valid to invoke this if |
| 89 // set_ondemand_mode was set before. The requests are resolved asynchronously, |
| 90 // after this call returns. |
| 91 void ResolveAllPending(); |
| 92 |
| 93 // Returns true if there are pending requests that can be resolved by invoking |
| 94 // ResolveAllPending(). |
| 95 bool has_pending_requests() const { return !requests_.empty(); } |
| 96 |
80 protected: | 97 protected: |
81 explicit MockHostResolverBase(bool use_caching); | 98 explicit MockHostResolverBase(bool use_caching); |
82 | 99 |
83 private: | 100 private: |
84 struct Request; | 101 struct Request; |
85 typedef std::map<size_t, Request*> RequestMap; | 102 typedef std::map<size_t, Request*> RequestMap; |
86 | 103 |
87 // Resolve as IP or from |cache_| return cached error or | 104 // Resolve as IP or from |cache_| return cached error or |
88 // DNS_CACHE_MISS if failed. | 105 // DNS_CACHE_MISS if failed. |
89 int ResolveFromIPLiteralOrCache(const RequestInfo& info, | 106 int ResolveFromIPLiteralOrCache(const RequestInfo& info, |
90 AddressList* addresses); | 107 AddressList* addresses); |
91 // Resolve via |proc_|. | 108 // Resolve via |proc_|. |
92 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); | 109 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); |
93 // Resolve request stored in |requests_|. Pass rv to callback. | 110 // Resolve request stored in |requests_|. Pass rv to callback. |
94 void ResolveNow(size_t id); | 111 void ResolveNow(size_t id); |
95 | 112 |
96 bool synchronous_mode_; | 113 bool synchronous_mode_; |
| 114 bool ondemand_mode_; |
97 scoped_refptr<RuleBasedHostResolverProc> rules_; | 115 scoped_refptr<RuleBasedHostResolverProc> rules_; |
98 scoped_ptr<HostCache> cache_; | 116 scoped_ptr<HostCache> cache_; |
99 RequestMap requests_; | 117 RequestMap requests_; |
100 size_t next_request_id_; | 118 size_t next_request_id_; |
101 | 119 |
102 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); | 120 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); |
103 }; | 121 }; |
104 | 122 |
105 class MockHostResolver : public MockHostResolverBase { | 123 class MockHostResolver : public MockHostResolverBase { |
106 public: | 124 public: |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 void Init(HostResolverProc* proc); | 227 void Init(HostResolverProc* proc); |
210 | 228 |
211 private: | 229 private: |
212 scoped_refptr<HostResolverProc> current_proc_; | 230 scoped_refptr<HostResolverProc> current_proc_; |
213 scoped_refptr<HostResolverProc> previous_proc_; | 231 scoped_refptr<HostResolverProc> previous_proc_; |
214 }; | 232 }; |
215 | 233 |
216 } // namespace net | 234 } // namespace net |
217 | 235 |
218 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ | 236 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ |
OLD | NEW |