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