| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/cronet/android/test/cronet_test_util.h" | 5 #include "components/cronet/android/test/cronet_test_util.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 "components/cronet/android/cronet_url_request_adapter.h" | 9 #include "components/cronet/android/cronet_url_request_adapter.h" |
| 10 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
| 11 #include "components/cronet/android/test/native_test_server.h" | 10 #include "components/cronet/android/test/native_test_server.h" |
| 12 #include "components/cronet/android/url_request_context_adapter.h" | |
| 13 #include "jni/CronetTestUtil_jni.h" | 11 #include "jni/CronetTestUtil_jni.h" |
| 14 #include "net/dns/host_resolver_impl.h" | |
| 15 #include "net/dns/mock_host_resolver.h" | |
| 16 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
| 17 | 13 |
| 18 using base::android::JavaParamRef; | 14 using base::android::JavaParamRef; |
| 19 | 15 |
| 20 namespace cronet { | 16 namespace cronet { |
| 21 | 17 |
| 22 const char kFakeSdchDomain[] = "fake.sdch.domain"; | |
| 23 // This must match the certificate used | |
| 24 // (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and | |
| 25 // the file served ( | |
| 26 // components/cronet/android/test/assets/test/quic_data/simple.txt). | |
| 27 const char kFakeQuicDomain[] = "test.example.com"; | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // Install host resolver rules to map fake domains to |destination|, usually an | |
| 32 // IP address. | |
| 33 void RegisterHostResolverProcHelper(net::URLRequestContext* url_request_context, | |
| 34 const std::string& destination) { | |
| 35 net::HostResolverImpl* resolver = | |
| 36 static_cast<net::HostResolverImpl*>(url_request_context->host_resolver()); | |
| 37 scoped_refptr<net::RuleBasedHostResolverProc> proc = | |
| 38 new net::RuleBasedHostResolverProc(NULL); | |
| 39 proc->AddRule(kFakeSdchDomain, destination); | |
| 40 proc->AddRule(kFakeQuicDomain, destination); | |
| 41 resolver->set_proc_params_for_test( | |
| 42 net::HostResolverImpl::ProcTaskParams(proc.get(), 1u)); | |
| 43 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 44 Java_CronetTestUtil_onHostResolverProcRegistered(env); | |
| 45 } | |
| 46 | |
| 47 void RegisterHostResolverProcOnNetworkThread( | |
| 48 CronetURLRequestContextAdapter* context_adapter, | |
| 49 const std::string& destination) { | |
| 50 RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext(), | |
| 51 destination); | |
| 52 } | |
| 53 | |
| 54 // TODO(xunjieli): Delete this once legacy API is removed. | |
| 55 void RegisterHostResolverProcOnNetworkThreadLegacyAPI( | |
| 56 URLRequestContextAdapter* context_adapter, | |
| 57 const std::string& destination) { | |
| 58 RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext(), | |
| 59 destination); | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 void RegisterHostResolverProc(JNIEnv* env, | |
| 65 const JavaParamRef<jclass>& jcaller, | |
| 66 jlong jadapter, | |
| 67 jboolean jlegacy_api, | |
| 68 const JavaParamRef<jstring>& jdestination) { | |
| 69 std::string destination( | |
| 70 base::android::ConvertJavaStringToUTF8(env, jdestination)); | |
| 71 if (jlegacy_api == JNI_TRUE) { | |
| 72 URLRequestContextAdapter* context_adapter = | |
| 73 reinterpret_cast<URLRequestContextAdapter*>(jadapter); | |
| 74 context_adapter->PostTaskToNetworkThread( | |
| 75 FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI, | |
| 76 base::Unretained(context_adapter), destination)); | |
| 77 } else { | |
| 78 CronetURLRequestContextAdapter* context_adapter = | |
| 79 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | |
| 80 context_adapter->PostTaskToNetworkThread( | |
| 81 FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThread, | |
| 82 base::Unretained(context_adapter), destination)); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 jint GetLoadFlags(JNIEnv* env, | 18 jint GetLoadFlags(JNIEnv* env, |
| 87 const JavaParamRef<jclass>& jcaller, | 19 const JavaParamRef<jclass>& jcaller, |
| 88 const jlong urlRequest) { | 20 const jlong urlRequest) { |
| 89 return reinterpret_cast<CronetURLRequestAdapter*>(urlRequest) | 21 return reinterpret_cast<CronetURLRequestAdapter*>(urlRequest) |
| 90 ->GetURLRequestForTesting() | 22 ->GetURLRequestForTesting() |
| 91 ->load_flags(); | 23 ->load_flags(); |
| 92 } | 24 } |
| 93 | 25 |
| 94 bool RegisterCronetTestUtil(JNIEnv* env) { | 26 bool RegisterCronetTestUtil(JNIEnv* env) { |
| 95 return RegisterNativesImpl(env); | 27 return RegisterNativesImpl(env); |
| 96 } | 28 } |
| 97 | 29 |
| 98 } // namespace cronet | 30 } // namespace cronet |
| OLD | NEW |