| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/observer_list.h" | |
| 14 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 16 #include "net/base/host_resolver.h" | 15 #include "net/base/host_resolver.h" |
| 17 #include "net/base/host_resolver_proc.h" | 16 #include "net/base/host_resolver_proc.h" |
| 18 | 17 |
| 19 namespace net { | 18 namespace net { |
| 20 | 19 |
| 21 class HostCache; | 20 class HostCache; |
| 22 class RuleBasedHostResolverProc; | 21 class RuleBasedHostResolverProc; |
| 23 | 22 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 public: | 55 public: |
| 57 virtual ~MockHostResolverBase(); | 56 virtual ~MockHostResolverBase(); |
| 58 | 57 |
| 59 RuleBasedHostResolverProc* rules() { return rules_; } | 58 RuleBasedHostResolverProc* rules() { return rules_; } |
| 60 | 59 |
| 61 // Controls whether resolutions complete synchronously or asynchronously. | 60 // Controls whether resolutions complete synchronously or asynchronously. |
| 62 void set_synchronous_mode(bool is_synchronous) { | 61 void set_synchronous_mode(bool is_synchronous) { |
| 63 synchronous_mode_ = is_synchronous; | 62 synchronous_mode_ = is_synchronous; |
| 64 } | 63 } |
| 65 | 64 |
| 66 // Resets the mock. | |
| 67 void Reset(HostResolverProc* interceptor); | |
| 68 | |
| 69 // HostResolver methods: | 65 // HostResolver methods: |
| 70 virtual int Resolve(const RequestInfo& info, | 66 virtual int Resolve(const RequestInfo& info, |
| 71 AddressList* addresses, | 67 AddressList* addresses, |
| 72 OldCompletionCallback* callback, | 68 OldCompletionCallback* callback, |
| 73 RequestHandle* out_req, | 69 RequestHandle* out_req, |
| 74 const BoundNetLog& net_log) OVERRIDE; | 70 const BoundNetLog& net_log) OVERRIDE; |
| 75 virtual int ResolveFromCache(const RequestInfo& info, | 71 virtual int ResolveFromCache(const RequestInfo& info, |
| 76 AddressList* addresses, | 72 AddressList* addresses, |
| 77 const BoundNetLog& net_log) OVERRIDE; | 73 const BoundNetLog& net_log) OVERRIDE; |
| 78 virtual void CancelRequest(RequestHandle req) OVERRIDE; | 74 virtual void CancelRequest(RequestHandle req) OVERRIDE; |
| 79 virtual void AddObserver(Observer* observer) OVERRIDE; | |
| 80 virtual void RemoveObserver(Observer* observer) OVERRIDE; | |
| 81 virtual HostCache* GetHostCache() OVERRIDE; | 75 virtual HostCache* GetHostCache() OVERRIDE; |
| 82 | 76 |
| 83 protected: | 77 protected: |
| 84 explicit MockHostResolverBase(bool use_caching); | 78 explicit MockHostResolverBase(bool use_caching); |
| 85 | 79 |
| 86 private: | 80 private: |
| 87 struct Request; | 81 struct Request; |
| 88 typedef std::map<size_t, Request*> RequestMap; | 82 typedef std::map<size_t, Request*> RequestMap; |
| 89 | 83 |
| 90 // Resolve as IP or from |cache_| return cached error or | 84 // Resolve as IP or from |cache_| return cached error or |
| 91 // DNS_CACHE_MISS if failed. | 85 // DNS_CACHE_MISS if failed. |
| 92 int ResolveFromIPLiteralOrCache(const RequestInfo& info, | 86 int ResolveFromIPLiteralOrCache(const RequestInfo& info, |
| 93 AddressList* addresses); | 87 AddressList* addresses); |
| 94 // Resolve via |proc_|. | 88 // Resolve via |proc_|. |
| 95 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); | 89 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); |
| 96 // Resolve request stored in |requests_|. Pass rv to callback. | 90 // Resolve request stored in |requests_|. Pass rv to callback. |
| 97 void ResolveNow(size_t id); | 91 void ResolveNow(size_t id); |
| 98 | 92 |
| 99 bool synchronous_mode_; | 93 bool synchronous_mode_; |
| 100 ObserverList<Observer> observers_; | |
| 101 scoped_refptr<RuleBasedHostResolverProc> rules_; | 94 scoped_refptr<RuleBasedHostResolverProc> rules_; |
| 102 scoped_refptr<HostResolverProc> proc_; | 95 scoped_refptr<HostResolverProc> proc_; |
| 103 scoped_ptr<HostCache> cache_; | 96 scoped_ptr<HostCache> cache_; |
| 104 RequestMap requests_; | 97 RequestMap requests_; |
| 105 size_t next_request_id_; | 98 size_t next_request_id_; |
| 106 | 99 |
| 107 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); | 100 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); |
| 108 }; | 101 }; |
| 109 | 102 |
| 110 class MockHostResolver : public MockHostResolverBase { | 103 class MockHostResolver : public MockHostResolverBase { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 public: | 179 public: |
| 187 virtual int Resolve(const RequestInfo& info, | 180 virtual int Resolve(const RequestInfo& info, |
| 188 AddressList* addresses, | 181 AddressList* addresses, |
| 189 OldCompletionCallback* callback, | 182 OldCompletionCallback* callback, |
| 190 RequestHandle* out_req, | 183 RequestHandle* out_req, |
| 191 const BoundNetLog& net_log) OVERRIDE; | 184 const BoundNetLog& net_log) OVERRIDE; |
| 192 virtual int ResolveFromCache(const RequestInfo& info, | 185 virtual int ResolveFromCache(const RequestInfo& info, |
| 193 AddressList* addresses, | 186 AddressList* addresses, |
| 194 const BoundNetLog& net_log) OVERRIDE; | 187 const BoundNetLog& net_log) OVERRIDE; |
| 195 virtual void CancelRequest(RequestHandle req) OVERRIDE {} | 188 virtual void CancelRequest(RequestHandle req) OVERRIDE {} |
| 196 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
| 197 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
| 198 }; | 189 }; |
| 199 | 190 |
| 200 // This class sets the default HostResolverProc for a particular scope. The | 191 // This class sets the default HostResolverProc for a particular scope. The |
| 201 // chain of resolver procs starting at |proc| is placed in front of any existing | 192 // chain of resolver procs starting at |proc| is placed in front of any existing |
| 202 // default resolver proc(s). This means that if multiple | 193 // default resolver proc(s). This means that if multiple |
| 203 // ScopedDefaultHostResolverProcs are declared, then resolving will start with | 194 // ScopedDefaultHostResolverProcs are declared, then resolving will start with |
| 204 // the procs given to the last-allocated one, then fall back to the procs given | 195 // the procs given to the last-allocated one, then fall back to the procs given |
| 205 // to the previously-allocated one, and so forth. | 196 // to the previously-allocated one, and so forth. |
| 206 // | 197 // |
| 207 // NOTE: Only use this as a catch-all safety net. Individual tests should use | 198 // NOTE: Only use this as a catch-all safety net. Individual tests should use |
| 208 // MockHostResolver. | 199 // MockHostResolver. |
| 209 class ScopedDefaultHostResolverProc { | 200 class ScopedDefaultHostResolverProc { |
| 210 public: | 201 public: |
| 211 ScopedDefaultHostResolverProc(); | 202 ScopedDefaultHostResolverProc(); |
| 212 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); | 203 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); |
| 213 | 204 |
| 214 ~ScopedDefaultHostResolverProc(); | 205 ~ScopedDefaultHostResolverProc(); |
| 215 | 206 |
| 216 void Init(HostResolverProc* proc); | 207 void Init(HostResolverProc* proc); |
| 217 | 208 |
| 218 private: | 209 private: |
| 219 scoped_refptr<HostResolverProc> current_proc_; | 210 scoped_refptr<HostResolverProc> current_proc_; |
| 220 scoped_refptr<HostResolverProc> previous_proc_; | 211 scoped_refptr<HostResolverProc> previous_proc_; |
| 221 }; | 212 }; |
| 222 | 213 |
| 223 } // namespace net | 214 } // namespace net |
| 224 | 215 |
| 225 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ | 216 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ |
| OLD | NEW |