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

Commit 669e3d4

Browse files
committed
Enhanced Tracking Protection
1 parent 8142076 commit 669e3d4

38 files changed

Lines changed: 1021 additions & 223 deletions

app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ protected void onDestroy() {
484484

485485
SessionStore.get().onDestroy();
486486

487-
488487
super.onDestroy();
489488
mLifeCycle.setCurrentState(Lifecycle.State.DESTROYED);
490489
mViewModelStore.clear();

app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public void onCreate() {
3434
mAppExecutors = new AppExecutors();
3535
mBitmapCache = new BitmapCache(this, mAppExecutors.diskIO(), mAppExecutors.mainThread());
3636

37-
3837
TelemetryWrapper.init(this);
3938
GleanMetricsService.init(this);
4039
}

app/src/common/shared/org/mozilla/vrbrowser/browser/PermissionDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public void setPermissionAllowed(String uri, @SitePermission.Category int catego
286286
mSitePermissionModel.deleteSite(site);
287287
} else {
288288
if (site == null) {
289-
site = new SitePermission(uri, false, SitePermission.SITE_PERMISSION_WEBXR);
289+
site = new SitePermission(uri, uri, false, SitePermission.SITE_PERMISSION_WEBXR);
290290
mSitePermissions.add(site);
291291
}
292292
site.allowed = false;

app/src/common/shared/org/mozilla/vrbrowser/browser/PromptDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private void showPopUp(int sessionId, @NonNull Pair<String, LinkedList<PopUpRequ
357357
boolean allowed = index != PopUpBlockDialogWidget.NEGATIVE;
358358
boolean askAgain = mPopUpPrompt.askAgain();
359359
if (allowed && !askAgain) {
360-
SitePermission permission = new SitePermission(uri, allowed, SitePermission.SITE_PERMISSION_POPUP);
360+
SitePermission permission = new SitePermission(uri, uri, allowed, SitePermission.SITE_PERMISSION_POPUP);
361361
mAllowedPopUpSites.add(permission);
362362
mViewModel.insertSite(permission);
363363
}

app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.mozilla.geckoview.GeckoSessionSettings;
1515
import org.mozilla.telemetry.TelemetryHolder;
1616
import org.mozilla.vrbrowser.R;
17+
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionPolicy;
1718
import org.mozilla.vrbrowser.telemetry.GleanMetricsService;
1819
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
1920
import org.mozilla.vrbrowser.utils.DeviceType;
@@ -53,7 +54,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
5354
public final static boolean UI_HARDWARE_ACCELERATION_DEFAULT_WAVEVR = false;
5455
public final static boolean PERFORMANCE_MONITOR_DEFAULT = true;
5556
public final static boolean DRM_PLAYBACK_DEFAULT = false;
56-
public final static boolean TRACKING_DEFAULT = true;
57+
public final static @TrackingProtectionPolicy.ETP_Level int TRACKING_DEFAULT = TrackingProtectionPolicy.ETP_DEFAULT;
5758
public final static boolean NOTIFICATIONS_DEFAULT = true;
5859
public final static boolean SPEECH_DATA_COLLECTION_DEFAULT = false;
5960
public final static boolean SPEECH_DATA_COLLECTION_REVIEWED_DEFAULT = false;
@@ -206,14 +207,15 @@ public void setDrmContentPlaybackEnabled(boolean isEnabled) {
206207
editor.commit();
207208
}
208209

209-
public boolean isTrackingProtectionEnabled() {
210-
return mPrefs.getBoolean(
211-
mContext.getString(R.string.settings_key_tracking_protection), TRACKING_DEFAULT);
210+
public @TrackingProtectionPolicy.ETP_Level
211+
int getTrackingProtectionLevel() {
212+
return mPrefs.getInt(
213+
mContext.getString(R.string.settings_key_tracking_protection_level), TRACKING_DEFAULT);
212214
}
213215

214-
public void setTrackingProtectionEnabled(boolean isEnabled) {
216+
public void setTrackingProtectionLevel(@TrackingProtectionPolicy.ETP_Level int level) {
215217
SharedPreferences.Editor editor = mPrefs.edit();
216-
editor.putBoolean(mContext.getString(R.string.settings_key_tracking_protection), isEnabled);
218+
editor.putInt(mContext.getString(R.string.settings_key_tracking_protection_level), level);
217219
editor.commit();
218220
}
219221

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/EngineProvider.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ object EngineProvider {
2525

2626
builder.crashHandler(CrashReporterService::class.java)
2727
builder.contentBlocking(ContentBlocking.Settings.Builder()
28-
.antiTracking(ContentBlocking.AntiTracking.AD or ContentBlocking.AntiTracking.SOCIAL or ContentBlocking.AntiTracking.ANALYTIC)
28+
.antiTracking(
29+
ContentBlocking.AntiTracking.AD or
30+
ContentBlocking.AntiTracking.SOCIAL or
31+
ContentBlocking.AntiTracking.ANALYTIC)
32+
.enhancedTrackingProtectionLevel(SettingsStore.getInstance(context).trackingProtectionLevel)
2933
.build())
3034
builder.consoleOutput(SettingsStore.getInstance(context).isConsoleLogsEnabled)
3135
builder.displayDensityOverride(SettingsStore.getInstance(context).displayDensity)
@@ -52,8 +56,6 @@ object EngineProvider {
5256
val path = "resource://android/assets/web_extensions/$extension/"
5357
runtime!!.registerWebExtension(WebExtension(path, runtime!!.webExtensionController))
5458
}
55-
56-
5759
}
5860

5961
return runtime!!

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ private void setupSessionListeners(GeckoSession aSession) {
337337
aSession.setMediaDelegate(this);
338338
aSession.setHistoryDelegate(this);
339339
aSession.setSelectionActionDelegate(this);
340+
aSession.setContentBlockingDelegate(this);
340341
}
341342

342343
private void cleanSessionListeners(GeckoSession aSession) {
@@ -350,6 +351,7 @@ private void cleanSessionListeners(GeckoSession aSession) {
350351
aSession.setMediaDelegate(null);
351352
aSession.setHistoryDelegate(null);
352353
aSession.setSelectionActionDelegate(null);
354+
aSession.setContentBlockingDelegate(null);
353355
}
354356

355357
public void suspend() {
@@ -869,16 +871,6 @@ public void setUaMode(int mode) {
869871
}
870872
}
871873

872-
873-
protected void setTrackingProtection(final boolean aEnabled) {
874-
if (mState.mSettings.isTrackingProtectionEnabled() != aEnabled) {
875-
mState.mSettings.setTrackingProtectionEnabled(aEnabled);
876-
if (mState.mSession != null) {
877-
mState.mSession.getSettings().setUseTrackingProtection(aEnabled);
878-
}
879-
}
880-
}
881-
882874
public void updateLastUse() {
883875
mState.mLastUse = System.currentTimeMillis();
884876
}
@@ -1239,6 +1231,25 @@ public void onContentBlocked(@NonNull final GeckoSession session, @NonNull final
12391231
}
12401232
}
12411233

1234+
@Override
1235+
public void onContentLoaded(@NonNull GeckoSession geckoSession, @NonNull ContentBlocking.BlockEvent event) {
1236+
if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.AD) != 0) {
1237+
Log.i(LOGTAG, "Loading Ad: " + event.uri);
1238+
}
1239+
1240+
if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.ANALYTIC) != 0) {
1241+
Log.i(LOGTAG, "Loading Analytic: " + event.uri);
1242+
}
1243+
1244+
if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.CONTENT) != 0) {
1245+
Log.i(LOGTAG, "Loading Content: " + event.uri);
1246+
}
1247+
1248+
if ((event.getAntiTrackingCategory() & ContentBlocking.AntiTracking.SOCIAL) != 0) {
1249+
Log.i(LOGTAG, "Loading Social: " + event.uri);
1250+
}
1251+
}
1252+
12421253
// PromptDelegate
12431254

