1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +00:00

Make cookies handling more efficient

This commit is contained in:
davigonz 2018-09-28 13:42:01 +02:00
parent f2ccc62b88
commit 549cc32703
3 changed files with 22 additions and 5 deletions

View File

@ -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<Cookie> cookieList = getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString()));
if (cookieList != null) {
for (Cookie cookie : cookieList) {
cookiesString += cookie.toString() + ";";
}
}
return cookiesString;

View File

@ -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) {

View File

@ -146,7 +146,6 @@ public abstract class RemoteOperation<T extends Object> 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)");