Remove rtc::EnsureWinsockInit and g_winsockinit.

In the effort of enabling -Wglobal-constructors and
-Wexit-time-destructors, WebRTC has to remove the Winsock global
initializer.

This will also remove it from Chromium (since it was unused).

After this CL, applications will have to explicitly initialize Winsock
before using WebRTC, this can be done by using the class
rtc::WinsockInitializer provided in rtc_base/win32socketinit.h.

Bug: webrtc:9693, webrtc:9754
Change-Id: I4aae12ff43671ef2713a6fc4592e20759dc6b495
Reviewed-on: https://webrtc-review.googlesource.com/99660
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24903}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index e72e740..8dc98b6 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -871,10 +871,6 @@
   ]
 
   if (build_with_chromium) {
-    if (is_win) {
-      sources += [ "../../webrtc_overrides/rtc_base/win32socketinit.cc" ]
-      deps += [ "//net" ]
-    }
     include_dirs = [ "../../boringssl/src/include" ]
     public_configs += [ ":rtc_base_chromium_config" ]
   } else {
@@ -890,12 +886,7 @@
     ]
 
     if (is_win) {
-      configs += [
-        "..:no_exit_time_destructors",
-        "..:no_global_constructors",
-      ]
       sources += [
-        "win32socketinit.cc",
         "win32socketinit.h",
         "win32socketserver.cc",
         "win32socketserver.h",
diff --git a/rtc_base/physicalsocketserver.cc b/rtc_base/physicalsocketserver.cc
index 9a3fc4d..4ad2857 100644
--- a/rtc_base/physicalsocketserver.cc
+++ b/rtc_base/physicalsocketserver.cc
@@ -50,7 +50,6 @@
 #include "rtc_base/networkmonitor.h"
 #include "rtc_base/nullsocketserver.h"
 #include "rtc_base/timeutils.h"
-#include "rtc_base/win32socketinit.h"
 
 #if defined(WEBRTC_WIN)
 #define LAST_SYSTEM_ERROR (::GetLastError())
@@ -117,14 +116,6 @@
       error_(0),
       state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED),
       resolver_(nullptr) {
-#if defined(WEBRTC_WIN)
-  // EnsureWinsockInit() ensures that winsock is initialized. The default
-  // version of this function doesn't do anything because winsock is
-  // initialized by constructor of a static object. If neccessary libjingle
-  // users can link it with a different version of this function by replacing
-  // win32socketinit.cc. See win32socketinit.cc for more details.
-  EnsureWinsockInit();
-#endif
   if (s_ != INVALID_SOCKET) {
     SetEnabledEvents(DE_READ | DE_WRITE);
 
diff --git a/rtc_base/unittest_main.cc b/rtc_base/unittest_main.cc
index a52f727..8d4ff2d 100644
--- a/rtc_base/unittest_main.cc
+++ b/rtc_base/unittest_main.cc
@@ -24,6 +24,10 @@
 #include "test/field_trial.h"
 #include "test/testsupport/fileutils.h"
 
+#if defined(WEBRTC_WIN)
+#include "rtc_base/win32socketinit.h"
+#endif
+
 #if defined(WEBRTC_IOS)
 #include "test/ios/test_support.h"
 #endif
@@ -85,6 +89,8 @@
   webrtc::metrics::Enable();
 
 #if defined(WEBRTC_WIN)
+  rtc::WinsockInitializer winsock_init;
+
   if (!FLAG_default_error_handlers) {
     // Make sure any errors don't throw dialogs hanging the test run.
     _set_invalid_parameter_handler(TestInvalidParameterHandler);
diff --git a/rtc_base/win32socketinit.cc b/rtc_base/win32socketinit.cc
deleted file mode 100644
index ea0aae6..0000000
--- a/rtc_base/win32socketinit.cc
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *  Copyright 2009 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include "rtc_base/win32socketinit.h"
-
-#include "rtc_base/win32.h"
-
-namespace rtc {
-
-// Please don't remove this function.
-void EnsureWinsockInit() {
-  // The default implementation uses a global initializer, so WSAStartup
-  // happens at module load time.  Thus we don't need to do anything here.
-  // The hook is provided so that a client that statically links with
-  // libjingle can override it, to provide its own initialization.
-}
-
-#if defined(WEBRTC_WIN)
-class WinsockInitializer {
- public:
-  WinsockInitializer() {
-    WSADATA wsaData;
-    WORD wVersionRequested = MAKEWORD(1, 0);
-    err_ = WSAStartup(wVersionRequested, &wsaData);
-  }
-  ~WinsockInitializer() {
-    if (!err_)
-      WSACleanup();
-  }
-  int error() { return err_; }
-
- private:
-  int err_;
-};
-WinsockInitializer g_winsockinit;
-#endif
-
-}  // namespace rtc
diff --git a/rtc_base/win32socketinit.h b/rtc_base/win32socketinit.h
index ea74809..7f9bdcc 100644
--- a/rtc_base/win32socketinit.h
+++ b/rtc_base/win32socketinit.h
@@ -11,9 +11,30 @@
 #ifndef RTC_BASE_WIN32SOCKETINIT_H_
 #define RTC_BASE_WIN32SOCKETINIT_H_
 
+#ifndef WEBRTC_WIN
+#error "Only #include this header in Windows builds"
+#endif
+
+#include "rtc_base/win32.h"
+
 namespace rtc {
 
-void EnsureWinsockInit();
+class WinsockInitializer {
+ public:
+  WinsockInitializer() {
+    WSADATA wsaData;
+    WORD wVersionRequested = MAKEWORD(1, 0);
+    err_ = WSAStartup(wVersionRequested, &wsaData);
+  }
+  ~WinsockInitializer() {
+    if (!err_)
+      WSACleanup();
+  }
+  int error() { return err_; }
+
+ private:
+  int err_;
+};
 
 }  // namespace rtc