summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authorsatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-14 07:48:49 +0000
committersatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-14 07:48:49 +0000
commit625332e06437018bf696dce93a4b2bd2c5e0b118 (patch)
tree56e128ddf94a81b6de1f29a9eabe59f0d79346c9 /net/base
parentdc7cdcb970254f223262a66c812f240a8269ae87 (diff)
downloadchromium_src-625332e06437018bf696dce93a4b2bd2c5e0b118.zip
chromium_src-625332e06437018bf696dce93a4b2bd2c5e0b118.tar.gz
chromium_src-625332e06437018bf696dce93a4b2bd2c5e0b118.tar.bz2
Make members of Singleton<T> private and only visible to the singleton type. This enforces that the Singleton<T> pattern can only be used within classes which want singleton-ness.
As part of this CL I have also fixed up files which got missed in my previous CLs to use a GetInstance() method and use Singleton<T> from the source file. There are a small number of places where I have also switched to LazyInstance as that was more appropriate for types used in a single source file. BUG=65298 TEST=all existing tests should continue to pass. Review URL: http://codereview.chromium.org/5682008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69107 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/bandwidth_metrics.cc27
-rw-r--r--net/base/bandwidth_metrics.h22
-rw-r--r--net/base/cert_database_nss_unittest.cc19
-rw-r--r--net/base/mime_util.cc39
-rw-r--r--net/base/winsock_init.cc7
-rw-r--r--net/base/x509_certificate_win.cc9
6 files changed, 62 insertions, 61 deletions
diff --git a/net/base/bandwidth_metrics.cc b/net/base/bandwidth_metrics.cc
index eaaa3c0..fa23a77 100644
--- a/net/base/bandwidth_metrics.cc
+++ b/net/base/bandwidth_metrics.cc
@@ -2,14 +2,35 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/singleton.h"
+#include "base/lazy_instance.h"
#include "net/base/bandwidth_metrics.h"
+static base::LazyInstance<net::BandwidthMetrics> g_bandwidth_metrics(
+ base::LINKER_INITIALIZED);
+
namespace net {
ScopedBandwidthMetrics::ScopedBandwidthMetrics()
- : metrics_(Singleton<BandwidthMetrics>::get()),
- started_(false) {
+ : started_(false) {
+}
+
+ScopedBandwidthMetrics::~ScopedBandwidthMetrics() {
+ if (started_)
+ g_bandwidth_metrics.Get().StopStream();
+}
+
+void ScopedBandwidthMetrics::StartStream() {
+ started_ = true;
+ g_bandwidth_metrics.Get().StartStream();
+}
+
+void ScopedBandwidthMetrics::StopStream() {
+ started_ = false;
+ g_bandwidth_metrics.Get().StopStream();
+}
+
+void ScopedBandwidthMetrics::RecordBytes(int bytes) {
+ g_bandwidth_metrics.Get().RecordBytes(bytes);
}
} // namespace net
diff --git a/net/base/bandwidth_metrics.h b/net/base/bandwidth_metrics.h
index aef366d..ce1a498 100644
--- a/net/base/bandwidth_metrics.h
+++ b/net/base/bandwidth_metrics.h
@@ -122,30 +122,16 @@ class BandwidthMetrics {
class ScopedBandwidthMetrics {
public:
ScopedBandwidthMetrics();
+ ~ScopedBandwidthMetrics();
- ~ScopedBandwidthMetrics() {
- if (started_)
- metrics_->StopStream();
- }
-
- void StartStream() {
- started_ = true;
- metrics_->StartStream();
- }
-
- void StopStream() {
- started_ = false;
- metrics_->StopStream();
- }
-
- void RecordBytes(int bytes) { metrics_->RecordBytes(bytes); }
+ void StartStream();
+ void StopStream();
+ void RecordBytes(int bytes);
private:
- BandwidthMetrics* metrics_;
bool started_;
};
} // namespace net
#endif // NET_BASE_BANDWIDTH_METRICS_H_
-
diff --git a/net/base/cert_database_nss_unittest.cc b/net/base/cert_database_nss_unittest.cc
index 5056e5d..8e69104 100644
--- a/net/base/cert_database_nss_unittest.cc
+++ b/net/base/cert_database_nss_unittest.cc
@@ -104,16 +104,9 @@ bool ReadCertIntoList(const std::string& name, CertificateList* certs) {
class CertDatabaseNSSTest : public testing::Test {
public:
virtual void SetUp() {
- if (!temp_db_initialized_) {
- ScopedTempDir* temp_db_dir = Singleton<
- ScopedTempDir,
- DefaultSingletonTraits<ScopedTempDir>,
- CertDatabaseNSSTest>::get();
- ASSERT_TRUE(temp_db_dir->CreateUniqueTempDir());
- ASSERT_TRUE(
- base::OpenTestNSSDB(temp_db_dir->path(), "CertDatabaseNSSTest db"));
- temp_db_initialized_ = true;
- }
+ ASSERT_TRUE(temp_db_dir_.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::OpenTestNSSDB(temp_db_dir_.path(), "CertDatabaseNSSTest db"));
slot_.reset(base::GetDefaultNSSKeySlot());
// Test db should be empty at start of test.
@@ -121,7 +114,6 @@ class CertDatabaseNSSTest : public testing::Test {
}
virtual void TearDown() {
// Don't try to cleanup if the setup failed.
- ASSERT_TRUE(temp_db_initialized_);
ASSERT_TRUE(slot_.get());
EXPECT_TRUE(CleanupSlotContents(slot_.get()));
@@ -133,12 +125,9 @@ class CertDatabaseNSSTest : public testing::Test {
CertDatabase cert_db_;
private:
- static bool temp_db_initialized_;
+ ScopedTempDir temp_db_dir_;
};
-// static
-bool CertDatabaseNSSTest::temp_db_initialized_ = false;
-
TEST_F(CertDatabaseNSSTest, ListCerts) {
// This test isn't terribly useful, though it will at least let valgrind test
// for leaks.
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc
index ddcfc4b..d95c029 100644
--- a/net/base/mime_util.cc
+++ b/net/base/mime_util.cc
@@ -9,8 +9,8 @@
#include "net/base/platform_mime_util.h"
#include "base/hash_tables.h"
+#include "base/lazy_instance.h"
#include "base/logging.h"
-#include "base/singleton.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
@@ -51,7 +51,7 @@ class MimeUtil : public PlatformMimeUtil {
const std::vector<std::string>& codecs) const;
private:
- friend struct DefaultSingletonTraits<MimeUtil>;
+ friend struct base::DefaultLazyInstanceTraits<MimeUtil>;
MimeUtil() {
InitializeMimeTypeMaps();
}
@@ -71,6 +71,8 @@ class MimeUtil : public PlatformMimeUtil {
StrictMappings strict_format_map_;
}; // class MimeUtil
+static base::LazyInstance<MimeUtil> g_mime_util(base::LINKER_INITIALIZED);
+
struct MimeInfo {
const char* mime_type;
const char* extensions; // comma separated list
@@ -473,70 +475,67 @@ bool MimeUtil::IsSupportedStrictMediaMimeType(const std::string& mime_type,
// Wrappers for the singleton
//----------------------------------------------------------------------------
-static MimeUtil* GetMimeUtil() {
- return Singleton<MimeUtil>::get();
-}
-
bool GetMimeTypeFromExtension(const FilePath::StringType& ext,
std::string* mime_type) {
- return GetMimeUtil()->GetMimeTypeFromExtension(ext, mime_type);
+ return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type);
}
bool GetMimeTypeFromFile(const FilePath& file_path, std::string* mime_type) {
- return GetMimeUtil()->GetMimeTypeFromFile(file_path, mime_type);
+ return g_mime_util.Get().GetMimeTypeFromFile(file_path, mime_type);
}
bool GetPreferredExtensionForMimeType(const std::string& mime_type,
FilePath::StringType* extension) {
- return GetMimeUtil()->GetPreferredExtensionForMimeType(mime_type, extension);
+ return g_mime_util.Get().GetPreferredExtensionForMimeType(mime_type,
+ extension);
}
bool IsSupportedImageMimeType(const char* mime_type) {
- return GetMimeUtil()->IsSupportedImageMimeType(mime_type);
+ return g_mime_util.Get().IsSupportedImageMimeType(mime_type);
}
bool IsSupportedMediaMimeType(const char* mime_type) {
- return GetMimeUtil()->IsSupportedMediaMimeType(mime_type);
+ return g_mime_util.Get().IsSupportedMediaMimeType(mime_type);
}
bool IsSupportedNonImageMimeType(const char* mime_type) {
- return GetMimeUtil()->IsSupportedNonImageMimeType(mime_type);
+ return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type);
}
bool IsSupportedJavascriptMimeType(const char* mime_type) {
- return GetMimeUtil()->IsSupportedJavascriptMimeType(mime_type);
+ return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type);
}
bool IsViewSourceMimeType(const char* mime_type) {
- return GetMimeUtil()->IsViewSourceMimeType(mime_type);
+ return g_mime_util.Get().IsViewSourceMimeType(mime_type);
}
bool IsSupportedMimeType(const std::string& mime_type) {
- return GetMimeUtil()->IsSupportedMimeType(mime_type);
+ return g_mime_util.Get().IsSupportedMimeType(mime_type);
}
bool MatchesMimeType(const std::string &mime_type_pattern,
const std::string &mime_type) {
- return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type);
+ return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type);
}
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
- return GetMimeUtil()->AreSupportedMediaCodecs(codecs);
+ return g_mime_util.Get().AreSupportedMediaCodecs(codecs);
}
bool IsStrictMediaMimeType(const std::string& mime_type) {
- return GetMimeUtil()->IsStrictMediaMimeType(mime_type);
+ return g_mime_util.Get().IsStrictMediaMimeType(mime_type);
}
bool IsSupportedStrictMediaMimeType(const std::string& mime_type,
const std::vector<std::string>& codecs) {
- return GetMimeUtil()->IsSupportedStrictMediaMimeType(mime_type, codecs);
+ return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs);
}
void ParseCodecString(const std::string& codecs,
std::vector<std::string>* codecs_out,
const bool strip) {
- GetMimeUtil()->ParseCodecString(codecs, codecs_out, strip);
+ g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
}
namespace {
diff --git a/net/base/winsock_init.cc b/net/base/winsock_init.cc
index ccaf01c..41810ef 100644
--- a/net/base/winsock_init.cc
+++ b/net/base/winsock_init.cc
@@ -6,8 +6,8 @@
#include "net/base/winsock_init.h"
+#include "base/lazy_instance.h"
#include "base/logging.h"
-#include "base/singleton.h"
namespace {
@@ -37,12 +37,15 @@ class WinsockInitSingleton {
}
};
+static base::LazyInstance<WinsockInitSingleton> g_winsock_init_singleton(
+ base::LINKER_INITIALIZED);
+
} // namespace
namespace net {
void EnsureWinsockInit() {
- Singleton<WinsockInitSingleton>::get();
+ g_winsock_init_singleton.Get();
}
} // namespace net
diff --git a/net/base/x509_certificate_win.cc b/net/base/x509_certificate_win.cc
index 75cdf40..71aa545 100644
--- a/net/base/x509_certificate_win.cc
+++ b/net/base/x509_certificate_win.cc
@@ -4,9 +4,9 @@
#include "net/base/x509_certificate.h"
+#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/pickle.h"
-#include "base/singleton.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
@@ -529,7 +529,7 @@ class GlobalCertStore {
}
private:
- friend struct DefaultSingletonTraits<GlobalCertStore>;
+ friend struct base::DefaultLazyInstanceTraits<GlobalCertStore>;
GlobalCertStore()
: cert_store_(CertOpenStore(CERT_STORE_PROV_MEMORY, 0, NULL, 0, NULL)) {
@@ -544,9 +544,12 @@ class GlobalCertStore {
DISALLOW_COPY_AND_ASSIGN(GlobalCertStore);
};
+static base::LazyInstance<GlobalCertStore> g_cert_store(
+ base::LINKER_INITIALIZED);
+
// static
HCERTSTORE X509Certificate::cert_store() {
- return Singleton<GlobalCertStore>::get()->cert_store();
+ return g_cert_store.Get().cert_store();
}
int X509Certificate::Verify(const std::string& hostname,