| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "sdch_test_util.h" | 5 #include "sdch_test_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "components/cronet/android/cronet_url_request_context_adapter.h" | 14 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 15 #include "components/cronet/android/url_request_context_adapter.h" | |
| 16 #include "jni/SdchObserver_jni.h" | 15 #include "jni/SdchObserver_jni.h" |
| 17 #include "net/base/sdch_manager.h" | 16 #include "net/base/sdch_manager.h" |
| 18 #include "net/base/sdch_observer.h" | 17 #include "net/base/sdch_observer.h" |
| 19 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 20 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 21 | 20 |
| 22 using base::android::JavaParamRef; | 21 using base::android::JavaParamRef; |
| 23 | 22 |
| 24 namespace cronet { | 23 namespace cronet { |
| 25 | 24 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 81 } |
| 83 | 82 |
| 84 void AddSdchObserverOnNetworkThread( | 83 void AddSdchObserverOnNetworkThread( |
| 85 const GURL& target_url, | 84 const GURL& target_url, |
| 86 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, | 85 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, |
| 87 CronetURLRequestContextAdapter* context_adapter) { | 86 CronetURLRequestContextAdapter* context_adapter) { |
| 88 AddSdchObserverHelper(target_url, jsdch_observer_ref, | 87 AddSdchObserverHelper(target_url, jsdch_observer_ref, |
| 89 context_adapter->GetURLRequestContext()); | 88 context_adapter->GetURLRequestContext()); |
| 90 } | 89 } |
| 91 | 90 |
| 92 // TODO(xunjieli): Delete this once legacy API is removed. | |
| 93 void AddSdchObserverOnNetworkThreadLegacyAPI( | |
| 94 const GURL& target_url, | |
| 95 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, | |
| 96 URLRequestContextAdapter* context_adapter) { | |
| 97 AddSdchObserverHelper(target_url, jsdch_observer_ref, | |
| 98 context_adapter->GetURLRequestContext()); | |
| 99 } | |
| 100 | |
| 101 } // namespace | 91 } // namespace |
| 102 | 92 |
| 103 void AddSdchObserver(JNIEnv* env, | 93 void AddSdchObserver(JNIEnv* env, |
| 104 const JavaParamRef<jobject>& jsdch_observer, | 94 const JavaParamRef<jobject>& jsdch_observer, |
| 105 const JavaParamRef<jstring>& jtarget_url, | 95 const JavaParamRef<jstring>& jtarget_url, |
| 106 jlong jadapter) { | 96 jlong jadapter) { |
| 107 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; | 97 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; |
| 108 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to | 98 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to |
| 109 // use it across threads. |AddSdchObserverHelper| will acquire a new | 99 // use it across threads. |AddSdchObserverHelper| will acquire a new |
| 110 // JNIEnv before calling into Java. | 100 // JNIEnv before calling into Java. |
| 111 jsdch_observer_ref.Reset(env, jsdch_observer); | 101 jsdch_observer_ref.Reset(env, jsdch_observer); |
| 112 | 102 |
| 113 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); | 103 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
| 114 CronetURLRequestContextAdapter* context_adapter = | 104 CronetURLRequestContextAdapter* context_adapter = |
| 115 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | 105 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
| 116 context_adapter->PostTaskToNetworkThread( | 106 context_adapter->PostTaskToNetworkThread( |
| 117 FROM_HERE, | 107 FROM_HERE, |
| 118 base::Bind(&AddSdchObserverOnNetworkThread, target_url, | 108 base::Bind(&AddSdchObserverOnNetworkThread, target_url, |
| 119 jsdch_observer_ref, base::Unretained(context_adapter))); | 109 jsdch_observer_ref, base::Unretained(context_adapter))); |
| 120 } | 110 } |
| 121 | 111 |
| 122 void AddSdchObserverLegacyAPI(JNIEnv* env, | |
| 123 const JavaParamRef<jobject>& jsdch_observer, | |
| 124 const JavaParamRef<jstring>& jtarget_url, | |
| 125 jlong jadapter) { | |
| 126 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; | |
| 127 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to | |
| 128 // use it across threads. |AddSdchObserverHelper| will acquire a new | |
| 129 // JNIEnv before calling into Java. | |
| 130 jsdch_observer_ref.Reset(env, jsdch_observer); | |
| 131 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); | |
| 132 URLRequestContextAdapter* context_adapter = | |
| 133 reinterpret_cast<URLRequestContextAdapter*>(jadapter); | |
| 134 context_adapter->PostTaskToNetworkThread( | |
| 135 FROM_HERE, | |
| 136 base::Bind(&AddSdchObserverOnNetworkThreadLegacyAPI, target_url, | |
| 137 jsdch_observer_ref, base::Unretained(context_adapter))); | |
| 138 } | |
| 139 | |
| 140 bool RegisterSdchTestUtil(JNIEnv* env) { | 112 bool RegisterSdchTestUtil(JNIEnv* env) { |
| 141 return RegisterNativesImpl(env); | 113 return RegisterNativesImpl(env); |
| 142 } | 114 } |
| 143 | 115 |
| 144 } // namespace cronet | 116 } // namespace cronet |
| OLD | NEW |