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(net::SdchManager* manager, | |
| 30 const GURL& dictionary_url, | |
| 31 const std::string& server_hash) override { | |
| 32 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 33 Java_SdchTestUtil_onDictionaryAdded( | |
| 34 env, base::android::ConvertUTF8ToJavaString(env, dictionary_url.spec()) | |
| 35 .Release()); | |
| 36 } | |
| 37 | |
| 38 void OnDictionaryRemoved(net::SdchManager* manager, | |
| 39 const std::string& server_hash){}; | |
| 40 | |
| 41 void OnDictionaryUsed(net::SdchManager* manager, | |
| 42 const std::string& server_hash){}; | |
| 43 | |
| 44 void OnGetDictionary(net::SdchManager* manager, | |
| 45 const GURL& request_url, | |
| 46 const GURL& dictionary_url){}; | |
| 47 | |
| 48 void OnClearDictionaries(net::SdchManager* manager){}; | |
| 49 | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); | |
| 52 }; | |
| 53 | |
| 54 void AddRemoveSdchObserverHelper(net::URLRequestContext* url_request_context, | |
| 55 bool add) { | |
| 56 // Add a SdchObserver for testing. | |
| 57 if (add) { | |
| 58 DCHECK(!g_sdch_observer); | |
| 59 g_sdch_observer = new TestSdchObserver(); | |
| 60 url_request_context->sdch_manager()->AddObserver(g_sdch_observer); | |
| 61 } else { | |
| 62 DCHECK(g_sdch_observer); | |
| 63 url_request_context->sdch_manager()->RemoveObserver(g_sdch_observer); | |
| 64 g_sdch_observer = nullptr; | |
|
mef
2015/05/13 17:57:13
do we intentionally leak |g_sdch_observer| here?
xunjieli
2015/05/13 19:32:47
yes, the reason I did this is because we are requi
| |
| 65 } | |
| 66 | |
| 67 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 68 Java_SdchTestUtil_onAddRemoveSdchObserver(env); | |
| 69 } | |
| 70 | |
| 71 void AddRemoveSdchObserverOnNetworkThread( | |
| 72 CronetURLRequestContextAdapter* context_adapter, | |
| 73 bool add) { | |
| 74 AddRemoveSdchObserverHelper(context_adapter->GetURLRequestContext(), add); | |
| 75 } | |
| 76 | |
| 77 // TODO(xunjieli): Delete this once legacy API is removed. | |
| 78 void AddRemoveSdchObserverOnNetworkThreadLegacyAPI( | |
| 79 URLRequestContextAdapter* context_adapter, | |
| 80 bool add) { | |
| 81 AddRemoveSdchObserverHelper(context_adapter->GetURLRequestContext(), add); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 void AddRemoveSdchObserver(JNIEnv* env, | |
| 87 jclass jcaller, | |
| 88 jlong jadapter, | |
| 89 jboolean jlegacy_api, | |
| 90 jboolean add) { | |
| 91 if (jlegacy_api == JNI_TRUE) { | |
| 92 URLRequestContextAdapter* context_adapter = | |
| 93 reinterpret_cast<URLRequestContextAdapter*>(jadapter); | |
| 94 context_adapter->PostTaskToNetworkThread( | |
| 95 FROM_HERE, base::Bind(&AddRemoveSdchObserverOnNetworkThreadLegacyAPI, | |
| 96 base::Unretained(context_adapter), | |
| 97 add ? JNI_TRUE : JNI_FALSE)); | |
| 98 return; | |
| 99 } | |
| 100 CronetURLRequestContextAdapter* context_adapter = | |
| 101 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | |
| 102 context_adapter->PostTaskToNetworkThread( | |
| 103 FROM_HERE, base::Bind(&AddRemoveSdchObserverOnNetworkThread, | |
| 104 base::Unretained(context_adapter), | |
| 105 add ? JNI_TRUE : JNI_FALSE)); | |
| 106 } | |
| 107 | |
| 108 bool RegisterSdchTestUtil(JNIEnv* env) { | |
| 109 return RegisterNativesImpl(env); | |
| 110 } | |
| 111 | |
| 112 } // namespace cronet | |
| OLD | NEW |