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 "chrome/browser/extensions/api/dns/dns_api.h" | 5 #include "chrome/browser/extensions/api/dns/dns_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/io_thread.h" | 10 #include "chrome/browser/io_thread.h" |
11 #include "chrome/common/extensions/api/experimental.dns.h" | 11 #include "chrome/common/extensions/api/experimental.dns.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/host_port_pair.h" | 13 #include "net/base/host_port_pair.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
16 | 16 |
17 using content::BrowserThread; | 17 using content::BrowserThread; |
18 using extensions::api::experimental::ResolveCallbackResolveInfo; | 18 using extensions::api::experimental::ResolveCallbackResolveInfo; |
19 | 19 |
20 namespace Resolve = extensions::api::experimental::Resolve; | 20 namespace Resolve = extensions::api::experimental::Resolve; |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 | 23 |
24 // static | 24 // static |
25 net::HostResolver* DNSResolveFunction::host_resolver_for_testing; | 25 net::HostResolver* DnsResolveFunction::host_resolver_for_testing; |
26 | 26 |
27 DNSResolveFunction::DNSResolveFunction() | 27 DnsResolveFunction::DnsResolveFunction() |
28 : response_(false), | 28 : response_(false), |
29 io_thread_(g_browser_process->io_thread()), | 29 io_thread_(g_browser_process->io_thread()), |
30 capturing_bound_net_log_(new net::CapturingBoundNetLog( | 30 capturing_bound_net_log_(new net::CapturingBoundNetLog( |
31 net::CapturingNetLog::kUnbounded)), | 31 net::CapturingNetLog::kUnbounded)), |
32 request_handle_(new net::HostResolver::RequestHandle()), | 32 request_handle_(new net::HostResolver::RequestHandle()), |
33 addresses_(new net::AddressList) { | 33 addresses_(new net::AddressList) { |
34 } | 34 } |
35 | 35 |
36 DNSResolveFunction::~DNSResolveFunction() { | 36 DnsResolveFunction::~DnsResolveFunction() { |
37 } | 37 } |
38 | 38 |
39 // static | 39 // static |
40 void DNSResolveFunction::set_host_resolver_for_testing( | 40 void DnsResolveFunction::set_host_resolver_for_testing( |
41 net::HostResolver* host_resolver_for_testing_param) { | 41 net::HostResolver* host_resolver_for_testing_param) { |
42 host_resolver_for_testing = host_resolver_for_testing_param; | 42 host_resolver_for_testing = host_resolver_for_testing_param; |
43 } | 43 } |
44 | 44 |
45 bool DNSResolveFunction::RunImpl() { | 45 bool DnsResolveFunction::RunImpl() { |
46 scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_)); | 46 scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_)); |
47 EXTENSION_FUNCTION_VALIDATE(params.get()); | 47 EXTENSION_FUNCTION_VALIDATE(params.get()); |
48 | 48 |
49 hostname_ = params->hostname; | 49 hostname_ = params->hostname; |
50 | 50 |
51 bool result = BrowserThread::PostTask( | 51 bool result = BrowserThread::PostTask( |
52 BrowserThread::IO, FROM_HERE, | 52 BrowserThread::IO, FROM_HERE, |
53 base::Bind(&DNSResolveFunction::WorkOnIOThread, this)); | 53 base::Bind(&DnsResolveFunction::WorkOnIOThread, this)); |
54 DCHECK(result); | 54 DCHECK(result); |
55 return true; | 55 return true; |
56 } | 56 } |
57 | 57 |
58 void DNSResolveFunction::WorkOnIOThread() { | 58 void DnsResolveFunction::WorkOnIOThread() { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
60 | 60 |
61 net::HostResolver* host_resolver = host_resolver_for_testing ? | 61 net::HostResolver* host_resolver = host_resolver_for_testing ? |
62 host_resolver_for_testing : io_thread_->globals()->host_resolver.get(); | 62 host_resolver_for_testing : io_thread_->globals()->host_resolver.get(); |
63 DCHECK(host_resolver); | 63 DCHECK(host_resolver); |
64 | 64 |
65 // Yes, we are passing zero as the port. There are some interesting but not | 65 // Yes, we are passing zero as the port. There are some interesting but not |
66 // presently relevant reasons why HostResolver asks for the port of the | 66 // presently relevant reasons why HostResolver asks for the port of the |
67 // hostname you'd like to resolve, even though it doesn't use that value in | 67 // hostname you'd like to resolve, even though it doesn't use that value in |
68 // determining its answer. | 68 // determining its answer. |
69 net::HostPortPair host_port_pair(hostname_, 0); | 69 net::HostPortPair host_port_pair(hostname_, 0); |
70 | 70 |
71 net::HostResolver::RequestInfo request_info(host_port_pair); | 71 net::HostResolver::RequestInfo request_info(host_port_pair); |
72 int resolve_result = host_resolver->Resolve( | 72 int resolve_result = host_resolver->Resolve( |
73 request_info, addresses_.get(), | 73 request_info, addresses_.get(), |
74 base::Bind(&DNSResolveFunction::OnLookupFinished, this), | 74 base::Bind(&DnsResolveFunction::OnLookupFinished, this), |
75 request_handle_.get(), capturing_bound_net_log_->bound()); | 75 request_handle_.get(), capturing_bound_net_log_->bound()); |
76 | 76 |
77 // Balanced in OnLookupFinished. | 77 // Balanced in OnLookupFinished. |
78 AddRef(); | 78 AddRef(); |
79 | 79 |
80 if (resolve_result != net::ERR_IO_PENDING) | 80 if (resolve_result != net::ERR_IO_PENDING) |
81 OnLookupFinished(resolve_result); | 81 OnLookupFinished(resolve_result); |
82 } | 82 } |
83 | 83 |
84 void DNSResolveFunction::OnLookupFinished(int resolve_result) { | 84 void DnsResolveFunction::OnLookupFinished(int resolve_result) { |
85 | 85 |
86 scoped_ptr<ResolveCallbackResolveInfo> resolve_info( | 86 scoped_ptr<ResolveCallbackResolveInfo> resolve_info( |
87 new ResolveCallbackResolveInfo()); | 87 new ResolveCallbackResolveInfo()); |
88 resolve_info->result_code = resolve_result; | 88 resolve_info->result_code = resolve_result; |
89 if (resolve_result == net::OK) { | 89 if (resolve_result == net::OK) { |
90 const struct addrinfo* head = addresses_->head(); | 90 const struct addrinfo* head = addresses_->head(); |
91 DCHECK(head); | 91 DCHECK(head); |
92 resolve_info->address.reset( | 92 resolve_info->address.reset( |
93 new std::string(net::NetAddressToString(head))); | 93 new std::string(net::NetAddressToString(head))); |
94 } | 94 } |
95 result_.reset(Resolve::Result::Create(*resolve_info)); | 95 result_.reset(Resolve::Result::Create(*resolve_info)); |
96 response_ = true; | 96 response_ = true; |
97 | 97 |
98 bool post_task_result = BrowserThread::PostTask( | 98 bool post_task_result = BrowserThread::PostTask( |
99 BrowserThread::UI, FROM_HERE, | 99 BrowserThread::UI, FROM_HERE, |
100 base::Bind(&DNSResolveFunction::RespondOnUIThread, this)); | 100 base::Bind(&DnsResolveFunction::RespondOnUIThread, this)); |
101 DCHECK(post_task_result); | 101 DCHECK(post_task_result); |
102 | 102 |
103 Release(); // Added in WorkOnIOThread(). | 103 Release(); // Added in WorkOnIOThread(). |
104 } | 104 } |
105 | 105 |
106 void DNSResolveFunction::RespondOnUIThread() { | 106 void DnsResolveFunction::RespondOnUIThread() { |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
108 SendResponse(response_); | 108 SendResponse(response_); |
109 } | 109 } |
110 | 110 |
111 } // namespace extensions | 111 } // namespace extensions |
OLD | NEW |