| OLD | NEW | 
|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "quic_test_server.h" | 5 #include "quic_test_server.h" | 
| 6 | 6 | 
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" | 
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" | 
|  | 9 #include "base/android/path_utils.h" | 
| 9 #include "base/bind.h" | 10 #include "base/bind.h" | 
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" | 
| 11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" | 
| 12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" | 
| 13 #include "jni/QuicTestServer_jni.h" | 14 #include "jni/QuicTestServer_jni.h" | 
| 14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" | 
| 15 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" | 
|  | 17 #include "net/base/test_data_directory.h" | 
|  | 18 #include "net/quic/crypto/proof_source_chromium.h" | 
| 16 #include "net/tools/quic/quic_in_memory_cache.h" | 19 #include "net/tools/quic/quic_in_memory_cache.h" | 
| 17 #include "net/tools/quic/quic_simple_server.h" | 20 #include "net/tools/quic/quic_simple_server.h" | 
| 18 | 21 | 
| 19 namespace cronet { | 22 namespace cronet { | 
| 20 | 23 | 
| 21 namespace { | 24 namespace { | 
| 22 | 25 | 
| 23 static const char kServerHost[] = "127.0.0.1"; | 26 static const char kServerHost[] = "test.example.com"; | 
| 24 static const int kServerPort = 6121; | 27 static const int kServerPort = 6121; | 
| 25 | 28 | 
| 26 base::Thread* g_quic_server_thread = nullptr; | 29 base::Thread* g_quic_server_thread = nullptr; | 
| 27 net::tools::QuicSimpleServer* g_quic_server = nullptr; | 30 net::tools::QuicSimpleServer* g_quic_server = nullptr; | 
| 28 | 31 | 
| 29 void ServeFilesFromDirectory( | 32 void StartOnServerThread(const base::FilePath& test_files_root) { | 
| 30     const base::FilePath& directory) { |  | 
| 31   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 33   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 
| 32   DCHECK(!g_quic_server); | 34   DCHECK(!g_quic_server); | 
| 33   base::FilePath file_dir = directory.Append("quic_data"); | 35 | 
|  | 36   // Set up in-memory cache. | 
|  | 37   base::FilePath file_dir = test_files_root.Append("quic_data"); | 
| 34   CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; | 38   CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; | 
| 35   net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( | 39   net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( | 
| 36       file_dir.value()); | 40       file_dir.value()); | 
| 37   net::IPAddressNumber ip; | 41   net::IPAddressNumber ip; | 
| 38   net::ParseIPLiteralToNumber(kServerHost, &ip); | 42   net::ParseIPLiteralToNumber(kServerHost, &ip); | 
| 39   net::QuicConfig config; | 43   net::QuicConfig config; | 
| 40   g_quic_server = | 44   g_quic_server = | 
| 41       new net::tools::QuicSimpleServer(config, net::QuicSupportedVersions()); | 45       new net::tools::QuicSimpleServer(config, net::QuicSupportedVersions()); | 
|  | 46 | 
|  | 47   // Set up server certs. | 
|  | 48   base::FilePath directory; | 
|  | 49   CHECK(base::android::GetExternalStorageDirectory(&directory)); | 
|  | 50   directory = directory.Append("net/data/ssl/certificates"); | 
|  | 51   net::ProofSourceChromium* proof_source = new net::ProofSourceChromium(); | 
|  | 52   CHECK(proof_source->Initialize( | 
|  | 53       directory.Append("quic_test.example.com.crt"), | 
|  | 54       directory.Append("quic_test.example.com.key.pkcs8"))); | 
|  | 55   g_quic_server->SetProofSource(proof_source); | 
|  | 56 | 
|  | 57   // Start listening. | 
| 42   int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); | 58   int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); | 
| 43   CHECK_GE(rv, 0) << "Quic server fails to start"; | 59   CHECK_GE(rv, 0) << "Quic server fails to start"; | 
| 44   JNIEnv* env = base::android::AttachCurrentThread(); | 60   JNIEnv* env = base::android::AttachCurrentThread(); | 
| 45   Java_QuicTestServer_onServerStarted(env); | 61   Java_QuicTestServer_onServerStarted(env); | 
| 46 } | 62 } | 
| 47 | 63 | 
| 48 void ShutdownOnServerThread() { | 64 void ShutdownOnServerThread() { | 
| 49   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 65   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 
| 50   g_quic_server->Shutdown(); | 66   g_quic_server->Shutdown(); | 
| 51   delete g_quic_server; | 67   delete g_quic_server; | 
| 52 } | 68 } | 
| 53 | 69 | 
| 54 }  // namespace | 70 }  // namespace | 
| 55 | 71 | 
| 56 // Quic server is currently hardcoded to run on port 6121 of the localhost on | 72 // Quic server is currently hardcoded to run on port 6121 of the localhost on | 
| 57 // the device. | 73 // the device. | 
| 58 void StartQuicTestServer(JNIEnv* env, | 74 void StartQuicTestServer(JNIEnv* env, | 
| 59                          const JavaParamRef<jclass>& /*jcaller*/, | 75                          const JavaParamRef<jclass>& /*jcaller*/, | 
| 60                          const JavaParamRef<jstring>& jtest_files_root) { | 76                          const JavaParamRef<jstring>& jtest_files_root) { | 
| 61   DCHECK(!g_quic_server_thread); | 77   DCHECK(!g_quic_server_thread); | 
| 62   g_quic_server_thread = new base::Thread("quic server thread"); | 78   g_quic_server_thread = new base::Thread("quic server thread"); | 
| 63   base::Thread::Options thread_options; | 79   base::Thread::Options thread_options; | 
| 64   thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 80   thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 
| 65   bool started = g_quic_server_thread->StartWithOptions(thread_options); | 81   bool started = g_quic_server_thread->StartWithOptions(thread_options); | 
| 66   DCHECK(started); | 82   DCHECK(started); | 
| 67   base::FilePath test_files_root( | 83   base::FilePath test_files_root( | 
| 68       base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 84       base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 
| 69   g_quic_server_thread->task_runner()->PostTask( | 85   g_quic_server_thread->task_runner()->PostTask( | 
| 70       FROM_HERE, base::Bind(&ServeFilesFromDirectory, test_files_root)); | 86       FROM_HERE, base::Bind(&StartOnServerThread, test_files_root)); | 
| 71 } | 87 } | 
| 72 | 88 | 
| 73 void ShutdownQuicTestServer(JNIEnv* env, | 89 void ShutdownQuicTestServer(JNIEnv* env, | 
| 74                             const JavaParamRef<jclass>& /*jcaller*/) { | 90                             const JavaParamRef<jclass>& /*jcaller*/) { | 
| 75   DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 91   DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 
| 76   g_quic_server_thread->task_runner()->PostTask( | 92   g_quic_server_thread->task_runner()->PostTask( | 
| 77       FROM_HERE, base::Bind(&ShutdownOnServerThread)); | 93       FROM_HERE, base::Bind(&ShutdownOnServerThread)); | 
| 78   delete g_quic_server_thread; | 94   delete g_quic_server_thread; | 
| 79 } | 95 } | 
| 80 | 96 | 
| 81 ScopedJavaLocalRef<jstring> GetServerHost( | 97 ScopedJavaLocalRef<jstring> GetServerHost( | 
| 82     JNIEnv* env, | 98     JNIEnv* env, | 
| 83     const JavaParamRef<jclass>& /*jcaller*/) { | 99     const JavaParamRef<jclass>& /*jcaller*/) { | 
| 84   return base::android::ConvertUTF8ToJavaString(env, kServerHost); | 100   return base::android::ConvertUTF8ToJavaString(env, kServerHost); | 
| 85 } | 101 } | 
| 86 | 102 | 
| 87 int GetServerPort(JNIEnv* env, const JavaParamRef<jclass>& /*jcaller*/) { | 103 int GetServerPort(JNIEnv* env, const JavaParamRef<jclass>& /*jcaller*/) { | 
| 88   return kServerPort; | 104   return kServerPort; | 
| 89 } | 105 } | 
| 90 | 106 | 
| 91 bool RegisterQuicTestServer(JNIEnv* env) { | 107 bool RegisterQuicTestServer(JNIEnv* env) { | 
| 92   return RegisterNativesImpl(env); | 108   return RegisterNativesImpl(env); | 
| 93 } | 109 } | 
| 94 | 110 | 
| 95 }  // namespace cronet | 111 }  // namespace cronet | 
| OLD | NEW | 
|---|