Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mock_url_request_job_factory.h" | 5 #include "mock_url_request_job_factory.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/memory/ptr_util.h" | |
| 10 #include "components/cronet/android/test/cronet_test_util.h" | |
| 9 #include "jni/MockUrlRequestJobFactory_jni.h" | 11 #include "jni/MockUrlRequestJobFactory_jni.h" |
| 10 #include "net/test/url_request/ssl_certificate_error_job.h" | 12 #include "net/test/url_request/ssl_certificate_error_job.h" |
| 11 #include "net/test/url_request/url_request_failed_job.h" | 13 #include "net/test/url_request/url_request_failed_job.h" |
| 12 #include "net/test/url_request/url_request_hanging_read_job.h" | 14 #include "net/test/url_request/url_request_hanging_read_job.h" |
| 13 #include "net/test/url_request/url_request_mock_data_job.h" | 15 #include "net/test/url_request/url_request_mock_data_job.h" |
| 16 #include "net/url_request/url_request_context.h" | |
| 17 #include "net/url_request/url_request_filter.h" | |
| 18 #include "net/url_request/url_request_intercepting_job_factory.h" | |
| 14 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 15 | 20 |
| 16 using base::android::JavaParamRef; | 21 using base::android::JavaParamRef; |
| 17 using base::android::ScopedJavaLocalRef; | 22 using base::android::ScopedJavaLocalRef; |
| 18 | 23 |
| 19 namespace cronet { | 24 namespace cronet { |
| 20 | 25 |
| 26 // Intercept URLRequestJob creation using URLRequestFilter from | |
| 27 // libcronet_tests.so | |
| 28 class UrlInterceptorJobFactoryHandle { | |
| 29 public: | |
| 30 // |jcontext_adapter| points to a URLRequestContextAdapater. | |
| 31 UrlInterceptorJobFactoryHandle(jlong jcontext_adapter) | |
| 32 : jcontext_adapter_(jcontext_adapter) { | |
| 33 TestUtil::RunAfterContextInit( | |
| 34 jcontext_adapter, | |
| 35 base::Bind(&UrlInterceptorJobFactoryHandle::InitOnNetworkThread, | |
| 36 base::Unretained(this))); | |
| 37 } | |
| 38 // Should only be called on network thread; other threads should use | |
| 39 // shutdown(). | |
| 40 ~UrlInterceptorJobFactoryHandle() { | |
| 41 TestUtil::GetURLRequestContext(jcontext_adapter_) | |
| 42 ->set_job_factory(old_job_factory_); | |
| 43 } | |
| 44 | |
| 45 void Shutdown() { | |
|
xunjieli
2016/10/27 14:49:43
nit: Matt told me that Shutdown() is a noun, and S
pauljensen
2016/10/27 18:43:26
Done. I should mention that Shutdown() appears 25
| |
| 46 TestUtil::GetTaskRunner(jcontext_adapter_)->DeleteSoon(FROM_HERE, this); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 void InitOnNetworkThread() { | |
| 51 net::URLRequestContext* request_context = | |
| 52 TestUtil::GetURLRequestContext(jcontext_adapter_); | |
| 53 old_job_factory_ = request_context->job_factory(); | |
| 54 new_job_factory_.reset(new net::URLRequestInterceptingJobFactory( | |
| 55 const_cast<net::URLRequestJobFactory*>(old_job_factory_), | |
| 56 net::URLRequestFilter::GetInstance())); | |
| 57 request_context->set_job_factory(new_job_factory_.get()); | |
| 58 } | |
| 59 | |
| 60 // The URLRequestContextAdapater this object intercepts from. | |
| 61 const jlong jcontext_adapter_; | |
| 62 // URLRequestJobFactory previously used in URLRequestContext. | |
| 63 const net::URLRequestJobFactory* old_job_factory_; | |
| 64 // URLRequestJobFactory inserted during tests to intercept URLRequests with | |
| 65 // libcronet's URLRequestFilter. | |
| 66 std::unique_ptr<net::URLRequestInterceptingJobFactory> new_job_factory_; | |
| 67 }; | |
| 68 | |
| 21 void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) { | 69 void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) { |
| 22 net::URLRequestMockDataJob::AddUrlHandler(); | 70 net::URLRequestMockDataJob::AddUrlHandler(); |
| 23 net::URLRequestFailedJob::AddUrlHandler(); | 71 net::URLRequestFailedJob::AddUrlHandler(); |
| 24 net::URLRequestHangingReadJob::AddUrlHandler(); | 72 net::URLRequestHangingReadJob::AddUrlHandler(); |
| 25 net::SSLCertificateErrorJob::AddUrlHandler(); | 73 net::SSLCertificateErrorJob::AddUrlHandler(); |
| 26 } | 74 } |
| 27 | 75 |
| 76 // URL interceptors are registered with the URLRequestFilter in | |
| 77 // libcronet_tests.so. However tests are run on libcronet.so. Use the | |
| 78 // URLRequestFilter in libcronet_tests.so with the URLRequestContext in | |
| 79 // libcronet.so by installing a URLRequestInterceptingJobFactory | |
| 80 // that calls into libcronet_tests.so's URLRequestFilter. | |
| 81 jlong AddUrlInterceptorJobFactory(JNIEnv* env, | |
|
xunjieli
2016/10/27 14:49:43
cleanup idea: maybe combine AddUrlInterceptorJobFa
pauljensen
2016/10/27 18:43:26
Done.
| |
| 82 const JavaParamRef<jclass>& jcaller, | |
| 83 jlong jcontext_adapter) { | |
| 84 return reinterpret_cast<jlong>( | |
| 85 new UrlInterceptorJobFactoryHandle(jcontext_adapter)); | |
| 86 } | |
| 87 | |
| 88 // Put back the old URLRequestJobFactory into the URLRequestContext. | |
| 89 void RemoveUrlInterceptorJobFactory(JNIEnv* env, | |
|
xunjieli
2016/10/27 14:49:43
Optional nit:
Use @NativeClassQualifiedName() and
pauljensen
2016/10/27 18:43:25
I tried to get this to work for a while, but it ap
| |
| 90 const JavaParamRef<jclass>& jcaller, | |
| 91 jlong jinterceptor_handle) { | |
| 92 reinterpret_cast<UrlInterceptorJobFactoryHandle*>(jinterceptor_handle) | |
| 93 ->Shutdown(); | |
| 94 } | |
| 95 | |
| 28 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( | 96 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( |
| 29 JNIEnv* jenv, | 97 JNIEnv* jenv, |
| 30 const JavaParamRef<jclass>& jcaller, | 98 const JavaParamRef<jclass>& jcaller, |
| 31 jint jphase, | 99 jint jphase, |
| 32 jint jnet_error) { | 100 jint jnet_error) { |
| 33 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 101 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
| 34 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), | 102 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), |
| 35 static_cast<int>(jnet_error))); | 103 static_cast<int>(jnet_error))); |
| 36 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); | 104 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); |
| 37 } | 105 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 66 const JavaParamRef<jclass>& jcaller) { | 134 const JavaParamRef<jclass>& jcaller) { |
| 67 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); | 135 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); |
| 68 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); | 136 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); |
| 69 } | 137 } |
| 70 | 138 |
| 71 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { | 139 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { |
| 72 return RegisterNativesImpl(env); | 140 return RegisterNativesImpl(env); |
| 73 } | 141 } |
| 74 | 142 |
| 75 } // namespace cronet | 143 } // namespace cronet |
| OLD | NEW |