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 |
21 void AddUrlInterceptors(JNIEnv* env, const JavaParamRef<jclass>& jcaller) { | 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 DCHECK( |
| 42 TestUtil::GetTaskRunner(jcontext_adapter_)->BelongsToCurrentThread()); |
| 43 TestUtil::GetURLRequestContext(jcontext_adapter_) |
| 44 ->set_job_factory(old_job_factory_); |
| 45 } |
| 46 |
| 47 void ShutDown() { |
| 48 TestUtil::RunAfterContextInit( |
| 49 jcontext_adapter_, |
| 50 base::Bind(&UrlInterceptorJobFactoryHandle::ShutdownOnNetworkThread, |
| 51 base::Unretained(this))); |
| 52 } |
| 53 |
| 54 private: |
| 55 void InitOnNetworkThread() { |
| 56 net::URLRequestContext* request_context = |
| 57 TestUtil::GetURLRequestContext(jcontext_adapter_); |
| 58 old_job_factory_ = request_context->job_factory(); |
| 59 new_job_factory_.reset(new net::URLRequestInterceptingJobFactory( |
| 60 const_cast<net::URLRequestJobFactory*>(old_job_factory_), |
| 61 net::URLRequestFilter::GetInstance())); |
| 62 request_context->set_job_factory(new_job_factory_.get()); |
| 63 } |
| 64 |
| 65 void ShutdownOnNetworkThread() { delete this; } |
| 66 |
| 67 // The URLRequestContextAdapater this object intercepts from. |
| 68 const jlong jcontext_adapter_; |
| 69 // URLRequestJobFactory previously used in URLRequestContext. |
| 70 const net::URLRequestJobFactory* old_job_factory_; |
| 71 // URLRequestJobFactory inserted during tests to intercept URLRequests with |
| 72 // libcronet's URLRequestFilter. |
| 73 std::unique_ptr<net::URLRequestInterceptingJobFactory> new_job_factory_; |
| 74 }; |
| 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 AddUrlInterceptors(JNIEnv* env, |
| 82 const JavaParamRef<jclass>& jcaller, |
| 83 jlong jcontext_adapter) { |
22 net::URLRequestMockDataJob::AddUrlHandler(); | 84 net::URLRequestMockDataJob::AddUrlHandler(); |
23 net::URLRequestFailedJob::AddUrlHandler(); | 85 net::URLRequestFailedJob::AddUrlHandler(); |
24 net::URLRequestHangingReadJob::AddUrlHandler(); | 86 net::URLRequestHangingReadJob::AddUrlHandler(); |
25 net::SSLCertificateErrorJob::AddUrlHandler(); | 87 net::SSLCertificateErrorJob::AddUrlHandler(); |
| 88 return reinterpret_cast<jlong>( |
| 89 new UrlInterceptorJobFactoryHandle(jcontext_adapter)); |
| 90 } |
| 91 |
| 92 // Put back the old URLRequestJobFactory into the URLRequestContext. |
| 93 void RemoveUrlInterceptorJobFactory(JNIEnv* env, |
| 94 const JavaParamRef<jclass>& jcaller, |
| 95 jlong jinterceptor_handle) { |
| 96 reinterpret_cast<UrlInterceptorJobFactoryHandle*>(jinterceptor_handle) |
| 97 ->ShutDown(); |
26 } | 98 } |
27 | 99 |
28 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( | 100 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( |
29 JNIEnv* jenv, | 101 JNIEnv* jenv, |
30 const JavaParamRef<jclass>& jcaller, | 102 const JavaParamRef<jclass>& jcaller, |
31 jint jphase, | 103 jint jphase, |
32 jint jnet_error) { | 104 jint jnet_error) { |
33 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 105 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
34 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), | 106 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), |
35 static_cast<int>(jnet_error))); | 107 static_cast<int>(jnet_error))); |
(...skipping 30 matching lines...) Expand all Loading... |
66 const JavaParamRef<jclass>& jcaller) { | 138 const JavaParamRef<jclass>& jcaller) { |
67 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); | 139 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); |
68 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); | 140 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); |
69 } | 141 } |
70 | 142 |
71 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { | 143 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { |
72 return RegisterNativesImpl(env); | 144 return RegisterNativesImpl(env); |
73 } | 145 } |
74 | 146 |
75 } // namespace cronet | 147 } // namespace cronet |
OLD | NEW |