| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/platform_thread.h" | 8 #include "base/platform_thread.h" |
| 9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 10 #include "googleurl/src/url_canon_ip.h" | 10 #include "googleurl/src/url_canon_ip.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 rules_.push_back(rule); | 158 rules_.push_back(rule); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void RuleBasedHostResolverProc::AddSimulatedFailure( | 161 void RuleBasedHostResolverProc::AddSimulatedFailure( |
| 162 const std::string& host_pattern) { | 162 const std::string& host_pattern) { |
| 163 Rule rule(Rule::kResolverTypeFail, host_pattern, "", 0); | 163 Rule rule(Rule::kResolverTypeFail, host_pattern, "", 0); |
| 164 rules_.push_back(rule); | 164 rules_.push_back(rule); |
| 165 } | 165 } |
| 166 | 166 |
| 167 int RuleBasedHostResolverProc::Resolve(const std::string& host, | 167 int RuleBasedHostResolverProc::Resolve(const std::string& host, |
| 168 AddressFamily address_family, |
| 168 AddressList* addrlist) { | 169 AddressList* addrlist) { |
| 169 RuleList::iterator r; | 170 RuleList::iterator r; |
| 170 for (r = rules_.begin(); r != rules_.end(); ++r) { | 171 for (r = rules_.begin(); r != rules_.end(); ++r) { |
| 171 if (MatchPattern(host, r->host_pattern)) { | 172 if (MatchPattern(host, r->host_pattern)) { |
| 172 if (r->latency_ms != 0) | 173 if (r->latency_ms != 0) |
| 173 PlatformThread::Sleep(r->latency_ms); | 174 PlatformThread::Sleep(r->latency_ms); |
| 174 | 175 |
| 175 // Remap to a new host. | 176 // Remap to a new host. |
| 176 const std::string& effective_host = | 177 const std::string& effective_host = |
| 177 r->replacement.empty() ? host : r->replacement; | 178 r->replacement.empty() ? host : r->replacement; |
| 178 | 179 |
| 179 // Apply the resolving function to the remapped hostname. | 180 // Apply the resolving function to the remapped hostname. |
| 180 switch (r->resolver_type) { | 181 switch (r->resolver_type) { |
| 181 case Rule::kResolverTypeFail: | 182 case Rule::kResolverTypeFail: |
| 182 return ERR_NAME_NOT_RESOLVED; | 183 return ERR_NAME_NOT_RESOLVED; |
| 183 case Rule::kResolverTypeSystem: | 184 case Rule::kResolverTypeSystem: |
| 184 return SystemHostResolverProc(effective_host, addrlist); | 185 return SystemHostResolverProc(effective_host, |
| 186 address_family, |
| 187 addrlist); |
| 185 case Rule::kResolverTypeIPV6Literal: | 188 case Rule::kResolverTypeIPV6Literal: |
| 186 return ResolveIPV6LiteralUsingGURL(effective_host, addrlist); | 189 return ResolveIPV6LiteralUsingGURL(effective_host, addrlist); |
| 187 default: | 190 default: |
| 188 NOTREACHED(); | 191 NOTREACHED(); |
| 189 return ERR_UNEXPECTED; | 192 return ERR_UNEXPECTED; |
| 190 } | 193 } |
| 191 } | 194 } |
| 192 } | 195 } |
| 193 return ResolveUsingPrevious(host, addrlist); | 196 return ResolveUsingPrevious(host, address_family, addrlist); |
| 194 } | 197 } |
| 195 | 198 |
| 196 //----------------------------------------------------------------------------- | 199 //----------------------------------------------------------------------------- |
| 197 | 200 |
| 198 ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc( | 201 ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc( |
| 199 HostResolverProc* proc) : current_proc_(proc) { | 202 HostResolverProc* proc) : current_proc_(proc) { |
| 200 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 203 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 201 current_proc_->set_previous_proc(previous_proc_); | 204 current_proc_->set_previous_proc(previous_proc_); |
| 202 } | 205 } |
| 203 | 206 |
| 204 ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() { | 207 ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() { |
| 205 HostResolverProc* old_proc = HostResolverProc::SetDefault(previous_proc_); | 208 HostResolverProc* old_proc = HostResolverProc::SetDefault(previous_proc_); |
| 206 // The lifetimes of multiple instances must be nested. | 209 // The lifetimes of multiple instances must be nested. |
| 207 CHECK(old_proc == current_proc_); | 210 CHECK(old_proc == current_proc_); |
| 208 } | 211 } |
| 209 | 212 |
| 210 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 213 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 211 current_proc_ = proc; | 214 current_proc_ = proc; |
| 212 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 215 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 213 current_proc_->set_previous_proc(previous_proc_); | 216 current_proc_->set_previous_proc(previous_proc_); |
| 214 } | 217 } |
| 215 | 218 |
| 216 } // namespace net | 219 } // namespace net |
| OLD | NEW |