summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorsatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-09 08:14:10 +0000
committersatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-09 08:14:10 +0000
commitee16679f293ca27ce3d822a97efad7d3fa8a049e (patch)
tree4d531d3ecd47038f476a405372acfbdd20c493f3 /net
parentdce3a57799f52dc3edc0bee78c5e74fed362c365 (diff)
downloadchromium_src-ee16679f293ca27ce3d822a97efad7d3fa8a049e.zip
chromium_src-ee16679f293ca27ce3d822a97efad7d3fa8a049e.tar.gz
chromium_src-ee16679f293ca27ce3d822a97efad7d3fa8a049e.tar.bz2
Add a new GetInstance() method for singleton classes under chrome/service and /net.
This is a small step towards making all singleton classes use the Singleton<T> pattern within their code and not expect the callers to know about it. This CL includes files under chrome/service and /net with related files elsewhere. Suggested files to focus for reviewers: - @sanjeevr for chrome/common and chrome/service - @ukai for net/websockets - @agl for rest of net BUG=65298 TEST=all existing tests should continue to pass. Review URL: http://codereview.chromium.org/5634005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68722 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/bandwidth_metrics.cc15
-rw-r--r--net/base/bandwidth_metrics.h4
-rw-r--r--net/net.gyp2
-rw-r--r--net/socket_stream/socket_stream_job.cc9
-rw-r--r--net/socket_stream/socket_stream_job_manager.cc7
-rw-r--r--net/socket_stream/socket_stream_job_manager.h9
-rw-r--r--net/spdy/spdy_stream.cc2
-rw-r--r--net/url_request/https_prober.cc6
-rw-r--r--net/url_request/https_prober.h9
-rw-r--r--net/url_request/url_request.cc29
-rw-r--r--net/url_request/url_request_http_job.cc2
-rw-r--r--net/url_request/url_request_job_manager.cc6
-rw-r--r--net/url_request/url_request_job_manager.h10
-rw-r--r--net/websockets/websocket_job.cc15
-rw-r--r--net/websockets/websocket_job_unittest.cc4
-rw-r--r--net/websockets/websocket_throttle.cc5
-rw-r--r--net/websockets/websocket_throttle.h6
17 files changed, 97 insertions, 43 deletions
diff --git a/net/base/bandwidth_metrics.cc b/net/base/bandwidth_metrics.cc
new file mode 100644
index 0000000..eaaa3c0
--- /dev/null
+++ b/net/base/bandwidth_metrics.cc
@@ -0,0 +1,15 @@
+// Copyright (c) 2010 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 "base/singleton.h"
+#include "net/base/bandwidth_metrics.h"
+
+namespace net {
+
+ScopedBandwidthMetrics::ScopedBandwidthMetrics()
+ : metrics_(Singleton<BandwidthMetrics>::get()),
+ started_(false) {
+}
+
+} // namespace net
diff --git a/net/base/bandwidth_metrics.h b/net/base/bandwidth_metrics.h
index 80fa005..aef366d 100644
--- a/net/base/bandwidth_metrics.h
+++ b/net/base/bandwidth_metrics.h
@@ -121,9 +121,7 @@ class BandwidthMetrics {
// ensure we always stop them.
class ScopedBandwidthMetrics {
public:
- explicit ScopedBandwidthMetrics(BandwidthMetrics* metrics)
- : metrics_(metrics), started_(false) {
- }
+ ScopedBandwidthMetrics();
~ScopedBandwidthMetrics() {
if (started_)
diff --git a/net/net.gyp b/net/net.gyp
index 447c4f6..82359ab 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -29,6 +29,8 @@
'base/address_list_net_log_param.h',
'base/auth.cc',
'base/auth.h',
+ 'base/bandwidth_metrics.cc',
+ 'base/bandwidth_metrics.h',
'base/cache_type.h',
'base/capturing_net_log.cc',
'base/capturing_net_log.h',
diff --git a/net/socket_stream/socket_stream_job.cc b/net/socket_stream/socket_stream_job.cc
index 8d1da73..6636233 100644
--- a/net/socket_stream/socket_stream_job.cc
+++ b/net/socket_stream/socket_stream_job.cc
@@ -9,20 +9,17 @@
namespace net {
-static SocketStreamJobManager* GetJobManager() {
- return Singleton<SocketStreamJobManager>::get();
-}
-
// static
SocketStreamJob::ProtocolFactory* SocketStreamJob::RegisterProtocolFactory(
const std::string& scheme, ProtocolFactory* factory) {
- return GetJobManager()->RegisterProtocolFactory(scheme, factory);
+ return SocketStreamJobManager::GetInstance()->RegisterProtocolFactory(
+ scheme, factory);
}
// static
SocketStreamJob* SocketStreamJob::CreateSocketStreamJob(
const GURL& url, SocketStream::Delegate* delegate) {
- return GetJobManager()->CreateJob(url, delegate);
+ return SocketStreamJobManager::GetInstance()->CreateJob(url, delegate);
}
SocketStreamJob::SocketStreamJob() {}
diff --git a/net/socket_stream/socket_stream_job_manager.cc b/net/socket_stream/socket_stream_job_manager.cc
index 7dd0d6b..de2f0a8 100644
--- a/net/socket_stream/socket_stream_job_manager.cc
+++ b/net/socket_stream/socket_stream_job_manager.cc
@@ -4,6 +4,8 @@
#include "net/socket_stream/socket_stream_job_manager.h"
+#include "base/singleton.h"
+
namespace net {
SocketStreamJobManager::SocketStreamJobManager() {
@@ -12,6 +14,11 @@ SocketStreamJobManager::SocketStreamJobManager() {
SocketStreamJobManager::~SocketStreamJobManager() {
}
+// static
+SocketStreamJobManager* SocketStreamJobManager::GetInstance() {
+ return Singleton<SocketStreamJobManager>::get();
+}
+
SocketStreamJob* SocketStreamJobManager::CreateJob(
const GURL& url, SocketStream::Delegate* delegate) const {
// If url is invalid, create plain SocketStreamJob, which will close
diff --git a/net/socket_stream/socket_stream_job_manager.h b/net/socket_stream/socket_stream_job_manager.h
index 1150058..fbd572d 100644
--- a/net/socket_stream/socket_stream_job_manager.h
+++ b/net/socket_stream/socket_stream_job_manager.h
@@ -12,14 +12,15 @@
#include "net/socket_stream/socket_stream.h"
#include "net/socket_stream/socket_stream_job.h"
+template <typename T> struct DefaultSingletonTraits;
class GURL;
namespace net {
class SocketStreamJobManager {
public:
- SocketStreamJobManager();
- ~SocketStreamJobManager();
+ // Returns the singleton instance.
+ static SocketStreamJobManager* GetInstance();
SocketStreamJob* CreateJob(
const GURL& url, SocketStream::Delegate* delegate) const;
@@ -28,8 +29,12 @@ class SocketStreamJobManager {
const std::string& scheme, SocketStreamJob::ProtocolFactory* factory);
private:
+ friend struct DefaultSingletonTraits<SocketStreamJobManager>;
typedef std::map<std::string, SocketStreamJob::ProtocolFactory*> FactoryMap;
+ SocketStreamJobManager();
+ ~SocketStreamJobManager();
+
mutable Lock lock_;
FactoryMap factories_;
diff --git a/net/spdy/spdy_stream.cc b/net/spdy/spdy_stream.cc
index 8a5d4a4..3900993 100644
--- a/net/spdy/spdy_stream.cc
+++ b/net/spdy/spdy_stream.cc
@@ -6,7 +6,6 @@
#include "base/logging.h"
#include "base/message_loop.h"
-#include "base/singleton.h"
#include "base/values.h"
#include "net/spdy/spdy_session.h"
@@ -47,7 +46,6 @@ SpdyStream::SpdyStream(SpdySession* session,
send_window_size_(spdy::kSpdyStreamInitialWindowSize),
recv_window_size_(spdy::kSpdyStreamInitialWindowSize),
pushed_(pushed),
- metrics_(Singleton<BandwidthMetrics>::get()),
response_received_(false),
session_(session),
delegate_(NULL),
diff --git a/net/url_request/https_prober.cc b/net/url_request/https_prober.cc
index 4388294..bdafcf6 100644
--- a/net/url_request/https_prober.cc
+++ b/net/url_request/https_prober.cc
@@ -2,6 +2,7 @@
// 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 "net/url_request/https_prober.h"
#include "net/url_request/url_request.h"
@@ -15,6 +16,11 @@ HTTPSProber::HTTPSProber() {
HTTPSProber::~HTTPSProber() {
}
+// static
+HTTPSProber* HTTPSProber::GetInstance() {
+ return Singleton<HTTPSProber>::get();
+}
+
bool HTTPSProber::HaveProbed(const std::string& host) const {
return probed_.find(host) != probed_.end();
}
diff --git a/net/url_request/https_prober.h b/net/url_request/https_prober.h
index 1667911..5d1a6222 100644
--- a/net/url_request/https_prober.h
+++ b/net/url_request/https_prober.h
@@ -10,10 +10,10 @@
#include <set>
#include <string>
-#include "base/singleton.h"
#include "base/task.h"
#include "net/url_request/url_request.h"
+template <typename T> struct DefaultSingletonTraits;
class URLRequestContext;
namespace net {
@@ -33,8 +33,8 @@ class HTTPSProberDelegate {
// transparently upgrading from HTTP to HTTPS (for example, for SPDY).
class HTTPSProber : public net::URLRequest::Delegate {
public:
- HTTPSProber();
- ~HTTPSProber();
+ // Returns the singleton instance.
+ static HTTPSProber* GetInstance();
// HaveProbed returns true if the given host is known to have been probed
// since the browser was last started.
@@ -62,6 +62,9 @@ class HTTPSProber : public net::URLRequest::Delegate {
virtual void OnReadCompleted(net::URLRequest* request, int bytes_read);
private:
+ HTTPSProber();
+ ~HTTPSProber();
+
void Success(net::URLRequest* request);
void Failure(net::URLRequest* request);
void DoCallback(net::URLRequest* request, bool result);
diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc
index b71e764..26a9da2 100644
--- a/net/url_request/url_request.cc
+++ b/net/url_request/url_request.cc
@@ -29,10 +29,6 @@ namespace {
// Max number of http redirects to follow. Same number as gecko.
const int kMaxRedirects = 20;
-URLRequestJobManager* GetJobManager() {
- return Singleton<URLRequestJobManager>::get();
-}
-
// Discard headers which have meaning in POST (Content-Length, Content-Type,
// Origin).
void StripPostSpecificHeaders(net::HttpRequestHeaders* headers) {
@@ -130,17 +126,19 @@ URLRequest::~URLRequest() {
// static
URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory(
const string& scheme, ProtocolFactory* factory) {
- return GetJobManager()->RegisterProtocolFactory(scheme, factory);
+ return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme,
+ factory);
}
// static
void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) {
- GetJobManager()->RegisterRequestInterceptor(interceptor);
+ URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor);
}
// static
void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) {
- GetJobManager()->UnregisterRequestInterceptor(interceptor);
+ URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor(
+ interceptor);
}
void URLRequest::AppendBytesToUpload(const char* bytes, int bytes_len) {
@@ -264,7 +262,7 @@ int URLRequest::GetResponseCode() {
// static
bool URLRequest::IsHandledProtocol(const std::string& scheme) {
- return GetJobManager()->SupportsScheme(scheme);
+ return URLRequestJobManager::GetInstance()->SupportsScheme(scheme);
}
// static
@@ -279,12 +277,12 @@ bool URLRequest::IsHandledURL(const GURL& url) {
// static
void URLRequest::AllowFileAccess() {
- GetJobManager()->set_enable_file_access(true);
+ URLRequestJobManager::GetInstance()->set_enable_file_access(true);
}
// static
bool URLRequest::IsFileAccessAllowed() {
- return GetJobManager()->enable_file_access();
+ return URLRequestJobManager::GetInstance()->enable_file_access();
}
@@ -318,7 +316,7 @@ GURL URLRequest::GetSanitizedReferrer() const {
}
void URLRequest::Start() {
- StartJob(GetJobManager()->CreateJob(this));
+ StartJob(URLRequestJobManager::GetInstance()->CreateJob(this));
}
///////////////////////////////////////////////////////////////////////////////
@@ -353,7 +351,7 @@ void URLRequest::StartJob(URLRequestJob* job) {
void URLRequest::Restart() {
// Should only be called if the original job didn't make any progress.
DCHECK(job_ && !job_->has_response_started());
- RestartWithJob(GetJobManager()->CreateJob(this));
+ RestartWithJob(URLRequestJobManager::GetInstance()->CreateJob(this));
}
void URLRequest::RestartWithJob(URLRequestJob *job) {
@@ -427,7 +425,9 @@ void URLRequest::StopCaching() {
}
void URLRequest::ReceivedRedirect(const GURL& location, bool* defer_redirect) {
- URLRequestJob* job = GetJobManager()->MaybeInterceptRedirect(this, location);
+ URLRequestJob* job =
+ URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(this,
+ location);
if (job) {
RestartWithJob(job);
} else if (delegate_) {
@@ -441,7 +441,8 @@ void URLRequest::ResponseStarted() {
params = new net::NetLogIntegerParameter("net_error", status_.os_error());
net_log_.EndEvent(net::NetLog::TYPE_URL_REQUEST_START_JOB, params);
- URLRequestJob* job = GetJobManager()->MaybeInterceptResponse(this);
+ URLRequestJob* job =
+ URLRequestJobManager::GetInstance()->MaybeInterceptResponse(this);
if (job) {
RestartWithJob(job);
} else if (delegate_) {
diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc
index c951088..f2b1b4f 100644
--- a/net/url_request/url_request_http_job.cc
+++ b/net/url_request/url_request_http_job.cc
@@ -937,7 +937,7 @@ void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
// At this point, we have a request for opportunistic encryption over HTTP.
// In this case we need to probe to check that we can make HTTPS
// connections to that host.
- net::HTTPSProber* const prober = Singleton<net::HTTPSProber>::get();
+ net::HTTPSProber* const prober = net::HTTPSProber::GetInstance();
if (prober->HaveProbed(request_info_.url.host()) ||
prober->InFlight(request_info_.url.host())) {
continue;
diff --git a/net/url_request/url_request_job_manager.cc b/net/url_request/url_request_job_manager.cc
index 1bc0be1..117c15a 100644
--- a/net/url_request/url_request_job_manager.cc
+++ b/net/url_request/url_request_job_manager.cc
@@ -7,6 +7,7 @@
#include <algorithm>
#include "build/build_config.h"
+#include "base/singleton.h"
#include "base/string_util.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
@@ -45,6 +46,11 @@ URLRequestJobManager::URLRequestJobManager() : enable_file_access_(false) {
URLRequestJobManager::~URLRequestJobManager() {}
+// static
+URLRequestJobManager* URLRequestJobManager::GetInstance() {
+ return Singleton<URLRequestJobManager>::get();
+}
+
URLRequestJob* URLRequestJobManager::CreateJob(net::URLRequest* request) const {
#ifndef NDEBUG
DCHECK(IsAllowedThread());
diff --git a/net/url_request/url_request_job_manager.h b/net/url_request/url_request_job_manager.h
index 6d2421a..1e56b12 100644
--- a/net/url_request/url_request_job_manager.h
+++ b/net/url_request/url_request_job_manager.h
@@ -14,6 +14,8 @@
#include "base/platform_thread.h"
#include "net/url_request/url_request.h"
+template <typename T> struct DefaultSingletonTraits;
+
// This class is responsible for managing the set of protocol factories and
// request interceptors that determine how an URLRequestJob gets created to
// handle an net::URLRequest.
@@ -27,8 +29,8 @@
//
class URLRequestJobManager {
public:
- URLRequestJobManager();
- ~URLRequestJobManager();
+ // Returns the singleton instance.
+ static URLRequestJobManager* GetInstance();
// Instantiate an URLRequestJob implementation based on the registered
// interceptors and protocol factories. This will always succeed in
@@ -67,6 +69,10 @@ class URLRequestJobManager {
private:
typedef std::map<std::string, net::URLRequest::ProtocolFactory*> FactoryMap;
typedef std::vector<net::URLRequest::Interceptor*> InterceptorList;
+ friend struct DefaultSingletonTraits<URLRequestJobManager>;
+
+ URLRequestJobManager();
+ ~URLRequestJobManager();
mutable Lock lock_;
FactoryMap factories_;
diff --git a/net/websockets/websocket_job.cc b/net/websockets/websocket_job.cc
index a1e2dde..9adbaa3 100644
--- a/net/websockets/websocket_job.cc
+++ b/net/websockets/websocket_job.cc
@@ -6,6 +6,7 @@
#include <algorithm>
+#include "base/singleton.h"
#include "base/string_tokenizer.h"
#include "googleurl/src/gurl.h"
#include "net/base/net_errors.h"
@@ -144,8 +145,8 @@ void WebSocketJob::RestartWithAuth(
void WebSocketJob::DetachDelegate() {
state_ = CLOSED;
- Singleton<WebSocketThrottle>::get()->RemoveFromQueue(this);
- Singleton<WebSocketThrottle>::get()->WakeupSocketIfNecessary();
+ WebSocketThrottle::GetInstance()->RemoveFromQueue(this);
+ WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary();
scoped_refptr<WebSocketJob> protect(this);
@@ -165,7 +166,7 @@ int WebSocketJob::OnStartOpenConnection(
DCHECK(!callback_);
state_ = CONNECTING;
addresses_.Copy(socket->address_list().head(), true);
- Singleton<WebSocketThrottle>::get()->PutInQueue(this);
+ WebSocketThrottle::GetInstance()->PutInQueue(this);
if (!waiting_)
return OK;
callback_ = callback;
@@ -237,8 +238,8 @@ void WebSocketJob::OnReceivedData(
void WebSocketJob::OnClose(SocketStream* socket) {
state_ = CLOSED;
- Singleton<WebSocketThrottle>::get()->RemoveFromQueue(this);
- Singleton<WebSocketThrottle>::get()->WakeupSocketIfNecessary();
+ WebSocketThrottle::GetInstance()->RemoveFromQueue(this);
+ WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary();
scoped_refptr<WebSocketJob> protect(this);
@@ -405,8 +406,8 @@ void WebSocketJob::SaveNextCookie() {
handshake_response_.reset();
- Singleton<WebSocketThrottle>::get()->RemoveFromQueue(this);
- Singleton<WebSocketThrottle>::get()->WakeupSocketIfNecessary();
+ WebSocketThrottle::GetInstance()->RemoveFromQueue(this);
+ WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary();
return;
}
diff --git a/net/websockets/websocket_job_unittest.cc b/net/websockets/websocket_job_unittest.cc
index 53d4a625..43d8509 100644
--- a/net/websockets/websocket_job_unittest.cc
+++ b/net/websockets/websocket_job_unittest.cc
@@ -222,7 +222,7 @@ class WebSocketJobTest : public PlatformTest {
addr.ai_addr = reinterpret_cast<sockaddr*>(&sa_in);
addr.ai_next = NULL;
websocket_->addresses_.Copy(&addr, true);
- Singleton<WebSocketThrottle>::get()->PutInQueue(websocket_);
+ WebSocketThrottle::GetInstance()->PutInQueue(websocket_);
}
WebSocketJob::State GetWebSocketJobState() {
return websocket_->state_;
@@ -230,7 +230,7 @@ class WebSocketJobTest : public PlatformTest {
void CloseWebSocketJob() {
if (websocket_->socket_) {
websocket_->socket_->DetachDelegate();
- Singleton<WebSocketThrottle>::get()->RemoveFromQueue(websocket_);
+ WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_);
}
websocket_->state_ = WebSocketJob::CLOSED;
websocket_->delegate_ = NULL;
diff --git a/net/websockets/websocket_throttle.cc b/net/websockets/websocket_throttle.cc
index 2d62815..c714de6 100644
--- a/net/websockets/websocket_throttle.cc
+++ b/net/websockets/websocket_throttle.cc
@@ -54,6 +54,11 @@ WebSocketThrottle::~WebSocketThrottle() {
DCHECK(addr_map_.empty());
}
+// static
+WebSocketThrottle* WebSocketThrottle::GetInstance() {
+ return Singleton<WebSocketThrottle>::get();
+}
+
void WebSocketThrottle::PutInQueue(WebSocketJob* job) {
queue_.push_back(job);
const AddressList& address_list = job->address_list();
diff --git a/net/websockets/websocket_throttle.h b/net/websockets/websocket_throttle.h
index 0849834..9becc62 100644
--- a/net/websockets/websocket_throttle.h
+++ b/net/websockets/websocket_throttle.h
@@ -10,7 +10,8 @@
#include <string>
#include "base/hash_tables.h"
-#include "base/singleton.h"
+
+template <typename T> struct DefaultSingletonTraits;
namespace net {
@@ -27,6 +28,9 @@ class WebSocketJob;
// for that connection to have failed.
class WebSocketThrottle {
public:
+ // Returns the singleton instance.
+ static WebSocketThrottle* GetInstance();
+
// Puts |job| in |queue_| and queues for the destination addresses
// of |job|.
// If other job is using the same destination address, set |job| waiting.