From 09faedf0597bf216373da09d9009f2af3c679942 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Wed, 9 Aug 2017 18:16:10 -0700 Subject: [PATCH] make the site mostly work when cookies (localStorage) are disabled --- frontend/src/download.js | 3 ++- frontend/src/metrics.js | 14 ++++++++++++-- frontend/src/storage.js | 38 ++++++++++++++++++++++++++++++++------ frontend/src/upload.js | 2 +- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/frontend/src/download.js b/frontend/src/download.js index 21819bd6..97cca47f 100644 --- a/frontend/src/download.js +++ b/frontend/src/download.js @@ -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'); @@ -87,6 +87,7 @@ function download() { a.download = fname; document.body.appendChild(a); a.click(); + URL.revokeObjectURL(downloadUrl); }) .catch(err => { Raven.captureException(err); diff --git a/frontend/src/metrics.js b/frontend/src/metrics.js index c7dff67d..436b9d6d 100644 --- a/frontend/src/metrics.js +++ b/frontend/src/metrics.js @@ -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', @@ -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) { diff --git a/frontend/src/storage.js b/frontend/src/storage.js index d83e1727..2721390f 100644 --- a/frontend/src/storage.js +++ b/frontend/src/storage.js @@ -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() { @@ -59,10 +89,6 @@ class Storage { return this.engine.getItem(id); } - has(property) { - return this.engine.hasOwnProperty(property); - } - remove(property) { this.engine.removeItem(property); } diff --git a/frontend/src/upload.js b/frontend/src/upload.js index 2bf7ea38..24e2308b 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -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');