Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Enhanced Tracking Protection
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Mar 24, 2020
1 parent 8142076 commit 669e3d4
Show file tree
Hide file tree
Showing 38 changed files with 1,021 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ protected void onDestroy() {

SessionStore.get().onDestroy();


super.onDestroy();
mLifeCycle.setCurrentState(Lifecycle.State.DESTROYED);
mViewModelStore.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void onCreate() {
mAppExecutors = new AppExecutors();
mBitmapCache = new BitmapCache(this, mAppExecutors.diskIO(), mAppExecutors.mainThread());


TelemetryWrapper.init(this);
GleanMetricsService.init(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void setPermissionAllowed(String uri, @SitePermission.Category int catego
mSitePermissionModel.deleteSite(site);
} else {
if (site == null) {
site = new SitePermission(uri, false, SitePermission.SITE_PERMISSION_WEBXR);
site = new SitePermission(uri, uri, false, SitePermission.SITE_PERMISSION_WEBXR);
mSitePermissions.add(site);
}
site.allowed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private void showPopUp(int sessionId, @NonNull Pair<String, LinkedList<PopUpRequ
boolean allowed = index != PopUpBlockDialogWidget.NEGATIVE;
boolean askAgain = mPopUpPrompt.askAgain();
if (allowed && !askAgain) {
SitePermission permission = new SitePermission(uri, allowed, SitePermission.SITE_PERMISSION_POPUP);
SitePermission permission = new SitePermission(uri, uri, allowed, SitePermission.SITE_PERMISSION_POPUP);
mAllowedPopUpSites.add(permission);
mViewModel.insertSite(permission);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.telemetry.TelemetryHolder;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionPolicy;
import org.mozilla.vrbrowser.telemetry.GleanMetricsService;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.DeviceType;
Expand Down Expand Up @@ -53,7 +54,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static boolean UI_HARDWARE_ACCELERATION_DEFAULT_WAVEVR = false;
public final static boolean PERFORMANCE_MONITOR_DEFAULT = true;
public final static boolean DRM_PLAYBACK_DEFAULT = false;
public final static boolean TRACKING_DEFAULT = true;
public final static @TrackingProtectionPolicy.ETP_Level int TRACKING_DEFAULT = TrackingProtectionPolicy.ETP_DEFAULT;
public final static boolean NOTIFICATIONS_DEFAULT = true;
public final static boolean SPEECH_DATA_COLLECTION_DEFAULT = false;
public final static boolean SPEECH_DATA_COLLECTION_REVIEWED_DEFAULT = false;
Expand Down Expand Up @@ -206,14 +207,15 @@ public void setDrmContentPlaybackEnabled(boolean isEnabled) {
editor.commit();
}

public boolean isTrackingProtectionEnabled() {
return mPrefs.getBoolean(
mContext.getString(R.string.settings_key_tracking_protection), TRACKING_DEFAULT);
public @TrackingProtectionPolicy.ETP_Level
int getTrackingProtectionLevel() {
return mPrefs.getInt(
mContext.getString(R.string.settings_key_tracking_protection_level), TRACKING_DEFAULT);
}

public void setTrackingProtectionEnabled(boolean isEnabled) {
public void setTrackingProtectionLevel(@TrackingProtectionPolicy.ETP_Level int level) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_tracking_protection), isEnabled);
editor.putInt(mContext.getString(R.string.settings_key_tracking_protection_level), level);
editor.commit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ object EngineProvider {

builder.crashHandler(CrashReporterService::class.java)
builder.contentBlocking(ContentBlocking.Settings.Builder()
.antiTracking(ContentBlocking.AntiTracking.AD or ContentBlocking.AntiTracking.SOCIAL or ContentBlocking.AntiTracking.ANALYTIC)
.antiTracking(
ContentBlocking.AntiTracking.AD or
ContentBlocking.AntiTracking.SOCIAL or
ContentBlocking.AntiTracking.ANALYTIC)
.enhancedTrackingProtectionLevel(SettingsStore.getInstance(context).trackingProtectionLevel)
.build())
builder.consoleOutput(SettingsStore.getInstance(context).isConsoleLogsEnabled)
builder.displayDensityOverride(SettingsStore.getInstance(context).displayDensity)
Expand All @@ -52,8 +56,6 @@ object EngineProvider {
val path = "resource://android/assets/web_extensions/$extension/"
runtime!!.registerWebExtension(WebExtension(path, runtime!!.webExtensionController))
}


}

return runtime!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ private void setupSessionListeners(GeckoSession aSession) {
aSession.setMediaDelegate(this);
aSession.setHistoryDelegate(this);
aSession.setSelectionActionDelegate(this);
aSession.setContentBlockingDelegate(this);
}

private void cleanSessionListeners(GeckoSession aSession) {
Expand All @@ -350,6 +351,7 @@ private void cleanSessionListeners(GeckoSession aSession) {
aSession.setMediaDelegate(null);
aSession.setHistoryDelegate(null);
aSession.setSelectionActionDelegate(null);
aSession.setContentBlockingDelegate(null);
}

public void suspend() {
Expand Down Expand Up @@ -869,16 +871,6 @@ public void setUaMode(int mode) {
}
}


protected void setTrackingProtection(final boolean aEnabled) {
if (mState.mSettings.isTrackingProtectionEnabled() != aEnabled) {
mState.mSettings.setTrackingProtectionEnabled(aEnabled);
if (mState.mSession != null) {
mState.mSession.getSettings().setUseTrackingProtection(aEnabled);
}
}
}

public void updateLastUse() {
mState.mLastUse = System.currentTimeMillis();
}
Expand Down Expand Up @@ -1239,6 +1231,25 @@ public void onContentBlocked(@NonNull final GeckoSession session, @NonNull final
}
}

@Override
public void onContentLoaded(@NonNull GeckoSession geckoSession, @NonNull ContentBlocking.BlockEvent event) {
if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.AD) != 0) {
Log.i(LOGTAG, "Loading Ad: " + event.uri);
}

if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.ANALYTIC) != 0) {
Log.i(LOGTAG, "Loading Analytic: " + event.uri);
}

if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.CONTENT) != 0) {
Log.i(LOGTAG, "Loading Content: " + event.uri);
}

if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.SOCIAL) != 0) {
Log.i(LOGTAG, "Loading Social: " + event.uri);
}
}

