Skip to content
Permalink
Browse files

Fix IllegalArgumentException when creating CookieManager

Summary:
This prevents this https://bugs.chromium.org/p/chromium/issues/detail?id=559720 to crash the app.
When using this fix the webView will not function correctly

More details about the bug https://bugs.chromium.org/p/chromium/issues/detail?id=559720

Reviewed By: fkgozali

Differential Revision: D14181851

fbshipit-source-id: 5a8c149df82a7373fe8b5b32006f034868532485
  • Loading branch information...
mdvacca authored and grabbou committed Feb 22, 2019
1 parent fbf039b commit fee503174856872e2cca1d37419eeaa64702795f
@@ -56,7 +56,10 @@ public ForwardingCookieHandler(ReactContext context) {
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> headers)
throws IOException {
String cookies = getCookieManager().getCookie(uri.toString());
CookieManager cookieManager = getCookieManager();
if (cookieManager == null) return Collections.emptyMap();

String cookies = cookieManager.getCookie(uri.toString());
if (TextUtils.isEmpty(cookies)) {
return Collections.emptyMap();
}
@@ -80,7 +83,10 @@ public void clearCookies(final Callback callback) {
new GuardedResultAsyncTask<Boolean>(mContext) {
@Override
protected Boolean doInBackgroundGuarded() {
getCookieManager().removeAllCookie();
CookieManager cookieManager = getCookieManager();
if (cookieManager != null) {
cookieManager.removeAllCookie();
}
mCookieSaver.onCookiesModified();
return true;
}
@@ -96,31 +102,40 @@ protected void onPostExecuteGuarded(Boolean result) {
}

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

public void destroy() {
if (USES_LEGACY_STORE) {
getCookieManager().removeExpiredCookie();
CookieManager cookieManager = getCookieManager();
if (cookieManager != null) {
cookieManager.removeExpiredCookie();
}
mCookieSaver.persistCookies();
}
}

private void addCookies(final String url, final List<String> cookies) {
final CookieManager cookieManager = getCookieManager();
if (cookieManager == null) return;

if (USES_LEGACY_STORE) {
runInBackground(
new Runnable() {
@Override
public void run() {
for (String cookie : cookies) {
getCookieManager().setCookie(url, cookie);
cookieManager.setCookie(url, cookie);
}
mCookieSaver.onCookiesModified();
}
@@ -129,14 +144,17 @@ public void run() {
for (String cookie : cookies) {
addCookieAsync(url, cookie);
}
getCookieManager().flush();
cookieManager.flush();
mCookieSaver.onCookiesModified();
}
}

@TargetApi(21)
private void addCookieAsync(String url, String cookie) {
getCookieManager().setCookie(url, cookie, null);
CookieManager cookieManager = getCookieManager();
if (cookieManager != null) {
cookieManager.setCookie(url, cookie, null);
}
}

private static boolean isCookieHeader(String name) {
@@ -156,10 +174,15 @@ protected void doInBackgroundGuarded(Void... params) {
* Instantiating CookieManager in KitKat+ 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 CookieManager getCookieManager() {
private @Nullable CookieManager getCookieManager() {
if (mCookieManager == null) {
possiblyWorkaroundSyncManager(mContext);
mCookieManager = CookieManager.getInstance();
try {
mCookieManager = CookieManager.getInstance();
} catch (IllegalArgumentException ex) {
// https://bugs.chromium.org/p/chromium/issues/detail?id=559720
return null;
}

if (USES_LEGACY_STORE) {
mCookieManager.removeExpiredCookie();
@@ -228,7 +251,10 @@ public void run() {

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

0 comments on commit fee5031

Please sign in to comment.
You can’t perform that action at this time.