diff options
author | shalev@chromium.org <shalev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-24 17:42:49 +0000 |
---|---|---|
committer | shalev@chromium.org <shalev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-24 17:42:49 +0000 |
commit | 9d5730b2fd88a6e7710c250b5b0d875d193d8325 (patch) | |
tree | d3bc335707cdaf28335577a627e9a3c8a336a514 /net/url_request | |
parent | 468dc4e4a82846f5677c9a906afadb8df590871a (diff) | |
download | chromium_src-9d5730b2fd88a6e7710c250b5b0d875d193d8325.zip chromium_src-9d5730b2fd88a6e7710c250b5b0d875d193d8325.tar.gz chromium_src-9d5730b2fd88a6e7710c250b5b0d875d193d8325.tar.bz2 |
Turned job_factory into a pure virtual class
This makes it possible to create wrappers for job_factory that handle a particular job type, and forward the rest to the old job_factory.
TBR=michaeln@chromium.org, brettw@chromium.org, davemoore@chromium.org, kalman@chromium.org, benjhayden@chromium.org, jochen@chromium.org
BUG=None
Review URL: https://chromiumcodereview.appspot.com/10836248
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153241 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request')
-rw-r--r-- | net/url_request/url_request_job_factory.cc | 118 | ||||
-rw-r--r-- | net/url_request/url_request_job_factory.h | 44 | ||||
-rw-r--r-- | net/url_request/url_request_job_factory_impl.cc | 130 | ||||
-rw-r--r-- | net/url_request/url_request_job_factory_impl.h | 52 | ||||
-rw-r--r-- | net/url_request/url_request_job_factory_impl_unittest.cc (renamed from net/url_request/url_request_job_factory_unittest.cc) | 18 | ||||
-rw-r--r-- | net/url_request/url_request_test_util.cc | 4 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 6 |
7 files changed, 219 insertions, 153 deletions
diff --git a/net/url_request/url_request_job_factory.cc b/net/url_request/url_request_job_factory.cc index 4961c03..e07152a 100644 --- a/net/url_request/url_request_job_factory.cc +++ b/net/url_request/url_request_job_factory.cc @@ -4,11 +4,6 @@ #include "net/url_request/url_request_job_factory.h" -#include "base/stl_util.h" -#include "googleurl/src/gurl.h" -#include "net/base/load_flags.h" -#include "net/url_request/url_request_job_manager.h" - namespace net { URLRequestJobFactory::ProtocolHandler::~ProtocolHandler() {} @@ -22,117 +17,6 @@ bool URLRequestJobFactory::Interceptor::WillHandleProtocol( URLRequestJobFactory::URLRequestJobFactory() {} -URLRequestJobFactory::~URLRequestJobFactory() { - STLDeleteValues(&protocol_handler_map_); - STLDeleteElements(&interceptors_); -} - -bool URLRequestJobFactory::SetProtocolHandler( - const std::string& scheme, - ProtocolHandler* protocol_handler) { - DCHECK(CalledOnValidThread()); - - if (!protocol_handler) { - ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme); - if (it == protocol_handler_map_.end()) - return false; - - delete it->second; - protocol_handler_map_.erase(it); - return true; - } - - if (ContainsKey(protocol_handler_map_, scheme)) - return false; - protocol_handler_map_[scheme] = protocol_handler; - return true; -} - -void URLRequestJobFactory::AddInterceptor(Interceptor* interceptor) { - DCHECK(CalledOnValidThread()); - CHECK(interceptor); - - interceptors_.push_back(interceptor); -} - -URLRequestJob* URLRequestJobFactory::MaybeCreateJobWithInterceptor( - URLRequest* request, NetworkDelegate* network_delegate) const { - DCHECK(CalledOnValidThread()); - URLRequestJob* job = NULL; - - if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { - InterceptorList::const_iterator i; - for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { - job = (*i)->MaybeIntercept(request, network_delegate); - if (job) - return job; - } - } - return NULL; -} - -URLRequestJob* URLRequestJobFactory::MaybeCreateJobWithProtocolHandler( - const std::string& scheme, - URLRequest* request, - NetworkDelegate* network_delegate) const { - DCHECK(CalledOnValidThread()); - ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme); - if (it == protocol_handler_map_.end()) - return NULL; - return it->second->MaybeCreateJob(request, network_delegate); -} - -URLRequestJob* URLRequestJobFactory::MaybeInterceptRedirect( - const GURL& location, - URLRequest* request, - NetworkDelegate* network_delegate) const { - DCHECK(CalledOnValidThread()); - URLRequestJob* job = NULL; - - if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { - InterceptorList::const_iterator i; - for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { - job = (*i)->MaybeInterceptRedirect(location, request, network_delegate); - if (job) - return job; - } - } - return NULL; -} - -URLRequestJob* URLRequestJobFactory::MaybeInterceptResponse( - URLRequest* request, NetworkDelegate* network_delegate) const { - DCHECK(CalledOnValidThread()); - URLRequestJob* job = NULL; - - if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { - InterceptorList::const_iterator i; - for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { - job = (*i)->MaybeInterceptResponse(request, network_delegate); - if (job) - return job; - } - } - return NULL; -} - -bool URLRequestJobFactory::IsHandledProtocol(const std::string& scheme) const { - DCHECK(CalledOnValidThread()); - InterceptorList::const_iterator i; - for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { - if ((*i)->WillHandleProtocol(scheme)) - return true; - } - return ContainsKey(protocol_handler_map_, scheme) || - URLRequestJobManager::GetInstance()->SupportsScheme(scheme); -} - -bool URLRequestJobFactory::IsHandledURL(const GURL& url) const { - if (!url.is_valid()) { - // We handle error cases. - return true; - } - return IsHandledProtocol(url.scheme()); -} +URLRequestJobFactory::~URLRequestJobFactory() {} } // namespace net diff --git a/net/url_request/url_request_job_factory.h b/net/url_request/url_request_job_factory.h index 5f4e1ae..adff7f1 100644 --- a/net/url_request/url_request_job_factory.h +++ b/net/url_request/url_request_job_factory.h @@ -5,10 +5,10 @@ #ifndef NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_ #define NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_ -#include <map> #include <string> -#include <vector> + #include "base/basictypes.h" +#include "base/compiler_specific.h" #include "base/threading/non_thread_safe.h" #include "net/base/net_export.h" @@ -23,6 +23,7 @@ class URLRequestJob; class NET_EXPORT URLRequestJobFactory : NON_EXPORTED_BASE(public base::NonThreadSafe) { public: + // TODO(shalev): Move this to URLRequestJobFactoryImpl. class NET_EXPORT ProtocolHandler { public: virtual ~ProtocolHandler(); @@ -31,6 +32,7 @@ class NET_EXPORT URLRequestJobFactory URLRequest* request, NetworkDelegate* network_delegate) const = 0; }; + // TODO(shalev): Move this to URLRequestJobFactoryImpl. class NET_EXPORT Interceptor { public: virtual ~Interceptor(); @@ -70,45 +72,43 @@ class NET_EXPORT URLRequestJobFactory }; URLRequestJobFactory(); - ~URLRequestJobFactory(); + virtual ~URLRequestJobFactory(); + // TODO(shalev): Remove this from the interface. // Sets the ProtocolHandler for a scheme. Returns true on success, false on // failure (a ProtocolHandler already exists for |scheme|). On success, // URLRequestJobFactory takes ownership of |protocol_handler|. - bool SetProtocolHandler(const std::string& scheme, - ProtocolHandler* protocol_handler); + virtual bool SetProtocolHandler(const std::string& scheme, + ProtocolHandler* protocol_handler) = 0; + // TODO(shalev): Remove this from the interface. // Takes ownership of |interceptor|. Adds it to the end of the Interceptor // list. - void AddInterceptor(Interceptor* interceptor); + virtual void AddInterceptor(Interceptor* interceptor) = 0; - URLRequestJob* MaybeCreateJobWithInterceptor( - URLRequest* request, NetworkDelegate* network_delegate) const; + // TODO(shalev): Consolidate MaybeCreateJobWithInterceptor and + // MaybeCreateJobWithProtocolHandler into a single method. + virtual URLRequestJob* MaybeCreateJobWithInterceptor( + URLRequest* request, NetworkDelegate* network_delegate) const = 0; - URLRequestJob* MaybeCreateJobWithProtocolHandler( + virtual URLRequestJob* MaybeCreateJobWithProtocolHandler( const std::string& scheme, URLRequest* request, - NetworkDelegate* network_delegate) const; + NetworkDelegate* network_delegate) const = 0; - URLRequestJob* MaybeInterceptRedirect( + virtual URLRequestJob* MaybeInterceptRedirect( const GURL& location, URLRequest* request, - NetworkDelegate* network_delegate) const; + NetworkDelegate* network_delegate) const = 0; - URLRequestJob* MaybeInterceptResponse( - URLRequest* request, NetworkDelegate* network_delegate) const; + virtual URLRequestJob* MaybeInterceptResponse( + URLRequest* request, NetworkDelegate* network_delegate) const = 0; - bool IsHandledProtocol(const std::string& scheme) const; + virtual bool IsHandledProtocol(const std::string& scheme) const = 0; - bool IsHandledURL(const GURL& url) const; + virtual bool IsHandledURL(const GURL& url) const = 0; private: - typedef std::map<std::string, ProtocolHandler*> ProtocolHandlerMap; - typedef std::vector<Interceptor*> InterceptorList; - - ProtocolHandlerMap protocol_handler_map_; - InterceptorList interceptors_; - DISALLOW_COPY_AND_ASSIGN(URLRequestJobFactory); }; diff --git a/net/url_request/url_request_job_factory_impl.cc b/net/url_request/url_request_job_factory_impl.cc new file mode 100644 index 0000000..aaeed79 --- /dev/null +++ b/net/url_request/url_request_job_factory_impl.cc @@ -0,0 +1,130 @@ +// 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/url_request/url_request_job_factory_impl.h" + +#include "base/stl_util.h" +#include "googleurl/src/gurl.h" +#include "net/base/load_flags.h" +#include "net/url_request/url_request_job_manager.h" + +namespace net { + +URLRequestJobFactoryImpl::URLRequestJobFactoryImpl() {} + +URLRequestJobFactoryImpl::~URLRequestJobFactoryImpl() { + STLDeleteValues(&protocol_handler_map_); + STLDeleteElements(&interceptors_); +} + +bool URLRequestJobFactoryImpl::SetProtocolHandler( + const std::string& scheme, + ProtocolHandler* protocol_handler) { + DCHECK(CalledOnValidThread()); + + if (!protocol_handler) { + ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme); + if (it == protocol_handler_map_.end()) + return false; + + delete it->second; + protocol_handler_map_.erase(it); + return true; + } + + if (ContainsKey(protocol_handler_map_, scheme)) + return false; + protocol_handler_map_[scheme] = protocol_handler; + return true; +} + +void URLRequestJobFactoryImpl::AddInterceptor(Interceptor* interceptor) { + DCHECK(CalledOnValidThread()); + CHECK(interceptor); + + interceptors_.push_back(interceptor); +} + +URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithInterceptor( + URLRequest* request, NetworkDelegate* network_delegate) const { + DCHECK(CalledOnValidThread()); + URLRequestJob* job = NULL; + + if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { + InterceptorList::const_iterator i; + for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { + job = (*i)->MaybeIntercept(request, network_delegate); + if (job) + return job; + } + } + return NULL; +} + +URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithProtocolHandler( + const std::string& scheme, + URLRequest* request, + NetworkDelegate* network_delegate) const { + DCHECK(CalledOnValidThread()); + ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme); + if (it == protocol_handler_map_.end()) + return NULL; + return it->second->MaybeCreateJob(request, network_delegate); +} + +URLRequestJob* URLRequestJobFactoryImpl::MaybeInterceptRedirect( + const GURL& location, + URLRequest* request, + NetworkDelegate* network_delegate) const { + DCHECK(CalledOnValidThread()); + URLRequestJob* job = NULL; + + if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { + InterceptorList::const_iterator i; + for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { + job = (*i)->MaybeInterceptRedirect(location, request, network_delegate); + if (job) + return job; + } + } + return NULL; +} + +URLRequestJob* URLRequestJobFactoryImpl::MaybeInterceptResponse( + URLRequest* request, NetworkDelegate* network_delegate) const { + DCHECK(CalledOnValidThread()); + URLRequestJob* job = NULL; + + if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) { + InterceptorList::const_iterator i; + for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { + job = (*i)->MaybeInterceptResponse(request, network_delegate); + if (job) + return job; + } + } + return NULL; +} + +bool URLRequestJobFactoryImpl::IsHandledProtocol( + const std::string& scheme) const { + DCHECK(CalledOnValidThread()); + InterceptorList::const_iterator i; + for (i = interceptors_.begin(); i != interceptors_.end(); ++i) { + if ((*i)->WillHandleProtocol(scheme)) + return true; + } + return ContainsKey(protocol_handler_map_, scheme) || + URLRequestJobManager::GetInstance()->SupportsScheme(scheme); +} + +bool URLRequestJobFactoryImpl::IsHandledURL(const GURL& url) const { + if (!url.is_valid()) { + // We handle error cases. + return true; + } + return IsHandledProtocol(url.scheme()); +} + +} // namespace net diff --git a/net/url_request/url_request_job_factory_impl.h b/net/url_request/url_request_job_factory_impl.h new file mode 100644 index 0000000..ff27185 --- /dev/null +++ b/net/url_request/url_request_job_factory_impl.h @@ -0,0 +1,52 @@ +// 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. + +#ifndef NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_IMPL_H_ +#define NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_IMPL_H_ + +#include <map> +#include <vector> +#include "base/basictypes.h" +#include "net/base/net_export.h" +#include "net/url_request/url_request_job_factory.h" + +namespace net { + +class NET_EXPORT URLRequestJobFactoryImpl : public URLRequestJobFactory { + public: + URLRequestJobFactoryImpl(); + virtual ~URLRequestJobFactoryImpl(); + + // URLRequestJobFactory implementation + virtual bool SetProtocolHandler(const std::string& scheme, + ProtocolHandler* protocol_handler) OVERRIDE; + virtual void AddInterceptor(Interceptor* interceptor) OVERRIDE; + virtual URLRequestJob* MaybeCreateJobWithInterceptor( + URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE; + virtual URLRequestJob* MaybeCreateJobWithProtocolHandler( + const std::string& scheme, + URLRequest* request, + NetworkDelegate* network_delegate) const OVERRIDE; + virtual URLRequestJob* MaybeInterceptRedirect( + const GURL& location, + URLRequest* request, + NetworkDelegate* network_delegate) const OVERRIDE; + virtual URLRequestJob* MaybeInterceptResponse( + URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE; + virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE; + virtual bool IsHandledURL(const GURL& url) const OVERRIDE; + + private: + typedef std::map<std::string, ProtocolHandler*> ProtocolHandlerMap; + typedef std::vector<Interceptor*> InterceptorList; + + ProtocolHandlerMap protocol_handler_map_; + InterceptorList interceptors_; + + DISALLOW_COPY_AND_ASSIGN(URLRequestJobFactoryImpl); +}; + +} // namespace net + +#endif // NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_IMPL_H_ diff --git a/net/url_request/url_request_job_factory_unittest.cc b/net/url_request/url_request_job_factory_impl_unittest.cc index 673e007..232b21a 100644 --- a/net/url_request/url_request_job_factory_unittest.cc +++ b/net/url_request/url_request_job_factory_impl_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "net/url_request/url_request_job_factory.h" +#include "net/url_request/url_request_job_factory_impl.h" #include "base/bind.h" #include "base/memory/weak_ptr.h" @@ -108,7 +108,7 @@ TEST(URLRequestJobFactoryTest, NoProtocolHandler) { TEST(URLRequestJobFactoryTest, BasicProtocolHandler) { TestDelegate delegate; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); @@ -121,7 +121,7 @@ TEST(URLRequestJobFactoryTest, BasicProtocolHandler) { } TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) { - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); @@ -130,7 +130,7 @@ TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) { TEST(URLRequestJobFactoryTest, BasicInterceptor) { TestDelegate delegate; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); job_factory.AddInterceptor(new DummyInterceptor); @@ -144,7 +144,7 @@ TEST(URLRequestJobFactoryTest, BasicInterceptor) { TEST(URLRequestJobFactoryTest, InterceptorNeedsValidSchemeStill) { TestDelegate delegate; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); job_factory.AddInterceptor(new DummyInterceptor); @@ -158,7 +158,7 @@ TEST(URLRequestJobFactoryTest, InterceptorNeedsValidSchemeStill) { TEST(URLRequestJobFactoryTest, InterceptorOverridesProtocolHandler) { TestDelegate delegate; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); @@ -173,7 +173,7 @@ TEST(URLRequestJobFactoryTest, InterceptorOverridesProtocolHandler) { TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) { TestDelegate delegate; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); DummyInterceptor* interceptor = new DummyInterceptor; @@ -187,7 +187,7 @@ TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) { TEST(URLRequestJobFactoryTest, InterceptorInterceptsHandledUnknownProtocols) { TestDelegate delegate; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; TestURLRequestContext request_context; request_context.set_job_factory(&job_factory); DummyInterceptor* interceptor = new DummyInterceptor; @@ -204,7 +204,7 @@ TEST(URLRequestJobFactoryTest, InterceptorInterceptsHandledUnknownProtocols) { TEST(URLRequestJobFactoryTest, InterceptorAffectsIsHandledProtocol) { DummyInterceptor* interceptor = new DummyInterceptor; - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; job_factory.AddInterceptor(interceptor); EXPECT_FALSE(interceptor->WillHandleProtocol("anything")); EXPECT_FALSE(job_factory.IsHandledProtocol("anything")); diff --git a/net/url_request/url_request_test_util.cc b/net/url_request/url_request_test_util.cc index 5e66c71..53574f7 100644 --- a/net/url_request/url_request_test_util.cc +++ b/net/url_request/url_request_test_util.cc @@ -16,7 +16,7 @@ #include "net/base/server_bound_cert_service.h" #include "net/http/http_network_session.h" #include "net/http/http_server_properties_impl.h" -#include "net/url_request/url_request_job_factory.h" +#include "net/url_request/url_request_job_factory_impl.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -109,7 +109,7 @@ void TestURLRequestContext::Init() { if (accept_charset().empty()) set_accept_charset("iso-8859-1,*,utf-8"); if (!job_factory()) - context_storage_.set_job_factory(new net::URLRequestJobFactory); + context_storage_.set_job_factory(new net::URLRequestJobFactoryImpl); } TestURLRequest::TestURLRequest(const GURL& url, diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index 325cdce..406dabf 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -56,7 +56,7 @@ #include "net/url_request/url_request.h" #include "net/url_request/url_request_file_dir_job.h" #include "net/url_request/url_request_http_job.h" -#include "net/url_request/url_request_job_factory.h" +#include "net/url_request/url_request_job_factory_impl.h" #include "net/url_request/url_request_redirect_job.h" #include "net/url_request/url_request_test_job.h" #include "net/url_request/url_request_test_util.h" @@ -438,7 +438,7 @@ class URLRequestTest : public PlatformTest { protected: TestNetworkDelegate default_network_delegate_; // must outlive URLRequest - URLRequestJobFactory job_factory_; + URLRequestJobFactoryImpl job_factory_; TestURLRequestContext default_context_; }; @@ -4168,7 +4168,7 @@ class URLRequestTestFTP : public URLRequestTest { TEST_F(URLRequestTestFTP, UnsafePort) { ASSERT_TRUE(test_server_.Start()); - URLRequestJobFactory job_factory; + URLRequestJobFactoryImpl job_factory; GURL url("ftp://127.0.0.1:7"); FtpProtocolHandler ftp_protocol_handler( |