| 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_DNS_MOCK_HOST_RESOLVER_H_ | 5 #ifndef NET_DNS_MOCK_HOST_RESOLVER_H_ |
| 6 #define NET_DNS_MOCK_HOST_RESOLVER_H_ | 6 #define NET_DNS_MOCK_HOST_RESOLVER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 // Replacement doesn't have to be string representing an IP address. It can | 51 // Replacement doesn't have to be string representing an IP address. It can |
| 52 // re-map one hostname to another as well. | 52 // re-map one hostname to another as well. |
| 53 // | 53 // |
| 54 // By default, MockHostResolvers include a single rule that maps all hosts to | 54 // By default, MockHostResolvers include a single rule that maps all hosts to |
| 55 // 127.0.0.1. | 55 // 127.0.0.1. |
| 56 | 56 |
| 57 // Base class shared by MockHostResolver and MockCachingHostResolver. | 57 // Base class shared by MockHostResolver and MockCachingHostResolver. |
| 58 class MockHostResolverBase : public HostResolver, | 58 class MockHostResolverBase : public HostResolver, |
| 59 public base::SupportsWeakPtr<MockHostResolverBase>, | 59 public base::SupportsWeakPtr<MockHostResolverBase>, |
| 60 public base::NonThreadSafe { | 60 public base::NonThreadSafe { |
| 61 private: |
| 62 class RequestImpl; |
| 63 |
| 61 public: | 64 public: |
| 62 ~MockHostResolverBase() override; | 65 ~MockHostResolverBase() override; |
| 63 | 66 |
| 64 RuleBasedHostResolverProc* rules() { return rules_.get(); } | 67 RuleBasedHostResolverProc* rules() { return rules_.get(); } |
| 65 void set_rules(RuleBasedHostResolverProc* rules) { rules_ = rules; } | 68 void set_rules(RuleBasedHostResolverProc* rules) { rules_ = rules; } |
| 66 | 69 |
| 67 // Controls whether resolutions complete synchronously or asynchronously. | 70 // Controls whether resolutions complete synchronously or asynchronously. |
| 68 void set_synchronous_mode(bool is_synchronous) { | 71 void set_synchronous_mode(bool is_synchronous) { |
| 69 synchronous_mode_ = is_synchronous; | 72 synchronous_mode_ = is_synchronous; |
| 70 } | 73 } |
| 71 | 74 |
| 72 // Asynchronous requests are automatically resolved by default. | 75 // Asynchronous requests are automatically resolved by default. |
| 73 // If set_ondemand_mode() is set then Resolve() returns IO_PENDING and | 76 // If set_ondemand_mode() is set then Resolve() returns IO_PENDING and |
| 74 // ResolveAllPending() must be explicitly invoked to resolve all requests | 77 // ResolveAllPending() must be explicitly invoked to resolve all requests |
| 75 // that are pending. | 78 // that are pending. |
| 76 void set_ondemand_mode(bool is_ondemand) { | 79 void set_ondemand_mode(bool is_ondemand) { |
| 77 ondemand_mode_ = is_ondemand; | 80 ondemand_mode_ = is_ondemand; |
| 78 } | 81 } |
| 79 | 82 |
| 80 // HostResolver methods: | 83 // HostResolver methods: |
| 81 int Resolve(const RequestInfo& info, | 84 int Resolve(const RequestInfo& info, |
| 82 RequestPriority priority, | 85 RequestPriority priority, |
| 83 AddressList* addresses, | 86 AddressList* addresses, |
| 84 const CompletionCallback& callback, | 87 const CompletionCallback& callback, |
| 85 RequestHandle* out_req, | 88 std::unique_ptr<Request>* request, |
| 86 const BoundNetLog& net_log) override; | 89 const BoundNetLog& net_log) override; |
| 87 int ResolveFromCache(const RequestInfo& info, | 90 int ResolveFromCache(const RequestInfo& info, |
| 88 AddressList* addresses, | 91 AddressList* addresses, |
| 89 const BoundNetLog& net_log) override; | 92 const BoundNetLog& net_log) override; |
| 90 void CancelRequest(RequestHandle req) override; | |
| 91 HostCache* GetHostCache() override; | 93 HostCache* GetHostCache() override; |
| 92 | 94 |
| 95 // Detach cancelled request. |
| 96 void DetachRequest(size_t id); |
| 97 |
| 93 // Resolves all pending requests. It is only valid to invoke this if | 98 // Resolves all pending requests. It is only valid to invoke this if |
| 94 // set_ondemand_mode was set before. The requests are resolved asynchronously, | 99 // set_ondemand_mode was set before. The requests are resolved asynchronously, |
| 95 // after this call returns. | 100 // after this call returns. |
| 96 void ResolveAllPending(); | 101 void ResolveAllPending(); |
| 97 | 102 |
| 98 // Returns true if there are pending requests that can be resolved by invoking | 103 // Returns true if there are pending requests that can be resolved by invoking |
| 99 // ResolveAllPending(). | 104 // ResolveAllPending(). |
| 100 bool has_pending_requests() const { return !requests_.empty(); } | 105 bool has_pending_requests() const { return !requests_.empty(); } |
| 101 | 106 |
| 102 // The number of times that Resolve() has been called. | 107 // The number of times that Resolve() has been called. |
| 103 size_t num_resolve() const { | 108 size_t num_resolve() const { |
| 104 return num_resolve_; | 109 return num_resolve_; |
| 105 } | 110 } |
| 106 | 111 |
| 107 // The number of times that ResolveFromCache() has been called. | 112 // The number of times that ResolveFromCache() has been called. |
| 108 size_t num_resolve_from_cache() const { | 113 size_t num_resolve_from_cache() const { |
| 109 return num_resolve_from_cache_; | 114 return num_resolve_from_cache_; |
| 110 } | 115 } |
| 111 | 116 |
| 112 // Returns the RequestPriority of the last call to Resolve() (or | 117 // Returns the RequestPriority of the last call to Resolve() (or |
| 113 // DEFAULT_PRIORITY if Resolve() hasn't been called yet). | 118 // DEFAULT_PRIORITY if Resolve() hasn't been called yet). |
| 114 RequestPriority last_request_priority() const { | 119 RequestPriority last_request_priority() const { |
| 115 return last_request_priority_; | 120 return last_request_priority_; |
| 116 } | 121 } |
| 117 | 122 |
| 118 protected: | 123 protected: |
| 119 explicit MockHostResolverBase(bool use_caching); | 124 explicit MockHostResolverBase(bool use_caching); |
| 120 | 125 |
| 121 private: | 126 private: |
| 122 struct Request; | 127 typedef std::map<size_t, RequestImpl*> RequestMap; |
| 123 typedef std::map<size_t, Request*> RequestMap; | |
| 124 | 128 |
| 125 // Resolve as IP or from |cache_| return cached error or | 129 // Resolve as IP or from |cache_| return cached error or |
| 126 // DNS_CACHE_MISS if failed. | 130 // DNS_CACHE_MISS if failed. |
| 127 int ResolveFromIPLiteralOrCache(const RequestInfo& info, | 131 int ResolveFromIPLiteralOrCache(const RequestInfo& info, |
| 128 AddressList* addresses); | 132 AddressList* addresses); |
| 129 // Resolve via |proc_|. | 133 // Resolve via |proc_|. |
| 130 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); | 134 int ResolveProc(const RequestInfo& info, AddressList* addresses); |
| 131 // Resolve request stored in |requests_|. Pass rv to callback. | 135 // Resolve request stored in |requests_|. Pass rv to callback. |
| 132 void ResolveNow(size_t id); | 136 void ResolveNow(size_t id); |
| 133 | 137 |
| 134 RequestPriority last_request_priority_; | 138 RequestPriority last_request_priority_; |
| 135 bool synchronous_mode_; | 139 bool synchronous_mode_; |
| 136 bool ondemand_mode_; | 140 bool ondemand_mode_; |
| 137 scoped_refptr<RuleBasedHostResolverProc> rules_; | 141 scoped_refptr<RuleBasedHostResolverProc> rules_; |
| 138 std::unique_ptr<HostCache> cache_; | 142 std::unique_ptr<HostCache> cache_; |
| 139 RequestMap requests_; | 143 RequestMap requests_; |
| 140 size_t next_request_id_; | 144 size_t next_request_id_; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // Create rules that map all requests to localhost. | 236 // Create rules that map all requests to localhost. |
| 233 RuleBasedHostResolverProc* CreateCatchAllHostResolverProc(); | 237 RuleBasedHostResolverProc* CreateCatchAllHostResolverProc(); |
| 234 | 238 |
| 235 // HangingHostResolver never completes its |Resolve| request. | 239 // HangingHostResolver never completes its |Resolve| request. |
| 236 class HangingHostResolver : public HostResolver { | 240 class HangingHostResolver : public HostResolver { |
| 237 public: | 241 public: |
| 238 int Resolve(const RequestInfo& info, | 242 int Resolve(const RequestInfo& info, |
| 239 RequestPriority priority, | 243 RequestPriority priority, |
| 240 AddressList* addresses, | 244 AddressList* addresses, |
| 241 const CompletionCallback& callback, | 245 const CompletionCallback& callback, |
| 242 RequestHandle* out_req, | 246 std::unique_ptr<Request>* out_req, |
| 243 const BoundNetLog& net_log) override; | 247 const BoundNetLog& net_log) override; |
| 244 int ResolveFromCache(const RequestInfo& info, | 248 int ResolveFromCache(const RequestInfo& info, |
| 245 AddressList* addresses, | 249 AddressList* addresses, |
| 246 const BoundNetLog& net_log) override; | 250 const BoundNetLog& net_log) override; |
| 247 void CancelRequest(RequestHandle req) override {} | |
| 248 }; | 251 }; |
| 249 | 252 |
| 250 // This class sets the default HostResolverProc for a particular scope. The | 253 // This class sets the default HostResolverProc for a particular scope. The |
| 251 // chain of resolver procs starting at |proc| is placed in front of any existing | 254 // chain of resolver procs starting at |proc| is placed in front of any existing |
| 252 // default resolver proc(s). This means that if multiple | 255 // default resolver proc(s). This means that if multiple |
| 253 // ScopedDefaultHostResolverProcs are declared, then resolving will start with | 256 // ScopedDefaultHostResolverProcs are declared, then resolving will start with |
| 254 // the procs given to the last-allocated one, then fall back to the procs given | 257 // the procs given to the last-allocated one, then fall back to the procs given |
| 255 // to the previously-allocated one, and so forth. | 258 // to the previously-allocated one, and so forth. |
| 256 // | 259 // |
| 257 // NOTE: Only use this as a catch-all safety net. Individual tests should use | 260 // NOTE: Only use this as a catch-all safety net. Individual tests should use |
| 258 // MockHostResolver. | 261 // MockHostResolver. |
| 259 class ScopedDefaultHostResolverProc { | 262 class ScopedDefaultHostResolverProc { |
| 260 public: | 263 public: |
| 261 ScopedDefaultHostResolverProc(); | 264 ScopedDefaultHostResolverProc(); |
| 262 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); | 265 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); |
| 263 | 266 |
| 264 ~ScopedDefaultHostResolverProc(); | 267 ~ScopedDefaultHostResolverProc(); |
| 265 | 268 |
| 266 void Init(HostResolverProc* proc); | 269 void Init(HostResolverProc* proc); |
| 267 | 270 |
| 268 private: | 271 private: |
| 269 scoped_refptr<HostResolverProc> current_proc_; | 272 scoped_refptr<HostResolverProc> current_proc_; |
| 270 scoped_refptr<HostResolverProc> previous_proc_; | 273 scoped_refptr<HostResolverProc> previous_proc_; |
| 271 }; | 274 }; |
| 272 | 275 |
| 273 } // namespace net | 276 } // namespace net |
| 274 | 277 |
| 275 #endif // NET_DNS_MOCK_HOST_RESOLVER_H_ | 278 #endif // NET_DNS_MOCK_HOST_RESOLVER_H_ |
| OLD | NEW |