toolkit/mozapps/extensions/content/newaddon.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/content/newaddon.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +const Cu = Components.utils;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/Services.jsm");
    1.13 +Cu.import("resource://gre/modules/AddonManager.jsm");
    1.14 +
    1.15 +var gAddon = null;
    1.16 +
    1.17 +// If the user enables the add-on through some other UI close this window
    1.18 +var EnableListener = {
    1.19 +  onEnabling: function EnableListener_onEnabling(aAddon) {
    1.20 +    if (aAddon.id == gAddon.id)
    1.21 +      window.close();
    1.22 +  }
    1.23 +}
    1.24 +AddonManager.addAddonListener(EnableListener);
    1.25 +
    1.26 +function initialize() {
    1.27 +  // About URIs don't implement nsIURL so we have to find the query string
    1.28 +  // manually
    1.29 +  let spec = document.location.href;
    1.30 +  let pos = spec.indexOf("?");
    1.31 +  let query = "";
    1.32 +  if (pos >= 0)
    1.33 +    query = spec.substring(pos + 1);
    1.34 +
    1.35 +  // Just assume the query is "id=<id>"
    1.36 +  let id = query.substring(3);
    1.37 +  if (!id) {
    1.38 +    window.location = "about:blank";
    1.39 +    return;
    1.40 +  }
    1.41 +
    1.42 +  let bundle = Services.strings.createBundle("chrome://mozapps/locale/extensions/newaddon.properties");
    1.43 +
    1.44 +  AddonManager.getAddonByID(id, function initialize_getAddonByID(aAddon) {
    1.45 +    // If the add-on doesn't exist or it is already enabled or it cannot be
    1.46 +    // enabled then this UI is useless, just close it. This shouldn't normally
    1.47 +    // happen unless session restore restores the tab
    1.48 +    if (!aAddon || !aAddon.userDisabled ||
    1.49 +        !(aAddon.permissions & AddonManager.PERM_CAN_ENABLE)) {
    1.50 +      window.close();
    1.51 +      return;
    1.52 +    }
    1.53 +
    1.54 +    gAddon = aAddon;
    1.55 +
    1.56 +    document.getElementById("addon-info").setAttribute("type", aAddon.type);
    1.57 +
    1.58 +    let icon = document.getElementById("icon");
    1.59 +    if (aAddon.icon64URL)
    1.60 +      icon.src = aAddon.icon64URL;
    1.61 +    else if (aAddon.iconURL)
    1.62 +      icon.src = aAddon.iconURL;
    1.63 +
    1.64 +    let name = bundle.formatStringFromName("name", [aAddon.name, aAddon.version],
    1.65 +                                           2);
    1.66 +    document.getElementById("name").value = name;
    1.67 +
    1.68 +    if (aAddon.creator) {
    1.69 +      let creator = bundle.formatStringFromName("author", [aAddon.creator], 1);
    1.70 +      document.getElementById("author").value = creator;
    1.71 +    } else {
    1.72 +      document.getElementById("author").hidden = true;
    1.73 +    }
    1.74 +
    1.75 +    let uri = "getResourceURI" in aAddon ? aAddon.getResourceURI() : null;
    1.76 +    let locationLabel = document.getElementById("location");
    1.77 +    if (uri instanceof Ci.nsIFileURL) {
    1.78 +      let location = bundle.formatStringFromName("location", [uri.file.path], 1);
    1.79 +      locationLabel.value = location;
    1.80 +      locationLabel.setAttribute("tooltiptext", location);
    1.81 +    } else {
    1.82 +      document.getElementById("location").hidden = true;
    1.83 +    }
    1.84 +
    1.85 +    var event = document.createEvent("Events");
    1.86 +    event.initEvent("AddonDisplayed", true, true);
    1.87 +    document.dispatchEvent(event);
    1.88 +  });
    1.89 +}
    1.90 +
    1.91 +function unload() {
    1.92 +  AddonManager.removeAddonListener(EnableListener);
    1.93 +}
    1.94 +
    1.95 +function continueClicked() {
    1.96 +  AddonManager.removeAddonListener(EnableListener);
    1.97 +
    1.98 +  if (document.getElementById("allow").checked) {
    1.99 +    gAddon.userDisabled = false;
   1.100 +
   1.101 +    if (gAddon.pendingOperations & AddonManager.PENDING_ENABLE) {
   1.102 +      document.getElementById("allow").disabled = true;
   1.103 +      document.getElementById("buttonDeck").selectedPanel = document.getElementById("restartPanel");
   1.104 +      return;
   1.105 +    }
   1.106 +  }
   1.107 +
   1.108 +  window.close();
   1.109 +}
   1.110 +
   1.111 +function restartClicked() {
   1.112 +  let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].
   1.113 +                   createInstance(Ci.nsISupportsPRBool);
   1.114 +  Services.obs.notifyObservers(cancelQuit, "quit-application-requested",
   1.115 +                               "restart");
   1.116 +  if (cancelQuit.data)
   1.117 +    return; // somebody canceled our quit request
   1.118 +
   1.119 +  window.close();
   1.120 +
   1.121 +  let appStartup = Components.classes["@mozilla.org/toolkit/app-startup;1"].
   1.122 +                   getService(Components.interfaces.nsIAppStartup);
   1.123 +  appStartup.quit(Ci.nsIAppStartup.eAttemptQuit |  Ci.nsIAppStartup.eRestart);
   1.124 +}
   1.125 +
   1.126 +function cancelClicked() {
   1.127 +  gAddon.userDisabled = true;
   1.128 +  AddonManager.addAddonListener(EnableListener);
   1.129 +
   1.130 +  document.getElementById("allow").disabled = false;
   1.131 +  document.getElementById("buttonDeck").selectedPanel = document.getElementById("continuePanel");
   1.132 +}

mercurial