Skip to content

Commit

Permalink
fix: importing should actually persist storage (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
stoically committed Jun 30, 2019
1 parent 20eff15 commit 17998cc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 45 deletions.
8 changes: 1 addition & 7 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ class TemporaryContainers {
notifications: permissions.includes('notifications'),
};

await this.storage.load();
if (this.version !== this.storage.local.version) {
await this.migration.migrate();
this.storage.local.version = this.version;
await this.storage.persist();
}
await this.storage.initialize();

this.request.initialize();
this.runtime.initialize();
Expand All @@ -56,7 +51,6 @@ class TemporaryContainers {
await this.tabs.initialize();



debug('[tmp] initialized');
this.initialized = true;
window.tmpInitialized(true);
Expand Down
7 changes: 2 additions & 5 deletions src/background/event-listeners.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// to have a persistent listeners we need to register them early
// and wait for tmp to fully initialize before handling them
// to have persistent listeners we need to register them early
// and wait for tmp to fully initialize before handling events
const tmpInitializedPromise = new window.PCancelable(resolve => window.tmpInitialized = resolve);
window.setTimeout(() => {
tmpInitializedPromise.cancel('[event-listeners] tmpInitialized timed out');
}, 5000);

[
{
name: 'onBeforeRequest',
func: browser.webRequest.onBeforeRequest,
options: [{
urls: ['<all_urls>'],
Expand All @@ -20,14 +19,12 @@ window.setTimeout(() => {
},
},
{
name: 'onMessage',
func: browser.runtime.onMessage,
listener: function() {
return tmp.runtime.onMessage.call(tmp.runtime, ...arguments);
},
},
{
name: 'onStartup',
func: browser.runtime.onStartup,
listener: function() {
return tmp.runtime.onStartup.call(tmp.runtime, ...arguments);
Expand Down
4 changes: 4 additions & 0 deletions src/background/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const logOnInstalledListener = details => {
if (details.temporary) {
log.DEBUG = true;
log.stringify = false;

browser.tabs.create({
url: browser.runtime.getURL('options.html')
});
}
debug('[log] onInstalled', details);
};
Expand Down
58 changes: 31 additions & 27 deletions src/background/migration.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable require-atomic-updates */
/* istanbul ignore next */
class Migration {
constructor(background) {
this.background = background;
}

async migrate(previousVersion) {
async migrate({preferences, previousVersion}) {
this.storage = this.background.storage;
this.previousVersion = previousVersion || this.storage.local.version;
this.previousVersion = previousVersion;

if (!this.previousVersion) {
await window.migrationLegacy(this);
Expand All @@ -21,26 +22,26 @@ class Migration {

if (this.updatedFromVersionEqualToOrLessThan('0.16')) {
debug('updated from version <= 0.16, adapt old automaticmode behaviour if necessary');
if (!this.storage.local.preferences.automaticMode) {
this.storage.local.preferences.linkClickGlobal.middle.action = 'never';
this.storage.local.preferences.linkClickGlobal.ctrlleft.action = 'never';
if (!preferences.automaticMode) {
preferences.linkClickGlobal.middle.action = 'never';
preferences.linkClickGlobal.ctrlleft.action = 'never';
}
}
if (this.updatedFromVersionEqualToOrLessThan('0.33')) {
debug('updated from version <= 0.33, make sure to set all left-clicks to "never"');
this.storage.local.preferences.linkClickGlobal.left.action = 'never';
const linkClickDomainPatterns = Object.keys(this.storage.local.preferences.linkClickDomain);
preferences.linkClickGlobal.left.action = 'never';
const linkClickDomainPatterns = Object.keys(preferences.linkClickDomain);
if (linkClickDomainPatterns.length) {
linkClickDomainPatterns.map(linkClickDomainPattern => {
this.storage.local.preferences.linkClickDomain[linkClickDomainPattern].left.action = 'never';
preferences.linkClickDomain[linkClickDomainPattern].left.action = 'never';
});
}
}
if (this.updatedFromVersionEqualToOrLessThan('0.57')) {
debug('updated from version <= 0.57, potentially inform user about automatic mode preference change');
if (this.storage.local.preferences.automaticMode &&
this.storage.local.preferences.automaticModeNewTab === 'navigation') {
this.storage.local.preferences.automaticModeNewTab = 'created';
if (preferences.automaticMode &&
preferences.automaticModeNewTab === 'navigation') {
preferences.automaticModeNewTab = 'created';

const url = browser.runtime.getURL('notifications/update_from_0.57_and_below.html');
browser.tabs.create({
Expand All @@ -50,10 +51,10 @@ class Migration {
}
if (this.updatedFromVersionEqualToOrLessThan('0.59')) {
debug('updated from version <= 0.59, potentially migrate always open in preferences');
const alwaysOpenInDomains = Object.keys(this.storage.local.preferences.alwaysOpenInDomain);
const alwaysOpenInDomains = Object.keys(preferences.alwaysOpenInDomain);
if (alwaysOpenInDomains.length) {
alwaysOpenInDomains.map(alwaysOpenInDomainPattern => {
this.storage.local.preferences.alwaysOpenInDomain[alwaysOpenInDomainPattern] = {
preferences.alwaysOpenInDomain[alwaysOpenInDomainPattern] = {
allowedInPermanent: false
};
});
Expand All @@ -66,7 +67,6 @@ class Migration {
}
if (this.updatedFromVersionEqualToOrLessThan('0.77')) {
debug('updated from version <= 0.77, migrate preferences');
const preferences = this.storage.local.preferences;
const newPreferences = Object.assign({}, this.storage.preferencesDefault, {
automaticMode: {
active: preferences.automaticMode,
Expand Down Expand Up @@ -161,7 +161,7 @@ class Migration {
newPreferences.isolation.domain[pattern].mouseClick = _preferences;
});

this.storage.local.preferences = newPreferences;
preferences = newPreferences;
delete this.storage.local.tabContainerMap;
}
if (this.updatedFromVersionEqualToOrLessThan('0.81')) {
Expand Down Expand Up @@ -189,35 +189,39 @@ class Migration {
(this.previousVersionBeta && this.updatedFromVersionEqualToOrLessThan('1.0.1'))) {
debug('updated from version <= 0.103, migrate deletesHistory.active and ignoreRequestsTo');
if (this.background.permissions.history) {
this.storage.local.preferences.deletesHistory.active = true;
preferences.deletesHistory.active = true;
}

if (this.storage.local.preferences.ignoreRequestsToAMO === false) {
this.storage.local.preferences.ignoreRequests =
this.storage.local.preferences.ignoreRequests.filter(ignoredPattern =>
if (preferences.ignoreRequestsToAMO === false) {
preferences.ignoreRequests =
preferences.ignoreRequests.filter(ignoredPattern =>
ignoredPattern !== 'addons.mozilla.org'
);
}
if (this.storage.local.preferences.ignoreRequestsToPocket === false) {
this.storage.local.preferences.ignoreRequests =
this.storage.local.preferences.ignoreRequests.filter(ignoredPattern =>
if (preferences.ignoreRequestsToPocket === false) {
preferences.ignoreRequests =
preferences.ignoreRequests.filter(ignoredPattern =>
ignoredPattern !== 'getpocket.com'
);
}
delete this.storage.local.preferences.ignoreRequestsToAMO;
delete this.storage.local.preferences.ignoreRequestsToPocket;
delete preferences.ignoreRequestsToAMO;
delete preferences.ignoreRequestsToPocket;
}
if ((this.updatedFromVersionEqualToOrLessThan('0.103') && !this.previousVersionBeta) ||
(this.previousVersionBeta && this.updatedFromVersionEqualToOrLessThan('1.0.6'))) {
debug('updated from version <= 0.103, migrate per domain isolation to array');
const perDomainIsolation = [];
Object.keys(this.storage.local.preferences.isolation.domain).map(domainPattern => {
Object.keys(preferences.isolation.domain).map(domainPattern => {
perDomainIsolation.push(Object.assign({
pattern: domainPattern,
}, this.storage.local.preferences.isolation.domain[domainPattern]));
}, preferences.isolation.domain[domainPattern]));
});
this.storage.local.preferences.isolation.domain = perDomainIsolation;
preferences.isolation.domain = perDomainIsolation;
}

this.storage.local.version = this.background.version;
this.storage.local.preferences = preferences;
await this.storage.persist();
}

updatedFromVersionEqualToOrLessThan(compareVersion) {
Expand Down
Empty file added src/background/preferences.js
Empty file.
5 changes: 4 additions & 1 deletion src/background/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class Runtime {
case 'savePreferences':
debug('[onMessage] saving preferences');
if (message.payload.migrate) {
await this.migration.migrate(message.payload.previousVersion);
await this.migration.migrate({
preferences: message.payload.preferences,
previousVersion: message.payload.previousVersion
});
}

if (this.storage.local.preferences.iconColor !== message.payload.preferences.iconColor) {
Expand Down
16 changes: 12 additions & 4 deletions src/background/storage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class TmpStorage {
constructor(background) {
this.background = background;
this.loaded = false;
this.installed = false;
this.local = null;
this.preferencesDefault = {
Expand Down Expand Up @@ -107,18 +106,27 @@ class TmpStorage {
};
}

async load() {
async initialize() {
this.local = await browser.storage.local.get();

// empty storage *should* mean new install
if (!this.local || !Object.keys(this.local).length) {
return this.install();
}
debug('[load] storage loaded', this.local);

debug('[initialize] storage initialized', this.local);
if (this.addMissingStorageKeys()) {
await this.persist();
}
this.loaded = true;

// migrate if currently running version is different from version in storage
if (this.background.version !== this.local.version) {
await this.background.migration.migrate({
preferences: this.local.preferences,
previousVersion: this.local.version
});
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "Open tabs, websites, and links in automatically managed disposable containers. Containers isolate the data websites store (cookies, cache, and more) from each other, further enhancing your privacy while you browse.",
"manifest_version": 2,
"name": "Temporary Containers",
"version": "1.0beta9",
"version": "1.0beta13",
"homepage_url": "https://github.com/stoically/temporary-containers",
"icons": {
"48": "icons/tmpcontainer-48.png"
Expand Down

0 comments on commit 17998cc

Please sign in to comment.