Chromium Code Reviews| 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 "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
| 12 #include "components/cronet/android/url_request_context_adapter.h" | |
| 13 #include "jni/SdchTestUtil_jni.h" | |
| 14 #include "net/base/sdch_manager.h" | |
| 15 #include "net/base/sdch_observer.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace cronet { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 net::SdchObserver* g_sdch_observer = nullptr; | |
| 23 | |
| 24 class TestSdchObserver : public net::SdchObserver { | |
| 25 public: | |
| 26 TestSdchObserver() {} | |
| 27 | |
| 28 // SdchObserver implementation | |
| 29 void OnDictionaryAdded(const GURL& dictionary_url, | |
| 30 const std::string& server_hash) override { | |
| 31 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 32 Java_SdchTestUtil_onDictionaryAdded( | |
| 33 env, base::android::ConvertUTF8ToJavaString(env, dictionary_url.spec()) | |
| 34 .Release()); | |
| 35 } | |
| 36 | |
| 37 void OnDictionaryRemoved(const std::string& server_hash) override{}; | |
| 38 | |
| 39 void OnDictionaryUsed(const std::string& server_hash) override{}; | |
| 40 | |
| 41 void OnGetDictionary(const GURL& request_url, | |
| 42 const GURL& dictionary_url) override{}; | |
| 43 | |
| 44 void OnClearDictionaries() override{}; | |
| 45 | |
| 46 private: | |
| 47 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); | |
| 48 }; | |
| 49 | |
| 50 void AddRemoveSdchObserverHelper(net::URLRequestContext* url_request_context, | |
| 51 bool add) { | |
| 52 // Add a SdchObserver for testing. | |
| 53 if (add) { | |
| 54 DCHECK(!g_sdch_observer); | |
| 55 g_sdch_observer = new TestSdchObserver(); | |
| 56 url_request_context->sdch_manager()->AddObserver(g_sdch_observer); | |
| 57 } else { | |
| 58 DCHECK(g_sdch_observer); | |
| 59 url_request_context->sdch_manager()->RemoveObserver(g_sdch_observer); | |
| 60 g_sdch_observer = nullptr; | |
| 61 } | |
| 62 | |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 64 Java_SdchTestUtil_onAddRemoveSdchObserver(env); | |
| 65 } | |
| 66 | |
| 67 void AddRemoveSdchObserverOnNetworkThread( | |
| 68 CronetURLRequestContextAdapter* context_adapter, | |
| 69 bool add) { | |
| 70 AddRemoveSdchObserverHelper(context_adapter->GetURLRequestContext(), add); | |
| 71 } | |
| 72 | |
| 73 // TODO(xunjieli): Delete this once legacy API is removed. | |
| 74 void AddRemoveSdchObserverOnNetworkThreadLegacyAPI( | |
| 75 URLRequestContextAdapter* context_adapter, | |
| 76 bool add) { | |
| 77 AddRemoveSdchObserverHelper(context_adapter->GetURLRequestContext(), add); | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 void AddRemoveSdchObserver(JNIEnv* env, | |
|
mef
2015/05/15 14:30:39
Would it be cleaner to just have 4 separate method
xunjieli
2015/05/15 16:28:04
I tried that before. But having 4 separate methods
| |
| 83 jclass jcaller, | |
| 84 jlong jadapter, | |
| 85 jboolean jlegacy_api, | |
| 86 jboolean add) { | |
| 87 if (jlegacy_api == JNI_TRUE) { | |
| 88 URLRequestContextAdapter* context_adapter = | |
| 89 reinterpret_cast<URLRequestContextAdapter*>(jadapter); | |
| 90 context_adapter->PostTaskToNetworkThread( | |
| 91 FROM_HERE, base::Bind(&AddRemoveSdchObserverOnNetworkThreadLegacyAPI, | |
| 92 base::Unretained(context_adapter), | |
| 93 add ? JNI_TRUE : JNI_FALSE)); | |
| 94 return; | |
| 95 } | |
| 96 CronetURLRequestContextAdapter* context_adapter = | |
| 97 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | |
| 98 context_adapter->PostTaskToNetworkThread( | |
| 99 FROM_HERE, base::Bind(&AddRemoveSdchObserverOnNetworkThread, | |
| 100 base::Unretained(context_adapter), | |
| 101 add ? JNI_TRUE : JNI_FALSE)); | |
| 102 } | |
| 103 | |
| 104 bool RegisterSdchTestUtil(JNIEnv* env) { | |
| 105 return RegisterNativesImpl(env); | |
| 106 } | |
| 107 | |
| 108 } // namespace cronet | |
| OLD | NEW |