Commit aae2eba1 authored by rsleevi@chromium.org's avatar rsleevi@chromium.org

Make RegistryControlledDomainService a static-function container class, rather than a Singleton

BUG=none
TEST=none


Review URL: http://codereview.chromium.org/8395025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107539 0039d316-1c4b-4281-b951-d872f2087c98
parent a98dbdb7
//* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ // 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.
// NB: Modelled after Mozilla's code (originally written by Pamela Greene,
// later modified by others), but almost entirely rewritten for Chrome.
// (netwerk/dns/src/nsEffectiveTLDService.cpp)
/* ***** BEGIN LICENSE BLOCK ***** /* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
* *
...@@ -57,10 +63,12 @@ namespace { ...@@ -57,10 +63,12 @@ namespace {
const int kExceptionRule = 1; const int kExceptionRule = 1;
const int kWildcardRule = 2; const int kWildcardRule = 2;
RegistryControlledDomainService* test_instance_;
} // namespace } // namespace
RegistryControlledDomainService::FindDomainPtr
RegistryControlledDomainService::find_domain_function_ =
Perfect_Hash::FindDomain;
// static // static
std::string RegistryControlledDomainService::GetDomainAndRegistry( std::string RegistryControlledDomainService::GetDomainAndRegistry(
const GURL& gurl) { const GURL& gurl) {
...@@ -114,7 +122,7 @@ size_t RegistryControlledDomainService::GetRegistryLength( ...@@ -114,7 +122,7 @@ size_t RegistryControlledDomainService::GetRegistryLength(
return std::string::npos; return std::string::npos;
if (gurl.HostIsIPAddress()) if (gurl.HostIsIPAddress())
return 0; return 0;
return GetInstance()->GetRegistryLengthImpl( return GetRegistryLengthImpl(
std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len),
allow_unknown_registries); allow_unknown_registries);
} }
...@@ -129,36 +137,17 @@ size_t RegistryControlledDomainService::GetRegistryLength( ...@@ -129,36 +137,17 @@ size_t RegistryControlledDomainService::GetRegistryLength(
return std::string::npos; return std::string::npos;
if (host_info.IsIPAddress()) if (host_info.IsIPAddress())
return 0; return 0;
return GetInstance()->GetRegistryLengthImpl(canon_host, return GetRegistryLengthImpl(canon_host, allow_unknown_registries);
allow_unknown_registries);
}
// static
RegistryControlledDomainService* RegistryControlledDomainService::GetInstance()
{
if (test_instance_)
return test_instance_;
return Singleton<RegistryControlledDomainService>::get();
}
RegistryControlledDomainService::RegistryControlledDomainService()
: find_domain_function_(Perfect_Hash::FindDomain) {
}
// static
RegistryControlledDomainService* RegistryControlledDomainService::SetInstance(
RegistryControlledDomainService* instance) {
RegistryControlledDomainService* old_instance = test_instance_;
test_instance_ = instance;
return old_instance;
} }
// static // static
void RegistryControlledDomainService::UseFindDomainFunction( void RegistryControlledDomainService::UseFindDomainFunction(
FindDomainPtr function) { FindDomainPtr function) {
RegistryControlledDomainService* instance = GetInstance(); if (function) {
instance->find_domain_function_ = function; find_domain_function_ = function;
} else {
find_domain_function_ = Perfect_Hash::FindDomain;
}
} }
// static // static
...@@ -167,8 +156,7 @@ std::string RegistryControlledDomainService::GetDomainAndRegistryImpl( ...@@ -167,8 +156,7 @@ std::string RegistryControlledDomainService::GetDomainAndRegistryImpl(
DCHECK(!host.empty()); DCHECK(!host.empty());
// Find the length of the registry for this host. // Find the length of the registry for this host.
const size_t registry_length = const size_t registry_length = GetRegistryLengthImpl(host, true);
GetInstance()->GetRegistryLengthImpl(host, true);
if ((registry_length == std::string::npos) || (registry_length == 0)) if ((registry_length == std::string::npos) || (registry_length == 0))
return std::string(); // No registry. return std::string(); // No registry.
// The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding
......
//* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ // 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.
// NB: Modelled after Mozilla's code (originally written by Pamela Greene,
// later modified by others), but almost entirely rewritten for Chrome.
// (netwerk/dns/src/nsEffectiveTLDService.h)
/* ***** BEGIN LICENSE BLOCK ***** /* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
* *
...@@ -36,9 +42,6 @@ ...@@ -36,9 +42,6 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
// NB: Modelled after Mozilla's code (originally written by Pamela Greene,
// later modified by others), but almost entirely rewritten for Chrome.
/* /*
(Documentation based on the Mozilla documentation currently at (Documentation based on the Mozilla documentation currently at
http://wiki.mozilla.org/Gecko:Effective_TLD_Service, written by the same http://wiki.mozilla.org/Gecko:Effective_TLD_Service, written by the same
...@@ -118,19 +121,12 @@ ...@@ -118,19 +121,12 @@
class GURL; class GURL;
template <typename T>
struct DefaultSingletonTraits;
struct DomainRule; struct DomainRule;
namespace net { namespace net {
struct RegistryControlledDomainServiceSingletonTraits;
// This class is a singleton.
class NET_EXPORT RegistryControlledDomainService { class NET_EXPORT RegistryControlledDomainService {
public: public:
~RegistryControlledDomainService() { }
// Returns the registered, organization-identifying host and all its registry // Returns the registered, organization-identifying host and all its registry
// information, but no subdomains, from the given GURL. Returns an empty // information, but no subdomains, from the given GURL. Returns an empty
// string if the GURL is invalid, has no host (e.g. a file: URL), has multiple // string if the GURL is invalid, has no host (e.g. a file: URL), has multiple
...@@ -196,43 +192,25 @@ class NET_EXPORT RegistryControlledDomainService { ...@@ -196,43 +192,25 @@ class NET_EXPORT RegistryControlledDomainService {
static size_t GetRegistryLength(const std::string& host, static size_t GetRegistryLength(const std::string& host,
bool allow_unknown_registries); bool allow_unknown_registries);
// Returns the singleton instance, after attempting to initialize it. private:
// NOTE that if the effective-TLD data resource can't be found, the instance friend class RegistryControlledDomainTest;
// will be initialized and continue operation with simple default TLD data.
static RegistryControlledDomainService* GetInstance();
protected:
typedef const struct DomainRule* (*FindDomainPtr)(const char *, unsigned int);
// The entire protected API is only for unit testing. I mean it. Don't make // Internal workings of the static public methods. See above.
// me come over there! static std::string GetDomainAndRegistryImpl(const std::string& host);
RegistryControlledDomainService(); static size_t GetRegistryLengthImpl(const std::string& host,
bool allow_unknown_registries);
// Set the RegistryControledDomainService instance to be used internally. typedef const struct DomainRule* (*FindDomainPtr)(const char *, unsigned int);
// |instance| will supersede the singleton instance normally used. If
// |instance| is NULL, normal behavior is restored, and internal operations
// will return to using the singleton. This function always returns the
// instance set by the most recent call to SetInstance.
static RegistryControlledDomainService* SetInstance(
RegistryControlledDomainService* instance);
// Used for unit tests, so that a different perfect hash map from the full // Used for unit tests, so that a different perfect hash map from the full
// list is used. // list is used. Set to NULL to use the Default function.
static void UseFindDomainFunction(FindDomainPtr function); static void UseFindDomainFunction(FindDomainPtr function);
private:
// To allow construction of the internal singleton instance.
friend struct DefaultSingletonTraits<RegistryControlledDomainService>;
// Internal workings of the static public methods. See above.
static std::string GetDomainAndRegistryImpl(const std::string& host);
size_t GetRegistryLengthImpl(const std::string& host,
bool allow_unknown_registries);
// Function that returns a DomainRule given a domain. // Function that returns a DomainRule given a domain.
FindDomainPtr find_domain_function_; static FindDomainPtr find_domain_function_;
DISALLOW_COPY_AND_ASSIGN(RegistryControlledDomainService); DISALLOW_IMPLICIT_CONSTRUCTORS(RegistryControlledDomainService);
}; };
} // namespace net } // namespace net
......
...@@ -17,69 +17,48 @@ ...@@ -17,69 +17,48 @@
namespace net { namespace net {
namespace { namespace {
class TestRegistryControlledDomainService :
public RegistryControlledDomainService {
public:
// Sets the given data.
static void UseDomainData(FindDomainPtr function) {
RegistryControlledDomainService::UseFindDomainFunction(function);
}
// Creates a new dedicated instance to be used for testing, deleting any
// previously-set one.
static void UseDedicatedInstance() {
delete static_cast<TestRegistryControlledDomainService*>(
SetInstance(new TestRegistryControlledDomainService()));
}
// Restores RegistryControlledDomainService to using its default instance,
// deleting any previously-set test instance.
static void UseDefaultInstance() {
delete static_cast<TestRegistryControlledDomainService*>(SetInstance(NULL));
}
};
class RegistryControlledDomainTest : public testing::Test {
protected:
virtual void SetUp() {
TestRegistryControlledDomainService::UseDedicatedInstance();
}
virtual void TearDown() {
TestRegistryControlledDomainService::UseDefaultInstance();
}
};
std::string GetDomainFromURL(const std::string& url) { std::string GetDomainFromURL(const std::string& url) {
return TestRegistryControlledDomainService::GetDomainAndRegistry(GURL(url)); return RegistryControlledDomainService::GetDomainAndRegistry(GURL(url));
} }
std::string GetDomainFromHost(const std::string& host) { std::string GetDomainFromHost(const std::string& host) {
return TestRegistryControlledDomainService::GetDomainAndRegistry(host); return RegistryControlledDomainService::GetDomainAndRegistry(host);
} }
size_t GetRegistryLengthFromURL(const std::string& url, size_t GetRegistryLengthFromURL(const std::string& url,
bool allow_unknown_registries) { bool allow_unknown_registries) {
return TestRegistryControlledDomainService::GetRegistryLength(GURL(url), return RegistryControlledDomainService::GetRegistryLength(GURL(url),
allow_unknown_registries); allow_unknown_registries);
} }
size_t GetRegistryLengthFromHost(const std::string& host, size_t GetRegistryLengthFromHost(const std::string& host,
bool allow_unknown_registries) { bool allow_unknown_registries) {
return TestRegistryControlledDomainService::GetRegistryLength(host, return RegistryControlledDomainService::GetRegistryLength(host,
allow_unknown_registries); allow_unknown_registries);
} }
bool CompareDomains(const std::string& url1, const std::string& url2) { bool CompareDomains(const std::string& url1, const std::string& url2) {
GURL g1 = GURL(url1); GURL g1 = GURL(url1);
GURL g2 = GURL(url2); GURL g2 = GURL(url2);
return TestRegistryControlledDomainService::SameDomainOrHost(g1, g2); return RegistryControlledDomainService::SameDomainOrHost(g1, g2);
} }
} // namespace
class RegistryControlledDomainTest : public testing::Test {
protected:
typedef RegistryControlledDomainService::FindDomainPtr FindDomainPtr;
void UseDomainData(FindDomainPtr function) {
RegistryControlledDomainService::UseFindDomainFunction(function);
}
virtual void TearDown() {
RegistryControlledDomainService::UseFindDomainFunction(NULL);
}
};
TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) {
TestRegistryControlledDomainService::UseDomainData( UseDomainData(Perfect_Hash_Test1::FindDomain);
Perfect_Hash_Test1::FindDomain);
// Test GURL version of GetDomainAndRegistry(). // Test GURL version of GetDomainAndRegistry().
EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1
...@@ -138,8 +117,7 @@ TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { ...@@ -138,8 +117,7 @@ TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) {
} }
TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) {
TestRegistryControlledDomainService::UseDomainData( UseDomainData(Perfect_Hash_Test1::FindDomain);
Perfect_Hash_Test1::FindDomain);
// Test GURL version of GetRegistryLength(). // Test GURL version of GetRegistryLength().
EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", false)); EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", false));
...@@ -210,8 +188,7 @@ TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { ...@@ -210,8 +188,7 @@ TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) {
} }
TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) {
TestRegistryControlledDomainService::UseDomainData( UseDomainData(Perfect_Hash_Test2::FindDomain);
Perfect_Hash_Test2::FindDomain);
EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
"http://a.b.bar.jp/file.html")); // b.bar.jp "http://a.b.bar.jp/file.html")); // b.bar.jp
...@@ -240,8 +217,6 @@ TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { ...@@ -240,8 +217,6 @@ TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) {
} }
TEST_F(RegistryControlledDomainTest, TestDefaultData) { TEST_F(RegistryControlledDomainTest, TestDefaultData) {
TestRegistryControlledDomainService::UseDefaultInstance();
// Note that no data is set: we're using the default rules. // Note that no data is set: we're using the default rules.
EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com", false)); EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com", false));
EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu", false)); EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu", false));
...@@ -252,5 +227,4 @@ TEST_F(RegistryControlledDomainTest, TestDefaultData) { ...@@ -252,5 +227,4 @@ TEST_F(RegistryControlledDomainTest, TestDefaultData) {
EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", true)); EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", true));
} }
} // namespace
} // namespace net } // namespace net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment