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(
32 base::Bind(&MockHostResolverCreator::CreateMockHostResolverOnIOThread
,
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_
)
53 resolver_event_
.Reset();
54 bool result
= BrowserThread::PostTask(
57 base::Bind(&MockHostResolverCreator::DeleteMockHostResolverOnIOThread
,
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