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

make the site mostly work when cookies (localStorage) are disabled #492

Merged
merged 1 commit into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion frontend/src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Raven } = require('./common');
const FileReceiver = require('./fileReceiver');
const { bytes, notify, gcmCompliant } = require('./utils');
const Storage = require('./storage');
const storage = new Storage(localStorage);
const storage = new Storage();
const links = require('./links');
const metrics = require('./metrics');
const progress = require('./progress');
Expand Down Expand Up @@ -87,6 +87,7 @@ function download() {
a.download = fname;
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(downloadUrl);
})
.catch(err => {
Raven.captureException(err);
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/metrics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const testPilotGA = require('testpilot-ga/src/TestPilotGA');
const Storage = require('./storage');
const storage = new Storage(localStorage);
const storage = new Storage();

let hasLocalStorage = false;
try {
hasLocalStorage = !!localStorage;
} catch (e) {
// don't care
}

const analytics = new testPilotGA({
an: 'Firefox Send',
Expand All @@ -18,7 +25,10 @@ document.addEventListener('DOMContentLoaded', function() {
});

function sendEvent() {
return analytics.sendEvent.apply(analytics, arguments).catch(() => 0);
return (
hasLocalStorage &&
analytics.sendEvent.apply(analytics, arguments).catch(() => 0)
);
}

function urlToMetric(url) {
Expand Down
38 changes: 32 additions & 6 deletions frontend/src/storage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
const { isFile } = require('./utils');

class Mem {
constructor() {
this.items = new Map();
}

get length() {
return this.items.size;
}

getItem(key) {
return this.items.get(key);
}

setItem(key, value) {
return this.items.set(key, value);
}

removeItem(key) {
return this.items.delete(key);
}

key(i) {
return this.items.keys()[i];
}
}

class Storage {
constructor(engine) {
this.engine = engine;
constructor() {
try {
this.engine = localStorage || new Mem();
} catch (e) {
this.engine = new Mem();
}
}

get totalDownloads() {
Expand Down Expand Up @@ -59,10 +89,6 @@ class Storage {
return this.engine.getItem(id);
}

has(property) {
return this.engine.hasOwnProperty(property);
}

remove(property) {
this.engine.removeItem(property);
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
ONE_DAY_IN_MS
} = require('./utils');
const Storage = require('./storage');
const storage = new Storage(localStorage);
const storage = new Storage();
const metrics = require('./metrics');
const progress = require('./progress');

Expand Down