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" |
12 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
10 #include "components/cronet/android/test/native_test_server.h" | 13 #include "components/cronet/android/test/native_test_server.h" |
11 #include "jni/CronetTestUtil_jni.h" | 14 #include "jni/CronetTestUtil_jni.h" |
12 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
13 | 16 |
14 using base::android::JavaParamRef; | 17 using base::android::JavaParamRef; |
15 | 18 |
16 namespace cronet { | 19 namespace cronet { |
17 | 20 |
18 jint GetLoadFlags(JNIEnv* env, | 21 jint GetLoadFlags(JNIEnv* env, |
19 const JavaParamRef<jclass>& jcaller, | 22 const JavaParamRef<jclass>& jcaller, |
20 const jlong urlRequest) { | 23 const jlong jurl_request_adapter) { |
21 return reinterpret_cast<CronetURLRequestAdapter*>(urlRequest) | 24 return TestUtil::getURLRequest(jurl_request_adapter)->load_flags(); |
kapishnikov
2016/10/24 21:17:34
Is it safe to call load_flags() here? As I underst
pauljensen
2016/10/25 11:55:38
If the method isn't inline, as I said, it's fine b
| |
22 ->GetURLRequestForTesting() | |
23 ->load_flags(); | |
24 } | 25 } |
25 | 26 |
26 bool RegisterCronetTestUtil(JNIEnv* env) { | 27 // static |
28 scoped_refptr<base::SingleThreadTaskRunner> TestUtil::getTaskRunner( | |
29 jlong jcontext_adapter) { | |
30 CronetURLRequestContextAdapter* context_adapter = | |
31 (CronetURLRequestContextAdapter*)jcontext_adapter; | |
32 return context_adapter->network_thread_->task_runner(); | |
kapishnikov
2016/10/24 21:17:34
The task_runner() is inline but if DCHECK is enabl
pauljensen
2016/10/25 11:55:38
As I said, this is all calling stuff in net/, not
| |
33 } | |
34 | |
35 // static | |
36 net::URLRequestContext* TestUtil::getURLRequestContext(jlong jcontext_adapter) { | |
37 CronetURLRequestContextAdapter* context_adapter = | |
38 (CronetURLRequestContextAdapter*)jcontext_adapter; | |
39 return context_adapter->context_.get(); | |
40 } | |
41 | |
42 // static | |
43 void TestUtil::runAfterContextInit(jlong jcontext_adapter, | |
44 const base::Closure& task) { | |
45 CronetURLRequestContextAdapter* context_adapter = | |
46 (CronetURLRequestContextAdapter*)jcontext_adapter; | |
xunjieli
2016/10/25 17:51:38
reinterpret_cast<CronetURLRequestContextAdapter*>
pauljensen
2016/10/26 17:55:18
Done.
| |
47 if (context_adapter->is_context_initialized_) { | |
48 task.Run(); | |
49 } else { | |
50 context_adapter->tasks_waiting_for_context_.push(task); | |
51 } | |
52 } | |
53 | |
54 // static | |
55 net::URLRequest* TestUtil::getURLRequest(jlong jrequest_adapter) { | |
56 CronetURLRequestAdapter* request_adapter = | |
57 (CronetURLRequestAdapter*)jrequest_adapter; | |
58 return request_adapter->url_request_.get(); | |
59 } | |
60 | |
61 static void PrepareNetworkThreadOnNetworkThread(jlong jcontext_adapter) { | |
xunjieli
2016/10/25 17:51:38
Can this be a file local function (in an anonymous
pauljensen
2016/10/26 17:55:18
It's static, which means file local. I'd rather n
| |
62 (new base::MessageLoopForIO()) | |
63 ->SetTaskRunner(TestUtil::getTaskRunner(jcontext_adapter)); | |
64 } | |
65 | |
66 // Tests need to call into libcronet.so code on libcronet.so threads. | |
67 // libcronet.so's threads are registered with static tables for MessageLoops | |
68 // and SingleThreadTaskRunners in libcronet.so, so libcronet_test.so | |
69 // functions that try and access these tables will find missing entries in | |
70 // the corresponding static tables in libcronet_test.so. Fix this by | |
71 // initializing a MessageLoop and SingleThreadTaskRunner in libcronet_test.so | |
72 // for these threads. | |
73 void PrepareNetworkThread(JNIEnv* env, | |
74 const JavaParamRef<jclass>& jcaller, | |
75 jlong jcontext_adapter) { | |
76 TestUtil::getTaskRunner(jcontext_adapter) | |
77 ->PostTask(FROM_HERE, base::Bind(&PrepareNetworkThreadOnNetworkThread, | |
78 jcontext_adapter)); | |
79 } | |
80 | |
81 static void CleanupNetorkThreadOnNetworkThread() { | |
xunjieli
2016/10/25 17:51:38
file local function? Also there is a typo in Netwo
pauljensen
2016/10/26 17:55:18
Same comment about file local. I fixed the typo i
| |
82 delete base::MessageLoop::current(); | |
83 } | |
84 | |
85 void CleanupNetorkThread(JNIEnv* env, | |
86 const JavaParamRef<jclass>& jcaller, | |
87 jlong jcontext_adapter) { | |
88 TestUtil::getTaskRunner(jcontext_adapter) | |
89 ->PostTask(FROM_HERE, base::Bind(&CleanupNetorkThreadOnNetworkThread)); | |
90 } | |
91 | |
92 bool TestUtil::Register(JNIEnv* env) { | |
27 return RegisterNativesImpl(env); | 93 return RegisterNativesImpl(env); |
28 } | 94 } |
29 | 95 |
30 } // namespace cronet | 96 } // namespace cronet |
OLD | NEW |