| 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_dns::ResolveCallbackResolveInfo; | 18 using extensions::api::experimental_dns::ResolveCallbackResolveInfo; |
| 19 | 19 |
| 20 namespace Resolve = extensions::api::experimental_dns::Resolve; | 20 namespace Resolve = extensions::api::experimental_dns::Resolve; |
| 21 | 21 |
| 22 namespace extensions { | 22 namespace extensions { |
| 23 | 23 |
| 24 // static | 24 namespace { |
| 25 net::HostResolver* DnsResolveFunction::host_resolver_for_testing; | 25 |
| 26 // If null, then we'll use io_thread_ to obtain the real HostResolver. We use |
| 27 // a plain pointer for to be consistent with the ownership model of the real |
| 28 // one. |
| 29 net::HostResolver* g_host_resolver_for_testing = NULL; |
| 30 |
| 31 } // namespace |
| 26 | 32 |
| 27 DnsResolveFunction::DnsResolveFunction() | 33 DnsResolveFunction::DnsResolveFunction() |
| 28 : response_(false), | 34 : response_(false), |
| 29 io_thread_(g_browser_process->io_thread()), | 35 io_thread_(g_browser_process->io_thread()), |
| 30 capturing_bound_net_log_(new net::CapturingBoundNetLog( | 36 capturing_bound_net_log_(new net::CapturingBoundNetLog( |
| 31 net::CapturingNetLog::kUnbounded)), | 37 net::CapturingNetLog::kUnbounded)), |
| 32 request_handle_(new net::HostResolver::RequestHandle()), | 38 request_handle_(new net::HostResolver::RequestHandle()), |
| 33 addresses_(new net::AddressList) { | 39 addresses_(new net::AddressList) { |
| 34 } | 40 } |
| 35 | 41 |
| 36 DnsResolveFunction::~DnsResolveFunction() { | |
| 37 } | |
| 38 | |
| 39 // static | 42 // static |
| 40 void DnsResolveFunction::set_host_resolver_for_testing( | 43 void DnsResolveFunction::set_host_resolver_for_testing( |
| 41 net::HostResolver* host_resolver_for_testing_param) { | 44 net::HostResolver* host_resolver_for_testing_param) { |
| 42 host_resolver_for_testing = host_resolver_for_testing_param; | 45 g_host_resolver_for_testing = host_resolver_for_testing_param; |
| 43 } | 46 } |
| 44 | 47 |
| 48 DnsResolveFunction::~DnsResolveFunction() {} |
| 49 |
| 45 bool DnsResolveFunction::RunImpl() { | 50 bool DnsResolveFunction::RunImpl() { |
| 46 scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_)); | 51 scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_)); |
| 47 EXTENSION_FUNCTION_VALIDATE(params.get()); | 52 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 48 | 53 |
| 49 hostname_ = params->hostname; | 54 hostname_ = params->hostname; |
| 50 | 55 |
| 51 bool result = BrowserThread::PostTask( | 56 bool result = BrowserThread::PostTask( |
| 52 BrowserThread::IO, FROM_HERE, | 57 BrowserThread::IO, FROM_HERE, |
| 53 base::Bind(&DnsResolveFunction::WorkOnIOThread, this)); | 58 base::Bind(&DnsResolveFunction::WorkOnIOThread, this)); |
| 54 DCHECK(result); | 59 DCHECK(result); |
| 55 return true; | 60 return true; |
| 56 } | 61 } |
| 57 | 62 |
| 58 void DnsResolveFunction::WorkOnIOThread() { | 63 void DnsResolveFunction::WorkOnIOThread() { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 60 | 65 |
| 61 net::HostResolver* host_resolver = host_resolver_for_testing ? | 66 net::HostResolver* host_resolver = g_host_resolver_for_testing ? |
| 62 host_resolver_for_testing : io_thread_->globals()->host_resolver.get(); | 67 g_host_resolver_for_testing : io_thread_->globals()->host_resolver.get(); |
| 63 DCHECK(host_resolver); | 68 DCHECK(host_resolver); |
| 64 | 69 |
| 65 // Yes, we are passing zero as the port. There are some interesting but not | 70 // 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 | 71 // 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 | 72 // hostname you'd like to resolve, even though it doesn't use that value in |
| 68 // determining its answer. | 73 // determining its answer. |
| 69 net::HostPortPair host_port_pair(hostname_, 0); | 74 net::HostPortPair host_port_pair(hostname_, 0); |
| 70 | 75 |
| 71 net::HostResolver::RequestInfo request_info(host_port_pair); | 76 net::HostResolver::RequestInfo request_info(host_port_pair); |
| 72 int resolve_result = host_resolver->Resolve( | 77 int resolve_result = host_resolver->Resolve( |
| 73 request_info, addresses_.get(), | 78 request_info, addresses_.get(), |
| 74 base::Bind(&DnsResolveFunction::OnLookupFinished, this), | 79 base::Bind(&DnsResolveFunction::OnLookupFinished, this), |
| 75 request_handle_.get(), capturing_bound_net_log_->bound()); | 80 request_handle_.get(), capturing_bound_net_log_->bound()); |
| 76 | 81 |
| 77 // Balanced in OnLookupFinished. | 82 // Balanced in OnLookupFinished. |
| 78 AddRef(); | 83 AddRef(); |
| 79 | 84 |
| 80 if (resolve_result != net::ERR_IO_PENDING) | 85 if (resolve_result != net::ERR_IO_PENDING) |
| 81 OnLookupFinished(resolve_result); | 86 OnLookupFinished(resolve_result); |
| 82 } | 87 } |
| 83 | 88 |
| 89 void DnsResolveFunction::RespondOnUIThread() { |
| 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 91 SendResponse(response_); |
| 92 } |
| 93 |
| 84 void DnsResolveFunction::OnLookupFinished(int resolve_result) { | 94 void DnsResolveFunction::OnLookupFinished(int resolve_result) { |
| 85 | |
| 86 scoped_ptr<ResolveCallbackResolveInfo> resolve_info( | 95 scoped_ptr<ResolveCallbackResolveInfo> resolve_info( |
| 87 new ResolveCallbackResolveInfo()); | 96 new ResolveCallbackResolveInfo()); |
| 88 resolve_info->result_code = resolve_result; | 97 resolve_info->result_code = resolve_result; |
| 89 if (resolve_result == net::OK) { | 98 if (resolve_result == net::OK) { |
| 90 const struct addrinfo* head = addresses_->head(); | 99 const struct addrinfo* head = addresses_->head(); |
| 91 DCHECK(head); | 100 DCHECK(head); |
| 92 resolve_info->address.reset( | 101 resolve_info->address.reset( |
| 93 new std::string(net::NetAddressToString(head))); | 102 new std::string(net::NetAddressToString(head))); |
| 94 } | 103 } |
| 95 result_.reset(Resolve::Result::Create(*resolve_info)); | 104 result_.reset(Resolve::Result::Create(*resolve_info)); |
| 96 response_ = true; | 105 response_ = true; |
| 97 | 106 |
| 98 bool post_task_result = BrowserThread::PostTask( | 107 bool post_task_result = BrowserThread::PostTask( |
| 99 BrowserThread::UI, FROM_HERE, | 108 BrowserThread::UI, FROM_HERE, |
| 100 base::Bind(&DnsResolveFunction::RespondOnUIThread, this)); | 109 base::Bind(&DnsResolveFunction::RespondOnUIThread, this)); |
| 101 DCHECK(post_task_result); | 110 DCHECK(post_task_result); |
| 102 | 111 |
| 103 Release(); // Added in WorkOnIOThread(). | 112 Release(); // Added in WorkOnIOThread(). |
| 104 } | 113 } |
| 105 | 114 |
| 106 void DnsResolveFunction::RespondOnUIThread() { | |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 108 SendResponse(response_); | |
| 109 } | |
| 110 | |
| 111 } // namespace extensions | 115 } // namespace extensions |
| OLD | NEW |