Chromium Code Reviews| 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" |
|
xunjieli
2016/10/27 14:49:42
not used? (not sure why the original code has it)
pauljensen
2016/10/27 18:43:25
Done.
| |
| 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(); |
| 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 reinterpret_cast<CronetURLRequestContextAdapter*>(jcontext_adapter); | |
| 32 return context_adapter->network_thread_->task_runner(); | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 net::URLRequestContext* TestUtil::GetURLRequestContext(jlong jcontext_adapter) { | |
| 37 CronetURLRequestContextAdapter* context_adapter = | |
| 38 reinterpret_cast<CronetURLRequestContextAdapter*>(jcontext_adapter); | |
| 39 return context_adapter->context_.get(); | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 void TestUtil::RunAfterContextInitOnNetworkThread(jlong jcontext_adapter, | |
| 44 const base::Closure& task) { | |
| 45 CronetURLRequestContextAdapter* context_adapter = | |
| 46 reinterpret_cast<CronetURLRequestContextAdapter*>(jcontext_adapter); | |
| 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 void TestUtil::RunAfterContextInit(jlong jcontext_adapter, | |
| 56 const base::Closure& task) { | |
| 57 GetTaskRunner(jcontext_adapter) | |
| 58 ->PostTask(FROM_HERE, | |
| 59 base::Bind(&TestUtil::RunAfterContextInitOnNetworkThread, | |
| 60 jcontext_adapter, task)); | |
| 61 } | |
| 62 | |
| 63 // static | |
| 64 net::URLRequest* TestUtil::GetURLRequest(jlong jrequest_adapter) { | |
| 65 CronetURLRequestAdapter* request_adapter = | |
| 66 reinterpret_cast<CronetURLRequestAdapter*>(jrequest_adapter); | |
| 67 return request_adapter->url_request_.get(); | |
| 68 } | |
| 69 | |
| 70 static void PrepareNetworkThreadOnNetworkThread(jlong jcontext_adapter) { | |
| 71 (new base::MessageLoopForIO()) | |
| 72 ->SetTaskRunner(TestUtil::GetTaskRunner(jcontext_adapter)); | |
| 73 } | |
| 74 | |
| 75 // Tests need to call into libcronet.so code on libcronet.so threads. | |
| 76 // libcronet.so's threads are registered with static tables for MessageLoops | |
| 77 // and SingleThreadTaskRunners in libcronet.so, so libcronet_test.so | |
| 78 // functions that try and access these tables will find missing entries in | |
| 79 // the corresponding static tables in libcronet_test.so. Fix this by | |
| 80 // initializing a MessageLoop and SingleThreadTaskRunner in libcronet_test.so | |
| 81 // for these threads. | |
| 82 void PrepareNetworkThread(JNIEnv* env, | |
| 83 const JavaParamRef<jclass>& jcaller, | |
| 84 jlong jcontext_adapter) { | |
| 85 TestUtil::GetTaskRunner(jcontext_adapter) | |
| 86 ->PostTask(FROM_HERE, base::Bind(&PrepareNetworkThreadOnNetworkThread, | |
| 87 jcontext_adapter)); | |
| 88 } | |
| 89 | |
| 90 static void CleanupNetworkThreadOnNetworkThread() { | |
| 91 delete base::MessageLoop::current(); | |
| 92 } | |
| 93 | |
| 94 void CleanupNetworkThread(JNIEnv* env, | |
| 95 const JavaParamRef<jclass>& jcaller, | |
| 96 jlong jcontext_adapter) { | |
| 97 TestUtil::GetTaskRunner(jcontext_adapter) | |
| 98 ->PostTask(FROM_HERE, base::Bind(&CleanupNetworkThreadOnNetworkThread)); | |
| 99 } | |
| 100 | |
| 101 bool TestUtil::Register(JNIEnv* env) { | |
| 27 return RegisterNativesImpl(env); | 102 return RegisterNativesImpl(env); |
| 28 } | 103 } |
| 29 | 104 |
| 30 } // namespace cronet | 105 } // namespace cronet |
| OLD | NEW |