Skip to content

Commit

Permalink
Make extra caching explicit, fix clearing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Feb 19, 2014
1 parent 291cb93 commit 6d10af6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
8 changes: 6 additions & 2 deletions src/biz/bokhorst/xprivacy/CRestriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public CRestriction(int uid, String restrictionName, String methodName, String e
asked = false;
}

public CRestriction(PRestriction restriction) {
public CRestriction(PRestriction restriction, String extra) {
mExpiry = new Date().getTime() + PrivacyManager.cRestrictionCacheTimeoutMs;
mUid = restriction.uid;
mRestrictionName = restriction.restrictionName;
mMethodName = restriction.methodName;
mExtra = restriction.extra;
mExtra = extra;
restricted = restriction.restricted;
asked = restriction.asked;
}
Expand All @@ -39,6 +39,10 @@ public boolean isExpired() {
return (new Date().getTime() > mExpiry);
}

public int getUid() {
return mUid;
}

public String getRestrictionName() {
return mRestrictionName;
}
Expand Down
2 changes: 2 additions & 0 deletions src/biz/bokhorst/xprivacy/PRestriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class PRestriction implements Parcelable {
public boolean asked;
public long time;

// The extra is never needed in the result

public PRestriction() {
}

Expand Down
47 changes: 23 additions & 24 deletions src/biz/bokhorst/xprivacy/PrivacyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static PSetting getSetting(PSetting setting) throws RemoteException {
private boolean mSelectOnce = false;

private Map<CSetting, CSetting> mSettingCache = new HashMap<CSetting, CSetting>();
private Map<CRestriction, CRestriction> mAskedCache = new HashMap<CRestriction, CRestriction>();
private Map<CRestriction, CRestriction> mAskedOnceCache = new HashMap<CRestriction, CRestriction>();
private Map<CRestriction, CRestriction> mRestrictionCache = new HashMap<CRestriction, CRestriction>();

private ExecutorService mExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Expand Down Expand Up @@ -369,15 +369,19 @@ private void setRestrictionInternal(PRestriction restriction) throws RemoteExcep
// Update cache
if (mUseCache)
synchronized (mRestrictionCache) {
// Clear cache
if (restriction.restrictionName != null)
if (restriction.methodName == null) {
// Clear cache
for (CRestriction key : new ArrayList<CRestriction>(mRestrictionCache.keySet()))
if (restriction.restrictionName.equals(key.getRestrictionName()))
if (restriction.uid == key.getUid()
&& restriction.restrictionName.equals(key.getRestrictionName()))
mRestrictionCache.remove(key);

// Update cache
CRestriction key = new CRestriction(restriction);
mRestrictionCache.put(key, key);
} else {
// Update cache
CRestriction key = new CRestriction(restriction, restriction.extra);
if (mRestrictionCache.containsKey(key))
mRestrictionCache.remove(key);
mRestrictionCache.put(key, key);
}
}
} catch (Throwable ex) {
Util.bug(null, ex);
Expand Down Expand Up @@ -446,7 +450,7 @@ else if (PrivacyManager.cView.equals(restriction.restrictionName))
// Check cache
boolean cached = false;
if (mUseCache) {
CRestriction key = new CRestriction(restriction);
CRestriction key = new CRestriction(restriction, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key)) {
cached = true;
Expand Down Expand Up @@ -538,16 +542,11 @@ else if (PrivacyManager.cView.equals(restriction.restrictionName))

// Update cache
if (mUseCache) {
// The category is cached for on demand restricting
CRestriction ckey = new CRestriction(cresult);
CRestriction mkey = new CRestriction(mresult);
CRestriction key = new CRestriction(mresult, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(ckey))
mRestrictionCache.remove(ckey);
mRestrictionCache.put(ckey, ckey);
if (mRestrictionCache.containsKey(mkey))
mRestrictionCache.remove(mkey);
mRestrictionCache.put(mkey, mkey);
if (mRestrictionCache.containsKey(key))
mRestrictionCache.remove(key);
mRestrictionCache.put(key, key);
}
}
}
Expand Down Expand Up @@ -1145,15 +1144,15 @@ private void onDemandDialog(final Hook hook, final PRestriction restriction, fin
Util.log(null, Log.WARN, "On demanding " + restriction);

// Check if not asked before
CRestriction key = new CRestriction(restriction);
CRestriction key = new CRestriction(restriction, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key))
if (mRestrictionCache.get(key).asked) {
Util.log(null, Log.WARN, "Already asked " + restriction);
return;
}
}
if (mAskedCache.containsKey(key) && !mAskedCache.get(key).isExpired()) {
if (mAskedOnceCache.containsKey(key) && !mAskedOnceCache.get(key).isExpired()) {
Util.log(null, Log.WARN, "Already asked once " + restriction);
return;
}
Expand Down Expand Up @@ -1461,10 +1460,10 @@ public void onClick(DialogInterface dialog, int which) {
private void onDemandOnce(final PRestriction restriction, final PRestriction result) {
Util.log(null, Log.WARN, (result.restricted ? "Deny" : "Allow") + " once " + restriction);
result.time = new Date().getTime() + PrivacyManager.cRestrictionCacheTimeoutMs;
CRestriction key = new CRestriction(restriction);
if (mAskedCache.containsKey(key))
mAskedCache.remove(key);
mAskedCache.put(key, key);
CRestriction key = new CRestriction(restriction, restriction.extra);
if (mAskedOnceCache.containsKey(key))
mAskedOnceCache.remove(key);
mAskedOnceCache.put(key, key);
}

private void onDemandChoice(PRestriction restriction, boolean category, boolean restrict) {
Expand Down

0 comments on commit 6d10af6

Please sign in to comment.