Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #168 from mozilla/privacy-controls
Browse files Browse the repository at this point in the history
 #72: Disable extraction in private browsing sessions.
  • Loading branch information
Osmose authored Oct 18, 2018
2 parents ba7fe28 + 42a19c7 commit 402bc22
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/background/price_updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import config from 'commerce/config';
import store from 'commerce/state';
import {shouldUpdatePrices} from 'commerce/privacy';
import {addPriceFromExtracted, getLatestPriceForProduct} from 'commerce/state/prices';
import {getAllProducts, getProduct, getProductIdFromExtracted} from 'commerce/state/products';

Expand All @@ -31,6 +32,10 @@ export function handleWebRequest(details) {
* Find products that are due for price updates and update them.
*/
export async function updatePrices() {
if (!(await shouldUpdatePrices())) {
return;
}

const state = store.getState();
const products = getAllProducts(state);
const now = new Date();
Expand Down
5 changes: 4 additions & 1 deletion src/background/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @module
*/

import {shouldCollectTelemetry} from 'commerce/privacy';

const CATEGORY = 'extension.price_alerts';

const EVENTS = {
Expand Down Expand Up @@ -156,9 +158,10 @@ export async function registerEvents() {
}

export async function recordEvent(method, object, value, extra) {
if (!browser.telemetry.canUpload()) {
if (!browser.telemetry.canUpload() || !(await shouldCollectTelemetry())) {
return;
}

await browser.telemetry.recordEvent(
CATEGORY,
method,
Expand Down
6 changes: 6 additions & 0 deletions src/extraction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import config from 'commerce/config/content';
import extractProductWithFathom from 'commerce/extraction/fathom';
import extractProductWithFallback from 'commerce/extraction/selector';
import extractProductWithOpenGraph from 'commerce/extraction/open_graph';
import {shouldExtract} from 'commerce/privacy';

/**
* Extraction methods are given the document object for the page, and must
Expand Down Expand Up @@ -67,6 +68,11 @@ async function attemptExtraction() {
return;
}

// Check privacy settings to determine if we should extract.
if (!(await shouldExtract())) {
return;
}

// Only perform extraction on allowlisted sites. Background updates get a
// pass; we don't want to accidentally freeze updates for products that are
// being tracked no matter what.
Expand Down
1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"storage",
"unlimitedStorage",
"notifications",
"privacy",
"webRequest",
"webRequestBlocking",
"telemetry"
Expand Down
78 changes: 78 additions & 0 deletions src/privacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
* Handles checking privacy settings to gate functionality across the add-on.
* @module
*/

/**
* Determine if a content script should extract a product.
* @return {boolean}
*/
export async function shouldExtract() {
return !browser.extension.inIncognitoContext;
}

/**
* Determine if a telemetry event should be recorded
* @return {boolean}
*/
export async function shouldCollectTelemetry() {
if (await trackingProtectionEnabled()) {
return false;
}

if (doNotTrackEnabled()) {
return false;
}

if (await cookiesBlocked()) {
return false;
}

return true;
}

/**
* Determine if a background price update should start
* @return {boolean}
*/
export async function shouldUpdatePrices() {
if (await trackingProtectionEnabled()) {
return false;
}

if (doNotTrackEnabled()) {
return false;
}

if (await cookiesBlocked()) {
return false;
}

return true;
}

async function trackingProtectionEnabled() {
const result = await browser.privacy.websites.trackingProtectionMode.get({});
return result.value === 'always';
}

function doNotTrackEnabled() {
return navigator.doNotTrack === '1';
}

async function cookiesBlocked() {
if (browser.privacy.websites.cookieConfig) {
const {behavior} = (await browser.privacy.websites.cookieConfig.get({})).value;
if (!['allow_all', 'allow_visited'].includes(behavior)) {
return true;
}

return false;
}

return true;
}

0 comments on commit 402bc22

Please sign in to comment.