Index: components/cronet/android/test/quic_test_server.cc |
diff --git a/components/cronet/android/test/quic_test_server.cc b/components/cronet/android/test/quic_test_server.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c5533f2fcfb19795ba7d9e1fd5170bbf5b7e8a9e |
--- /dev/null |
+++ b/components/cronet/android/test/quic_test_server.cc |
@@ -0,0 +1,75 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "quic_test_server.h" |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "base/bind.h" |
+#include "base/files/file_path.h" |
+#include "base/threading/thread.h" |
+#include "jni/QuicTestServer_jni.h" |
+#include "net/base/ip_endpoint.h" |
+#include "net/base/net_util.h" |
+#include "net/tools/quic/quic_in_memory_cache.h" |
+#include "net/tools/quic/quic_simple_server.h" |
+ |
+namespace cronet { |
+ |
+namespace { |
+ |
+base::Thread* g_quic_server_thread = nullptr; |
+net::tools::QuicSimpleServer* g_quic_server = nullptr; |
+ |
+void ServeFilesFromDirectory(const base::FilePath& directory) { |
+ DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
+ DCHECK(!g_quic_server); |
+ base::FilePath file_dir = directory.Append("quic_data"); |
+ net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( |
+ file_dir.value()); |
+ net::IPAddressNumber ip; |
+ DCHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip)); |
+ net::QuicConfig config; |
+ g_quic_server = |
+ new net::tools::QuicSimpleServer(config, net::QuicSupportedVersions()); |
+ DCHECK(g_quic_server->Listen(net::IPEndPoint(ip, 6121)) >= 0) << |
+ "Quic server fails to start"; |
+} |
+ |
+void ShutdownOnServerThread() { |
+ DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
+ g_quic_server->Shutdown(); |
+ delete g_quic_server; |
+} |
+ |
+} // namespace |
+ |
+// Quic server is currently hardcoded to run on port 6121 of the localhost on |
+// the device. |
+void StartQuicTestServer(JNIEnv* env, |
+ jclass jcaller, |
+ jstring jtest_files_root) { |
+ DCHECK(!g_quic_server_thread); |
+ g_quic_server_thread = new base::Thread("quic server thread"); |
+ base::Thread::Options thread_options; |
+ thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
+ DCHECK(g_quic_server_thread->StartWithOptions(thread_options)); |
+ base::FilePath test_files_root( |
+ base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
+ g_quic_server_thread->task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&ServeFilesFromDirectory, test_files_root)); |
+} |
+ |
+void ShutdownQuicTestServer(JNIEnv* env, jclass jcaller) { |
+ DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
+ g_quic_server_thread->task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&ShutdownOnServerThread)); |
+ delete g_quic_server_thread; |
+} |
+ |
+bool RegisterQuicTestServer(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+} // namespace cronet |