diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-03 21:00:14 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-03 21:00:14 +0000 |
commit | 1ac6af94287a2f821a43003b2be2ba21f319a66d (patch) | |
tree | 3398fd0b22e8f9bde49f5aa7f50ff3ab1a42a81c /webkit | |
parent | cc204b2cfb421e72bb9d6187fa88cb412576fc94 (diff) | |
download | chromium_src-1ac6af94287a2f821a43003b2be2ba21f319a66d.zip chromium_src-1ac6af94287a2f821a43003b2be2ba21f319a66d.tar.gz chromium_src-1ac6af94287a2f821a43003b2be2ba21f319a66d.tar.bz2 |
Make HostResolver NonThreadSafe and not thread safe refcounted.
Required making SyncHostResolverBridge not use RefCountedThreadSafe. I refactored the innards to have a thread safe refcounted Core implementation.
BUG=45298
Review URL: http://codereview.chromium.org/2122015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@48867 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/appcache/appcache_update_job_unittest.cc | 46 | ||||
-rw-r--r-- | webkit/support/test_webkit_client.cc | 3 | ||||
-rw-r--r-- | webkit/tools/test_shell/node_leak_test.cc | 3 | ||||
-rw-r--r-- | webkit/tools/test_shell/simple_resource_loader_bridge.cc | 152 | ||||
-rw-r--r-- | webkit/tools/test_shell/simple_resource_loader_bridge.h | 14 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell_main.cc | 3 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell_request_context.cc | 15 |
7 files changed, 151 insertions, 85 deletions
diff --git a/webkit/appcache/appcache_update_job_unittest.cc b/webkit/appcache/appcache_update_job_unittest.cc index 7ea664f..a10f53f 100644 --- a/webkit/appcache/appcache_update_job_unittest.cc +++ b/webkit/appcache/appcache_update_job_unittest.cc @@ -308,6 +308,36 @@ std::string HttpHeadersRequestTestJob::expect_if_none_match_; bool HttpHeadersRequestTestJob::saw_if_none_match_ = false; bool HttpHeadersRequestTestJob::already_checked_ = false; +namespace { + +class IOThread : public base::Thread { + public: + IOThread(const char* name) : base::Thread(name) { + } + + ~IOThread() { + // We cannot rely on our base class to stop the thread since we want our + // CleanUp function to run. + Stop(); + } + + const scoped_refptr<URLRequestContext>& request_context() { + return request_context_; + } + + virtual void Init() { + request_context_ = new TestURLRequestContext(); + } + + virtual void CleanUp() { + request_context_ = NULL; + } + + scoped_refptr<URLRequestContext> request_context_; +}; + +} // namespace + class AppCacheUpdateJobTest : public testing::Test, public AppCacheGroup::UpdateObserver { public: @@ -358,15 +388,13 @@ class AppCacheUpdateJobTest : public testing::Test, } static void SetUpTestCase() { - io_thread_ = new base::Thread("AppCacheUpdateJob IO test thread"); + io_thread_ = new IOThread("AppCacheUpdateJob IO test thread"); base::Thread::Options options(MessageLoop::TYPE_IO, 0); io_thread_->StartWithOptions(options); http_server_ = HTTPTestServer::CreateServer( kDocRoot, io_thread_->message_loop()).release(); ASSERT_TRUE(http_server_); - request_context_ = new TestURLRequestContext(); - request_context_->AddRef(); } static base::WaitableEvent* io_thread_shutdown_event_; @@ -377,10 +405,6 @@ class AppCacheUpdateJobTest : public testing::Test, http_server_->Release(); http_server_ = NULL; } - if (request_context_) { - request_context_->Release(); - request_context_ = NULL; - } io_thread_shutdown_event_->Signal(); } @@ -2630,7 +2654,7 @@ class AppCacheUpdateJobTest : public testing::Test, void MakeService() { service_.reset(new MockAppCacheService()); - service_->set_request_context(request_context_); + service_->set_request_context(io_thread_->request_context()); service_->set_appcache_policy(&policy_); } @@ -2915,9 +2939,8 @@ class AppCacheUpdateJobTest : public testing::Test, PENDING_MASTER_NO_UPDATE, }; - static base::Thread* io_thread_; + static IOThread* io_thread_; static HTTPTestServer* http_server_; - static TestURLRequestContext* request_context_; scoped_ptr<MockAppCacheService> service_; scoped_refptr<AppCacheGroup> group_; @@ -2950,9 +2973,8 @@ class AppCacheUpdateJobTest : public testing::Test, }; // static -base::Thread* AppCacheUpdateJobTest::io_thread_ = NULL; +IOThread* AppCacheUpdateJobTest::io_thread_ = NULL; HTTPTestServer* AppCacheUpdateJobTest::http_server_ = NULL; -TestURLRequestContext* AppCacheUpdateJobTest::request_context_ = NULL; base::WaitableEvent* AppCacheUpdateJobTest::io_thread_shutdown_event_ = NULL; diff --git a/webkit/support/test_webkit_client.cc b/webkit/support/test_webkit_client.cc index 21648f0..389a188 100644 --- a/webkit/support/test_webkit_client.cc +++ b/webkit/support/test_webkit_client.cc @@ -116,8 +116,7 @@ TestWebKitClient::TestWebKitClient() : url_loader_factory_(NULL) { // Initializing with a default context, which means no on-disk cookie DB, // and no support for directory listings. - SimpleResourceLoaderBridge::Init( - new TestShellRequestContext(FilePath(), cache_mode, true)); + SimpleResourceLoaderBridge::Init(FilePath(), cache_mode, true); // Test shell always exposes the GC. webkit_glue::SetJavaScriptFlags(L" --expose-gc"); diff --git a/webkit/tools/test_shell/node_leak_test.cc b/webkit/tools/test_shell/node_leak_test.cc index 40d19ff..6d303f1 100644 --- a/webkit/tools/test_shell/node_leak_test.cc +++ b/webkit/tools/test_shell/node_leak_test.cc @@ -52,8 +52,7 @@ class NodeLeakTest : public TestShellTest { net::HttpCache::Mode mode = parsed_command_line.HasSwitch(test_shell::kPlaybackMode) ? net::HttpCache::PLAYBACK : net::HttpCache::NORMAL; - SimpleResourceLoaderBridge::Init( - new TestShellRequestContext(cache_path, mode, false)); + SimpleResourceLoaderBridge::Init(cache_path, mode, false); TestShellTest::SetUp(); } diff --git a/webkit/tools/test_shell/simple_resource_loader_bridge.cc b/webkit/tools/test_shell/simple_resource_loader_bridge.cc index 690f169..ffa0d81 100644 --- a/webkit/tools/test_shell/simple_resource_loader_bridge.cc +++ b/webkit/tools/test_shell/simple_resource_loader_bridge.cc @@ -32,7 +32,11 @@ #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" +#include "base/file_path.h" #include "base/message_loop.h" +#if defined(OS_WIN) +#include "base/nss_util.h" +#endif #include "base/ref_counted.h" #include "base/time.h" #include "base/timer.h" @@ -44,9 +48,13 @@ #include "net/base/net_util.h" #include "net/base/static_cookie_policy.h" #include "net/base/upload_data.h" +#include "net/http/http_cache.h" #include "net/http/http_request_headers.h" #include "net/http/http_response_headers.h" #include "net/proxy/proxy_service.h" +#if defined(OS_WIN) +#include "net/socket/ssl_client_socket_nss_factory.h" +#endif #include "net/url_request/url_request.h" #include "webkit/appcache/appcache_interfaces.h" #include "webkit/glue/resource_loader_bridge.h" @@ -60,10 +68,26 @@ using net::HttpResponseHeaders; namespace { -//----------------------------------------------------------------------------- +struct TestShellRequestContextParams { + TestShellRequestContextParams( + const FilePath& in_cache_path, + net::HttpCache::Mode in_cache_mode, + bool in_no_proxy) + : cache_path(in_cache_path), + cache_mode(in_cache_mode), + no_proxy(in_no_proxy), + accept_all_cookies(false) {} + + FilePath cache_path; + net::HttpCache::Mode cache_mode; + bool no_proxy; + bool accept_all_cookies; +}; -URLRequestContext* request_context = NULL; -base::Thread* io_thread = NULL; +TestShellRequestContextParams* g_request_context_params = NULL; +URLRequestContext* g_request_context = NULL; + +//----------------------------------------------------------------------------- class IOThread : public base::Thread { public: @@ -77,19 +101,44 @@ class IOThread : public base::Thread { } virtual void Init() { - SimpleAppCacheSystem::InitializeOnIOThread(request_context); - SimpleSocketStreamBridge::InitializeOnIOThread(request_context); + if (g_request_context_params) { + g_request_context = new TestShellRequestContext( + g_request_context_params->cache_path, + g_request_context_params->cache_mode, + g_request_context_params->no_proxy); + SetAcceptAllCookies(g_request_context_params->accept_all_cookies); + delete g_request_context_params; + g_request_context_params = NULL; + } else { + g_request_context = new TestShellRequestContext(); + SetAcceptAllCookies(false); + } + + g_request_context->AddRef(); + + SimpleAppCacheSystem::InitializeOnIOThread(g_request_context); + SimpleSocketStreamBridge::InitializeOnIOThread(g_request_context); } virtual void CleanUp() { SimpleSocketStreamBridge::Cleanup(); - if (request_context) { - request_context->Release(); - request_context = NULL; + if (g_request_context) { + g_request_context->Release(); + g_request_context = NULL; } } + + void SetAcceptAllCookies(bool accept_all_cookies) { + StaticCookiePolicy::Type policy_type = accept_all_cookies ? + StaticCookiePolicy::ALLOW_ALL_COOKIES : + StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES; + static_cast<StaticCookiePolicy*>(g_request_context->cookie_policy())-> + set_type(policy_type); + } }; +IOThread* g_io_thread = NULL; + //----------------------------------------------------------------------------- struct RequestParams { @@ -128,13 +177,13 @@ class RequestProxy : public URLRequest::Delegate, owner_loop_ = MessageLoop::current(); // proxy over to the io thread - io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( this, &RequestProxy::AsyncStart, params)); } void Cancel() { // proxy over to the io thread - io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( this, &RequestProxy::AsyncCancel)); } @@ -144,7 +193,7 @@ class RequestProxy : public URLRequest::Delegate, virtual ~RequestProxy() { // If we have a request, then we'd better be on the io thread! DCHECK(!request_.get() || - MessageLoop::current() == io_thread->message_loop()); + MessageLoop::current() == g_io_thread->message_loop()); } // -------------------------------------------------------------------------- @@ -159,7 +208,7 @@ class RequestProxy : public URLRequest::Delegate, if (peer_ && peer_->OnReceivedRedirect(new_url, info, &has_new_first_party_for_cookies, &new_first_party_for_cookies)) { - io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( this, &RequestProxy::AsyncFollowDeferredRedirect, has_new_first_party_for_cookies, new_first_party_for_cookies)); } else { @@ -188,7 +237,7 @@ class RequestProxy : public URLRequest::Delegate, // peer could generate new requests in reponse to the received data, which // when run on the io thread, could race against this function in doing // another InvokeLater. See bug 769249. - io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( this, &RequestProxy::AsyncReadData)); peer_->OnReceivedData(buf_copy.get(), bytes_read); @@ -221,7 +270,7 @@ class RequestProxy : public URLRequest::Delegate, request_->SetExtraRequestHeaders(headers); request_->set_load_flags(params->load_flags); request_->set_upload(params->upload.get()); - request_->set_context(request_context); + request_->set_context(g_request_context); SimpleAppCacheSystem::SetExtraRequestInfo( request_.get(), params->appcache_host_id, params->request_type); @@ -504,7 +553,7 @@ class ResourceLoaderBridgeImpl : public ResourceLoaderBridge { if (proxy_) { proxy_->DropPeer(); // Let the proxy die on the IO thread - io_thread->message_loop()->ReleaseSoon(FROM_HERE, proxy_); + g_io_thread->message_loop()->ReleaseSoon(FROM_HERE, proxy_); } } @@ -591,8 +640,8 @@ class ResourceLoaderBridgeImpl : public ResourceLoaderBridge { class CookieSetter : public base::RefCountedThreadSafe<CookieSetter> { public: void Set(const GURL& url, const std::string& cookie) { - DCHECK(MessageLoop::current() == io_thread->message_loop()); - request_context->cookie_store()->SetCookie(url, cookie); + DCHECK(MessageLoop::current() == g_io_thread->message_loop()); + g_request_context->cookie_store()->SetCookie(url, cookie); } private: @@ -607,7 +656,7 @@ class CookieGetter : public base::RefCountedThreadSafe<CookieGetter> { } void Get(const GURL& url) { - result_ = request_context->cookie_store()->GetCookies(url); + result_ = g_request_context->cookie_store()->GetCookies(url); event_.Signal(); } @@ -641,11 +690,11 @@ ResourceLoaderBridge* ResourceLoaderBridge::Create( // Issue the proxy resolve request on the io thread, and wait // for the result. bool FindProxyForUrl(const GURL& url, std::string* proxy_list) { - DCHECK(request_context); + DCHECK(g_request_context); scoped_refptr<net::SyncProxyServiceHelper> sync_proxy_service( - new net::SyncProxyServiceHelper(io_thread->message_loop(), - request_context->proxy_service())); + new net::SyncProxyServiceHelper(g_io_thread->message_loop(), + g_request_context->proxy_service())); net::ProxyInfo proxy_info; int rv = sync_proxy_service->ResolveProxy(url, &proxy_info, @@ -662,27 +711,32 @@ bool FindProxyForUrl(const GURL& url, std::string* proxy_list) { //----------------------------------------------------------------------------- // static -void SimpleResourceLoaderBridge::Init(TestShellRequestContext* context) { +void SimpleResourceLoaderBridge::Init( + const FilePath& cache_path, + net::HttpCache::Mode cache_mode, + bool no_proxy) { // Make sure to stop any existing IO thread since it may be using the // current request context. Shutdown(); - if (context) { - request_context = context; - } else { - request_context = new TestShellRequestContext(); - } - request_context->AddRef(); - SimpleResourceLoaderBridge::SetAcceptAllCookies(false); + DCHECK(!g_request_context_params); + DCHECK(!g_request_context); + DCHECK(!g_io_thread); + + g_request_context_params = new TestShellRequestContextParams( + cache_path, cache_mode, no_proxy); } // static void SimpleResourceLoaderBridge::Shutdown() { - if (io_thread) { - delete io_thread; - io_thread = NULL; + if (g_io_thread) { + delete g_io_thread; + g_io_thread = NULL; - DCHECK(!request_context) << "should have been nulled by thread dtor"; + DCHECK(!g_request_context) << "should have been nulled by thread dtor"; + } else { + delete g_request_context_params; + g_request_context_params = NULL; } } @@ -698,7 +752,7 @@ void SimpleResourceLoaderBridge::SetCookie(const GURL& url, } scoped_refptr<CookieSetter> cookie_setter = new CookieSetter(); - io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( cookie_setter.get(), &CookieSetter::Set, url, cookie)); } @@ -714,7 +768,7 @@ std::string SimpleResourceLoaderBridge::GetCookies( scoped_refptr<CookieGetter> getter = new CookieGetter(); - io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( + g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( getter.get(), &CookieGetter::Get, url)); return getter->GetResult(); @@ -722,23 +776,31 @@ std::string SimpleResourceLoaderBridge::GetCookies( // static bool SimpleResourceLoaderBridge::EnsureIOThread() { - if (io_thread) + if (g_io_thread) return true; - if (!request_context) - SimpleResourceLoaderBridge::Init(NULL); +#if defined(OS_WIN) + // Use NSS for SSL on Windows. TODO(wtc): this should eventually be hidden + // inside DefaultClientSocketFactory::CreateSSLClientSocket. + net::ClientSocketFactory::SetSSLClientSocketFactory( + net::SSLClientSocketNSSFactory); + // We want to be sure to init NSPR on the main thread. + base::EnsureNSPRInit(); +#endif - io_thread = new IOThread(); + g_io_thread = new IOThread(); base::Thread::Options options; options.message_loop_type = MessageLoop::TYPE_IO; - return io_thread->StartWithOptions(options); + return g_io_thread->StartWithOptions(options); } // static void SimpleResourceLoaderBridge::SetAcceptAllCookies(bool accept_all_cookies) { - StaticCookiePolicy::Type policy_type = accept_all_cookies ? - StaticCookiePolicy::ALLOW_ALL_COOKIES : - StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES; - static_cast<StaticCookiePolicy*>(request_context->cookie_policy())-> - set_type(policy_type); + if (g_request_context_params) { + g_request_context_params->accept_all_cookies = accept_all_cookies; + DCHECK(!g_request_context); + DCHECK(!g_io_thread); + } else { + g_io_thread->SetAcceptAllCookies(accept_all_cookies); + } } diff --git a/webkit/tools/test_shell/simple_resource_loader_bridge.h b/webkit/tools/test_shell/simple_resource_loader_bridge.h index 6d1d822..1bcfe07 100644 --- a/webkit/tools/test_shell/simple_resource_loader_bridge.h +++ b/webkit/tools/test_shell/simple_resource_loader_bridge.h @@ -6,23 +6,23 @@ #define WEBKIT_TOOLS_TEST_SHELL_SIMPLE_RESOURCE_LOADER_BRIDGE_H__ #include <string> +#include "base/file_path.h" +#include "net/http/http_cache.h" class GURL; class TestShellRequestContext; class SimpleResourceLoaderBridge { public: - // Call this function to initialize the simple resource loader bridge. If - // the given context is null, then a default TestShellRequestContext will be - // instantiated. Otherwise, a reference is taken to the given request - // context, which will be released when Shutdown is called. The caller - // should not hold another reference to the request context! It is safe to - // call this function multiple times. + // Call this function to initialize the simple resource loader bridge. + // It is safe to call this function multiple times. // // NOTE: If this function is not called, then a default request context will // be initialized lazily. // - static void Init(TestShellRequestContext* context); + static void Init(const FilePath& cache_path, + net::HttpCache::Mode cache_mode, + bool no_proxy); // Call this function to shutdown the simple resource loader bridge. static void Shutdown(); diff --git a/webkit/tools/test_shell/test_shell_main.cc b/webkit/tools/test_shell/test_shell_main.cc index bcd4dfd..dc46850 100644 --- a/webkit/tools/test_shell/test_shell_main.cc +++ b/webkit/tools/test_shell/test_shell_main.cc @@ -164,8 +164,7 @@ int main(int argc, char* argv[]) { // Initializing with a default context, which means no on-disk cookie DB, // and no support for directory listings. - SimpleResourceLoaderBridge::Init( - new TestShellRequestContext(cache_path, cache_mode, layout_test_mode)); + SimpleResourceLoaderBridge::Init(cache_path, cache_mode, layout_test_mode); // Load ICU data tables icu_util::Initialize(); diff --git a/webkit/tools/test_shell/test_shell_request_context.cc b/webkit/tools/test_shell/test_shell_request_context.cc index 4c98f28..d76c6c5 100644 --- a/webkit/tools/test_shell/test_shell_request_context.cc +++ b/webkit/tools/test_shell/test_shell_request_context.cc @@ -7,9 +7,6 @@ #include "build/build_config.h" #include "base/file_path.h" -#if defined(OS_WIN) -#include "base/nss_util.h" -#endif #include "net/base/cookie_monster.h" #include "net/base/host_resolver.h" #include "net/base/ssl_config_service.h" @@ -19,9 +16,6 @@ #include "net/proxy/proxy_config_service.h" #include "net/proxy/proxy_config_service_fixed.h" #include "net/proxy/proxy_service.h" -#if defined(OS_WIN) -#include "net/socket/ssl_client_socket_nss_factory.h" -#endif #include "webkit/glue/webkit_glue.h" TestShellRequestContext::TestShellRequestContext() : cache_thread_("cache") { @@ -69,15 +63,6 @@ void TestShellRequestContext::Init( http_auth_handler_factory_ = net::HttpAuthHandlerFactory::CreateDefault(); -#if defined(OS_WIN) - // Use NSS for SSL on Windows. TODO(wtc): this should eventually be hidden - // inside DefaultClientSocketFactory::CreateSSLClientSocket. - net::ClientSocketFactory::SetSSLClientSocketFactory( - net::SSLClientSocketNSSFactory); - // We want to be sure to init NSPR on the main thread. - base::EnsureNSPRInit(); -#endif - if (!cache_path.empty()) CHECK(cache_thread_.StartWithOptions( base::Thread::Options(MessageLoop::TYPE_IO, 0))); |