From 549cc32703e1c0f51a25412cbae410b475cd5b01 Mon Sep 17 00:00:00 2001 From: davigonz Date: Fri, 28 Sep 2018 13:42:01 +0200 Subject: [PATCH] Make cookies handling more efficient --- .../android/lib/common/OwnCloudClient.java | 8 ++++++-- .../lib/common/SingleSessionManager.java | 18 ++++++++++++++++-- .../lib/common/operations/RemoteOperation.java | 1 - 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java index afc035c3..a6ed730b 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java @@ -316,8 +316,12 @@ public class OwnCloudClient extends HttpClient { public String getCookiesString() { String cookiesString = ""; - for (Cookie cookie : getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString()))) { - cookiesString += cookie.toString() + ";"; + List cookieList = getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString())); + + if (cookieList != null) { + for (Cookie cookie : cookieList) { + cookiesString += cookie.toString() + ";"; + } } return cookiesString; diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index 478490fd..7e3096c1 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -26,12 +26,14 @@ package com.owncloud.android.lib.common; import java.io.IOException; import java.util.Iterator; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import android.accounts.Account; +import android.accounts.AccountManager; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.content.Context; @@ -43,6 +45,8 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; import com.owncloud.android.lib.common.utils.Log_OC; +import okhttp3.Cookie; + /** * Implementation of {@link OwnCloudClientManager} * @@ -108,6 +112,8 @@ public class SingleSessionManager implements OwnCloudClientManager { reusingKnown = true; } + account.loadCredentials(context); + if (client == null) { // no client to reuse - create a new one client = OwnCloudClientFactory.createOwnCloudClient( @@ -121,7 +127,6 @@ public class SingleSessionManager implements OwnCloudClientManager { // enable cookie tracking AccountUtils.restoreCookies(account.getSavedAccount(), client, context); - account.loadCredentials(context); client.setCredentials(account.getCredentials()); if (accountName != null) { mClientsWithKnownUsername.put(accountName, client); @@ -140,6 +145,7 @@ public class SingleSessionManager implements OwnCloudClientManager { Log_OC.v(TAG, "reusing client for session " + sessionName); } keepCredentialsUpdated(account, client); + keepCookiesUpdated(context, account, client); keepUriUpdated(account, client); } @@ -213,7 +219,6 @@ public class SingleSessionManager implements OwnCloudClientManager { } } - private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { OwnCloudCredentials recentCredentials = account.getCredentials(); if (recentCredentials != null && !recentCredentials.getAuthToken().equals( @@ -222,6 +227,15 @@ public class SingleSessionManager implements OwnCloudClientManager { } } + private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) { + AccountManager am = AccountManager.get(context.getApplicationContext()); + String currentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES); + String previousCookies = reusedClient.getCookiesString(); + if (currentCookies != null && previousCookies != "" && !currentCookies.equals(previousCookies)) { + AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context); + } + } + // this method is just a patch; we need to distinguish accounts in the same host but // different paths; but that requires updating the accountNames for apps upgrading private void keepUriUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 0a1b67c7..2dc5487e 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -146,7 +146,6 @@ public abstract class RemoteOperation implements Runnable { mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). getClientFor(ocAccount, mContext); mClient.applyCredentials(); - mClient.applyCookies(); } else { throw new IllegalStateException("Trying to run a remote operation " + "asynchronously with no client and no chance to create one (no account)");