Skip to content

Commit

Permalink
Fix IllegalArgumentException when creating CookieManager
Browse files Browse the repository at this point in the history
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 facebook-github-bot committed Feb 22, 2019
1 parent e38be82 commit cda8171
Showing 1 changed file with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
}
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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 cda8171

Please sign in to comment.