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 .Release()); | |
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_onAddSdchObserverCompleted(env, JNI_FALSE); | |
61 return; | |
62 } | |
63 | |
64 url_request_context->sdch_manager()->AddObserver( | |
65 new TestSdchObserver(url_request_context->sdch_manager())); | |
66 Java_SdchTestUtil_onAddSdchObserverCompleted(env, JNI_TRUE); | |
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 jboolean jlegacy_api) { | |
89 GURL target_url(base::android::ConvertJavaStringToUTF8(env, jtarget_url)); | |
90 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.
| |
91 URLRequestContextAdapter* context_adapter = | |
92 reinterpret_cast<URLRequestContextAdapter*>(jadapter); | |
93 context_adapter->PostTaskToNetworkThread( | |
94 FROM_HERE, base::Bind(&AddSdchObserverOnNetworkThreadLegacyAPI, | |
95 target_url, base::Unretained(context_adapter))); | |
96 return; | |
97 } | |
98 CronetURLRequestContextAdapter* context_adapter = | |
99 reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter); | |
100 context_adapter->PostTaskToNetworkThread( | |
101 FROM_HERE, base::Bind(&AddSdchObserverOnNetworkThread, target_url, | |
102 base::Unretained(context_adapter))); | |
103 } | |
104 | |
105 bool RegisterSdchTestUtil(JNIEnv* env) { | |
106 return RegisterNativesImpl(env); | |
107 } | |
108 | |
109 } // namespace cronet | |
OLD | NEW |