OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "components/cronet/android/test/cronet_test_util.h" | 5 #include "components/cronet/android/test/cronet_test_util.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/message_loop/message_loop.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
9 #include "components/cronet/android/cronet_url_request_adapter.h" | 11 #include "components/cronet/android/cronet_url_request_adapter.h" |
10 #include "components/cronet/android/test/native_test_server.h" | 12 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
11 #include "jni/CronetTestUtil_jni.h" | 13 #include "jni/CronetTestUtil_jni.h" |
12 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
13 | 15 |
14 using base::android::JavaParamRef; | 16 using base::android::JavaParamRef; |
15 | 17 |
16 namespace cronet { | 18 namespace cronet { |
17 | 19 |
18 jint GetLoadFlags(JNIEnv* env, | 20 jint GetLoadFlags(JNIEnv* env, |
19 const JavaParamRef<jclass>& jcaller, | 21 const JavaParamRef<jclass>& jcaller, |
20 const jlong urlRequest) { | 22 const jlong jurl_request_adapter) { |
21 return reinterpret_cast<CronetURLRequestAdapter*>(urlRequest) | 23 return TestUtil::GetURLRequest(jurl_request_adapter)->load_flags(); |
22 ->GetURLRequestForTesting() | |
23 ->load_flags(); | |
24 } | 24 } |
25 | 25 |
26 bool RegisterCronetTestUtil(JNIEnv* env) { | 26 // static |
27 scoped_refptr<base::SingleThreadTaskRunner> TestUtil::GetTaskRunner( | |
28 jlong jcontext_adapter) { | |
29 CronetURLRequestContextAdapter* context_adapter = | |
30 reinterpret_cast<CronetURLRequestContextAdapter*>(jcontext_adapter); | |
31 return context_adapter->network_thread_->task_runner(); | |
32 } | |
33 | |
34 // static | |
35 net::URLRequestContext* TestUtil::GetURLRequestContext(jlong jcontext_adapter) { | |
36 CronetURLRequestContextAdapter* context_adapter = | |
37 reinterpret_cast<CronetURLRequestContextAdapter*>(jcontext_adapter); | |
38 return context_adapter->context_.get(); | |
39 } | |
40 | |
41 // static | |
42 void TestUtil::RunAfterContextInitOnNetworkThread(jlong jcontext_adapter, | |
43 const base::Closure& task) { | |
44 CronetURLRequestContextAdapter* context_adapter = | |
45 reinterpret_cast<CronetURLRequestContextAdapter*>(jcontext_adapter); | |
46 if (context_adapter->is_context_initialized_) { | |
47 task.Run(); | |
48 } else { | |
49 context_adapter->tasks_waiting_for_context_.push(task); | |
50 } | |
51 } | |
52 | |
53 // static | |
54 void TestUtil::RunAfterContextInit(jlong jcontext_adapter, | |
55 const base::Closure& task) { | |
56 GetTaskRunner(jcontext_adapter) | |
57 ->PostTask(FROM_HERE, | |
58 base::Bind(&TestUtil::RunAfterContextInitOnNetworkThread, | |
59 jcontext_adapter, task)); | |
60 } | |
61 | |
62 // static | |
63 net::URLRequest* TestUtil::GetURLRequest(jlong jrequest_adapter) { | |
64 CronetURLRequestAdapter* request_adapter = | |
65 reinterpret_cast<CronetURLRequestAdapter*>(jrequest_adapter); | |
66 return request_adapter->url_request_.get(); | |
67 } | |
68 | |
69 static void PrepareNetworkThreadOnNetworkThread(jlong jcontext_adapter) { | |
70 (new base::MessageLoopForIO()) | |
71 ->SetTaskRunner(TestUtil::GetTaskRunner(jcontext_adapter)); | |
72 } | |
73 | |
74 // Tests need to call into libcronet.so code on libcronet.so threads. | |
75 // libcronet.so's threads are registered with static tables for MessageLoops | |
76 // and SingleThreadTaskRunners in libcronet.so, so libcronet_test.so | |
77 // functions that try and access these tables will find missing entries in | |
78 // the corresponding static tables in libcronet_test.so. Fix this by | |
79 // initializing a MessageLoop and SingleThreadTaskRunner in libcronet_test.so | |
80 // for these threads. | |
81 void PrepareNetworkThread(JNIEnv* env, | |
mef
2016/11/16 20:01:27
nit: maybe add a comment that this is called from
pauljensen
2016/11/18 18:12:10
Done.
| |
82 const JavaParamRef<jclass>& jcaller, | |
83 jlong jcontext_adapter) { | |
84 TestUtil::GetTaskRunner(jcontext_adapter) | |
85 ->PostTask(FROM_HERE, base::Bind(&PrepareNetworkThreadOnNetworkThread, | |
86 jcontext_adapter)); | |
87 } | |
88 | |
89 static void CleanupNetworkThreadOnNetworkThread() { | |
90 delete base::MessageLoop::current(); | |
91 } | |
92 | |
93 void CleanupNetworkThread(JNIEnv* env, | |
mef
2016/11/16 20:01:27
nit: maybe add a comment that this is called from
pauljensen
2016/11/18 18:12:10
Done.
| |
94 const JavaParamRef<jclass>& jcaller, | |
95 jlong jcontext_adapter) { | |
96 TestUtil::GetTaskRunner(jcontext_adapter) | |
97 ->PostTask(FROM_HERE, base::Bind(&CleanupNetworkThreadOnNetworkThread)); | |
98 } | |
99 | |
100 bool TestUtil::Register(JNIEnv* env) { | |
27 return RegisterNativesImpl(env); | 101 return RegisterNativesImpl(env); |
28 } | 102 } |
29 | 103 |
30 } // namespace cronet | 104 } // namespace cronet |
OLD | NEW |