864159750a8d2c700af63ff7692633dac7e50453
[platform/framework/web/crosswalk.git] / src / net / socket / tcp_socket.cc
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.
4
5 #include "net/socket/tcp_socket.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/location.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/worker_pool.h"
12
13 namespace net {
14
15 namespace {
16
17 bool g_tcp_fastopen_enabled = false;
18
19 #if defined(OS_LINUX) || defined(OS_ANDROID)
20
21 typedef base::RefCountedData<bool> SharedBoolean;
22
23 // Checks to see if the system supports TCP FastOpen. Notably, it requires
24 // kernel support. Additionally, this checks system configuration to ensure that
25 // it's enabled.
26 void SystemSupportsTCPFastOpen(scoped_refptr<SharedBoolean> supported) {
27   supported->data = false;
28   static const base::FilePath::CharType kTCPFastOpenProcFilePath[] =
29       "/proc/sys/net/ipv4/tcp_fastopen";
30   std::string system_enabled_tcp_fastopen;
31   if (!base::ReadFileToString(base::FilePath(kTCPFastOpenProcFilePath),
32                               &system_enabled_tcp_fastopen)) {
33     return;
34   }
35
36   // As per http://lxr.linux.no/linux+v3.7.7/include/net/tcp.h#L225
37   // TFO_CLIENT_ENABLE is the LSB
38   if (system_enabled_tcp_fastopen.empty() ||
39       (system_enabled_tcp_fastopen[0] & 0x1) == 0) {
40     return;
41   }
42
43   supported->data = true;
44 }
45
46 void EnableCallback(scoped_refptr<SharedBoolean> supported) {
47   g_tcp_fastopen_enabled = supported->data;
48 }
49
50 // This is asynchronous because it needs to do file IO, and it isn't allowed to
51 // do that on the IO thread.
52 void EnableFastOpenIfSupported() {
53   scoped_refptr<SharedBoolean> supported = new SharedBoolean;
54   base::WorkerPool::PostTaskAndReply(
55       FROM_HERE,
56       base::Bind(SystemSupportsTCPFastOpen, supported),
57       base::Bind(EnableCallback, supported),
58       false);
59 }
60
61 #else
62
63 void EnableFastOpenIfSupported() {
64   g_tcp_fastopen_enabled = false;
65 }
66
67 #endif
68
69 }  // namespace
70
71 void SetTCPFastOpenEnabled(bool value) {
72   if (value) {
73     EnableFastOpenIfSupported();
74   } else {
75     g_tcp_fastopen_enabled = false;
76   }
77 }
78
79 bool IsTCPFastOpenEnabled() {
80   return g_tcp_fastopen_enabled;
81 }
82
83 }  // namespace net