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 "components/cronet/android/cronet_url_request_adapter.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "components/cronet/android/test/native_test_server.h" | 11 #include "components/cronet/android/test/native_test_server.h" |
| 11 #include "jni/CronetTestUtil_jni.h" | 12 #include "jni/CronetTestUtil_jni.h" |
| 12 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 13 | 14 |
| 14 using base::android::JavaParamRef; | 15 using base::android::JavaParamRef; |
| 15 | 16 |
| 16 namespace cronet { | 17 namespace cronet { |
| 17 | 18 |
| 19 namespace { | |
| 20 | |
| 21 // A SingleThreadTaskRunner that works when a thread's SingleThreadTaskRunner | |
| 22 // isn't accessible to native code, instead this implementation works by calling | |
| 23 // out to Java which can access libcronet's network threads by issuing | |
| 24 // UrlRequests with direct-executors. | |
| 25 class JavaTaskRunner : public base::SingleThreadTaskRunner { | |
|
kapishnikov
2016/10/20 17:25:46
Would it be better if we had a non-static CronetTe
pauljensen
2016/10/21 02:27:33
It would not eliminate the jumping back and forth
| |
| 26 public: | |
| 27 JavaTaskRunner(base::MessageLoop* message_loop) | |
| 28 : message_loop_(message_loop) {} | |
| 29 virtual ~JavaTaskRunner() {} | |
| 30 | |
| 31 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 32 const base::Closure& task, | |
| 33 base::TimeDelta delay) override { | |
| 34 // Use Java to post back to the network thread. There is no native access | |
| 35 // to the libcronet.so MessageLoop or SingleThreadTaskRunner. | |
| 36 Java_CronetTestUtil_postClosureToNetworkThread( | |
| 37 base::android::AttachCurrentThread(), | |
| 38 reinterpret_cast<jlong>(new base::Closure(task))); | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
| 43 const base::Closure& task, | |
| 44 base::TimeDelta delay) override { | |
| 45 return PostDelayedTask(from_here, task, delay); | |
| 46 } | |
| 47 | |
| 48 bool RunsTasksOnCurrentThread() const override { | |
| 49 return message_loop_ == base::MessageLoop::current(); | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 // MessageLoop that owns this instance. | |
| 54 base::MessageLoop* message_loop_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(JavaTaskRunner); | |
| 57 }; | |
| 58 | |
| 59 // MessageLoop for libcronet.so's network threads, to be accessed by | |
| 60 // libcronet_tests.so methods. | |
| 61 static std::unique_ptr<base::MessageLoop> message_loop; | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 18 jint GetLoadFlags(JNIEnv* env, | 65 jint GetLoadFlags(JNIEnv* env, |
| 19 const JavaParamRef<jclass>& jcaller, | 66 const JavaParamRef<jclass>& jcaller, |
| 20 const jlong urlRequest) { | 67 const jlong urlRequest) { |
| 21 return reinterpret_cast<CronetURLRequestAdapter*>(urlRequest) | 68 return reinterpret_cast<net::URLRequest*>(urlRequest)->load_flags(); |
| 22 ->GetURLRequestForTesting() | 69 } |
| 23 ->load_flags(); | 70 |
| 71 // Tests need to call into libcronet.so code on libcronet.so threads. | |
| 72 // libcronet.so's threads are registered with static tables for MessageLoops | |
| 73 // and SingleThreadTaskRunners in libcronet.so, so libcronet_test.so | |
| 74 // functions that try and access these tables will find missing entries in | |
| 75 // the corresponding static tables in libcronet_test.so. Fix this by | |
| 76 // initializing a MessageLoop and SingleThreadTaskRunner in libcronet_test.so | |
| 77 // for these threads. | |
| 78 void PrepareNetworkThreadForTesting(JNIEnv* env, | |
| 79 const JavaParamRef<jclass>& jcaller) { | |
| 80 message_loop.reset(new base::MessageLoopForIO()); | |
| 81 message_loop->SetTaskRunner(new JavaTaskRunner(message_loop.get())); | |
| 82 } | |
| 83 | |
| 84 jlong GetNativeSingleThreadTaskRunner(JNIEnv* env, | |
| 85 const JavaParamRef<jclass>& jcaller) { | |
| 86 return reinterpret_cast<jlong>(message_loop->task_runner().get()); | |
| 87 } | |
| 88 | |
| 89 void CleanupNetorkThreadForTesting(JNIEnv* env, | |
| 90 const JavaParamRef<jclass>& jcaller) { | |
| 91 message_loop.reset(); | |
| 92 } | |
| 93 | |
| 94 void RunClosure(JNIEnv* env, | |
| 95 const JavaParamRef<jclass>& jcaller, | |
| 96 jlong jclosure) { | |
| 97 base::Closure* closure = reinterpret_cast<base::Closure*>(jclosure); | |
| 98 closure->Run(); | |
| 99 delete closure; | |
| 24 } | 100 } |
| 25 | 101 |
| 26 bool RegisterCronetTestUtil(JNIEnv* env) { | 102 bool RegisterCronetTestUtil(JNIEnv* env) { |
| 27 return RegisterNativesImpl(env); | 103 return RegisterNativesImpl(env); |
| 28 } | 104 } |
| 29 | 105 |
| 30 } // namespace cronet | 106 } // namespace cronet |
| OLD | NEW |