| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "net/base/mock_host_resolver.h" | 5 #include "net/base/mock_host_resolver.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
| 9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 AddressList result(ip_number, -1, false); | 39 AddressList result(ip_number, -1, false); |
| 40 struct addrinfo* ai = const_cast<struct addrinfo*>(result.head()); | 40 struct addrinfo* ai = const_cast<struct addrinfo*>(result.head()); |
| 41 ai->ai_canonname = do_strdup(canonical_name.c_str()); | 41 ai->ai_canonname = do_strdup(canonical_name.c_str()); |
| 42 *addrlist = result; | 42 *addrlist = result; |
| 43 return OK; | 43 return OK; |
| 44 } | 44 } |
| 45 | 45 |
| 46 } // namespace | 46 } // namespace |
| 47 | 47 |
| 48 MockHostResolverBase::MockHostResolverBase(bool use_caching) | |
| 49 : use_caching_(use_caching) { | |
| 50 Reset(NULL); | |
| 51 } | |
| 52 | |
| 53 MockHostResolverBase::~MockHostResolverBase() {} | 48 MockHostResolverBase::~MockHostResolverBase() {} |
| 54 | 49 |
| 55 int MockHostResolverBase::Resolve(const RequestInfo& info, | |
| 56 AddressList* addresses, | |
| 57 CompletionCallback* callback, | |
| 58 RequestHandle* out_req, | |
| 59 const BoundNetLog& net_log) { | |
| 60 if (synchronous_mode_) { | |
| 61 callback = NULL; | |
| 62 out_req = NULL; | |
| 63 } | |
| 64 return impl_->Resolve(info, addresses, callback, out_req, net_log); | |
| 65 } | |
| 66 | |
| 67 void MockHostResolverBase::CancelRequest(RequestHandle req) { | |
| 68 impl_->CancelRequest(req); | |
| 69 } | |
| 70 | |
| 71 void MockHostResolverBase::AddObserver(Observer* observer) { | |
| 72 impl_->AddObserver(observer); | |
| 73 } | |
| 74 | |
| 75 void MockHostResolverBase::RemoveObserver(Observer* observer) { | |
| 76 impl_->RemoveObserver(observer); | |
| 77 } | |
| 78 | |
| 79 void MockHostResolverBase::Reset(HostResolverProc* interceptor) { | 50 void MockHostResolverBase::Reset(HostResolverProc* interceptor) { |
| 80 synchronous_mode_ = false; | 51 synchronous_mode_ = false; |
| 81 | 52 |
| 82 // At the root of the chain, map everything to localhost. | 53 // At the root of the chain, map everything to localhost. |
| 83 scoped_refptr<RuleBasedHostResolverProc> catchall( | 54 scoped_refptr<RuleBasedHostResolverProc> catchall( |
| 84 new RuleBasedHostResolverProc(NULL)); | 55 new RuleBasedHostResolverProc(NULL)); |
| 85 catchall->AddRule("*", "127.0.0.1"); | 56 catchall->AddRule("*", "127.0.0.1"); |
| 86 | 57 |
| 87 // Next add a rules-based layer the use controls. | 58 // Next add a rules-based layer the use controls. |
| 88 rules_ = new RuleBasedHostResolverProc(catchall); | 59 rules_ = new RuleBasedHostResolverProc(catchall); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 100 if (use_caching_) { | 71 if (use_caching_) { |
| 101 cache = new HostCache( | 72 cache = new HostCache( |
| 102 100, // max entries. | 73 100, // max entries. |
| 103 base::TimeDelta::FromMinutes(1), | 74 base::TimeDelta::FromMinutes(1), |
| 104 base::TimeDelta::FromSeconds(0)); | 75 base::TimeDelta::FromSeconds(0)); |
| 105 } | 76 } |
| 106 | 77 |
| 107 impl_.reset(new HostResolverImpl(proc, cache, 50u, NULL)); | 78 impl_.reset(new HostResolverImpl(proc, cache, 50u, NULL)); |
| 108 } | 79 } |
| 109 | 80 |
| 81 int MockHostResolverBase::Resolve(const RequestInfo& info, |
| 82 AddressList* addresses, |
| 83 CompletionCallback* callback, |
| 84 RequestHandle* out_req, |
| 85 const BoundNetLog& net_log) { |
| 86 if (synchronous_mode_) { |
| 87 callback = NULL; |
| 88 out_req = NULL; |
| 89 } |
| 90 return impl_->Resolve(info, addresses, callback, out_req, net_log); |
| 91 } |
| 92 |
| 93 void MockHostResolverBase::CancelRequest(RequestHandle req) { |
| 94 impl_->CancelRequest(req); |
| 95 } |
| 96 |
| 97 void MockHostResolverBase::AddObserver(Observer* observer) { |
| 98 impl_->AddObserver(observer); |
| 99 } |
| 100 |
| 101 void MockHostResolverBase::RemoveObserver(Observer* observer) { |
| 102 impl_->RemoveObserver(observer); |
| 103 } |
| 104 |
| 105 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
| 106 : use_caching_(use_caching) { |
| 107 Reset(NULL); |
| 108 } |
| 109 |
| 110 //----------------------------------------------------------------------------- | 110 //----------------------------------------------------------------------------- |
| 111 | 111 |
| 112 struct RuleBasedHostResolverProc::Rule { | 112 struct RuleBasedHostResolverProc::Rule { |
| 113 enum ResolverType { | 113 enum ResolverType { |
| 114 kResolverTypeFail, | 114 kResolverTypeFail, |
| 115 kResolverTypeSystem, | 115 kResolverTypeSystem, |
| 116 kResolverTypeIPLiteral, | 116 kResolverTypeIPLiteral, |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 ResolverType resolver_type; | 119 ResolverType resolver_type; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 host_resolver_flags(host_resolver_flags), | 137 host_resolver_flags(host_resolver_flags), |
| 138 replacement(replacement), | 138 replacement(replacement), |
| 139 canonical_name(canonical_name), | 139 canonical_name(canonical_name), |
| 140 latency_ms(latency_ms) {} | 140 latency_ms(latency_ms) {} |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) | 143 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) |
| 144 : HostResolverProc(previous) { | 144 : HostResolverProc(previous) { |
| 145 } | 145 } |
| 146 | 146 |
| 147 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { | |
| 148 } | |
| 149 | |
| 150 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, | 147 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, |
| 151 const std::string& replacement) { | 148 const std::string& replacement) { |
| 152 AddRuleForAddressFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, | 149 AddRuleForAddressFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, |
| 153 replacement); | 150 replacement); |
| 154 } | 151 } |
| 155 | 152 |
| 156 void RuleBasedHostResolverProc::AddRuleForAddressFamily( | 153 void RuleBasedHostResolverProc::AddRuleForAddressFamily( |
| 157 const std::string& host_pattern, | 154 const std::string& host_pattern, |
| 158 AddressFamily address_family, | 155 AddressFamily address_family, |
| 159 const std::string& replacement) { | 156 const std::string& replacement) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 default: | 248 default: |
| 252 NOTREACHED(); | 249 NOTREACHED(); |
| 253 return ERR_UNEXPECTED; | 250 return ERR_UNEXPECTED; |
| 254 } | 251 } |
| 255 } | 252 } |
| 256 } | 253 } |
| 257 return ResolveUsingPrevious(host, address_family, | 254 return ResolveUsingPrevious(host, address_family, |
| 258 host_resolver_flags, addrlist, os_error); | 255 host_resolver_flags, addrlist, os_error); |
| 259 } | 256 } |
| 260 | 257 |
| 258 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { |
| 259 } |
| 260 |
| 261 //----------------------------------------------------------------------------- | 261 //----------------------------------------------------------------------------- |
| 262 | 262 |
| 263 WaitingHostResolverProc::WaitingHostResolverProc(HostResolverProc* previous) | 263 WaitingHostResolverProc::WaitingHostResolverProc(HostResolverProc* previous) |
| 264 : HostResolverProc(previous), event_(false, false) {} | 264 : HostResolverProc(previous), event_(false, false) {} |
| 265 | 265 |
| 266 void WaitingHostResolverProc::Signal() { | 266 void WaitingHostResolverProc::Signal() { |
| 267 event_.Signal(); | 267 event_.Signal(); |
| 268 } | 268 } |
| 269 | 269 |
| 270 int WaitingHostResolverProc::Resolve(const std::string& host, | 270 int WaitingHostResolverProc::Resolve(const std::string& host, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 294 CHECK_EQ(old_proc, current_proc_); | 294 CHECK_EQ(old_proc, current_proc_); |
| 295 } | 295 } |
| 296 | 296 |
| 297 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 297 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 298 current_proc_ = proc; | 298 current_proc_ = proc; |
| 299 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 299 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 300 current_proc_->SetLastProc(previous_proc_); | 300 current_proc_->SetLastProc(previous_proc_); |
| 301 } | 301 } |
| 302 | 302 |
| 303 } // namespace net | 303 } // namespace net |
| OLD | NEW |