15
15
import android .os .Message ;
16
16
import android .text .TextUtils ;
17
17
import android .webkit .CookieManager ;
18
- import android .webkit .CookieSyncManager ;
19
18
import android .webkit .ValueCallback ;
20
19
import androidx .annotation .Nullable ;
21
20
import com .facebook .react .bridge .Callback ;
22
21
import com .facebook .react .bridge .GuardedAsyncTask ;
23
- import com .facebook .react .bridge .GuardedResultAsyncTask ;
24
22
import com .facebook .react .bridge .ReactContext ;
25
23
import java .io .IOException ;
26
24
import java .net .CookieHandler ;
@@ -40,10 +38,6 @@ public class ForwardingCookieHandler extends CookieHandler {
40
38
private static final String VERSION_ONE_HEADER = "Set-cookie2" ;
41
39
private static final String COOKIE_HEADER = "Cookie" ;
42
40
43
- // As CookieManager was synchronous before API 21 this class emulates the async behavior on < 21.
44
- private static final boolean USES_LEGACY_STORE =
45
- Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ;
46
-
47
41
private final CookieSaver mCookieSaver ;
48
42
private final ReactContext mContext ;
49
43
private @ Nullable CookieManager mCookieManager ;
@@ -79,26 +73,7 @@ public void put(URI uri, Map<String, List<String>> headers) throws IOException {
79
73
}
80
74
81
75
public void clearCookies (final Callback callback ) {
82
- if (USES_LEGACY_STORE ) {
83
- new GuardedResultAsyncTask <Boolean >(mContext ) {
84
- @ Override
85
- protected Boolean doInBackgroundGuarded () {
86
- CookieManager cookieManager = getCookieManager ();
87
- if (cookieManager != null ) {
88
- cookieManager .removeAllCookie ();
89
- }
90
- mCookieSaver .onCookiesModified ();
91
- return true ;
92
- }
93
-
94
- @ Override
95
- protected void onPostExecuteGuarded (Boolean result ) {
96
- callback .invoke (result );
97
- }
98
- }.execute ();
99
- } else {
100
- clearCookiesAsync (callback );
101
- }
76
+ clearCookiesAsync (callback );
102
77
}
103
78
104
79
@ TargetApi (Build .VERSION_CODES .LOLLIPOP )
@@ -116,38 +91,17 @@ public void onReceiveValue(Boolean value) {
116
91
}
117
92
}
118
93
119
- public void destroy () {
120
- if (USES_LEGACY_STORE ) {
121
- CookieManager cookieManager = getCookieManager ();
122
- if (cookieManager != null ) {
123
- cookieManager .removeExpiredCookie ();
124
- }
125
- mCookieSaver .persistCookies ();
126
- }
127
- }
94
+ public void destroy () {}
128
95
129
96
public void addCookies (final String url , final List <String > cookies ) {
130
97
final CookieManager cookieManager = getCookieManager ();
131
98
if (cookieManager == null ) return ;
132
99
133
- if (USES_LEGACY_STORE ) {
134
- runInBackground (
135
- new Runnable () {
136
- @ Override
137
- public void run () {
138
- for (String cookie : cookies ) {
139
- cookieManager .setCookie (url , cookie );
140
- }
141
- mCookieSaver .onCookiesModified ();
142
- }
143
- });
144
- } else {
145
- for (String cookie : cookies ) {
146
- addCookieAsync (url , cookie );
147
- }
148
- cookieManager .flush ();
149
- mCookieSaver .onCookiesModified ();
100
+ for (String cookie : cookies ) {
101
+ addCookieAsync (url , cookie );
150
102
}
103
+ cookieManager .flush ();
104
+ mCookieSaver .onCookiesModified ();
151
105
}
152
106
153
107
@ TargetApi (Build .VERSION_CODES .LOLLIPOP )
@@ -199,24 +153,12 @@ protected void doInBackgroundGuarded(Void... params) {
199
153
throw exception ;
200
154
}
201
155
}
202
-
203
- if (USES_LEGACY_STORE ) {
204
- mCookieManager .removeExpiredCookie ();
205
- }
206
156
}
207
157
208
158
return mCookieManager ;
209
159
}
210
160
211
- private static void possiblyWorkaroundSyncManager (Context context ) {
212
- if (USES_LEGACY_STORE ) {
213
- // This is to work around a bug where CookieManager may fail to instantiate if
214
- // CookieSyncManager has never been created. Note that the sync() may not be required but is
215
- // here of legacy reasons.
216
- CookieSyncManager syncManager = CookieSyncManager .createInstance (context );
217
- syncManager .sync ();
218
- }
219
- }
161
+ private static void possiblyWorkaroundSyncManager (Context context ) {}
220
162
221
163
/**
222
164
* Responsible for flushing cookies to disk. Flushes to disk with a maximum delay of 30 seconds.
@@ -246,24 +188,15 @@ public boolean handleMessage(Message msg) {
246
188
});
247
189
}
248
190
249
- public void onCookiesModified () {
250
- if (USES_LEGACY_STORE ) {
251
- mHandler .sendEmptyMessageDelayed (MSG_PERSIST_COOKIES , TIMEOUT );
252
- }
253
- }
191
+ public void onCookiesModified () {}
254
192
255
193
public void persistCookies () {
256
194
mHandler .removeMessages (MSG_PERSIST_COOKIES );
257
195
runInBackground (
258
196
new Runnable () {
259
197
@ Override
260
198
public void run () {
261
- if (USES_LEGACY_STORE ) {
262
- CookieSyncManager syncManager = CookieSyncManager .getInstance ();
263
- syncManager .sync ();
264
- } else {
265
- flush ();
266
- }
199
+ flush ();
267
200
}
268
201
});
269
202
}
0 commit comments