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.
5 #include "sdch_test_util.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h"
13 #include "base/macros.h"
14 #include "components/cronet/android/cronet_url_request_context_adapter.h"
15 #include "components/cronet/android/url_request_context_adapter.h"
16 #include "jni/SdchObserver_jni.h"
17 #include "net/base/sdch_manager.h"
18 #include "net/base/sdch_observer.h"
19 #include "net/url_request/url_request_context.h"
26 class TestSdchObserver
: public net::SdchObserver
{
29 const GURL
& target_url
,
30 net::SdchManager
* manager
,
31 const base::android::ScopedJavaGlobalRef
<jobject
>& jsdch_observer_ref
)
32 : target_url_(target_url
), manager_(manager
) {
33 jsdch_observer_ref_
.Reset(jsdch_observer_ref
);
36 // SdchObserver implementation
37 void OnDictionaryAdded(const GURL
& dictionary_url
,
38 const std::string
& server_hash
) override
{
39 // Only notify if the dictionary for the |target_url_| has been added.
40 if (manager_
->GetDictionarySet(target_url_
)) {
41 JNIEnv
* env
= base::android::AttachCurrentThread();
42 Java_SdchObserver_onDictionaryAdded(env
, jsdch_observer_ref_
.obj());
43 manager_
->RemoveObserver(this);
48 void OnDictionaryRemoved(const std::string
& server_hash
) override
{}
50 void OnDictionaryUsed(const std::string
& server_hash
) override
{}
52 void OnGetDictionary(const GURL
& request_url
,
53 const GURL
& dictionary_url
) override
{}
55 void OnClearDictionaries() override
{}
59 net::SdchManager
* manager_
;
60 base::android::ScopedJavaGlobalRef
<jobject
> jsdch_observer_ref_
;
62 DISALLOW_COPY_AND_ASSIGN(TestSdchObserver
);
65 void AddSdchObserverHelper(
66 const GURL
& target_url
,
67 const base::android::ScopedJavaGlobalRef
<jobject
>& jsdch_observer_ref
,
68 net::URLRequestContext
* url_request_context
) {
69 JNIEnv
* env
= base::android::AttachCurrentThread();
70 // If dictionaries for |target_url| are already added, skip adding the
72 if (url_request_context
->sdch_manager()->GetDictionarySet(target_url
)) {
73 Java_SdchObserver_onDictionarySetAlreadyPresent(env
,
74 jsdch_observer_ref
.obj());
78 url_request_context
->sdch_manager()->AddObserver(new TestSdchObserver(
79 target_url
, url_request_context
->sdch_manager(), jsdch_observer_ref
));
80 Java_SdchObserver_onAddSdchObserverCompleted(env
, jsdch_observer_ref
.obj());
83 void AddSdchObserverOnNetworkThread(
84 const GURL
& target_url
,
85 const base::android::ScopedJavaGlobalRef
<jobject
>& jsdch_observer_ref
,
86 CronetURLRequestContextAdapter
* context_adapter
) {
87 AddSdchObserverHelper(target_url
, jsdch_observer_ref
,
88 context_adapter
->GetURLRequestContext());
91 // TODO(xunjieli): Delete this once legacy API is removed.
92 void AddSdchObserverOnNetworkThreadLegacyAPI(
93 const GURL
& target_url
,
94 const base::android::ScopedJavaGlobalRef
<jobject
>& jsdch_observer_ref
,
95 URLRequestContextAdapter
* context_adapter
) {
96 AddSdchObserverHelper(target_url
, jsdch_observer_ref
,
97 context_adapter
->GetURLRequestContext());
102 void AddSdchObserver(JNIEnv
* env
,
103 const JavaParamRef
<jobject
>& jsdch_observer
,
104 const JavaParamRef
<jstring
>& jtarget_url
,
106 base::android::ScopedJavaGlobalRef
<jobject
> jsdch_observer_ref
;
107 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to
108 // use it across threads. |AddSdchObserverHelper| will acquire a new
109 // JNIEnv before calling into Java.
110 jsdch_observer_ref
.Reset(env
, jsdch_observer
);
112 GURL
target_url(base::android::ConvertJavaStringToUTF8(env
, jtarget_url
));
113 CronetURLRequestContextAdapter
* context_adapter
=
114 reinterpret_cast<CronetURLRequestContextAdapter
*>(jadapter
);
115 context_adapter
->PostTaskToNetworkThread(
117 base::Bind(&AddSdchObserverOnNetworkThread
, target_url
,
118 jsdch_observer_ref
, base::Unretained(context_adapter
)));
121 void AddSdchObserverLegacyAPI(JNIEnv
* env
,
122 const JavaParamRef
<jobject
>& jsdch_observer
,
123 const JavaParamRef
<jstring
>& jtarget_url
,
125 base::android::ScopedJavaGlobalRef
<jobject
> jsdch_observer_ref
;
126 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to
127 // use it across threads. |AddSdchObserverHelper| will acquire a new
128 // JNIEnv before calling into Java.
129 jsdch_observer_ref
.Reset(env
, jsdch_observer
);
130 GURL
target_url(base::android::ConvertJavaStringToUTF8(env
, jtarget_url
));
131 URLRequestContextAdapter
* context_adapter
=
132 reinterpret_cast<URLRequestContextAdapter
*>(jadapter
);
133 context_adapter
->PostTaskToNetworkThread(
135 base::Bind(&AddSdchObserverOnNetworkThreadLegacyAPI
, target_url
,
136 jsdch_observer_ref
, base::Unretained(context_adapter
)));
139 bool RegisterSdchTestUtil(JNIEnv
* env
) {
140 return RegisterNativesImpl(env
);
143 } // namespace cronet