12441255
@Nullable

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionSettings.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import org.mozilla.geckoview.GeckoSessionSettings;
88
import org.mozilla.vrbrowser.browser.SettingsStore;
9+
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionStore;
10+
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionPolicy;
911

1012
class SessionSettings {
1113

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

130+
TrackingProtectionPolicy policy = TrackingProtectionStore.getTrackingProtectionPolicy(context);
131+
boolean shouldBlockContent = policy.contains(
132+
TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES);
133+
128134
return new SessionSettings.Builder()
129135
.withPrivateBrowsing(false)
130-
.withTrackingProtection(SettingsStore.getInstance(context).isTrackingProtectionEnabled())
136+
.withTrackingProtection(shouldBlockContent)
131137
.withSuspendMediaWhenInactive(true)
132138
.withUserAgent(ua)
133139
.withViewport(viewport)

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
import org.mozilla.vrbrowser.browser.HistoryStore;
1717
import org.mozilla.vrbrowser.browser.PermissionDelegate;
1818
import org.mozilla.vrbrowser.browser.Services;
19+
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionStore;
20+
import org.mozilla.vrbrowser.browser.tracking.TrackingProtectionPolicy;
1921
import org.mozilla.vrbrowser.db.SitePermission;
2022
import org.mozilla.vrbrowser.utils.SystemUtils;
2123

2224
import java.util.ArrayList;
2325
import java.util.LinkedList;
2426
import java.util.List;
2527

26-
public class SessionStore implements GeckoSession.PermissionDelegate {
28+
public class SessionStore implements GeckoSession.PermissionDelegate{
2729
private static final String LOGTAG = SystemUtils.createLogtag(SessionStore.class);
2830
private static final int MAX_GECKO_SESSIONS = 5;
2931

@@ -45,6 +47,7 @@ public static SessionStore get() {
4547
private HistoryStore mHistoryStore;
4648
private Services mServices;
4749
private boolean mSuspendPending;
50+
private TrackingProtectionStore mTrackingProtectionStore;
4851

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

5962
mRuntime = EngineProvider.INSTANCE.getOrCreateRuntime(context);
63+
64+
mTrackingProtectionStore = new TrackingProtectionStore(context, mRuntime);
65+
mTrackingProtectionStore.addListener(new TrackingProtectionStore.TrackingProtectionListener() {
66+
@Override
67+
public void onTrackingProtectionLevelUpdated(@TrackingProtectionPolicy.ETP_Level int level) {
68+
mSessions.forEach(session -> session.reload(GeckoSession.LOAD_FLAGS_NONE));
69+
}
70+
});
6071
}
6172

6273
public void initializeServices() {
@@ -210,6 +221,10 @@ public HistoryStore getHistoryStore() {
210221
return mHistoryStore;
211222
}
212223

224+
public TrackingProtectionStore getTrackingProtectionStore() {
225+
return mTrackingProtectionStore;
226+
}
227+
213228
public void purgeSessionHistory() {
214229
for (Session session: mSessions) {
215230
session.purgeHistory();
@@ -246,18 +261,6 @@ public void setServo(final boolean enabled) {
246261
}
247262
}
248263

249-
public void setUaMode(final int mode) {
250-
for (Session session: mSessions) {
251-
session.setUaMode(mode);
252-
}
253-
}
254-
255-
public void setTrackingProtection(final boolean aEnabled) {
256-
for (Session session: mSessions) {
257-
session.setTrackingProtection(aEnabled);
258-
}
259-
}
260-
261264
// Runtime Settings
262265

263266
public void setConsoleOutputEnabled(boolean enabled) {
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package org.mozilla.vrbrowser.browser.tracking;
2+
3+
import androidx.annotation.IntDef;
4+
5+
import org.mozilla.geckoview.ContentBlocking;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
11+
public class TrackingProtectionPolicy {
12+
13+
@IntDef(value = { ETP_NONE, ETP_DEFAULT, ETP_STRICT})
14+
public @interface ETP_Level {}
15+
public static final int ETP_NONE = ContentBlocking.EtpLevel.NONE;
16+
public static final int ETP_DEFAULT = ContentBlocking.EtpLevel.DEFAULT;
17+
public static final int ETP_STRICT = ContentBlocking.EtpLevel.STRICT;
18+
19+
private List<TrackingCategory> trackingCategory;
20+
private CookiePolicy cookiePolicy;
21+
private Boolean strictSocialTrackingProtection = null;
22+
23+
private TrackingProtectionPolicy() {
24+
trackingCategory = new ArrayList<>();
25+
}
26+
27+
/**
28+
* Strict policy.
29+
* Combining the [TrackingCategory.STRICT] plus a cookiePolicy of [ACCEPT_NON_TRACKERS]
30+
* This is the strictest setting and may cause issues on some web sites.
31+
*/
32+
public static TrackingProtectionPolicy strict() {
33+
TrackingProtectionPolicy policy = new TrackingProtectionPolicy();
34+
policy.trackingCategory = Collections.singletonList(TrackingCategory.STRICT);
35+
policy.cookiePolicy = CookiePolicy.ACCEPT_NON_TRACKERS;
36+
policy.strictSocialTrackingProtection = true;
37+
return policy;
38+
}
39+
40+
/**
41+
* Recommended policy.
42+
* Combining the [TrackingCategory.RECOMMENDED] plus a [CookiePolicy] of [ACCEPT_NON_TRACKERS].
43+
* This is the recommended setting.
44+
*/
45+
public static TrackingProtectionPolicy recommended() {
46+
TrackingProtectionPolicy policy = new TrackingProtectionPolicy();
47+
policy.trackingCategory = Collections.singletonList(TrackingCategory.RECOMMENDED);
48+
policy.cookiePolicy = CookiePolicy.ACCEPT_NON_TRACKERS;
49+
policy.strictSocialTrackingProtection = false;
50+
return policy;
51+
}
52+
53+
public static TrackingProtectionPolicy none() {
54+
TrackingProtectionPolicy policy = new TrackingProtectionPolicy();
55+
policy.trackingCategory = Collections.singletonList(TrackingCategory.NONE);
56+
policy.cookiePolicy = CookiePolicy.ACCEPT_ALL;
57+
return policy;
58+
}
59+
60+
public enum TrackingCategory {
61+
NONE(0),
62+
AD(1 << 1),
63+
ANALYTICS(1 << 2),
64+
SOCIAL(1 << 3),
65+
CONTENT(1 << 4),
66+
TEST(1 << 5),
67+
CRYPTOMINING(1 << 6),
68+
FINGERPRINTING(1 << 7),
69+
MOZILLA_SOCIAL(1 << 8),
70+
SCRIPTS_AND_SUB_RESOURCES(1 << 9999),
71+
RECOMMENDED(AD.value + ANALYTICS.value + SOCIAL.value +
72+
TEST.value + MOZILLA_SOCIAL.value + CRYPTOMINING.value),
73+
STRICT(RECOMMENDED.value + SCRIPTS_AND_SUB_RESOURCES.value +
74+
FINGERPRINTING.value);
75+
76+
private final int value;
77+
78+
TrackingCategory(final int newValue) {
79+
value = newValue;
80+
}
81+
82+
public int getValue() { return value; }
83+
}
84+
85+
public enum CookiePolicy {
86+
ACCEPT_ALL(0),
87+
ACCEPT_ONLY_FIRST_PARTY(1),
88+
ACCEPT_NONE(2),
89+
ACCEPT_VISITED(3),
90+
ACCEPT_NON_TRACKERS(4);
91+
92+
private final int value;
93+
94+
CookiePolicy(final int newValue) {
95+
value = newValue;
96+
}
97+
98+
public int getValue() { return value; }
99+
}
100+
101+
public List<TrackingCategory> getTrackingCategory() {
102+
return trackingCategory;
103+
}
104+
105+
public Boolean getStrictSocialTrackingProtection() {
106+
return strictSocialTrackingProtection;
107+
}
108+
109+
public CookiePolicy getCookiePolicy() {
110+
return cookiePolicy;
111+
}
112+
113+
public int getAntiTrackingPolicy() {
114+
/*
115+
* The [TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES] is an
116+
* artificial category, created with the sole purpose of going around this bug
117+
* https://bugzilla.mozilla.org/show_bug.cgi?id=1579264, for this reason we have to
118+
* remove its value from the valid anti tracking categories, when is present.
119+
*/
120+
int total = trackingCategory.stream().mapToInt(TrackingCategory::getValue).sum();
121+
if (contains(TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)) {
122+
return total - TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.getValue();
123+
} else {
124+
return total;
125+
}
126+
}
127+
128+
public boolean contains(TrackingCategory category) {
129+
int sum = trackingCategory.stream().mapToInt(TrackingCategory::getValue).sum();
130+
return (sum & category.getValue()) != 0;
131+
}
132+
133+
}

0 commit comments

Comments
 (0)