| 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/test/cronet_test_util.h" |
| 15 #include "jni/SdchObserver_jni.h" | 15 #include "jni/SdchObserver_jni.h" |
| 16 #include "net/base/sdch_manager.h" | 16 #include "net/base/sdch_manager.h" |
| 17 #include "net/base/sdch_observer.h" | 17 #include "net/base/sdch_observer.h" |
| 18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 using base::android::JavaParamRef; | 21 using base::android::JavaParamRef; |
| 22 | 22 |
| 23 namespace cronet { | 23 namespace cronet { |
| 24 | 24 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 GURL target_url_; | 59 GURL target_url_; |
| 60 net::SdchManager* manager_; | 60 net::SdchManager* manager_; |
| 61 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref_; | 61 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref_; |
| 62 | 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); | 63 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 void AddSdchObserverHelper( | 66 void AddSdchObserverHelper( |
| 67 const GURL& target_url, | 67 const GURL& target_url, |
| 68 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, | 68 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, |
| 69 net::URLRequestContext* url_request_context) { | 69 jlong jadapter) { |
| 70 JNIEnv* env = base::android::AttachCurrentThread(); | 70 JNIEnv* env = base::android::AttachCurrentThread(); |
| 71 net::URLRequestContext* url_request_context = |
| 72 TestUtil::getURLRequestContext(jadapter); |
| 71 // If dictionaries for |target_url| are already added, skip adding the | 73 // If dictionaries for |target_url| are already added, skip adding the |
| 72 // observer. | 74 // observer. |
| 73 if (url_request_context->sdch_manager()->GetDictionarySet(target_url)) { | 75 if (url_request_context->sdch_manager()->GetDictionarySet(target_url)) { |
| 74 Java_SdchObserver_onDictionarySetAlreadyPresent(env, jsdch_observer_ref); | 76 Java_SdchObserver_onDictionarySetAlreadyPresent(env, jsdch_observer_ref); |
| 75 return; | 77 return; |
| 76 } | 78 } |
| 77 | 79 |
| 78 url_request_context->sdch_manager()->AddObserver(new TestSdchObserver( | 80 url_request_context->sdch_manager()->AddObserver(new TestSdchObserver( |
| 79 target_url, url_request_context->sdch_manager(), jsdch_observer_ref)); | 81 target_url, url_request_context->sdch_manager(), jsdch_observer_ref)); |
| 80 Java_SdchObserver_onAddSdchObserverCompleted(env, jsdch_observer_ref); | 82 Java_SdchObserver_onAddSdchObserverCompleted(env, jsdch_observer_ref); |
| 81 } | 83 } |
| 82 | 84 |
| 83 void AddSdchObserverOnNetworkThread( | |
| 84 const GURL& target_url, | |
| 85 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, | |
| 86 CronetURLRequestContextAdapter* context_adapter) { | |
| 87 AddSdchObserverHelper(target_url, jsdch_observer_ref, | |
| 88 context_adapter->GetURLRequestContext()); | |
| 89 } | |
| 90 | |
| 91 } // namespace | 85 } // namespace |
| 92 | 86 |
| 93 void AddSdchObserver(JNIEnv* env, | 87 void AddSdchObserver(JNIEnv* env, |
| 94 const JavaParamRef<jobject>& jsdch_observer, | 88 const JavaParamRef<jobject>& jsdch_observer, |
| 95 const JavaParamRef<jstring>& jtarget_url, | 89 const JavaParamRef<jstring>& jtarget_url, |
| 96 jlong jadapter) { | 90 jlong jadapter) { |
| 97 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; | 91 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; |
| 98 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to | 92 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to |
| 99 // use it across threads. |AddSdchObserverHelper| will acquire a new | 93 // use it across threads. |AddSdchObserverHelper| will acquire a new |
| 100 // JNIEnv before calling into Java. | 94 // JNIEnv before calling into Java. |
| 101 jsdch_observer_ref.Reset(env, jsdch_observer); | 95 jsdch_observer_ref.Reset(env, jsdch_observer); |
| 102 | 96 |
| 103 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); | 97 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
| 104 CronetURLRequestContextAdapter* context_adapter = | 98 TestUtil::getTaskRunner(jadapter)->PostTask( |
| 105 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | 99 FROM_HERE, base::Bind(&TestUtil::runAfterContextInit, jadapter, |
| 106 context_adapter->PostTaskToNetworkThread( | 100 base::Bind(&AddSdchObserverHelper, target_url, |
| 107 FROM_HERE, | 101 jsdch_observer_ref, jadapter))); |
| 108 base::Bind(&AddSdchObserverOnNetworkThread, target_url, | |
| 109 jsdch_observer_ref, base::Unretained(context_adapter))); | |
| 110 } | 102 } |
| 111 | 103 |
| 112 bool RegisterSdchTestUtil(JNIEnv* env) { | 104 bool RegisterSdchTestUtil(JNIEnv* env) { |
| 113 return RegisterNativesImpl(env); | 105 return RegisterNativesImpl(env); |
| 114 } | 106 } |
| 115 | 107 |
| 116 } // namespace cronet | 108 } // namespace cronet |
| OLD | NEW |