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

This commit is contained in:
Danny Coates 2017-08-09 18:16:10 -07:00
parent 493bf8dc89
commit 09faedf059
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 47 additions and 10 deletions

View File

@ -2,7 +2,7 @@ const { Raven } = require('./common');
const FileReceiver = require('./fileReceiver'); const FileReceiver = require('./fileReceiver');
const { bytes, notify, gcmCompliant } = require('./utils'); const { bytes, notify, gcmCompliant } = require('./utils');
const Storage = require('./storage'); const Storage = require('./storage');
const storage = new Storage(localStorage); const storage = new Storage();
const links = require('./links'); const links = require('./links');
const metrics = require('./metrics'); const metrics = require('./metrics');
const progress = require('./progress'); const progress = require('./progress');
@ -87,6 +87,7 @@ function download() {
a.download = fname; a.download = fname;
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
URL.revokeObjectURL(downloadUrl);
}) })
.catch(err => { .catch(err => {
Raven.captureException(err); Raven.captureException(err);

View File

@ -1,6 +1,13 @@
const testPilotGA = require('testpilot-ga/src/TestPilotGA'); const testPilotGA = require('testpilot-ga/src/TestPilotGA');
const Storage = require('./storage'); 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({ const analytics = new testPilotGA({
an: 'Firefox Send', an: 'Firefox Send',
@ -18,7 +25,10 @@ document.addEventListener('DOMContentLoaded', function() {
}); });
function sendEvent() { function sendEvent() {
return analytics.sendEvent.apply(analytics, arguments).catch(() => 0); return (
hasLocalStorage &&
analytics.sendEvent.apply(analytics, arguments).catch(() => 0)
);
} }
function urlToMetric(url) { function urlToMetric(url) {

View File

@ -1,8 +1,38 @@
const { isFile } = require('./utils'); 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 { class Storage {
constructor(engine) { constructor() {
this.engine = engine; try {
this.engine = localStorage || new Mem();
} catch (e) {
this.engine = new Mem();
}
} }
get totalDownloads() { get totalDownloads() {
@ -59,10 +89,6 @@ class Storage {
return this.engine.getItem(id); return this.engine.getItem(id);
} }
has(property) {
return this.engine.hasOwnProperty(property);
}
remove(property) { remove(property) {
this.engine.removeItem(property); this.engine.removeItem(property);
} }

View File

@ -9,7 +9,7 @@ const {
ONE_DAY_IN_MS ONE_DAY_IN_MS
} = require('./utils'); } = require('./utils');
const Storage = require('./storage'); const Storage = require('./storage');
const storage = new Storage(localStorage); const storage = new Storage();
const metrics = require('./metrics'); const metrics = require('./metrics');
const progress = require('./progress'); const progress = require('./progress');