diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-13 03:44:24 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-13 03:44:24 +0000 |
commit | d49801990067e7fe9a3caf141f31c6e6ba58fcfc (patch) | |
tree | 502fef7a064f7e1b67291d418b897117e2427822 /net/proxy | |
parent | 96508147cf0947fdd4ae19d9ee7f5f2e13268a5f (diff) | |
download | chromium_src-d49801990067e7fe9a3caf141f31c6e6ba58fcfc.zip chromium_src-d49801990067e7fe9a3caf141f31c6e6ba58fcfc.tar.gz chromium_src-d49801990067e7fe9a3caf141f31c6e6ba58fcfc.tar.bz2 |
Use automatic memory management for URLRequestContext's members.
Also make ProxyService refcounted so the sharing between profiles is explicit.
Review URL: http://codereview.chromium.org/13701
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6966 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/proxy')
-rw-r--r-- | net/proxy/proxy_script_fetcher_unittest.cc | 8 | ||||
-rw-r--r-- | net/proxy/proxy_service.cc | 5 | ||||
-rw-r--r-- | net/proxy/proxy_service.h | 6 | ||||
-rw-r--r-- | net/proxy/proxy_service_unittest.cc | 109 |
4 files changed, 70 insertions, 58 deletions
diff --git a/net/proxy/proxy_script_fetcher_unittest.cc b/net/proxy/proxy_script_fetcher_unittest.cc index 22cb91e..50a2f28d 100644 --- a/net/proxy/proxy_script_fetcher_unittest.cc +++ b/net/proxy/proxy_script_fetcher_unittest.cc @@ -28,12 +28,8 @@ class RequestContext : public URLRequestContext { RequestContext() { net::ProxyInfo no_proxy; proxy_service_ = net::ProxyService::Create(&no_proxy); - http_transaction_factory_ = net::HttpNetworkLayer::CreateFactory( - proxy_service_); - } - ~RequestContext() { - delete http_transaction_factory_; - delete proxy_service_; + http_transaction_factory_.reset(net::HttpNetworkLayer::CreateFactory( + proxy_service_)); } }; diff --git a/net/proxy/proxy_service.cc b/net/proxy/proxy_service.cc index 5f0f6b2..e8fa060 100644 --- a/net/proxy/proxy_service.cc +++ b/net/proxy/proxy_service.cc @@ -305,6 +305,11 @@ ProxyService* ProxyService::Create(const ProxyInfo* pi) { #endif } +// static +ProxyService* ProxyService::CreateNull() { + return new ProxyService(new ProxyResolverNull()); +} + int ProxyService::ResolveProxy(const GURL& url, ProxyInfo* result, CompletionCallback* callback, PacRequest** pac_request) { diff --git a/net/proxy/proxy_service.h b/net/proxy/proxy_service.h index bea4aef..6ab6a1b 100644 --- a/net/proxy/proxy_service.h +++ b/net/proxy/proxy_service.h @@ -83,7 +83,7 @@ typedef std::map<std::string, ProxyRetryInfo> ProxyRetryInfoMap; // This class can be used to resolve the proxy server to use when loading a // HTTP(S) URL. It uses the given ProxyResolver to handle the actual proxy // resolution. See ProxyResolverWinHttp for example. -class ProxyService { +class ProxyService : public base::RefCounted<ProxyService> { public: // The instance takes ownership of |resolver|. explicit ProxyService(ProxyResolver* resolver); @@ -138,6 +138,10 @@ class ProxyService { // use IE's settings). static ProxyService* Create(const ProxyInfo* pi); + // Create a ProxyService which fails every request, causing fallback to a + // direct connection. Convenience function used by unit tests. + static ProxyService* CreateNull(); + // TODO(eroman): remove once WinHTTP is gone. // Get the ProxyInfo used to create this proxy service (only used by WinHTTP). const ProxyInfo* proxy_info() const { diff --git a/net/proxy/proxy_service_unittest.cc b/net/proxy/proxy_service_unittest.cc index 1ad7d32..8be7762 100644 --- a/net/proxy/proxy_service_unittest.cc +++ b/net/proxy/proxy_service_unittest.cc @@ -62,12 +62,13 @@ TEST(ProxyListTest, GetAnnotatedList) { } TEST(ProxyServiceTest, Direct) { - net::ProxyService service(new MockProxyResolver); + scoped_refptr<net::ProxyService> service = + new net::ProxyService(new MockProxyResolver); GURL url("http://www.google.com/"); net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info.is_direct()); } @@ -78,12 +79,12 @@ TEST(ProxyServiceTest, PAC) { resolver->info.UseNamedProxy("foopy"); resolver->info_predicate_query_host = "www.google.com"; - net::ProxyService service(resolver); + scoped_refptr<net::ProxyService> service = new net::ProxyService(resolver); GURL url("http://www.google.com/"); net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); EXPECT_EQ(info.proxy_server(), "foopy"); @@ -95,18 +96,18 @@ TEST(ProxyServiceTest, PAC_FailoverToDirect) { resolver->info.UseNamedProxy("foopy:8080"); resolver->info_predicate_query_host = "www.google.com"; - net::ProxyService service(resolver); + scoped_refptr<net::ProxyService> service = new net::ProxyService(resolver); GURL url("http://www.google.com/"); net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); EXPECT_EQ(info.proxy_server(), "foopy:8080"); // Now, imagine that connecting to foopy:8080 fails. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info.is_direct()); } @@ -120,15 +121,15 @@ TEST(ProxyServiceTest, PAC_FailsToDownload) { resolver->info_predicate_query_host = "www.google.com"; resolver->fail_get_proxy_for_url = true; - net::ProxyService service(resolver); + scoped_refptr<net::ProxyService> service = new net::ProxyService(resolver); GURL url("http://www.google.com/"); net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info.is_direct()); - rv = service.ResolveProxy(url, &info, NULL, NULL); + rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info.is_direct()); @@ -137,7 +138,7 @@ TEST(ProxyServiceTest, PAC_FailsToDownload) { // But, if that fails, then we should give the proxy config another shot // since we have never tried it with this URL before. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); EXPECT_EQ(info.proxy_server(), "foopy_valid:8080"); @@ -153,13 +154,13 @@ TEST(ProxyServiceTest, ProxyFallback) { resolver->info_predicate_query_host = "www.google.com"; resolver->fail_get_proxy_for_url = false; - net::ProxyService service(resolver); + scoped_refptr<net::ProxyService> service = new net::ProxyService(resolver); GURL url("http://www.google.com/"); // Get the proxy information. net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); @@ -167,7 +168,7 @@ TEST(ProxyServiceTest, ProxyFallback) { EXPECT_EQ(info.proxy_server(), "foopy1:8080"); // Fake an error on the proxy. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); // The second proxy should be specified. @@ -180,23 +181,23 @@ TEST(ProxyServiceTest, ProxyFallback) { resolver->info_predicate_query_host = "www.google.com"; resolver->fail_get_proxy_for_url = false; - rv = service.ResolveProxy(url, &info, NULL, NULL); + rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); EXPECT_EQ(info.proxy_server(), "foopy3:7070"); // We fake another error. It should now try the third one. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_EQ(info.proxy_server(), "foopy2:9090"); // Fake another error, the last proxy is gone, the list should now be empty. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); // We try direct. EXPECT_TRUE(info.is_direct()); // If it fails again, we don't have anything else to try. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::ERR_FAILED); // We try direct. // TODO(nsylvain): Test that the proxy can be retried after the delay. @@ -211,13 +212,13 @@ TEST(ProxyServiceTest, ProxyFallback_NewSettings) { resolver->info_predicate_query_host = "www.google.com"; resolver->fail_get_proxy_for_url = false; - net::ProxyService service(resolver); + scoped_refptr<net::ProxyService> service = new net::ProxyService(resolver); GURL url("http://www.google.com/"); // Get the proxy information. net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); @@ -228,14 +229,14 @@ TEST(ProxyServiceTest, ProxyFallback_NewSettings) { resolver->config = net::ProxyConfig(); resolver->config.pac_url = GURL("http://foopy-new/proxy.pac"); - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); // The first proxy is still there since the configuration changed. EXPECT_EQ(info.proxy_server(), "foopy1:8080"); // We fake another error. It should now ignore the first one. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_EQ(info.proxy_server(), "foopy2:9090"); @@ -244,7 +245,7 @@ TEST(ProxyServiceTest, ProxyFallback_NewSettings) { resolver->config.pac_url = GURL("http://foopy-new2/proxy.pac"); // We fake anothe error. It should go back to the first proxy. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_EQ(info.proxy_server(), "foopy1:8080"); } @@ -258,13 +259,13 @@ TEST(ProxyServiceTest, ProxyFallback_BadConfig) { resolver->info_predicate_query_host = "www.google.com"; resolver->fail_get_proxy_for_url = false; - net::ProxyService service(resolver); + scoped_refptr<net::ProxyService> service = new net::ProxyService(resolver); GURL url("http://www.google.com/"); // Get the proxy information. net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); @@ -272,7 +273,7 @@ TEST(ProxyServiceTest, ProxyFallback_BadConfig) { EXPECT_EQ(info.proxy_server(), "foopy1:8080"); // Fake a proxy error. - rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); // The first proxy is ignored, and the second one is selected. @@ -282,7 +283,7 @@ TEST(ProxyServiceTest, ProxyFallback_BadConfig) { // Fake a PAC failure. net::ProxyInfo info2; resolver->fail_get_proxy_for_url = true; - rv = service.ResolveProxy(url, &info2, NULL, NULL); + rv = service->ResolveProxy(url, &info2, NULL, NULL); EXPECT_EQ(rv, net::OK); // No proxy servers are returned. It's a direct connection. @@ -295,14 +296,14 @@ TEST(ProxyServiceTest, ProxyFallback_BadConfig) { // Try to resolve, it will still return "direct" because we have no reason // to check the config since everything works. net::ProxyInfo info3; - rv = service.ResolveProxy(url, &info3, NULL, NULL); + rv = service->ResolveProxy(url, &info3, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info3.is_direct()); // But if the direct connection fails, we check if the ProxyInfo tried to // resolve the proxy before, and if not (like in this case), we give the // PAC another try. - rv = service.ReconsiderProxyAfterError(url, &info3, NULL, NULL); + rv = service->ReconsiderProxyAfterError(url, &info3, NULL, NULL); EXPECT_EQ(rv, net::OK); // The first proxy is still there since the list of bad proxies got cleared. @@ -318,18 +319,20 @@ TEST(ProxyServiceTest, ProxyBypassList) { config.auto_detect = false; config.proxy_bypass_local_names = true; - net::ProxyService service(new MockProxyResolver(config)); + scoped_refptr<net::ProxyService> service = + new net::ProxyService(new MockProxyResolver(config)); GURL url("http://www.google.com/"); // Get the proxy information. net::ProxyInfo info; - int rv = service.ResolveProxy(url, &info, NULL, NULL); + int rv = service->ResolveProxy(url, &info, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info.is_direct()); - net::ProxyService service1(new MockProxyResolver(config)); + scoped_refptr<net::ProxyService> service1 = + new net::ProxyService(new MockProxyResolver(config)); GURL test_url1("local"); net::ProxyInfo info1; - rv = service1.ResolveProxy(test_url1, &info1, NULL, NULL); + rv = service1->ResolveProxy(test_url1, &info1, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info1.is_direct()); @@ -337,10 +340,10 @@ TEST(ProxyServiceTest, ProxyBypassList) { config.proxy_bypass.push_back("*.org"); config.proxy_bypass_local_names = true; MockProxyResolver* resolver = new MockProxyResolver(config); - net::ProxyService service2(resolver); + scoped_refptr<net::ProxyService> service2 = new net::ProxyService(resolver); GURL test_url2("http://www.webkit.org"); net::ProxyInfo info2; - rv = service2.ResolveProxy(test_url2, &info2, NULL, NULL); + rv = service2->ResolveProxy(test_url2, &info2, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info2.is_direct()); @@ -349,10 +352,10 @@ TEST(ProxyServiceTest, ProxyBypassList) { config.proxy_bypass.push_back("7*"); config.proxy_bypass_local_names = true; resolver = new MockProxyResolver(config); - net::ProxyService service3(resolver); + scoped_refptr<net::ProxyService> service3 = new net::ProxyService(resolver); GURL test_url3("http://74.125.19.147"); net::ProxyInfo info3; - rv = service3.ResolveProxy(test_url3, &info3, NULL, NULL); + rv = service3->ResolveProxy(test_url3, &info3, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info3.is_direct()); @@ -360,10 +363,10 @@ TEST(ProxyServiceTest, ProxyBypassList) { config.proxy_bypass.push_back("*.org"); config.proxy_bypass_local_names = true; resolver = new MockProxyResolver(config); - net::ProxyService service4(resolver); + scoped_refptr<net::ProxyService> service4 = new net::ProxyService(resolver); GURL test_url4("http://www.msn.com"); net::ProxyInfo info4; - rv = service4.ResolveProxy(test_url4, &info4, NULL, NULL); + rv = service4->ResolveProxy(test_url4, &info4, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info4.is_direct()); @@ -371,10 +374,10 @@ TEST(ProxyServiceTest, ProxyBypassList) { config.proxy_bypass.push_back("*.MSN.COM"); config.proxy_bypass_local_names = true; resolver = new MockProxyResolver(config); - net::ProxyService service5(resolver); + scoped_refptr<net::ProxyService> service5 = new net::ProxyService(resolver); GURL test_url5("http://www.msnbc.msn.com"); net::ProxyInfo info5; - rv = service5.ResolveProxy(test_url5, &info5, NULL, NULL); + rv = service5->ResolveProxy(test_url5, &info5, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info5.is_direct()); @@ -382,10 +385,10 @@ TEST(ProxyServiceTest, ProxyBypassList) { config.proxy_bypass.push_back("*.msn.com"); config.proxy_bypass_local_names = true; resolver = new MockProxyResolver(config); - net::ProxyService service6(resolver); + scoped_refptr<net::ProxyService> service6 = new net::ProxyService(resolver); GURL test_url6("HTTP://WWW.MSNBC.MSN.COM"); net::ProxyInfo info6; - rv = service6.ResolveProxy(test_url6, &info6, NULL, NULL); + rv = service6->ResolveProxy(test_url6, &info6, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info6.is_direct()); } @@ -395,36 +398,40 @@ TEST(ProxyServiceTest, PerProtocolProxyTests) { config.proxy_server = "http=foopy1:8080;https=foopy2:8080"; config.auto_detect = false; - net::ProxyService service1(new MockProxyResolver(config)); + scoped_refptr<net::ProxyService> service1 = + new net::ProxyService(new MockProxyResolver(config)); GURL test_url1("http://www.msn.com"); net::ProxyInfo info1; - int rv = service1.ResolveProxy(test_url1, &info1, NULL, NULL); + int rv = service1->ResolveProxy(test_url1, &info1, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info1.is_direct()); EXPECT_TRUE(info1.proxy_server() == "foopy1:8080"); - net::ProxyService service2(new MockProxyResolver(config)); + scoped_refptr<net::ProxyService> service2 = + new net::ProxyService(new MockProxyResolver(config)); GURL test_url2("ftp://ftp.google.com"); net::ProxyInfo info2; - rv = service2.ResolveProxy(test_url2, &info2, NULL, NULL); + rv = service2->ResolveProxy(test_url2, &info2, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_TRUE(info2.is_direct()); EXPECT_TRUE(info2.proxy_server() == ""); - net::ProxyService service3(new MockProxyResolver(config)); + scoped_refptr<net::ProxyService> service3 = + new net::ProxyService(new MockProxyResolver(config)); GURL test_url3("https://webbranch.techcu.com"); net::ProxyInfo info3; - rv = service3.ResolveProxy(test_url3, &info3, NULL, NULL); + rv = service3->ResolveProxy(test_url3, &info3, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info3.is_direct()); EXPECT_TRUE(info3.proxy_server() == "foopy2:8080"); MockProxyResolver* resolver = new MockProxyResolver(config); resolver->config.proxy_server = "foopy1:8080"; - net::ProxyService service4(resolver); + scoped_refptr<net::ProxyService> service4 = + new net::ProxyService(resolver); GURL test_url4("www.microsoft.com"); net::ProxyInfo info4; - rv = service4.ResolveProxy(test_url4, &info4, NULL, NULL); + rv = service4->ResolveProxy(test_url4, &info4, NULL, NULL); EXPECT_EQ(rv, net::OK); EXPECT_FALSE(info4.is_direct()); EXPECT_TRUE(info4.proxy_server() == "foopy1:8080"); |