forked from Tylian/XKit
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asynchronously migrate extension data (#1385)
* Asynchronously migrate extension data * Add storage migration warning; Default to unlimited storage * Bump version to 7.7.6-less-broken * Skip update if current value is longer * copying -> migrating for clarity * Bump version * Excise misleading comment
- Loading branch information
Showing
5 changed files
with
84 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,44 @@ | ||
/* globals require, exports */ | ||
/* globals require, exports, Promise */ | ||
let prefs = require('sdk/preferences/service'); | ||
let {setTimeout} = require('sdk/timers'); | ||
let prefRoot = 'extensions.xkit7.'; | ||
|
||
function migratePreference(port, prefKey) { | ||
let name = prefKey.substring(prefRoot.length); | ||
let value = prefs.get(prefKey, null); | ||
let msg = {}; | ||
msg[name] = value; | ||
// postMessage doesn't return a promise even though it could :( | ||
port.postMessage(msg); | ||
return new Promise(function(resolve, reject) { | ||
setTimeout(function() { | ||
if (port.error) { | ||
reject(port.error); | ||
} else { | ||
resolve(); | ||
} | ||
}, 10); | ||
}); | ||
} | ||
|
||
function migratePreferences(port, existingKeys) { | ||
if (existingKeys.length === 0) { | ||
return Promise.resolve(); | ||
} | ||
let key = existingKeys.pop(); | ||
return migratePreference(port, key).then(function() { | ||
return migratePreferences(port, existingKeys); | ||
}); | ||
} | ||
|
||
exports.setSyncLegacyDataPort = function(port) { | ||
|
||
// Get all and broadcast to webext | ||
// Get all and broadcast to webext one at a time | ||
let existingKeys = prefs.keys(prefRoot); | ||
let xkitStorage = {}; | ||
for (let properName of existingKeys) { | ||
let name = properName.substring(prefRoot.length); | ||
let value = prefs.get(properName, null); | ||
xkitStorage[name] = value; | ||
} | ||
port.postMessage(xkitStorage); | ||
|
||
migratePreferences(port, existingKeys).then(() => { | ||
return port.postMessage({isProperlyMigrated: true}); | ||
}).catch(err => { | ||
console.error('preferences not migrated', err); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
/* global browser */ | ||
/* global browser, Promise */ | ||
|
||
// From the MDN WebExtensions hybrid addon example | ||
|
||
// Ask to the legacy part to dump the needed data and send it back | ||
// to the background page... | ||
var port = browser.runtime.connect({name: "sync-legacy-addon-data"}); | ||
port.onMessage.addListener((msg) => { | ||
if (msg) { | ||
browser.storage.local.get('isMigrated').then(function(item) { | ||
if (item.isMigrated) { | ||
console.warn('Skipping migration'); | ||
return; | ||
browser.storage.local.get('isProperlyMigrated').then(function(item) { | ||
if (item.isProperlyMigrated) { | ||
console.warn('Skipping migration', item); | ||
return; | ||
} | ||
|
||
var port = browser.runtime.connect({name: "sync-legacy-addon-data"}); | ||
function onMessage(msg) { | ||
if (!msg) { | ||
return; | ||
} | ||
let setPromises = Object.keys(msg).map(function(key) { | ||
return browser.storage.local.get({key: null}).then(function(msgItem) { | ||
if (typeof(msgItem[key]) === 'string') { | ||
if (msgItem[key].length > msg[key].length) { | ||
// Do not update the storage | ||
return Promise.resolve(); | ||
} | ||
} | ||
// The corresponding code in XKit is GM_setValue(name, value) -> set({name: value}) | ||
return browser.storage.local.set(msg); | ||
}); | ||
}); | ||
Promise.all(setPromises).catch(function(err) { | ||
let errorMessage = "XKit failed to migrate your preferences to the new storage system. The migration reported the following error messag: " + err + ". Please restart your browser to try again."; | ||
if (typeof(XKit) === 'undefined') { | ||
alert(errorMessage); | ||
} else { | ||
XKit.window.show("Storage migration in progress", errorMessage, "error", "<div class=\"xkit-button default\">OK</div>"); | ||
} | ||
// The corresponding code in XKit is GM_setValue(name, value) -> set({name: value}) | ||
msg.isMigrated = true; | ||
console.log('Migrating pref storage', msg); | ||
browser.storage.local.set(msg); | ||
browser.storage.local.set({isProperlyMigrated: false}); | ||
port.onMessage.removeListener(onMessage); | ||
}); | ||
} | ||
|
||
port.onMessage.addListener(onMessage); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters