Index: components/cronet/android/test/sdch_test_util.cc |
diff --git a/components/cronet/android/test/sdch_test_util.cc b/components/cronet/android/test/sdch_test_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..002d8df5dc6841363e25ed76739235fdf2274b62 |
--- /dev/null |
+++ b/components/cronet/android/test/sdch_test_util.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "sdch_test_util.h" |
+ |
+#include <string> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "base/bind.h" |
+#include "base/macros.h" |
+#include "components/cronet/android/cronet_url_request_context_adapter.h" |
+#include "components/cronet/android/url_request_context_adapter.h" |
+#include "jni/SdchTestUtil_jni.h" |
+#include "net/base/sdch_manager.h" |
+#include "net/base/sdch_observer.h" |
+#include "net/url_request/url_request_context.h" |
+#include "url/gurl.h" |
+ |
+namespace cronet { |
+ |
+namespace { |
+ |
+class TestSdchObserver : public net::SdchObserver { |
+ public: |
+ TestSdchObserver(net::SdchManager* manager) : manager_(manager) {} |
+ |
+ // SdchObserver implementation |
+ void OnDictionaryAdded(const GURL& dictionary_url, |
+ const std::string& server_hash) override { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ Java_SdchTestUtil_onDictionaryAdded( |
+ env, base::android::ConvertUTF8ToJavaString(env, dictionary_url.spec()) |
+ .Release()); |
+ manager_->RemoveObserver(this); |
+ delete this; |
+ } |
+ |
+ void OnDictionaryRemoved(const std::string& server_hash) override{}; |
+ |
+ void OnDictionaryUsed(const std::string& server_hash) override{}; |
+ |
+ void OnGetDictionary(const GURL& request_url, |
+ const GURL& dictionary_url) override{}; |
+ |
+ void OnClearDictionaries() override{}; |
+ |
+ private: |
+ net::SdchManager* manager_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); |
+}; |
+ |
+void AddSdchObserverHelper(const GURL& target_url, |
+ net::URLRequestContext* url_request_context) { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ // If dictionaries are already added, skip adding the observer. |
+ if (url_request_context->sdch_manager()->GetDictionarySet(target_url)) { |
+ Java_SdchTestUtil_onAddSdchObserverCompleted(env, JNI_FALSE); |
+ return; |
+ } |
+ |
+ url_request_context->sdch_manager()->AddObserver( |
+ new TestSdchObserver(url_request_context->sdch_manager())); |
+ Java_SdchTestUtil_onAddSdchObserverCompleted(env, JNI_TRUE); |
+} |
+ |
+void AddSdchObserverOnNetworkThread( |
+ const GURL& target_url, |
+ CronetURLRequestContextAdapter* context_adapter) { |
+ AddSdchObserverHelper(target_url, context_adapter->GetURLRequestContext()); |
+} |
+ |
+// TODO(xunjieli): Delete this once legacy API is removed. |
+void AddSdchObserverOnNetworkThreadLegacyAPI( |
+ const GURL& target_url, |
+ URLRequestContextAdapter* context_adapter) { |
+ AddSdchObserverHelper(target_url, context_adapter->GetURLRequestContext()); |
+} |
+ |
+} // namespace |
+ |
+void AddSdchObserver(JNIEnv* env, |
+ jclass jcaller, |
+ jstring jtarget_url, |
+ jlong jadapter, |
+ jboolean jlegacy_api) { |
+ GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
+ if (jlegacy_api == JNI_TRUE) { |
mef
2015/05/21 17:29:44
I'd still argue that there is too little in common
xunjieli
2015/05/21 17:45:26
Done. Agreed.
|
+ URLRequestContextAdapter* context_adapter = |
+ reinterpret_cast<URLRequestContextAdapter*>(jadapter); |
+ context_adapter->PostTaskToNetworkThread( |
+ FROM_HERE, base::Bind(&AddSdchObserverOnNetworkThreadLegacyAPI, |
+ target_url, base::Unretained(context_adapter))); |
+ return; |
+ } |
+ CronetURLRequestContextAdapter* context_adapter = |
+ reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
+ context_adapter->PostTaskToNetworkThread( |
+ FROM_HERE, base::Bind(&AddSdchObserverOnNetworkThread, target_url, |
+ base::Unretained(context_adapter))); |
+} |
+ |
+bool RegisterSdchTestUtil(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+} // namespace cronet |