diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-06 10:41:35 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-06 10:41:35 +0000 |
commit | 14d5cfbd4ceb886e92033092603f369e184539e0 (patch) | |
tree | a3d275f05a9e361581b964f33f9cbfb15a0fb2db /net | |
parent | 02c8796f86e238be73fecd15b894724d58a96f4d (diff) | |
download | chromium_src-14d5cfbd4ceb886e92033092603f369e184539e0.zip chromium_src-14d5cfbd4ceb886e92033092603f369e184539e0.tar.gz chromium_src-14d5cfbd4ceb886e92033092603f369e184539e0.tar.bz2 |
- Cleanup usage, EnsureWinsockInit is now the only method of initializing.
- Work around buggy third party hooks that can overwrite LastErrorValue during delay load binding.
Review URL: http://codereview.chromium.org/6483
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2877 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/winsock_init.cc | 51 | ||||
-rw-r--r-- | net/base/winsock_init.h | 26 |
2 files changed, 35 insertions, 42 deletions
diff --git a/net/base/winsock_init.cc b/net/base/winsock_init.cc index b7fbeaa..190ed77 100644 --- a/net/base/winsock_init.cc +++ b/net/base/winsock_init.cc @@ -8,30 +8,39 @@ #include "base/singleton.h" -namespace net { - -WinsockInit::WinsockInit() : did_init_(false) { - did_init_ = Init(); -} - -bool WinsockInit::Init() { - WORD winsock_ver = MAKEWORD(2,2); - WSAData wsa_data; - return (WSAStartup(winsock_ver, &wsa_data) == 0); -} +namespace { + +class WinsockInitSingleton { + public: + WinsockInitSingleton() : did_init_(false) { + WORD winsock_ver = MAKEWORD(2,2); + WSAData wsa_data; + did_init_ = (WSAStartup(winsock_ver, &wsa_data) == 0); + + // The first time WSAGetLastError is called, the delay load helper will + // resolve the address with GetProcAddress and fixup the import. If a third + // party application hooks system functions without correctly restoring the + // error code, it is possible that the error code will be overwritten during + // delay load resolution. The result of the first call may be incorrect, so + // make sure the function is bound and future results will be correct. + WSAGetLastError(); + } + + ~WinsockInitSingleton() { + if (did_init_) + WSACleanup(); + } + + private: + bool did_init_; +}; + +} // namespace -void WinsockInit::Cleanup() { - WSACleanup(); -} - -WinsockInit::~WinsockInit() { - if (did_init_) - Cleanup(); -} +namespace net { void EnsureWinsockInit() { - Singleton<WinsockInit>::get(); + Singleton<WinsockInitSingleton>::get(); } } // namespace net - diff --git a/net/base/winsock_init.h b/net/base/winsock_init.h index 9ce3be6..ab34fb0 100644 --- a/net/base/winsock_init.h +++ b/net/base/winsock_init.h @@ -2,35 +2,19 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Winsock initialization must happen before any Winsock calls are made. This -// class provides a wrapper for WSAStartup and WSACleanup. There are 3 ways to -// use it: either allocate a new WinsockInit object at startup and delete when -// shutting down, manually call Init and Cleanup, or use the EnsureWinsockInit -// method, which may be called multiple times. In the second case, Cleanup -// should only be called if Init was successful. +// Winsock initialization must happen before any Winsock calls are made. The +// EnsureWinsockInit method will make sure that WSAStartup has been called. If +// the call to WSAStartup caused Winsock to initialize, WSACleanup will be +// called automatically on program shutdown. #ifndef NET_BASE_WINSOCK_INIT_H_ #define NET_BASE_WINSOCK_INIT_H_ namespace net { -class WinsockInit { - public: - WinsockInit(); - ~WinsockInit(); - - static bool Init(); - static void Cleanup(); - - private: - bool did_init_; -}; - -// Force there to be a global WinsockInit object that gets created once and -// destroyed at application exit. This may be called multiple times. +// Make sure that Winsock is initialized, calling WSAStartup if needed. void EnsureWinsockInit(); } // namespace net #endif // NET_BASE_WINSOCK_INIT_H_ - |