1/* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4"use strict"; 5 6var EXPORTED_SYMBOLS = ["Addon", "STATE_ENABLED", "STATE_DISABLED"]; 7 8const { AddonManager } = ChromeUtils.import( 9 "resource://gre/modules/AddonManager.jsm" 10); 11const { AddonUtils } = ChromeUtils.import( 12 "resource://services-sync/addonutils.js" 13); 14const { Logger } = ChromeUtils.import("resource://tps/logger.jsm"); 15 16const STATE_ENABLED = 1; 17const STATE_DISABLED = 2; 18 19function Addon(TPS, id) { 20 this.TPS = TPS; 21 this.id = id; 22} 23 24Addon.prototype = { 25 addon: null, 26 27 async uninstall() { 28 // find our addon locally 29 let addon = await AddonManager.getAddonByID(this.id); 30 Logger.AssertTrue( 31 !!addon, 32 "could not find addon " + this.id + " to uninstall" 33 ); 34 await AddonUtils.uninstallAddon(addon); 35 }, 36 37 async find(state) { 38 let addon = await AddonManager.getAddonByID(this.id); 39 40 if (!addon) { 41 Logger.logInfo("Could not find add-on with ID: " + this.id); 42 return false; 43 } 44 45 this.addon = addon; 46 47 Logger.logInfo( 48 "add-on found: " + addon.id + ", enabled: " + !addon.userDisabled 49 ); 50 if (state == STATE_ENABLED) { 51 Logger.AssertFalse(addon.userDisabled, "add-on is disabled: " + addon.id); 52 return true; 53 } else if (state == STATE_DISABLED) { 54 Logger.AssertTrue(addon.userDisabled, "add-on is enabled: " + addon.id); 55 return true; 56 } else if (state) { 57 throw new Error("Don't know how to handle state: " + state); 58 } else { 59 // No state, so just checking that it exists. 60 return true; 61 } 62 }, 63 64 async install() { 65 // For Install, the id parameter initially passed is really the filename 66 // for the addon's install .xml; we'll read the actual id from the .xml. 67 68 const result = await AddonUtils.installAddons([ 69 { id: this.id, requireSecureURI: false }, 70 ]); 71 72 Logger.AssertEqual( 73 1, 74 result.installedIDs.length, 75 "Exactly 1 add-on was installed." 76 ); 77 Logger.AssertEqual( 78 this.id, 79 result.installedIDs[0], 80 "Add-on was installed successfully: " + this.id 81 ); 82 }, 83 84 async setEnabled(flag) { 85 Logger.AssertTrue(await this.find(), "Add-on is available."); 86 87 let userDisabled; 88 if (flag == STATE_ENABLED) { 89 userDisabled = false; 90 } else if (flag == STATE_DISABLED) { 91 userDisabled = true; 92 } else { 93 throw new Error("Unknown flag to setEnabled: " + flag); 94 } 95 96 AddonUtils.updateUserDisabled(this.addon, userDisabled); 97 98 return true; 99 }, 100}; 101