Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/dns/dns_api.h" | 5 #include "extensions/browser/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 "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/resource_context.h" | 11 #include "content/public/browser/resource_context.h" |
| 12 #include "extensions/browser/api/dns/host_resolver_wrapper.h" | 12 #include "extensions/browser/api/dns/host_resolver_wrapper.h" |
| 13 #include "extensions/common/api/dns.h" | 13 #include "extensions/common/api/dns.h" |
| 14 #include "net/base/host_port_pair.h" | 14 #include "net/base/host_port_pair.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
| 17 | 17 |
| 18 using content::BrowserThread; | 18 using content::BrowserThread; |
| 19 using extensions::api::dns::ResolveCallbackResolveInfo; | 19 using extensions::api::dns::ResolveCallbackResolveInfo; |
| 20 | 20 |
| 21 namespace Resolve = extensions::api::dns::Resolve; | 21 namespace Resolve = extensions::api::dns::Resolve; |
| 22 | 22 |
| 23 namespace extensions { | 23 namespace extensions { |
| 24 | 24 |
| 25 DnsResolveFunction::DnsResolveFunction() | 25 DnsResolveFunction::DnsResolveFunction() |
| 26 : resource_context_(NULL), | 26 : resource_context_(NULL), |
| 27 response_(false), | 27 response_(false), |
| 28 request_handle_(new net::HostResolver::RequestHandle()), | 28 request_handle_(nullptr), |
|
Devlin
2016/07/12 15:04:51
unnecessary (this is the default construction of s
maksims (do not use this acc)
2016/07/19 15:00:25
Done.
| |
| 29 addresses_(new net::AddressList) {} | 29 addresses_(new net::AddressList) {} |
| 30 | 30 |
| 31 DnsResolveFunction::~DnsResolveFunction() {} | 31 DnsResolveFunction::~DnsResolveFunction() {} |
| 32 | 32 |
| 33 bool DnsResolveFunction::RunAsync() { | 33 bool DnsResolveFunction::RunAsync() { |
| 34 std::unique_ptr<Resolve::Params> params(Resolve::Params::Create(*args_)); | 34 std::unique_ptr<Resolve::Params> params(Resolve::Params::Create(*args_)); |
| 35 EXTENSION_FUNCTION_VALIDATE(params.get()); | 35 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 36 | 36 |
| 37 hostname_ = params->hostname; | 37 hostname_ = params->hostname; |
| 38 resource_context_ = browser_context()->GetResourceContext(); | 38 resource_context_ = browser_context()->GetResourceContext(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 50 | 50 |
| 51 net::HostResolver* host_resolver = | 51 net::HostResolver* host_resolver = |
| 52 HostResolverWrapper::GetInstance()->GetHostResolver(resource_context_); | 52 HostResolverWrapper::GetInstance()->GetHostResolver(resource_context_); |
| 53 DCHECK(host_resolver); | 53 DCHECK(host_resolver); |
| 54 | 54 |
| 55 // Yes, we are passing zero as the port. There are some interesting but not | 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 | 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 | 57 // hostname you'd like to resolve, even though it doesn't use that value in |
| 58 // determining its answer. | 58 // determining its answer. |
| 59 net::HostPortPair host_port_pair(hostname_, 0); | 59 net::HostPortPair host_port_pair(hostname_, 0); |
| 60 | |
| 61 net::HostResolver::RequestInfo request_info(host_port_pair); | 60 net::HostResolver::RequestInfo request_info(host_port_pair); |
| 62 int resolve_result = host_resolver->Resolve( | 61 int resolve_result = host_resolver->Resolve( |
| 63 request_info, | 62 request_info, net::DEFAULT_PRIORITY, addresses_.get(), |
| 64 net::DEFAULT_PRIORITY, | 63 base::Bind(&DnsResolveFunction::OnLookupFinished, this), &request_handle_, |
| 65 addresses_.get(), | |
| 66 base::Bind(&DnsResolveFunction::OnLookupFinished, this), | |
| 67 request_handle_.get(), | |
| 68 net::BoundNetLog()); | 64 net::BoundNetLog()); |
| 69 | 65 |
| 70 // Balanced in OnLookupFinished. | 66 // Balanced in OnLookupFinished. |
| 71 AddRef(); | 67 AddRef(); |
| 72 | 68 |
| 73 if (resolve_result != net::ERR_IO_PENDING) | 69 if (resolve_result != net::ERR_IO_PENDING) |
| 74 OnLookupFinished(resolve_result); | 70 OnLookupFinished(resolve_result); |
| 75 } | 71 } |
| 76 | 72 |
| 77 void DnsResolveFunction::RespondOnUIThread() { | 73 void DnsResolveFunction::RespondOnUIThread() { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 74 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 79 SendResponse(response_); | 75 SendResponse(response_); |
| 80 } | 76 } |
| 81 | 77 |
| 82 void DnsResolveFunction::OnLookupFinished(int resolve_result) { | 78 void DnsResolveFunction::OnLookupFinished(int resolve_result) { |
| 83 std::unique_ptr<ResolveCallbackResolveInfo> resolve_info( | 79 std::unique_ptr<ResolveCallbackResolveInfo> resolve_info( |
| 84 new ResolveCallbackResolveInfo()); | 80 new ResolveCallbackResolveInfo()); |
| 85 resolve_info->result_code = resolve_result; | 81 resolve_info->result_code = resolve_result; |
| 86 if (resolve_result == net::OK) { | 82 if (resolve_result == net::OK) { |
| 87 DCHECK(!addresses_->empty()); | 83 DCHECK(!addresses_->empty()); |
| 88 resolve_info->address.reset( | 84 resolve_info->address.reset( |
| 89 new std::string(addresses_->front().ToStringWithoutPort())); | 85 new std::string(addresses_->front().ToStringWithoutPort())); |
| 90 } | 86 } |
| 91 results_ = Resolve::Results::Create(*resolve_info); | 87 results_ = Resolve::Results::Create(*resolve_info); |
| 92 response_ = true; | 88 response_ = true; |
| 93 | 89 |
| 90 // TODO(maksims): investigate why std::unique_ptr<HostResolver::Request>'s | |
| 91 // destructor is not called automatically. | |
|
Devlin
2016/07/12 15:04:51
I don't quite follow this comment. Automatically
maksims (do not use this acc)
2016/07/14 12:02:47
No, I mean that DnsResolveFunction's destructor is
mmenke
2016/07/14 18:25:31
Without digging into it, my guess is that the test
maksims (do not use this acc)
2016/07/19 15:00:25
Done.
maksims (do not use this acc)
2016/07/19 15:00:25
Done.
maksims (do not use this acc)
2016/07/19 15:00:25
I just the implementation of ::RequestImpl and it
maksims (do not use this acc)
2016/07/19 15:00:25
Done.
| |
| 92 request_handle_.reset(); | |
| 93 | |
| 94 bool post_task_result = BrowserThread::PostTask( | 94 bool post_task_result = BrowserThread::PostTask( |
| 95 BrowserThread::UI, | 95 BrowserThread::UI, |
| 96 FROM_HERE, | 96 FROM_HERE, |
| 97 base::Bind(&DnsResolveFunction::RespondOnUIThread, this)); | 97 base::Bind(&DnsResolveFunction::RespondOnUIThread, this)); |
| 98 DCHECK(post_task_result); | 98 DCHECK(post_task_result); |
| 99 | 99 |
| 100 Release(); // Added in WorkOnIOThread(). | 100 Release(); // Added in WorkOnIOThread(). |
| 101 } | 101 } |
| 102 | 102 |
| 103 } // namespace extensions | 103 } // namespace extensions |
| OLD | NEW |