| Index: chrome/android/java/src/org/chromium/chrome/browser/cookies/CookiesFetcher.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/cookies/CookiesFetcher.java b/chrome/android/java/src/org/chromium/chrome/browser/cookies/CookiesFetcher.java
|
| index c5c42bf94afc5739a956e1a1b7466b1275832121..2d32d2b4f5002b549b798168062ecc2783942ae6 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/cookies/CookiesFetcher.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/cookies/CookiesFetcher.java
|
| @@ -15,19 +15,12 @@ import org.chromium.chrome.browser.profiles.Profile;
|
| import org.chromium.content.browser.crypto.CipherFactory;
|
| import org.chromium.content.common.CleanupReference;
|
|
|
| -import java.io.ByteArrayOutputStream;
|
| -import java.io.DataInputStream;
|
| -import java.io.DataOutputStream;
|
| -import java.io.EOFException;
|
| import java.io.File;
|
| -import java.io.FileInputStream;
|
| import java.io.IOException;
|
| import java.util.ArrayList;
|
| import java.util.List;
|
|
|
| import javax.crypto.Cipher;
|
| -import javax.crypto.CipherInputStream;
|
| -import javax.crypto.CipherOutputStream;
|
|
|
| /**
|
| * Responsible for fetching, (de)serializing, and restoring cookies between the CookieJar and an
|
| @@ -40,13 +33,6 @@ public class CookiesFetcher {
|
| /** Used for logging. */
|
| private static final String TAG = "CookiesFetcher";
|
|
|
| - /**
|
| - * Used to confirm that the current cipher key matches the previously used cipher key when
|
| - * restoring data. If this value cannot be read from the file, the file is likely garbage.
|
| - * TODO(acleung): May be use real cryptographic integrity checks on the whole file, instead.
|
| - */
|
| - private static final String MAGIC_STRING = "c0Ok135";
|
| -
|
| /** Native-side pointer. */
|
| private final long mNativeCookiesFetcher;
|
|
|
| @@ -119,50 +105,26 @@ public class CookiesFetcher {
|
| @Override
|
| protected List<CanonicalCookie> doInBackground(Void... voids) {
|
| // Read cookies from disk on a background thread to avoid strict mode violations.
|
| - ArrayList<CanonicalCookie> cookies = new ArrayList<CanonicalCookie>();
|
| - DataInputStream in = null;
|
| + List<CanonicalCookie> cookies = new ArrayList<CanonicalCookie>();
|
| try {
|
| Cipher cipher = CipherFactory.getInstance().getCipher(Cipher.DECRYPT_MODE);
|
| if (cipher == null) {
|
| // Something is wrong. Can't encrypt, don't restore cookies.
|
| return cookies;
|
| }
|
| +
|
| File fileIn = new File(fetchFileName(context));
|
| if (!fileIn.exists()) return cookies; // Nothing to read
|
| -
|
| - FileInputStream streamIn = new FileInputStream(fileIn);
|
| - in = new DataInputStream(new CipherInputStream(streamIn, cipher));
|
| - String check = in.readUTF();
|
| - if (!MAGIC_STRING.equals(check)) {
|
| - // Stale cookie file. Chrome might have crashed before it
|
| - // can delete the old file.
|
| - return cookies;
|
| - }
|
| - try {
|
| - while (true) {
|
| - CanonicalCookie cookie = CanonicalCookie.createFromStream(in);
|
| - cookies.add(cookie);
|
| - }
|
| - } catch (EOFException ignored) {
|
| - // We are done.
|
| - }
|
| + cookies = CanonicalCookie.readFromFileUnknownFormat(fileIn, cipher);
|
|
|
| // The Cookie File should not be restored again. It'll be overwritten
|
| // on the next onPause.
|
| scheduleDeleteCookiesFile(context);
|
|
|
| } catch (IOException e) {
|
| - Log.w(TAG, "IOException during Cookie Restore");
|
| + Log.w(TAG, "IOException during Cookie Restore", e);
|
| } catch (Throwable t) {
|
| Log.w(TAG, "Error restoring cookies.", t);
|
| - } finally {
|
| - try {
|
| - if (in != null) in.close();
|
| - } catch (IOException e) {
|
| - Log.w(TAG, "IOException during Cooke Restore");
|
| - } catch (Throwable t) {
|
| - Log.w(TAG, "Error restoring cookies.", t);
|
| - }
|
| }
|
| return cookies;
|
| }
|
| @@ -174,7 +136,7 @@ public class CookiesFetcher {
|
| nativeRestoreCookies(cookie.getUrl(), cookie.getName(), cookie.getValue(),
|
| cookie.getDomain(), cookie.getPath(), cookie.getCreationDate(),
|
| cookie.getExpirationDate(), cookie.getLastAccessDate(),
|
| - cookie.isSecure(), cookie.isHttpOnly(), cookie.isSameSite(),
|
| + cookie.isSecure(), cookie.isHttpOnly(), cookie.getSameSite(),
|
| cookie.getPriority());
|
| }
|
| }
|
| @@ -219,7 +181,7 @@ public class CookiesFetcher {
|
| @CalledByNative
|
| private CanonicalCookie createCookie(String url, String name, String value, String domain,
|
| String path, long creation, long expiration, long lastAccess, boolean secure,
|
| - boolean httpOnly, boolean sameSite, int priority) {
|
| + boolean httpOnly, int sameSite, int priority) {
|
| return new CanonicalCookie(url, name, value, domain, path, creation, expiration, lastAccess,
|
| secure, httpOnly, sameSite, priority);
|
| }
|
| @@ -239,7 +201,6 @@ public class CookiesFetcher {
|
| }
|
|
|
| private void saveFetchedCookiesToDisk(CanonicalCookie[] cookies) {
|
| - DataOutputStream out = null;
|
| try {
|
| Cipher cipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
|
| if (cipher == null) {
|
| @@ -247,29 +208,12 @@ public class CookiesFetcher {
|
| return;
|
| }
|
|
|
| - ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
| - CipherOutputStream cipherOut =
|
| - new CipherOutputStream(byteOut, cipher);
|
| - out = new DataOutputStream(cipherOut);
|
| -
|
| - out.writeUTF(MAGIC_STRING);
|
| - for (CanonicalCookie cookie : cookies) {
|
| - cookie.saveToStream(out);
|
| - }
|
| - out.close();
|
| ImportantFileWriterAndroid.writeFileAtomically(
|
| - fetchFileName(mContext), byteOut.toByteArray());
|
| - out = null;
|
| + fetchFileName(mContext), CanonicalCookie.writeToByteArray(cipher, cookies));
|
| } catch (IOException e) {
|
| Log.w(TAG, "IOException during Cookie Fetch");
|
| } catch (Throwable t) {
|
| Log.w(TAG, "Error storing cookies.", t);
|
| - } finally {
|
| - try {
|
| - if (out != null) out.close();
|
| - } catch (IOException e) {
|
| - Log.w(TAG, "IOException during Cookie Fetch");
|
| - }
|
| }
|
| }
|
|
|
| @@ -296,5 +240,5 @@ public class CookiesFetcher {
|
| private native void nativePersistCookies(long nativeCookiesFetcher);
|
| private static native void nativeRestoreCookies(String url, String name, String value,
|
| String domain, String path, long creation, long expiration, long lastAccess,
|
| - boolean secure, boolean httpOnly, boolean sameSite, int priority);
|
| + boolean secure, boolean httpOnly, int sameSite, int priority);
|
| }
|
|
|