Skip to content

Commit

Permalink
Make RuleBasedHostResolverProc methods to add/remove rules threadsafe.
Browse files Browse the repository at this point in the history
They are currently being used unsafely on the UI thread after browser
startup in a lot of tests.  Seems simplest to just make that pattern
safe.

BUG=500595

Review URL: https://codereview.chromium.org/1267003006

Cr-Commit-Position: refs/heads/master@{#341757}
  • Loading branch information
mmenke authored and Commit bot committed Aug 4, 2015
1 parent 99b5189 commit a29b91e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 12 additions & 5 deletions net/dns/mock_host_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void RuleBasedHostResolverProc::AddRuleForAddressFamily(
replacement,
std::string(),
0);
rules_.push_back(rule);
AddRuleInternal(rule);
}

void RuleBasedHostResolverProc::AddIPLiteralRule(
Expand All @@ -294,7 +294,7 @@ void RuleBasedHostResolverProc::AddIPLiteralRule(
Rule rule(Rule::kResolverTypeIPLiteral, host_pattern,
ADDRESS_FAMILY_UNSPECIFIED, flags, ip_literal, canonical_name,
0);
rules_.push_back(rule);
AddRuleInternal(rule);
}

void RuleBasedHostResolverProc::AddRuleWithLatency(
Expand All @@ -311,7 +311,7 @@ void RuleBasedHostResolverProc::AddRuleWithLatency(
replacement,
std::string(),
latency_ms);
rules_.push_back(rule);
AddRuleInternal(rule);
}

void RuleBasedHostResolverProc::AllowDirectLookup(
Expand All @@ -325,7 +325,7 @@ void RuleBasedHostResolverProc::AllowDirectLookup(
std::string(),
std::string(),
0);
rules_.push_back(rule);
AddRuleInternal(rule);
}

void RuleBasedHostResolverProc::AddSimulatedFailure(
Expand All @@ -339,10 +339,11 @@ void RuleBasedHostResolverProc::AddSimulatedFailure(
std::string(),
std::string(),
0);
rules_.push_back(rule);
AddRuleInternal(rule);
}

void RuleBasedHostResolverProc::ClearRules() {
base::AutoLock lock(rule_lock_);
rules_.clear();
}

Expand All @@ -351,6 +352,7 @@ int RuleBasedHostResolverProc::Resolve(const std::string& host,
HostResolverFlags host_resolver_flags,
AddressList* addrlist,
int* os_error) {
base::AutoLock lock(rule_lock_);
RuleList::iterator r;
for (r = rules_.begin(); r != rules_.end(); ++r) {
bool matches_address_family =
Expand Down Expand Up @@ -404,6 +406,11 @@ int RuleBasedHostResolverProc::Resolve(const std::string& host,
RuleBasedHostResolverProc::~RuleBasedHostResolverProc() {
}

void RuleBasedHostResolverProc::AddRuleInternal(const Rule& rule) {
base::AutoLock lock(rule_lock_);
rules_.push_back(rule);
}

RuleBasedHostResolverProc* CreateCatchAllHostResolverProc() {
RuleBasedHostResolverProc* catchall = new RuleBasedHostResolverProc(NULL);
catchall->AddIPLiteralRule("*", "127.0.0.1", "localhost");
Expand Down
9 changes: 9 additions & 0 deletions net/dns/mock_host_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <map>

#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/non_thread_safe.h"
#include "net/dns/host_resolver.h"
Expand Down Expand Up @@ -162,6 +163,9 @@ class MockCachingHostResolver : public MockHostResolverBase {
// a replacement host string. It then uses the system host resolver to return
// a socket address. Generally the replacement should be an IPv4 literal so
// there is no network dependency.
//
// RuleBasedHostResolverProc is thread-safe, to a limited degree. Rules can be
// added or removed on any thread.
class RuleBasedHostResolverProc : public HostResolverProc {
public:
explicit RuleBasedHostResolverProc(HostResolverProc* previous);
Expand Down Expand Up @@ -214,7 +218,12 @@ class RuleBasedHostResolverProc : public HostResolverProc {

~RuleBasedHostResolverProc() override;

void AddRuleInternal(const Rule& rule);

RuleList rules_;

// Must be obtained before writing to or reading from |rules_|.
base::Lock rule_lock_;
};

// Create rules that map all requests to localhost.
Expand Down

0 comments on commit a29b91e

Please sign in to comment.