Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/dns/mock_host_resolver.h" | 5 #include "net/dns/mock_host_resolver.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 | 240 |
| 241 RequestImpl* req = it->second; | 241 RequestImpl* req = it->second; |
| 242 requests_.erase(it); | 242 requests_.erase(it); |
| 243 | 243 |
| 244 int error = ResolveProc(req->info(), req->addresses()); | 244 int error = ResolveProc(req->info(), req->addresses()); |
| 245 req->OnResolveCompleted(this, error); | 245 req->OnResolveCompleted(this, error); |
| 246 } | 246 } |
| 247 | 247 |
| 248 //----------------------------------------------------------------------------- | 248 //----------------------------------------------------------------------------- |
| 249 | 249 |
| 250 struct RuleBasedHostResolverProc::Rule { | 250 RuleBasedHostResolverProc::Rule::Rule( |
| 251 enum ResolverType { | 251 ResolverType resolver_type, |
| 252 kResolverTypeFail, | 252 const std::string& host_pattern, |
| 253 kResolverTypeSystem, | 253 AddressFamily address_family, |
| 254 kResolverTypeIPLiteral, | 254 HostResolverFlags host_resolver_flags, |
| 255 }; | 255 const std::string& replacement, |
| 256 const std::string& canonical_name, | |
| 257 int latency_ms) | |
| 258 : resolver_type(resolver_type), | |
| 259 host_pattern(host_pattern), | |
| 260 address_family(address_family), | |
| 261 host_resolver_flags(host_resolver_flags), | |
| 262 replacement(replacement), | |
| 263 canonical_name(canonical_name), | |
| 264 latency_ms(latency_ms) {} | |
| 256 | 265 |
| 257 ResolverType resolver_type; | 266 RuleBasedHostResolverProc::Rule::Rule(const Rule& other) = default; |
| 258 std::string host_pattern; | |
| 259 AddressFamily address_family; | |
| 260 HostResolverFlags host_resolver_flags; | |
| 261 std::string replacement; | |
| 262 std::string canonical_name; | |
| 263 int latency_ms; // In milliseconds. | |
| 264 | |
| 265 Rule(ResolverType resolver_type, | |
| 266 const std::string& host_pattern, | |
| 267 AddressFamily address_family, | |
| 268 HostResolverFlags host_resolver_flags, | |
| 269 const std::string& replacement, | |
| 270 const std::string& canonical_name, | |
| 271 int latency_ms) | |
| 272 : resolver_type(resolver_type), | |
| 273 host_pattern(host_pattern), | |
| 274 address_family(address_family), | |
| 275 host_resolver_flags(host_resolver_flags), | |
| 276 replacement(replacement), | |
| 277 canonical_name(canonical_name), | |
| 278 latency_ms(latency_ms) {} | |
| 279 }; | |
| 280 | 267 |
| 281 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) | 268 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) |
| 282 : HostResolverProc(previous) { | 269 : HostResolverProc(previous), locked_(false) {} |
| 283 } | |
| 284 | 270 |
| 285 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, | 271 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, |
| 286 const std::string& replacement) { | 272 const std::string& replacement) { |
| 287 AddRuleForAddressFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, | 273 AddRuleForAddressFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, |
| 288 replacement); | 274 replacement); |
| 289 } | 275 } |
| 290 | 276 |
| 291 void RuleBasedHostResolverProc::AddRuleForAddressFamily( | 277 void RuleBasedHostResolverProc::AddRuleForAddressFamily( |
| 292 const std::string& host_pattern, | 278 const std::string& host_pattern, |
| 293 AddressFamily address_family, | 279 AddressFamily address_family, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 host_pattern, | 348 host_pattern, |
| 363 ADDRESS_FAMILY_UNSPECIFIED, | 349 ADDRESS_FAMILY_UNSPECIFIED, |
| 364 flags, | 350 flags, |
| 365 std::string(), | 351 std::string(), |
| 366 std::string(), | 352 std::string(), |
| 367 0); | 353 0); |
| 368 AddRuleInternal(rule); | 354 AddRuleInternal(rule); |
| 369 } | 355 } |
| 370 | 356 |
| 371 void RuleBasedHostResolverProc::ClearRules() { | 357 void RuleBasedHostResolverProc::ClearRules() { |
| 358 CHECK(!locked_); | |
|
mmenke
2017/04/25 16:37:51
include base/logging.h
jam
2017/04/25 17:04:40
Done.
| |
| 372 base::AutoLock lock(rule_lock_); | 359 base::AutoLock lock(rule_lock_); |
| 373 rules_.clear(); | 360 rules_.clear(); |
| 374 } | 361 } |
| 375 | 362 |
| 363 void RuleBasedHostResolverProc::Lock() { | |
| 364 CHECK(!locked_); | |
| 365 locked_ = true; | |
| 366 } | |
| 367 | |
| 368 RuleBasedHostResolverProc::RuleList RuleBasedHostResolverProc::GetRules() { | |
| 369 RuleList rv; | |
| 370 { | |
| 371 base::AutoLock lock(rule_lock_); | |
| 372 rv = rules_; | |
| 373 } | |
| 374 return rv; | |
| 375 } | |
| 376 | |
| 376 int RuleBasedHostResolverProc::Resolve(const std::string& host, | 377 int RuleBasedHostResolverProc::Resolve(const std::string& host, |
| 377 AddressFamily address_family, | 378 AddressFamily address_family, |
| 378 HostResolverFlags host_resolver_flags, | 379 HostResolverFlags host_resolver_flags, |
| 379 AddressList* addrlist, | 380 AddressList* addrlist, |
| 380 int* os_error) { | 381 int* os_error) { |
| 381 base::AutoLock lock(rule_lock_); | 382 base::AutoLock lock(rule_lock_); |
| 382 RuleList::iterator r; | 383 RuleList::iterator r; |
| 383 for (r = rules_.begin(); r != rules_.end(); ++r) { | 384 for (r = rules_.begin(); r != rules_.end(); ++r) { |
| 384 bool matches_address_family = | 385 bool matches_address_family = |
| 385 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || | 386 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 } | 427 } |
| 427 } | 428 } |
| 428 return ResolveUsingPrevious(host, address_family, | 429 return ResolveUsingPrevious(host, address_family, |
| 429 host_resolver_flags, addrlist, os_error); | 430 host_resolver_flags, addrlist, os_error); |
| 430 } | 431 } |
| 431 | 432 |
| 432 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { | 433 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { |
| 433 } | 434 } |
| 434 | 435 |
| 435 void RuleBasedHostResolverProc::AddRuleInternal(const Rule& rule) { | 436 void RuleBasedHostResolverProc::AddRuleInternal(const Rule& rule) { |
| 437 CHECK(!locked_); | |
| 436 base::AutoLock lock(rule_lock_); | 438 base::AutoLock lock(rule_lock_); |
| 437 rules_.push_back(rule); | 439 rules_.push_back(rule); |
| 438 } | 440 } |
| 439 | 441 |
| 440 RuleBasedHostResolverProc* CreateCatchAllHostResolverProc() { | 442 RuleBasedHostResolverProc* CreateCatchAllHostResolverProc() { |
| 441 RuleBasedHostResolverProc* catchall = new RuleBasedHostResolverProc(NULL); | 443 RuleBasedHostResolverProc* catchall = new RuleBasedHostResolverProc(NULL); |
| 442 catchall->AddIPLiteralRule("*", "127.0.0.1", "localhost"); | 444 catchall->AddIPLiteralRule("*", "127.0.0.1", "localhost"); |
| 443 | 445 |
| 444 // Next add a rules-based layer the use controls. | 446 // Next add a rules-based layer the use controls. |
| 445 return new RuleBasedHostResolverProc(catchall); | 447 return new RuleBasedHostResolverProc(catchall); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 CHECK_EQ(old_proc, current_proc_.get()); | 480 CHECK_EQ(old_proc, current_proc_.get()); |
| 479 } | 481 } |
| 480 | 482 |
| 481 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 483 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 482 current_proc_ = proc; | 484 current_proc_ = proc; |
| 483 previous_proc_ = HostResolverProc::SetDefault(current_proc_.get()); | 485 previous_proc_ = HostResolverProc::SetDefault(current_proc_.get()); |
| 484 current_proc_->SetLastProc(previous_proc_.get()); | 486 current_proc_->SetLastProc(previous_proc_.get()); |
| 485 } | 487 } |
| 486 | 488 |
| 487 } // namespace net | 489 } // namespace net |
| OLD | NEW |