Created
April 9, 2015 19:01
-
-
Save yzen/ce55479d7518743f11f6 to your computer and use it in GitHub Desktop.
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
function renameAddon(addon, name) { | |
if (!name) { | |
return Promise.reject('no name given'); | |
} | |
if (!isAddon(addon)) { | |
return Promise.reject('not an addon'); | |
} | |
var manifest = getManifest(addon); | |
if (name === manifest.name) { | |
return Promise.reject('name is unchanged'); | |
} | |
return new Promise(function(resolve, reject) { | |
addon.manifest.name = name; | |
addon.export().then(function(blob) { | |
var request = navigator.mozApps.mgmt.uninstall(addon); | |
request.onsuccess = function() { | |
install(blob).then(resolve).catch(reject); | |
}; | |
request.onerror = function() { reject(); }; | |
}).catch(reject); | |
}); | |
} | |
function install(blob) { | |
return new Promise(function(resolve, reject) { | |
// Save the blob to a file because we don't support importing memory blobs | |
var sdcard = navigator.getDeviceStorage('sdcard'); | |
// Delete the tempfile from the SD card. | |
var deleteRequest = sdcard.delete(ADDON_FILENAME); | |
deleteRequest.onsuccess = deleteRequest.onerror = function() { | |
// Add the addon blob to the SD card using the temp filename. | |
var addNamedRequest = sdcard.addNamed(blob, ADDON_FILENAME); | |
addNamedRequest.onsuccess = function() { | |
// Retrieve the new tempfile. | |
var getRequest = sdcard.get(ADDON_FILENAME); | |
getRequest.onsuccess = function () { | |
var addonFile = getRequest.result; | |
// Import the addon using the tempfile. | |
navigator.mozApps.mgmt.import(addonFile).then(function (addon) { | |
// Enable the addon by default. | |
enableAddon(addon); | |
resolve(addon); | |
}).catch(reject); | |
}; | |
getRequest.onerror = function (error) { reject(error); }; | |
}; | |
addNamedRequest.onerror = function(error) { reject(error); }; | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment