Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / dns / dns_api.cc
blob5513e3a081ef9b6298ae2b441f17d1962e20215b
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/dns_api.h"
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/resource_context.h"
12 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
13 #include "extensions/common/api/dns.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_errors.h"
16 #include "net/log/net_log.h"
18 using content::BrowserThread;
19 using extensions::api::dns::ResolveCallbackResolveInfo;
21 namespace Resolve = extensions::api::dns::Resolve;
23 namespace extensions {
25 DnsResolveFunction::DnsResolveFunction()
26 : resource_context_(NULL),
27 response_(false),
28 request_handle_(new net::HostResolver::RequestHandle()),
29 addresses_(new net::AddressList) {}
31 DnsResolveFunction::~DnsResolveFunction() {}
33 bool DnsResolveFunction::RunAsync() {
34 scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_));
35 EXTENSION_FUNCTION_VALIDATE(params.get());
37 hostname_ = params->hostname;
38 resource_context_ = browser_context()->GetResourceContext();
40 bool result = BrowserThread::PostTask(
41 BrowserThread::IO,
42 FROM_HERE,
43 base::Bind(&DnsResolveFunction::WorkOnIOThread, this));
44 DCHECK(result);
45 return true;
48 void DnsResolveFunction::WorkOnIOThread() {
49 DCHECK_CURRENTLY_ON(BrowserThread::IO);
51 net::HostResolver* host_resolver =
52 HostResolverWrapper::GetInstance()->GetHostResolver(resource_context_);
53 DCHECK(host_resolver);
55 // Yes, we are passing zero as the port. There are some interesting but not
56 // presently relevant reasons why HostResolver asks for the port of the
57 // hostname you'd like to resolve, even though it doesn't use that value in
58 // determining its answer.
59 net::HostPortPair host_port_pair(hostname_, 0);
61 net::HostResolver::RequestInfo request_info(host_port_pair);
62 int resolve_result = host_resolver->Resolve(
63 request_info,
64 net::DEFAULT_PRIORITY,
65 addresses_.get(),
66 base::Bind(&DnsResolveFunction::OnLookupFinished, this),
67 request_handle_.get(),
68 net::BoundNetLog());
70 // Balanced in OnLookupFinished.
71 AddRef();
73 if (resolve_result != net::ERR_IO_PENDING)
74 OnLookupFinished(resolve_result);
77 void DnsResolveFunction::RespondOnUIThread() {
78 DCHECK_CURRENTLY_ON(BrowserThread::UI);
79 SendResponse(response_);
82 void DnsResolveFunction::OnLookupFinished(int resolve_result) {
83 scoped_ptr<ResolveCallbackResolveInfo> resolve_info(
84 new ResolveCallbackResolveInfo());
85 resolve_info->result_code = resolve_result;
86 if (resolve_result == net::OK) {
87 DCHECK(!addresses_->empty());
88 resolve_info->address.reset(
89 new std::string(addresses_->front().ToStringWithoutPort()));
91 results_ = Resolve::Results::Create(*resolve_info);
92 response_ = true;
94 bool post_task_result = BrowserThread::PostTask(
95 BrowserThread::UI,
96 FROM_HERE,
97 base::Bind(&DnsResolveFunction::RespondOnUIThread, this));
98 DCHECK(post_task_result);
100 Release(); // Added in WorkOnIOThread().
103 } // namespace extensions