// PromptDelegate

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionStore;
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionPolicy;

class SessionSettings {

Expand Down Expand Up @@ -125,9 +127,13 @@ public Builder withDefaultSettings(Context context) {
int viewport = ua == GeckoSessionSettings.USER_AGENT_MODE_DESKTOP ?
GeckoSessionSettings.VIEWPORT_MODE_DESKTOP : GeckoSessionSettings.VIEWPORT_MODE_MOBILE;

TrackingProtectionPolicy policy = TrackingProtectionStore.getTrackingProtectionPolicy(context);
boolean shouldBlockContent = policy.contains(
TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES);

return new SessionSettings.Builder()
.withPrivateBrowsing(false)
.withTrackingProtection(SettingsStore.getInstance(context).isTrackingProtectionEnabled())
.withTrackingProtection(shouldBlockContent)
.withSuspendMediaWhenInactive(true)
.withUserAgent(ua)
.withViewport(viewport)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
import org.mozilla.vrbrowser.browser.HistoryStore;
import org.mozilla.vrbrowser.browser.PermissionDelegate;
import org.mozilla.vrbrowser.browser.Services;
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionStore;
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionPolicy;
import org.mozilla.vrbrowser.db.SitePermission;
import org.mozilla.vrbrowser.utils.SystemUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class SessionStore implements GeckoSession.PermissionDelegate {
public class SessionStore implements GeckoSession.PermissionDelegate{
private static final String LOGTAG = SystemUtils.createLogtag(SessionStore.class);
private static final int MAX_GECKO_SESSIONS = 5;

Expand All @@ -45,6 +47,7 @@ public static SessionStore get() {
private HistoryStore mHistoryStore;
private Services mServices;
private boolean mSuspendPending;
private TrackingProtectionStore mTrackingProtectionStore;

private SessionStore() {
mSessions = new ArrayList<>();
Expand All @@ -57,6 +60,14 @@ public void setContext(Context context, Bundle aExtras) {
SessionUtils.vrPrefsWorkAround(context, aExtras);

mRuntime = EngineProvider.INSTANCE.getOrCreateRuntime(context);

mTrackingProtectionStore = new TrackingProtectionStore(context, mRuntime);
mTrackingProtectionStore.addListener(new TrackingProtectionStore.TrackingProtectionListener() {
@Override
public void onTrackingProtectionLevelUpdated(@TrackingProtectionPolicy.ETP_Level int level) {
mSessions.forEach(session -> session.reload(GeckoSession.LOAD_FLAGS_NONE));
}
});
}

public void initializeServices() {
Expand Down Expand Up @@ -210,6 +221,10 @@ public HistoryStore getHistoryStore() {
return mHistoryStore;
}

public TrackingProtectionStore getTrackingProtectionStore() {
return mTrackingProtectionStore;
}

public void purgeSessionHistory() {
for (Session session: mSessions) {
session.purgeHistory();
Expand Down Expand Up @@ -246,18 +261,6 @@ public void setServo(final boolean enabled) {
}
}

public void setUaMode(final int mode) {
for (Session session: mSessions) {
session.setUaMode(mode);
}
}

public void setTrackingProtection(final boolean aEnabled) {
for (Session session: mSessions) {
session.setTrackingProtection(aEnabled);
}
}

// Runtime Settings

public void setConsoleOutputEnabled(boolean enabled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.mozilla.vrbrowser.browser.tracking;

import androidx.annotation.IntDef;

import org.mozilla.geckoview.ContentBlocking;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TrackingProtectionPolicy {

@IntDef(value = { ETP_NONE, ETP_DEFAULT, ETP_STRICT})
public @interface ETP_Level {}
public static final int ETP_NONE = ContentBlocking.EtpLevel.NONE;
public static final int ETP_DEFAULT = ContentBlocking.EtpLevel.DEFAULT;
public static final int ETP_STRICT = ContentBlocking.EtpLevel.STRICT;

private List<TrackingCategory> trackingCategory;
private CookiePolicy cookiePolicy;
private Boolean strictSocialTrackingProtection = null;

private TrackingProtectionPolicy() {
trackingCategory = new ArrayList<>();
}

/**
* Strict policy.
* Combining the [TrackingCategory.STRICT] plus a cookiePolicy of [ACCEPT_NON_TRACKERS]
* This is the strictest setting and may cause issues on some web sites.
*/
public static TrackingProtectionPolicy strict() {
TrackingProtectionPolicy policy = new TrackingProtectionPolicy();
policy.trackingCategory = Collections.singletonList(TrackingCategory.STRICT);
policy.cookiePolicy = CookiePolicy.ACCEPT_NON_TRACKERS;
policy.strictSocialTrackingProtection = true;
return policy;
}

/**
* Recommended policy.
* Combining the [TrackingCategory.RECOMMENDED] plus a [CookiePolicy] of [ACCEPT_NON_TRACKERS].
* This is the recommended setting.
*/
public static TrackingProtectionPolicy recommended() {
TrackingProtectionPolicy policy = new TrackingProtectionPolicy();
policy.trackingCategory = Collections.singletonList(TrackingCategory.RECOMMENDED);
policy.cookiePolicy = CookiePolicy.ACCEPT_NON_TRACKERS;
policy.strictSocialTrackingProtection = false;
return policy;
}

public static TrackingProtectionPolicy none() {
TrackingProtectionPolicy policy = new TrackingProtectionPolicy();
policy.trackingCategory = Collections.singletonList(TrackingCategory.NONE);
policy.cookiePolicy = CookiePolicy.ACCEPT_ALL;
return policy;
}

public enum TrackingCategory {
NONE(0),
AD(1 << 1),
ANALYTICS(1 << 2),
SOCIAL(1 << 3),
CONTENT(1 << 4),
TEST(1 << 5),
CRYPTOMINING(1 << 6),
FINGERPRINTING(1 << 7),
MOZILLA_SOCIAL(1 << 8),
SCRIPTS_AND_SUB_RESOURCES(1 << 9999),
RECOMMENDED(AD.value + ANALYTICS.value + SOCIAL.value +
TEST.value + MOZILLA_SOCIAL.value + CRYPTOMINING.value),
STRICT(RECOMMENDED.value + SCRIPTS_AND_SUB_RESOURCES.value +
FINGERPRINTING.value);

private final int value;

TrackingCategory(final int newValue) {
value = newValue;
}

public int getValue() { return value; }
}

public enum CookiePolicy {
ACCEPT_ALL(0),
ACCEPT_ONLY_FIRST_PARTY(1),
ACCEPT_NONE(2),
ACCEPT_VISITED(3),
ACCEPT_NON_TRACKERS(4);

private final int value;

CookiePolicy(final int newValue) {
value = newValue;
}

public int getValue() { return value; }
}

public List<TrackingCategory> getTrackingCategory() {
return trackingCategory;
}

public Boolean getStrictSocialTrackingProtection() {
return strictSocialTrackingProtection;
}

public CookiePolicy getCookiePolicy() {
return cookiePolicy;
}

public int getAntiTrackingPolicy() {
/*
* The [TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES] is an
* artificial category, created with the sole purpose of going around this bug
* https://bugzilla.mozilla.org/show_bug.cgi?id=1579264, for this reason we have to
* remove its value from the valid anti tracking categories, when is present.
*/
int total = trackingCategory.stream().mapToInt(TrackingCategory::getValue).sum();
if (contains(TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)) {
return total - TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.getValue();
} else {
return total;
}
}

public boolean contains(TrackingCategory category) {
int sum = trackingCategory.stream().mapToInt(TrackingCategory::getValue).sum();
return (sum & category.getValue()) != 0;
}

}
Loading

0 comments on commit 669e3d4

Please sign in to comment.