| 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 namespace { |
| 19 |
| 20 // Install host resolver rules to map fake domains to |destination|, usually an |
| 21 // IP address. |
| 22 void RegisterHostResolverProcHelper(net::URLRequestContext* url_request_context, |
| 23 const std::string& destination) { |
| 24 net::HostResolverImpl* resolver = |
| 25 static_cast<net::HostResolverImpl*>(url_request_context->host_resolver()); |
| 26 scoped_refptr<net::RuleBasedHostResolverProc> proc = |
| 27 new net::RuleBasedHostResolverProc(NULL); |
| 28 proc->AddRule(kFakeSdchDomain, destination); |
| 29 proc->AddRule(kFakeQuicDomain, destination); |
| 30 resolver->set_proc_params_for_test( |
| 31 net::HostResolverImpl::ProcTaskParams(proc.get(), 1u)); |
| 32 JNIEnv* env = base::android::AttachCurrentThread(); |
| 33 Java_CronetTestUtil_onHostResolverProcRegistered(env); |
| 34 } |
| 35 |
| 36 void RegisterHostResolverProcOnNetworkThread( |
| 37 CronetURLRequestContextAdapter* context_adapter, |
| 38 const std::string& destination) { |
| 39 RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext(), |
| 40 destination); |
| 41 } |
| 42 |
| 43 // TODO(xunjieli): Delete this once legacy API is removed. |
| 44 void RegisterHostResolverProcOnNetworkThreadLegacyAPI( |
| 45 URLRequestContextAdapter* context_adapter, |
| 46 const std::string& destination) { |
| 47 RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext(), |
| 48 destination); |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 53 void RegisterHostResolverProc(JNIEnv* env, |
| 54 const JavaParamRef<jclass>& jcaller, |
| 55 jlong jadapter, |
| 56 jboolean jlegacy_api, |
| 57 const JavaParamRef<jstring>& jdestination) { |
| 58 std::string destination( |
| 59 base::android::ConvertJavaStringToUTF8(env, jdestination)); |
| 60 if (jlegacy_api == JNI_TRUE) { |
| 61 URLRequestContextAdapter* context_adapter = |
| 62 reinterpret_cast<URLRequestContextAdapter*>(jadapter); |
| 63 context_adapter->PostTaskToNetworkThread( |
| 64 FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI, |
| 65 base::Unretained(context_adapter), destination)); |
| 66 } else { |
| 67 CronetURLRequestContextAdapter* context_adapter = |
| 68 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
| 69 context_adapter->PostTaskToNetworkThread( |
| 70 FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThread, |
| 71 base::Unretained(context_adapter), destination)); |
| 72 } |
| 73 } |
| 74 |
| 75 bool RegisterCronetTestUtil(JNIEnv* env) { |
| 76 return RegisterNativesImpl(env); |
| 77 } |
| 78 |
| 79 } // namespace cronet |
| OLD | NEW |