diff options
| author | Robin Humble <plaguedbypenguins@gmail.com> | 2014-06-08 07:18:55 +0200 |
|---|---|---|
| committer | LorDClockaN <davor@losinj.com> | 2014-06-08 07:19:22 +0200 |
| commit | e4cb677f406fd7bedd5b3ee7ff6e249c2b6eb68f (patch) | |
| tree | 415683915b8ac310fc77dd25a234b17916c6875e /src/com/android/browser/BrowserSettings.java | |
| parent | 7fa622f559cfb6a504b281c4cb56077683d96d63 (diff) | |
Optionally delete unwanted cookies (and localstorage) at every Browser
resume. The default for this feature is off - ie. maintain the current
Browser "keep every cookie" behaviour.
Optionally localstorage files (site databases) are also removed whenever
cookies have been deleted. This helps to reduce evercookie/supercookie
persistence.
A whitelist of sites that are permitted to keep cookies is stored in the
standard Browser shared_prefs. The site's cookie preference is set via a
menu checkbox when viewing the page. This allows opt-in whitelisting
behaviour on a per-site basis, suitable for saving eg. login cookies.
The cookie deletion itself is done by using existing API's to delete all
cookies and then selectively restore just those from the whitelisted sites.
Cookie counting is the only new API needed by this patch, and is used to
eliminate unnecessary cookie and localstorage deletes.
Although simplistic, onResume cookie filtering seems to work well and in
testing hasn't broken any web browsing. The underlying CookieMonster
functions operate on cached copies in ram and are asynchronous to disk so
there should be little or no measurable performance impact on browsing from
cookies. localstorage deletion is not cached by any layer so, if enabled,
might have some minor performance impact.
Change-Id: I55c69292a5ddc460e0e50b340dc4330c28becc5e
Diffstat (limited to 'src/com/android/browser/BrowserSettings.java')
| -rw-r--r-- | src/com/android/browser/BrowserSettings.java | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java index 24cc0766..fe916564 100644 --- a/src/com/android/browser/BrowserSettings.java +++ b/src/com/android/browser/BrowserSettings.java @@ -51,6 +51,14 @@ import java.lang.ref.WeakReference; import java.util.Iterator; import java.util.LinkedList; import java.util.WeakHashMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.Arrays; + +import android.util.Log; +import android.net.WebAddress; +import android.widget.Toast; /** * Class for managing settings @@ -58,6 +66,8 @@ import java.util.WeakHashMap; public class BrowserSettings implements OnSharedPreferenceChangeListener, PreferenceKeys { + private final static String TAG = "BrowserSettings"; + // TODO: Do something with this UserAgent stuff private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " + "Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + @@ -502,6 +512,151 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, } } + private Set<String> getCookieWhitelist() { + requireInitialization(); + return mPrefs.getStringSet("cookies_whitelist", new HashSet<String>()); + } + + private void setCookieWhitelist(Set<String> wl) { + mPrefs.edit().putStringSet("cookies_whitelist", wl).apply(); + } + + private boolean addToCookieWhitelist(String s) { + Set<String> wl = getCookieWhitelist(); + boolean ok = wl.add(s); + setCookieWhitelist(wl); + return ok; + } + + private boolean removeFromCookieWhitelist(String s) { + Set<String> wl = getCookieWhitelist(); + boolean ok = wl.remove(s); + setCookieWhitelist(wl); + return ok; + } + + public boolean hasCookiesWhitelisted(WebView view) { + if (view == null) { + return false; + } + String url = view.getUrl(); + if (url == null) { // page hasn't finished loading yet + return false; + } + String host = new WebAddress(url).getHost(); + Set<String> wl = getCookieWhitelist(); + return wl.contains(host); + } + + public void toggleCookiesWhitelisted(WebView view) { + if (view == null) { + return; + } + if (!enableDeleteCookies()) { + return; + } + + String url = view.getUrl(); + if (url == null) { // page hasn't finished loading yet + return; + } + String host = new WebAddress(url).getHost(); + if (hasCookiesWhitelisted(view)) { + removeFromCookieWhitelist(host); + clearCookiesExceptWhitelist(); + } + else { + addToCookieWhitelist(host); + } + } + + public void clearCookiesExceptWhitelist() { + if (!enableDeleteCookies()) { + return; + } + + Set<String> domains = getCookieWhitelist(); + HashMap cookies = new HashMap(); + + // look for both domain and host cookies (domain cookies have a '.' prefix). + // query by https as this returns both http and https cookies. + String[] prefixes = { "https://.", "https://" }; + + // find all cookies in the whitelist + int savedCookies = 0; + for (String domain : domains) { + for (int p = 0; p < 2; p++) { + String url = prefixes[p] + domain; + String c = CookieManager.getInstance().getCookie(url); + if (c != null && !(c.equals("") || c.equals(null))) { + String[] cc = c.split(";"); // split into individual cookies + for (int i = 0; i < cc.length; i++) // get rid of leading blanks + cc[i] = cc[i].trim(); + Set<String> ccUniq = new HashSet<String>(Arrays.asList(cc)); + if (p == 1) { // remove domain cookies duplicated to the host + String urlDom = prefixes[0] + domain; + if (cookies.containsKey(urlDom)) { + Set<String> ccDom = (HashSet<String>)cookies.get(urlDom); + ccUniq.removeAll(ccDom); + } + } + savedCookies += ccUniq.size(); + cookies.put(url, ccUniq); + } + } + } + + int cookiesBefore = CookieManager.getInstance().countCookies(); + if (savedCookies == cookiesBefore) { + // all cookies are whitelisted cookies. our job is done. + return; + } + + // delete all cookies + CookieManager.getInstance().removeAllCookie(); + + // re-add all the whitelisted cookies + for (String domain : domains) { + for (String prefix : prefixes) { + String url = prefix + domain; + if (cookies.containsKey(url)) { + Set<String> cc = (HashSet<String>)cookies.get(url); + for (String i : cc) { + CookieManager.getInstance().setCookie(url, i + ";"); + } + } + } + } + + int cookiesAfter = CookieManager.getInstance().countCookies(); + int cookiesDeleted = cookiesBefore - cookiesAfter; + boolean munched = cookiesDeleted > 0; + + if (!munched) { // no cookies were deleted + return; + } + + // optionally clear all localstorage too + if (enableDeleteLocaldata()) { + clearDatabases(); + } + + Log.d(TAG, "clearCookiesExceptWhitelist: " + + cookiesDeleted + " cookies deleted, " + + cookiesAfter + " cookies remain"); + + // toast if we have munched cookies + if (munchToast()) { + String deleted_str = mContext.getResources() + .getString(R.string.cookies_deleted_info); + String remain_str = mContext.getResources() + .getString(R.string.cookies_remain_info); + CharSequence text = "" + cookiesDeleted + " " + deleted_str + + "\n" + cookiesAfter + " " + remain_str; + Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show(); + } + } + public static int getAdjustedMinimumFontSize(int rawValue) { rawValue++; // Preference starts at 0, min font at 1 if (rawValue > 1) { @@ -809,6 +964,20 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true); } + public boolean enableDeleteCookies() { + int value = Integer.valueOf(mPrefs.getString(PREF_SITE_WHITELIST_COOKIES, "0")); + return value != 0; + } + + public boolean enableDeleteLocaldata() { + int value = Integer.valueOf(mPrefs.getString(PREF_SITE_WHITELIST_COOKIES, "0")); + return value == 2; + } + + public boolean munchToast() { + return mPrefs.getBoolean(PREF_SITE_WHITELIST_COOKIES_VERBOSE, false); + } + public boolean saveFormdata() { return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true); } |
