| 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/bind.h" |
| 12 #include "base/macros.h" |
| 13 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 14 #include "components/cronet/android/url_request_context_adapter.h" |
| 15 #include "jni/SdchTestUtil_jni.h" |
| 16 #include "net/base/sdch_manager.h" |
| 17 #include "net/base/sdch_observer.h" |
| 18 #include "net/url_request/url_request_context.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace cronet { |
| 22 |
| 23 namespace { |
| 24 |
| 25 class TestSdchObserver : public net::SdchObserver { |
| 26 public: |
| 27 TestSdchObserver(net::SdchManager* manager) : manager_(manager) {} |
| 28 |
| 29 // SdchObserver implementation |
| 30 void OnDictionaryAdded(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 .obj()); |
| 36 manager_->RemoveObserver(this); |
| 37 delete this; |
| 38 } |
| 39 |
| 40 void OnDictionaryRemoved(const std::string& server_hash) override {} |
| 41 |
| 42 void OnDictionaryUsed(const std::string& server_hash) override {} |
| 43 |
| 44 void OnGetDictionary(const GURL& request_url, |
| 45 const GURL& dictionary_url) override {} |
| 46 |
| 47 void OnClearDictionaries() override {} |
| 48 |
| 49 private: |
| 50 net::SdchManager* manager_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver); |
| 53 }; |
| 54 |
| 55 void AddSdchObserverHelper(const GURL& target_url, |
| 56 net::URLRequestContext* url_request_context) { |
| 57 JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 // If dictionaries are already added, skip adding the observer. |
| 59 if (url_request_context->sdch_manager()->GetDictionarySet(target_url)) { |
| 60 Java_SdchTestUtil_onDictionaryAlreadyPresent(env); |
| 61 return; |
| 62 } |
| 63 |
| 64 url_request_context->sdch_manager()->AddObserver( |
| 65 new TestSdchObserver(url_request_context->sdch_manager())); |
| 66 Java_SdchTestUtil_onAddSdchObserverCompleted(env); |
| 67 } |
| 68 |
| 69 void AddSdchObserverOnNetworkThread( |
| 70 const GURL& target_url, |
| 71 CronetURLRequestContextAdapter* context_adapter) { |
| 72 AddSdchObserverHelper(target_url, context_adapter->GetURLRequestContext()); |
| 73 } |
| 74 |
| 75 // TODO(xunjieli): Delete this once legacy API is removed. |
| 76 void AddSdchObserverOnNetworkThreadLegacyAPI( |
| 77 const GURL& target_url, |
| 78 URLRequestContextAdapter* context_adapter) { |
| 79 AddSdchObserverHelper(target_url, context_adapter->GetURLRequestContext()); |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
| 84 void AddSdchObserver(JNIEnv* env, |
| 85 jclass jcaller, |
| 86 jstring jtarget_url, |
| 87 jlong jadapter) { |
| 88 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
| 89 CronetURLRequestContextAdapter* context_adapter = |
| 90 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); |
| 91 context_adapter->PostTaskToNetworkThread( |
| 92 FROM_HERE, base::Bind(&AddSdchObserverOnNetworkThread, target_url, |
| 93 base::Unretained(context_adapter))); |
| 94 } |
| 95 |
| 96 void AddSdchObserverLegacyAPI(JNIEnv* env, |
| 97 jclass jcaller, |
| 98 jstring jtarget_url, |
| 99 jlong jadapter) { |
| 100 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); |
| 101 URLRequestContextAdapter* context_adapter = |
| 102 reinterpret_cast<URLRequestContextAdapter*>(jadapter); |
| 103 context_adapter->PostTaskToNetworkThread( |
| 104 FROM_HERE, base::Bind(&AddSdchObserverOnNetworkThreadLegacyAPI, |
| 105 target_url, base::Unretained(context_adapter))); |
| 106 } |
| 107 |
| 108 bool RegisterSdchTestUtil(JNIEnv* env) { |
| 109 return RegisterNativesImpl(env); |
| 110 } |
| 111 |
| 112 } // namespace cronet |
| OLD | NEW |