Patch 3/3 to get WebScheduler via WebThread
[chromium-blink-merge.git] / net / socket / socket_descriptor.cc
blob81787a29d259feb645bfd53978cd33e71fadfd7c
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/socket/socket_descriptor.h"
7 #if defined(OS_POSIX)
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #endif
12 #include "base/basictypes.h"
14 #if defined(OS_WIN)
15 #include <ws2tcpip.h>
16 #include "base/win/windows_version.h"
17 #include "net/base/winsock_init.h"
18 #endif
20 namespace net {
22 PlatformSocketFactory* g_socket_factory = NULL;
24 PlatformSocketFactory::PlatformSocketFactory() {
27 PlatformSocketFactory::~PlatformSocketFactory() {
30 void PlatformSocketFactory::SetInstance(PlatformSocketFactory* factory) {
31 g_socket_factory = factory;
34 SocketDescriptor CreateSocketDefault(int family, int type, int protocol) {
35 #if defined(OS_WIN)
36 EnsureWinsockInit();
37 SocketDescriptor result = ::WSASocket(family, type, protocol, NULL, 0,
38 WSA_FLAG_OVERLAPPED);
39 if (result != kInvalidSocket && family == AF_INET6 &&
40 base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_VISTA) {
41 DWORD value = 0;
42 if (setsockopt(result, IPPROTO_IPV6, IPV6_V6ONLY,
43 reinterpret_cast<const char*>(&value), sizeof(value))) {
44 closesocket(result);
45 return kInvalidSocket;
48 return result;
49 #else // OS_WIN
50 return ::socket(family, type, protocol);
51 #endif // OS_WIN
54 SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) {
55 if (g_socket_factory)
56 return g_socket_factory->CreateSocket(family, type, protocol);
57 else
58 return CreateSocketDefault(family, type, protocol);
61 } // namespace net