Skip to content

Commit

Permalink
Remove unused CookieHandler from ForwardingCookieHandler (#37775)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37775

Most of this code was made irrelevant when we dropped support for API < 21 in D24548084.

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D46557892

fbshipit-source-id: 6af85a24d476fe1010eb62126b4c5e9df14a0ea2
  • Loading branch information
javache authored and facebook-github-bot committed Jun 13, 2023
1 parent efc5f73 commit 3006a33
Showing 1 changed file with 1 addition and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@

package com.facebook.react.modules.network;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.GuardedAsyncTask;
import com.facebook.react.bridge.ReactContext;
import java.io.IOException;
import java.net.CookieHandler;
Expand All @@ -36,13 +30,11 @@ public class ForwardingCookieHandler extends CookieHandler {
private static final String VERSION_ONE_HEADER = "Set-cookie2";
private static final String COOKIE_HEADER = "Cookie";

private final CookieSaver mCookieSaver;
private final ReactContext mContext;
private @Nullable CookieManager mCookieManager;

public ForwardingCookieHandler(ReactContext context) {
mContext = context;
mCookieSaver = new CookieSaver();
}

@Override
Expand Down Expand Up @@ -71,20 +63,9 @@ public void put(URI uri, Map<String, List<String>> headers) throws IOException {
}

public void clearCookies(final Callback callback) {
clearCookiesAsync(callback);
}

private void clearCookiesAsync(final Callback callback) {
CookieManager cookieManager = getCookieManager();
if (cookieManager != null) {
cookieManager.removeAllCookies(
new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean value) {
mCookieSaver.onCookiesModified();
callback.invoke(value);
}
});
cookieManager.removeAllCookies(value -> callback.invoke(value));
}
}

Expand All @@ -98,7 +79,6 @@ public void addCookies(final String url, final List<String> cookies) {
addCookieAsync(url, cookie);
}
cookieManager.flush();
mCookieSaver.onCookiesModified();
}

private void addCookieAsync(String url, String cookie) {
Expand All @@ -112,22 +92,12 @@ private static boolean isCookieHeader(String name) {
return name.equalsIgnoreCase(VERSION_ZERO_HEADER) || name.equalsIgnoreCase(VERSION_ONE_HEADER);
}

private void runInBackground(final Runnable runnable) {
new GuardedAsyncTask<Void, Void>(mContext) {
@Override
protected void doInBackgroundGuarded(Void... params) {
runnable.run();
}
}.execute();
}

/**
* Instantiating CookieManager will load the Chromium task taking a 100ish ms so we do it lazily
* to make sure it's done on a background thread as needed.
*/
private @Nullable CookieManager getCookieManager() {
if (mCookieManager == null) {
possiblyWorkaroundSyncManager(mContext);
try {
mCookieManager = CookieManager.getInstance();
} catch (IllegalArgumentException ex) {
Expand All @@ -151,55 +121,4 @@ protected void doInBackgroundGuarded(Void... params) {

return mCookieManager;
}

private static void possiblyWorkaroundSyncManager(Context context) {}

/**
* Responsible for flushing cookies to disk. Flushes to disk with a maximum delay of 30 seconds.
* This class is only active if we are on API < 21.
*/
private class CookieSaver {
private static final int MSG_PERSIST_COOKIES = 1;

private static final int TIMEOUT = 30 * 1000; // 30 seconds

private final Handler mHandler;

public CookieSaver() {
mHandler =
new Handler(
Looper.getMainLooper(),
new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == MSG_PERSIST_COOKIES) {
persistCookies();
return true;
} else {
return false;
}
}
});
}

public void onCookiesModified() {}

public void persistCookies() {
mHandler.removeMessages(MSG_PERSIST_COOKIES);
runInBackground(
new Runnable() {
@Override
public void run() {
flush();
}
});
}

private void flush() {
CookieManager cookieManager = getCookieManager();
if (cookieManager != null) {
cookieManager.flush();
}
}
}
}

0 comments on commit 3006a33

Please sign in to comment.