Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / dns / mock_host_resolver_creator.cc
blobe2841e8f2cf2f751172aba131c6c012b0da43022
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/api/dns/mock_host_resolver_creator.h"
7 #include "base/logging.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "net/dns/mock_host_resolver.h"
11 using content::BrowserThread;
13 namespace extensions {
15 const std::string MockHostResolverCreator::kHostname = "www.sowbug.com";
16 const std::string MockHostResolverCreator::kAddress = "9.8.7.6";
18 MockHostResolverCreator::MockHostResolverCreator()
19 : resolver_event_(true, false), mock_host_resolver_(NULL) {
22 MockHostResolverCreator::~MockHostResolverCreator() {
25 net::HostResolver* MockHostResolverCreator::CreateMockHostResolver() {
26 DCHECK(!mock_host_resolver_);
27 DCHECK_CURRENTLY_ON(BrowserThread::UI);
29 bool result = BrowserThread::PostTask(
30 BrowserThread::IO,
31 FROM_HERE,
32 base::Bind(&MockHostResolverCreator::CreateMockHostResolverOnIOThread,
33 this));
34 DCHECK(result);
36 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5);
37 resolver_event_.TimedWait(max_time);
39 return mock_host_resolver_;
42 void MockHostResolverCreator::CreateMockHostResolverOnIOThread() {
43 mock_host_resolver_ = new net::MockHostResolver();
44 mock_host_resolver_->rules()->AddRule(kHostname, kAddress);
45 mock_host_resolver_->rules()->AddSimulatedFailure("this.hostname.is.bogus");
46 resolver_event_.Signal();
49 void MockHostResolverCreator::DeleteMockHostResolver() {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI);
51 if (!mock_host_resolver_)
52 return;
53 resolver_event_.Reset();
54 bool result = BrowserThread::PostTask(
55 BrowserThread::IO,
56 FROM_HERE,
57 base::Bind(&MockHostResolverCreator::DeleteMockHostResolverOnIOThread,
58 this));
59 DCHECK(result);
61 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5);
62 CHECK(resolver_event_.TimedWait(max_time));
65 void MockHostResolverCreator::DeleteMockHostResolverOnIOThread() {
66 delete (mock_host_resolver_);
67 mock_host_resolver_ = NULL;
68 resolver_event_.Signal();
71 } // namespace extensions