diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-04 15:02:18 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-04 15:02:18 +0000 |
commit | b19537cdc317d6baab5183e111668ccc88c661f6 (patch) | |
tree | ff0677915cf56d7ca78150f6a279960c0c243907 | |
parent | d10f0a0460e6c508adf61e6825f390e6c346091f (diff) | |
download | chromium_src-b19537cdc317d6baab5183e111668ccc88c661f6.zip chromium_src-b19537cdc317d6baab5183e111668ccc88c661f6.tar.gz chromium_src-b19537cdc317d6baab5183e111668ccc88c661f6.tar.bz2 |
Introduce a new CompletionCallback and TestCompletionCallback.
Start using it in DnsRRResolver.
BUG=98719
TEST=none
Review URL: http://codereview.chromium.org/8095026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103885 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/base/completion_callback.h | 4 | ||||
-rw-r--r-- | net/base/dnsrr_resolver.cc | 15 | ||||
-rw-r--r-- | net/base/dnsrr_resolver.h | 2 | ||||
-rw-r--r-- | net/base/dnsrr_resolver_unittest.cc | 54 | ||||
-rw-r--r-- | net/base/test_completion_callback.cc | 18 | ||||
-rw-r--r-- | net/base/test_completion_callback.h | 27 | ||||
-rw-r--r-- | net/socket/dns_cert_provenance_checker.cc | 12 |
7 files changed, 88 insertions, 44 deletions
diff --git a/net/base/completion_callback.h b/net/base/completion_callback.h index 1d3e551..7474793 100644 --- a/net/base/completion_callback.h +++ b/net/base/completion_callback.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,12 +7,14 @@ #pragma once #include "base/callback_old.h" +#include "base/callback.h" namespace net { // A callback specialization that takes a single int parameter. Usually this // is used to report a byte count or network error code. typedef Callback1<int>::Type OldCompletionCallback; +typedef base::Callback<void(int)> CompletionCallback; // Used to implement a OldCompletionCallback. template <class T> diff --git a/net/base/dnsrr_resolver.cc b/net/base/dnsrr_resolver.cc index d74a92b..b11ea2e 100644 --- a/net/base/dnsrr_resolver.cc +++ b/net/base/dnsrr_resolver.cc @@ -113,30 +113,30 @@ RRResponse::~RRResponse() {} class RRResolverHandle { public: - RRResolverHandle(OldCompletionCallback* callback, RRResponse* response) + RRResolverHandle(const CompletionCallback& callback, RRResponse* response) : callback_(callback), response_(response) { } // Cancel ensures that the result callback will never be made. void Cancel() { - callback_ = NULL; + callback_.Reset(); response_ = NULL; } // Post copies the contents of |response| to the caller's RRResponse and // calls the callback. void Post(int rv, const RRResponse* response) { - if (callback_) { + if (!callback_.is_null()) { if (response_ && response) *response_ = *response; - callback_->Run(rv); + callback_.Run(rv); } delete this; } private: - OldCompletionCallback* callback_; + CompletionCallback callback_; RRResponse* response_; }; @@ -541,14 +541,15 @@ DnsRRResolver::~DnsRRResolver() { } intptr_t DnsRRResolver::Resolve(const std::string& name, uint16 rrtype, - uint16 flags, OldCompletionCallback* callback, + uint16 flags, + const CompletionCallback& callback, RRResponse* response, int priority /* ignored */, const BoundNetLog& netlog /* ignored */) { DCHECK(CalledOnValidThread()); DCHECK(!in_destructor_); - if (!callback || !response || name.empty()) + if (callback.is_null() || !response || name.empty()) return kInvalidHandle; // Don't allow queries of type ANY diff --git a/net/base/dnsrr_resolver.h b/net/base/dnsrr_resolver.h index 781a047..c6c5fef 100644 --- a/net/base/dnsrr_resolver.h +++ b/net/base/dnsrr_resolver.h @@ -99,7 +99,7 @@ class NET_EXPORT DnsRRResolver // this function returns kInvalidHandle then the resolution failed // immediately because it was improperly formed. Handle Resolve(const std::string& name, uint16 rrtype, - uint16 flags, OldCompletionCallback* callback, + uint16 flags, const CompletionCallback& callback, RRResponse* response, int priority, const BoundNetLog& netlog); diff --git a/net/base/dnsrr_resolver_unittest.cc b/net/base/dnsrr_resolver_unittest.cc index 4cf7da7..ed5f2c5 100644 --- a/net/base/dnsrr_resolver_unittest.cc +++ b/net/base/dnsrr_resolver_unittest.cc @@ -1,9 +1,10 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "net/base/dnsrr_resolver.h" +#include "base/bind.h" #include "base/callback.h" #include "base/synchronization/lock.h" #include "net/base/dns_util.h" @@ -14,26 +15,22 @@ namespace net { -class DnsRRResolverTest : public testing::Test { -}; +namespace { -class ExplodingCallback : public CallbackRunner<Tuple1<int> > { - public: - virtual void RunWithParams(const Tuple1<int>& params) { - FAIL(); - } -}; +void FailTest(int /* result */) { + FAIL(); +} // These tests are disabled because they depend on the external network to // pass. However, they may be useful when chaging the code. -TEST_F(DnsRRResolverTest, DISABLED_ResolveReal) { +TEST(DnsRRResolverTest, DISABLED_ResolveReal) { RRResponse response; - TestOldCompletionCallback callback; + TestCompletionCallback callback; DnsRRResolver resolver; DnsRRResolver::Handle handle; handle = resolver.Resolve("test.imperialviolet.org", 13172, 0, - &callback, &response, 0, BoundNetLog()); + callback.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); ASSERT_EQ(OK, callback.WaitForResult()); ASSERT_EQ(1u, response.rrdatas.size()); @@ -41,14 +38,14 @@ TEST_F(DnsRRResolverTest, DISABLED_ResolveReal) { LOG(ERROR) << "result is " << response.rrdatas[0]; } -TEST_F(DnsRRResolverTest, DISABLED_ResolveReal2) { +TEST(DnsRRResolverTest, DISABLED_ResolveReal2) { RRResponse response; - TestOldCompletionCallback callback; + TestCompletionCallback callback; DnsRRResolver resolver; DnsRRResolver::Handle handle; handle = resolver.Resolve("google.com", kDNS_TXT, 0, - &callback, &response, 0, BoundNetLog()); + callback.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); ASSERT_EQ(OK, callback.WaitForResult()); ASSERT_EQ(1u, response.rrdatas.size()); @@ -57,14 +54,14 @@ TEST_F(DnsRRResolverTest, DISABLED_ResolveReal2) { } -TEST_F(DnsRRResolverTest, Resolve) { +TEST(DnsRRResolverTest, Resolve) { RRResponse response; - TestOldCompletionCallback callback; + TestCompletionCallback callback; DnsRRResolver resolver; DnsRRResolver::Handle handle; handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, - &callback, &response, 0, BoundNetLog()); + callback.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); ASSERT_EQ(OK, callback.WaitForResult()); ASSERT_EQ(1u, response.rrdatas.size()); @@ -75,7 +72,7 @@ TEST_F(DnsRRResolverTest, Resolve) { // Test a cache hit. handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, - &callback, &response, 0, BoundNetLog()); + callback.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); ASSERT_EQ(OK, callback.WaitForResult()); ASSERT_EQ(1u, response.rrdatas.size()); @@ -86,9 +83,8 @@ TEST_F(DnsRRResolverTest, Resolve) { // Test that a callback is never made. This depends on there being another // test after this one which will pump the MessageLoop. - ExplodingCallback callback3; handle = resolver.Resolve("www.testing.notatld", kDNS_TESTING, 0, - &callback3, &response, 0, BoundNetLog()); + base::Bind(&FailTest), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); resolver.CancelResolve(handle); ASSERT_EQ(3u, resolver.requests()); @@ -97,7 +93,7 @@ TEST_F(DnsRRResolverTest, Resolve) { // Test what happens in the event of a network config change. handle = resolver.Resolve("nx.testing.notatld", kDNS_TESTING, 0, - &callback, &response, 0, BoundNetLog()); + callback.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); resolver.OnIPAddressChanged(); ASSERT_TRUE(callback.have_result()); @@ -108,13 +104,13 @@ TEST_F(DnsRRResolverTest, Resolve) { // Test an inflight join. (Note that this depends on the cache being flushed // by OnIPAddressChanged.) - TestOldCompletionCallback callback2; + TestCompletionCallback callback2; DnsRRResolver::Handle handle2; handle = resolver.Resolve("nx.testing.notatld", kDNS_TESTING, 0, - &callback, &response, 0, BoundNetLog()); + callback.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle != DnsRRResolver::kInvalidHandle); handle2 = resolver.Resolve("nx.testing.notatld", kDNS_TESTING, 0, - &callback2, &response, 0, BoundNetLog()); + callback2.callback(), &response, 0, BoundNetLog()); ASSERT_TRUE(handle2 != DnsRRResolver::kInvalidHandle); ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback2.WaitForResult()); @@ -170,7 +166,7 @@ static const uint8 kExamplePacket[] = { 0x00, 0x00, 0x00, }; -TEST_F(DnsRRResolverTest, ParseExample) { +TEST(DnsRRResolverTest, ParseExample) { RRResponse response; ASSERT_TRUE(response.ParseFromResponse(kExamplePacket, sizeof(kExamplePacket), kDNS_TXT)); @@ -183,7 +179,7 @@ TEST_F(DnsRRResolverTest, ParseExample) { ASSERT_FALSE(response.dnssec); } -TEST_F(DnsRRResolverTest, FuzzTruncation) { +TEST(DnsRRResolverTest, FuzzTruncation) { RRResponse response; for (unsigned len = sizeof(kExamplePacket); len <= sizeof(kExamplePacket); @@ -192,7 +188,7 @@ TEST_F(DnsRRResolverTest, FuzzTruncation) { } } -TEST_F(DnsRRResolverTest, FuzzCorruption) { +TEST(DnsRRResolverTest, FuzzCorruption) { RRResponse response; uint8 copy[sizeof(kExamplePacket)]; @@ -210,4 +206,6 @@ TEST_F(DnsRRResolverTest, FuzzCorruption) { } #endif +} // namespace + } // namespace net diff --git a/net/base/test_completion_callback.cc b/net/base/test_completion_callback.cc index b994bc3..6851a7c 100644 --- a/net/base/test_completion_callback.cc +++ b/net/base/test_completion_callback.cc @@ -4,6 +4,8 @@ #include "net/base/test_completion_callback.h" +#include "base/bind.h" +#include "base/compiler_specific.h" #include "base/message_loop.h" #include "net/base/net_errors.h" @@ -38,3 +40,19 @@ void TestOldCompletionCallback::RunWithParams(const Tuple1<int>& params) { if (waiting_for_result_) MessageLoop::current()->Quit(); } + +namespace net { + +TestCompletionCallback::TestCompletionCallback() + : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( + base::Bind(&TestCompletionCallback::OnComplete, + base::Unretained(this)))) { +} + +TestCompletionCallback::~TestCompletionCallback() {} + +void TestCompletionCallback::OnComplete(int result) { + old_callback_impl_.RunWithParams(Tuple1<int>(result)); +} + +} // namespace net diff --git a/net/base/test_completion_callback.h b/net/base/test_completion_callback.h index 520c5b6..e5135aa 100644 --- a/net/base/test_completion_callback.h +++ b/net/base/test_completion_callback.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,6 +8,7 @@ #include "base/callback_old.h" #include "base/tuple.h" +#include "net/base/completion_callback.h" //----------------------------------------------------------------------------- // completion callback helper @@ -39,4 +40,28 @@ class TestOldCompletionCallback : public CallbackRunner< Tuple1<int> > { bool waiting_for_result_; }; +namespace net { + +class TestCompletionCallback { + public: + TestCompletionCallback(); + ~TestCompletionCallback(); + + int WaitForResult() { return old_callback_impl_.WaitForResult(); } + + int GetResult(int result) { return old_callback_impl_.GetResult(result); } + + bool have_result() const { return old_callback_impl_.have_result(); } + + const CompletionCallback& callback() const { return callback_; } + + private: + void OnComplete(int result); + + const CompletionCallback callback_; + TestOldCompletionCallback old_callback_impl_; +}; + +} // namespace net + #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ diff --git a/net/socket/dns_cert_provenance_checker.cc b/net/socket/dns_cert_provenance_checker.cc index 38e6bbd..04cbce8 100644 --- a/net/socket/dns_cert_provenance_checker.cc +++ b/net/socket/dns_cert_provenance_checker.cc @@ -18,6 +18,7 @@ #include "base/base64.h" #include "base/basictypes.h" +#include "base/bind.h" #include "base/lazy_instance.h" #include "base/memory/scoped_ptr.h" #include "base/pickle.h" @@ -96,9 +97,7 @@ class DnsCertProvenanceCheck : public base::NonThreadSafe { dnsrr_resolver_(dnsrr_resolver), delegate_(delegate), der_certs_(der_certs.size()), - handle_(DnsRRResolver::kInvalidHandle), - ALLOW_THIS_IN_INITIALIZER_LIST(callback_( - this, &DnsCertProvenanceCheck::ResolutionComplete)) { + handle_(DnsRRResolver::kInvalidHandle) { for (size_t i = 0; i < der_certs.size(); i++) der_certs_[i] = der_certs[i].as_string(); } @@ -133,8 +132,10 @@ class DnsCertProvenanceCheck : public base::NonThreadSafe { domain_.append(kBaseCertName); handle_ = dnsrr_resolver_->Resolve( - domain_, kDNS_TXT, 0 /* flags */, &callback_, &response_, - 0 /* priority */, BoundNetLog()); + domain_, kDNS_TXT, 0 /* flags */, + base::Bind(&DnsCertProvenanceCheck::ResolutionComplete, + base::Unretained(this)), + &response_, 0 /* priority */, BoundNetLog()); if (handle_ == DnsRRResolver::kInvalidHandle) { LOG(ERROR) << "Failed to resolve " << domain_ << " for " << hostname_; delete this; @@ -200,7 +201,6 @@ class DnsCertProvenanceCheck : public base::NonThreadSafe { std::vector<std::string> der_certs_; RRResponse response_; DnsRRResolver::Handle handle_; - OldCompletionCallbackImpl<DnsCertProvenanceCheck> callback_; }; SECKEYPublicKey* GetServerPubKey() { |