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 | |
mef
2016/11/16 18:45:44
Can we have DCHECK for this?
pauljensen
2016/11/18 18:12:10
Done.
| |
39 // ShutDown(). | |
40 ~UrlInterceptorJobFactoryHandle() { | |
41 TestUtil::GetURLRequestContext(jcontext_adapter_) | |
42 ->set_job_factory(old_job_factory_); | |
43 } | |
44 | |
45 void ShutDown() { | |
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 | |
69 // URL interceptors are registered with the URLRequestFilter in | |
70 // libcronet_tests.so. However tests are run on libcronet.so. Use the | |
71 // URLRequestFilter in libcronet_tests.so with the URLRequestContext in | |
72 // libcronet.so by installing a URLRequestInterceptingJobFactory | |
73 // that calls into libcronet_tests.so's URLRequestFilter. | |
74 jlong AddUrlInterceptors(JNIEnv* env, | |
75 const JavaParamRef<jclass>& jcaller, | |
76 jlong jcontext_adapter) { | |
22 net::URLRequestMockDataJob::AddUrlHandler(); | 77 net::URLRequestMockDataJob::AddUrlHandler(); |
23 net::URLRequestFailedJob::AddUrlHandler(); | 78 net::URLRequestFailedJob::AddUrlHandler(); |
24 net::URLRequestHangingReadJob::AddUrlHandler(); | 79 net::URLRequestHangingReadJob::AddUrlHandler(); |
25 net::SSLCertificateErrorJob::AddUrlHandler(); | 80 net::SSLCertificateErrorJob::AddUrlHandler(); |
81 return reinterpret_cast<jlong>( | |
82 new UrlInterceptorJobFactoryHandle(jcontext_adapter)); | |
83 } | |
84 | |
85 // Put back the old URLRequestJobFactory into the URLRequestContext. | |
86 void RemoveUrlInterceptorJobFactory(JNIEnv* env, | |
87 const JavaParamRef<jclass>& jcaller, | |
88 jlong jinterceptor_handle) { | |
89 reinterpret_cast<UrlInterceptorJobFactoryHandle*>(jinterceptor_handle) | |
90 ->ShutDown(); | |
26 } | 91 } |
27 | 92 |
28 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( | 93 ScopedJavaLocalRef<jstring> GetMockUrlWithFailure( |
29 JNIEnv* jenv, | 94 JNIEnv* jenv, |
30 const JavaParamRef<jclass>& jcaller, | 95 const JavaParamRef<jclass>& jcaller, |
31 jint jphase, | 96 jint jphase, |
32 jint jnet_error) { | 97 jint jnet_error) { |
33 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 98 GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
34 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), | 99 static_cast<net::URLRequestFailedJob::FailurePhase>(jphase), |
35 static_cast<int>(jnet_error))); | 100 static_cast<int>(jnet_error))); |
(...skipping 30 matching lines...) Expand all Loading... | |
66 const JavaParamRef<jclass>& jcaller) { | 131 const JavaParamRef<jclass>& jcaller) { |
67 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); | 132 GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl()); |
68 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); | 133 return base::android::ConvertUTF8ToJavaString(jenv, url.spec()); |
69 } | 134 } |
70 | 135 |
71 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { | 136 bool RegisterMockUrlRequestJobFactory(JNIEnv* env) { |
72 return RegisterNativesImpl(env); | 137 return RegisterNativesImpl(env); |
73 } | 138 } |
74 | 139 |
75 } // namespace cronet | 140 } // namespace cronet |
OLD | NEW |