| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sdch_test_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" |
| 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/bind.h" |
| 13 #include "base/macros.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" |
| 17 #include "net/base/sdch_manager.h" |
| 18 #include "net/base/sdch_observer.h" |
| 19 #include "net/url_request/url_request_context.h" |
| 20 #include "url/gurl.h" |
| 21 |
| 22 namespace cronet { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class TestSdchObserver : public net::SdchObserver { |
| 27 public: |
| 28 TestSdchObserver( |
| 29 const GURL& target_url, |
| 30 net::SdchManager* manager, |
| 31 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref) |
| 32 : target_url_(target_url), manager_(manager) { |
| 33 jsdch_observer_ref_.Reset(jsdch_observer_ref); |
| 34 } |
| 35 |
| 36 // SdchObserver implementation |
| 37 void OnDictionaryAdded(const GURL& dictionary_url, |
| 38 const std::string& server_hash) override { |
| 39 // Only notify if the dictionary for the |target_url_| has been added. |
| 40 if (manager_->GetDictionarySet(target_url_)) { |
| 41 JNIEnv* env = base::android::AttachCurrentThread(); |
| 42 Java_SdchObserver_onDictionaryAdded(env, jsdch_observer_ref_.obj()); |
| 43 manager_->RemoveObserver(this); |
| 44 delete this; |
| 45 } |
| 46 } |
| 47 |
| 48 void OnDictionaryRemoved(const std::string& server_hash) override {} |
| 49 |
| 50 void OnDictionaryUsed(const std::string& server_hash) override {} |
| 51 |
| 52 void OnGetDictionary(const GURL& request_url, |
| 53 const GURL& dictionary_url) override {} |
| 54 |
| 55 void OnClearDictionaries() override {} |
| 56 |
| 57 private: |
| 58 GURL target_url_; |
| 59 net::SdchManager* manager_; |
| 60 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); |
| 63 }; |
| 64 |
| 65 void AddSdchObserverHelper( |
| 66 const GURL& target_url, |
| 67 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, |
| 68 net::URLRequestContext* url_request_context) { |
| 69 JNIEnv* env = base::android::AttachCurrentThread(); |
| 70 // If dictionaries for |target_url| are already added, skip adding the |
| 71 // observer. |
| 72 if (url_request_context->sdch_manager()->GetDictionarySet(target_url)) { |
| 73 Java_SdchObserver_onDictionarySetAlreadyPresent(env, |
| 74 jsdch_observer_ref.obj()); |
| 75 return; |
| 76 } |
| 77 |
| 78 url_request_context->sdch_manager()->AddObserver(new TestSdchObserver( |
| 79 target_url, url_request_context->sdch_manager(), jsdch_observer_ref)); |
| 80 Java_SdchObserver_onAddSdchObserverCompleted(env, jsdch_observer_ref.obj()); |
| 81 } |
| 82 |
| 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 // TODO(xunjieli): Delete this once legacy API is removed. |
| 92 void AddSdchObserverOnNetworkThreadLegacyAPI( |
| 93 const GURL& target_url, |
| 94 const base::android::ScopedJavaGlobalRef<jobject>& jsdch_observer_ref, |
| 95 URLRequestContextAdapter* context_adapter) { |
| 96 AddSdchObserverHelper(target_url, jsdch_observer_ref, |
| 97 context_adapter->GetURLRequestContext()); |
| 98 } |
| 99 |
| 100 } // namespace |
| 101 |
| 102 void AddSdchObserver(JNIEnv* env, |
| 103 jobject jsdch_observer, |
| 104 jstring jtarget_url, |
| 105 jlong jadapter) { |
| 106 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; |
| 107 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to |
| 108 // use it across threads. |AddSdchObserverHelper| will acquire a new |
| 109 // JNIEnv before calling into Java. |
| 110 jsdch_observer_ref.Reset(env, jsdch_observer); |
| 111 |
| 112 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
| 113 CronetURLRequestContextAdapter* context_adapter = |
| 114 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
| 115 context_adapter->PostTaskToNetworkThread( |
| 116 FROM_HERE, |
| 117 base::Bind(&AddSdchObserverOnNetworkThread, target_url, |
| 118 jsdch_observer_ref, base::Unretained(context_adapter))); |
| 119 } |
| 120 |
| 121 void AddSdchObserverLegacyAPI(JNIEnv* env, |
| 122 jobject jsdch_observer, |
| 123 jstring jtarget_url, |
| 124 jlong jadapter) { |
| 125 base::android::ScopedJavaGlobalRef<jobject> jsdch_observer_ref; |
| 126 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to |
| 127 // use it across threads. |AddSdchObserverHelper| will acquire a new |
| 128 // JNIEnv before calling into Java. |
| 129 jsdch_observer_ref.Reset(env, jsdch_observer); |
| 130 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
| 131 URLRequestContextAdapter* context_adapter = |
| 132 reinterpret_cast<URLRequestContextAdapter*>(jadapter); |
| 133 context_adapter->PostTaskToNetworkThread( |
| 134 FROM_HERE, |
| 135 base::Bind(&AddSdchObserverOnNetworkThreadLegacyAPI, target_url, |
| 136 jsdch_observer_ref, base::Unretained(context_adapter))); |
| 137 } |
| 138 |
| 139 bool RegisterSdchTestUtil(JNIEnv* env) { |
| 140 return RegisterNativesImpl(env); |
| 141 } |
| 142 |
| 143 } // namespace cronet |
| OLD | NEW |