browser/base/content/utilityOverlay.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/utilityOverlay.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,686 @@
     1.4 +# -*- Mode: javascript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 +
     1.9 +// Services = object with smart getters for common XPCOM services
    1.10 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.11 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.12 +Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
    1.13 +Components.utils.import("resource:///modules/RecentWindow.jsm");
    1.14 +
    1.15 +XPCOMUtils.defineLazyGetter(this, "BROWSER_NEW_TAB_URL", function () {
    1.16 +  const PREF = "browser.newtab.url";
    1.17 +
    1.18 +  function getNewTabPageURL() {
    1.19 +    if (!Services.prefs.prefHasUserValue(PREF)) {
    1.20 +      if (PrivateBrowsingUtils.isWindowPrivate(window) &&
    1.21 +          !PrivateBrowsingUtils.permanentPrivateBrowsing)
    1.22 +        return "about:privatebrowsing";
    1.23 +    }
    1.24 +    let url = Services.prefs.getComplexValue(PREF, Ci.nsISupportsString).data;
    1.25 +    return url || "about:blank";
    1.26 +  }
    1.27 +
    1.28 +  function update() {
    1.29 +    BROWSER_NEW_TAB_URL = getNewTabPageURL();
    1.30 +  }
    1.31 +
    1.32 +  Services.prefs.addObserver(PREF, update, false);
    1.33 +
    1.34 +  addEventListener("unload", function onUnload() {
    1.35 +    removeEventListener("unload", onUnload);
    1.36 +    Services.prefs.removeObserver(PREF, update);
    1.37 +  });
    1.38 +
    1.39 +  return getNewTabPageURL();
    1.40 +});
    1.41 +
    1.42 +var TAB_DROP_TYPE = "application/x-moz-tabbrowser-tab";
    1.43 +
    1.44 +var gBidiUI = false;
    1.45 +
    1.46 +/**
    1.47 + * Determines whether the given url is considered a special URL for new tabs.
    1.48 + */
    1.49 +function isBlankPageURL(aURL) {
    1.50 +  return aURL == "about:blank" || aURL == BROWSER_NEW_TAB_URL;
    1.51 +}
    1.52 +
    1.53 +function getBrowserURL()
    1.54 +{
    1.55 +  return "chrome://browser/content/browser.xul";
    1.56 +}
    1.57 +
    1.58 +function getTopWin(skipPopups) {
    1.59 +  // If this is called in a browser window, use that window regardless of
    1.60 +  // whether it's the frontmost window, since commands can be executed in
    1.61 +  // background windows (bug 626148).
    1.62 +  if (top.document.documentElement.getAttribute("windowtype") == "navigator:browser" &&
    1.63 +      (!skipPopups || top.toolbar.visible))
    1.64 +    return top;
    1.65 +
    1.66 +  let isPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
    1.67 +  return RecentWindow.getMostRecentBrowserWindow({private: isPrivate,
    1.68 +                                                  allowPopups: !skipPopups});
    1.69 +}
    1.70 +
    1.71 +function openTopWin(url) {
    1.72 +  /* deprecated */
    1.73 +  openUILinkIn(url, "current");
    1.74 +}
    1.75 +
    1.76 +function getBoolPref(prefname, def)
    1.77 +{
    1.78 +  try {
    1.79 +    return Services.prefs.getBoolPref(prefname);
    1.80 +  }
    1.81 +  catch(er) {
    1.82 +    return def;
    1.83 +  }
    1.84 +}
    1.85 +
    1.86 +/* openUILink handles clicks on UI elements that cause URLs to load.
    1.87 + *
    1.88 + * As the third argument, you may pass an object with the same properties as
    1.89 + * accepted by openUILinkIn, plus "ignoreButton" and "ignoreAlt".
    1.90 + */
    1.91 +function openUILink(url, event, aIgnoreButton, aIgnoreAlt, aAllowThirdPartyFixup,
    1.92 +                    aPostData, aReferrerURI) {
    1.93 +  let params;
    1.94 +
    1.95 +  if (aIgnoreButton && typeof aIgnoreButton == "object") {
    1.96 +    params = aIgnoreButton;
    1.97 +
    1.98 +    // don't forward "ignoreButton" and "ignoreAlt" to openUILinkIn
    1.99 +    aIgnoreButton = params.ignoreButton;
   1.100 +    aIgnoreAlt = params.ignoreAlt;
   1.101 +    delete params.ignoreButton;
   1.102 +    delete params.ignoreAlt;
   1.103 +  } else {
   1.104 +    params = {
   1.105 +      allowThirdPartyFixup: aAllowThirdPartyFixup,
   1.106 +      postData: aPostData,
   1.107 +      referrerURI: aReferrerURI,
   1.108 +      initiatingDoc: event ? event.target.ownerDocument : null
   1.109 +    };
   1.110 +  }
   1.111 +
   1.112 +  let where = whereToOpenLink(event, aIgnoreButton, aIgnoreAlt);
   1.113 +  openUILinkIn(url, where, params);
   1.114 +}
   1.115 +
   1.116 +
   1.117 +/* whereToOpenLink() looks at an event to decide where to open a link.
   1.118 + *
   1.119 + * The event may be a mouse event (click, double-click, middle-click) or keypress event (enter).
   1.120 + *
   1.121 + * On Windows, the modifiers are:
   1.122 + * Ctrl        new tab, selected
   1.123 + * Shift       new window
   1.124 + * Ctrl+Shift  new tab, in background
   1.125 + * Alt         save
   1.126 + *
   1.127 + * Middle-clicking is the same as Ctrl+clicking (it opens a new tab).
   1.128 + *
   1.129 + * Exceptions: 
   1.130 + * - Alt is ignored for menu items selected using the keyboard so you don't accidentally save stuff.  
   1.131 + *    (Currently, the Alt isn't sent here at all for menu items, but that will change in bug 126189.)
   1.132 + * - Alt is hard to use in context menus, because pressing Alt closes the menu.
   1.133 + * - Alt can't be used on the bookmarks toolbar because Alt is used for "treat this as something draggable".
   1.134 + * - The button is ignored for the middle-click-paste-URL feature, since it's always a middle-click.
   1.135 + */
   1.136 +function whereToOpenLink( e, ignoreButton, ignoreAlt )
   1.137 +{
   1.138 +  // This method must treat a null event like a left click without modifier keys (i.e.
   1.139 +  // e = { shiftKey:false, ctrlKey:false, metaKey:false, altKey:false, button:0 })
   1.140 +  // for compatibility purposes.
   1.141 +  if (!e)
   1.142 +    return "current";
   1.143 +
   1.144 +  var shift = e.shiftKey;
   1.145 +  var ctrl =  e.ctrlKey;
   1.146 +  var meta =  e.metaKey;
   1.147 +  var alt  =  e.altKey && !ignoreAlt;
   1.148 +
   1.149 +  // ignoreButton allows "middle-click paste" to use function without always opening in a new window.
   1.150 +  var middle = !ignoreButton && e.button == 1;
   1.151 +  var middleUsesTabs = getBoolPref("browser.tabs.opentabfor.middleclick", true);
   1.152 +
   1.153 +  // Don't do anything special with right-mouse clicks.  They're probably clicks on context menu items.
   1.154 +
   1.155 +#ifdef XP_MACOSX
   1.156 +  if (meta || (middle && middleUsesTabs))
   1.157 +#else
   1.158 +  if (ctrl || (middle && middleUsesTabs))
   1.159 +#endif
   1.160 +    return shift ? "tabshifted" : "tab";
   1.161 +
   1.162 +  if (alt && getBoolPref("browser.altClickSave", false))
   1.163 +    return "save";
   1.164 +
   1.165 +  if (shift || (middle && !middleUsesTabs))
   1.166 +    return "window";
   1.167 +
   1.168 +  return "current";
   1.169 +}
   1.170 +
   1.171 +/* openUILinkIn opens a URL in a place specified by the parameter |where|.
   1.172 + *
   1.173 + * |where| can be:
   1.174 + *  "current"     current tab            (if there aren't any browser windows, then in a new window instead)
   1.175 + *  "tab"         new tab                (if there aren't any browser windows, then in a new window instead)
   1.176 + *  "tabshifted"  same as "tab" but in background if default is to select new tabs, and vice versa
   1.177 + *  "window"      new window
   1.178 + *  "save"        save to disk (with no filename hint!)
   1.179 + *
   1.180 + * aAllowThirdPartyFixup controls whether third party services such as Google's
   1.181 + * I Feel Lucky are allowed to interpret this URL. This parameter may be
   1.182 + * undefined, which is treated as false.
   1.183 + *
   1.184 + * Instead of aAllowThirdPartyFixup, you may also pass an object with any of
   1.185 + * these properties:
   1.186 + *   allowThirdPartyFixup (boolean)
   1.187 + *   postData             (nsIInputStream)
   1.188 + *   referrerURI          (nsIURI)
   1.189 + *   relatedToCurrent     (boolean)
   1.190 + *   skipTabAnimation     (boolean)
   1.191 + */
   1.192 +function openUILinkIn(url, where, aAllowThirdPartyFixup, aPostData, aReferrerURI) {
   1.193 +  var params;
   1.194 +
   1.195 +  if (arguments.length == 3 && typeof arguments[2] == "object") {
   1.196 +    params = aAllowThirdPartyFixup;
   1.197 +  } else {
   1.198 +    params = {
   1.199 +      allowThirdPartyFixup: aAllowThirdPartyFixup,
   1.200 +      postData: aPostData,
   1.201 +      referrerURI: aReferrerURI
   1.202 +    };
   1.203 +  }
   1.204 +
   1.205 +  params.fromChrome = true;
   1.206 +
   1.207 +  openLinkIn(url, where, params);
   1.208 +}
   1.209 +
   1.210 +function openLinkIn(url, where, params) {
   1.211 +  if (!where || !url)
   1.212 +    return;
   1.213 +
   1.214 +  var aFromChrome           = params.fromChrome;
   1.215 +  var aAllowThirdPartyFixup = params.allowThirdPartyFixup;
   1.216 +  var aPostData             = params.postData;
   1.217 +  var aCharset              = params.charset;
   1.218 +  var aReferrerURI          = params.referrerURI;
   1.219 +  var aRelatedToCurrent     = params.relatedToCurrent;
   1.220 +  var aDisableMCB           = params.disableMCB;
   1.221 +  var aInBackground         = params.inBackground;
   1.222 +  var aDisallowInheritPrincipal = params.disallowInheritPrincipal;
   1.223 +  var aInitiatingDoc        = params.initiatingDoc;
   1.224 +  var aIsPrivate            = params.private;
   1.225 +  var aSkipTabAnimation     = params.skipTabAnimation;
   1.226 +
   1.227 +  if (where == "save") {
   1.228 +    if (!aInitiatingDoc) {
   1.229 +      Components.utils.reportError("openUILink/openLinkIn was called with " +
   1.230 +        "where == 'save' but without initiatingDoc.  See bug 814264.");
   1.231 +      return;
   1.232 +    }
   1.233 +    saveURL(url, null, null, true, null, aReferrerURI, aInitiatingDoc);
   1.234 +    return;
   1.235 +  }
   1.236 +  const Cc = Components.classes;
   1.237 +  const Ci = Components.interfaces;
   1.238 +
   1.239 +  var w = getTopWin();
   1.240 +  if ((where == "tab" || where == "tabshifted") &&
   1.241 +      w && !w.toolbar.visible) {
   1.242 +    w = getTopWin(true);
   1.243 +    aRelatedToCurrent = false;
   1.244 +  }
   1.245 +
   1.246 +  if (!w || where == "window") {
   1.247 +    var sa = Cc["@mozilla.org/supports-array;1"].
   1.248 +             createInstance(Ci.nsISupportsArray);
   1.249 +
   1.250 +    var wuri = Cc["@mozilla.org/supports-string;1"].
   1.251 +               createInstance(Ci.nsISupportsString);
   1.252 +    wuri.data = url;
   1.253 +
   1.254 +    let charset = null;
   1.255 +    if (aCharset) {
   1.256 +      charset = Cc["@mozilla.org/supports-string;1"]
   1.257 +                  .createInstance(Ci.nsISupportsString);
   1.258 +      charset.data = "charset=" + aCharset;
   1.259 +    }
   1.260 +
   1.261 +    var allowThirdPartyFixupSupports = Cc["@mozilla.org/supports-PRBool;1"].
   1.262 +                                       createInstance(Ci.nsISupportsPRBool);
   1.263 +    allowThirdPartyFixupSupports.data = aAllowThirdPartyFixup;
   1.264 +
   1.265 +    sa.AppendElement(wuri);
   1.266 +    sa.AppendElement(charset);
   1.267 +    sa.AppendElement(aReferrerURI);
   1.268 +    sa.AppendElement(aPostData);
   1.269 +    sa.AppendElement(allowThirdPartyFixupSupports);
   1.270 +
   1.271 +    let features = "chrome,dialog=no,all";
   1.272 +    if (aIsPrivate) {
   1.273 +      features += ",private";
   1.274 +    }
   1.275 +
   1.276 +    Services.ww.openWindow(w || window, getBrowserURL(), null, features, sa);
   1.277 +    return;
   1.278 +  }
   1.279 +
   1.280 +  let loadInBackground = where == "current" ? false : aInBackground;
   1.281 +  if (loadInBackground == null) {
   1.282 +    loadInBackground = aFromChrome ?
   1.283 +                         false :
   1.284 +                         getBoolPref("browser.tabs.loadInBackground");
   1.285 +  }
   1.286 +
   1.287 +  if (where == "current" && w.gBrowser.selectedTab.pinned) {
   1.288 +    try {
   1.289 +      let uriObj = Services.io.newURI(url, null, null);
   1.290 +      if (!uriObj.schemeIs("javascript") &&
   1.291 +          w.gBrowser.currentURI.host != uriObj.host) {
   1.292 +        where = "tab";
   1.293 +        loadInBackground = false;
   1.294 +      }
   1.295 +    } catch (err) {
   1.296 +      where = "tab";
   1.297 +      loadInBackground = false;
   1.298 +    }
   1.299 +  }
   1.300 +
   1.301 +  // Raise the target window before loading the URI, since loading it may
   1.302 +  // result in a new frontmost window (e.g. "javascript:window.open('');").
   1.303 +  w.focus();
   1.304 +
   1.305 +  switch (where) {
   1.306 +  case "current":
   1.307 +    let flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
   1.308 +    if (aAllowThirdPartyFixup) {
   1.309 +      flags |= Ci.nsIWebNavigation.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
   1.310 +      flags |= Ci.nsIWebNavigation.LOAD_FLAGS_FIXUP_SCHEME_TYPOS;
   1.311 +    }
   1.312 +    if (aDisallowInheritPrincipal)
   1.313 +      flags |= Ci.nsIWebNavigation.LOAD_FLAGS_DISALLOW_INHERIT_OWNER;
   1.314 +    w.gBrowser.loadURIWithFlags(url, flags, aReferrerURI, null, aPostData);
   1.315 +    break;
   1.316 +  case "tabshifted":
   1.317 +    loadInBackground = !loadInBackground;
   1.318 +    // fall through
   1.319 +  case "tab":
   1.320 +    let browser = w.gBrowser;
   1.321 +    browser.loadOneTab(url, {
   1.322 +                       referrerURI: aReferrerURI,
   1.323 +                       charset: aCharset,
   1.324 +                       postData: aPostData,
   1.325 +                       inBackground: loadInBackground,
   1.326 +                       allowThirdPartyFixup: aAllowThirdPartyFixup,
   1.327 +                       relatedToCurrent: aRelatedToCurrent,
   1.328 +                       skipAnimation: aSkipTabAnimation,
   1.329 +                       disableMCB: aDisableMCB});
   1.330 +    break;
   1.331 +  }
   1.332 +
   1.333 +  w.gBrowser.selectedBrowser.focus();
   1.334 +
   1.335 +  if (!loadInBackground && w.isBlankPageURL(url))
   1.336 +    w.focusAndSelectUrlBar();
   1.337 +}
   1.338 +
   1.339 +// Used as an onclick handler for UI elements with link-like behavior.
   1.340 +// e.g. onclick="checkForMiddleClick(this, event);"
   1.341 +function checkForMiddleClick(node, event) {
   1.342 +  // We should be using the disabled property here instead of the attribute,
   1.343 +  // but some elements that this function is used with don't support it (e.g.
   1.344 +  // menuitem).
   1.345 +  if (node.getAttribute("disabled") == "true")
   1.346 +    return; // Do nothing
   1.347 +
   1.348 +  if (event.button == 1) {
   1.349 +    /* Execute the node's oncommand or command.
   1.350 +     *
   1.351 +     * XXX: we should use node.oncommand(event) once bug 246720 is fixed.
   1.352 +     */
   1.353 +    var target = node.hasAttribute("oncommand") ? node :
   1.354 +                 node.ownerDocument.getElementById(node.getAttribute("command"));
   1.355 +    var fn = new Function("event", target.getAttribute("oncommand"));
   1.356 +    fn.call(target, event);
   1.357 +
   1.358 +    // If the middle-click was on part of a menu, close the menu.
   1.359 +    // (Menus close automatically with left-click but not with middle-click.)
   1.360 +    closeMenus(event.target);
   1.361 +  }
   1.362 +}
   1.363 +
   1.364 +// Closes all popups that are ancestors of the node.
   1.365 +function closeMenus(node)
   1.366 +{
   1.367 +  if ("tagName" in node) {
   1.368 +    if (node.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   1.369 +    && (node.tagName == "menupopup" || node.tagName == "popup"))
   1.370 +      node.hidePopup();
   1.371 +
   1.372 +    closeMenus(node.parentNode);
   1.373 +  }
   1.374 +}
   1.375 +
   1.376 +// Gather all descendent text under given document node.
   1.377 +function gatherTextUnder ( root ) 
   1.378 +{
   1.379 +  var text = "";
   1.380 +  var node = root.firstChild;
   1.381 +  var depth = 1;
   1.382 +  while ( node && depth > 0 ) {
   1.383 +    // See if this node is text.
   1.384 +    if ( node.nodeType == Node.TEXT_NODE ) {
   1.385 +      // Add this text to our collection.
   1.386 +      text += " " + node.data;
   1.387 +    } else if ( node instanceof HTMLImageElement) {
   1.388 +      // If it has an "alt" attribute, add that.
   1.389 +      var altText = node.getAttribute( "alt" );
   1.390 +      if ( altText && altText != "" ) {
   1.391 +        text += " " + altText;
   1.392 +      }
   1.393 +    }
   1.394 +    // Find next node to test.
   1.395 +    // First, see if this node has children.
   1.396 +    if ( node.hasChildNodes() ) {
   1.397 +      // Go to first child.
   1.398 +      node = node.firstChild;
   1.399 +      depth++;
   1.400 +    } else {
   1.401 +      // No children, try next sibling (or parent next sibling).
   1.402 +      while ( depth > 0 && !node.nextSibling ) {
   1.403 +        node = node.parentNode;
   1.404 +        depth--;
   1.405 +      }
   1.406 +      if ( node.nextSibling ) {
   1.407 +        node = node.nextSibling;
   1.408 +      }
   1.409 +    }
   1.410 +  }
   1.411 +  // Strip leading and tailing whitespace.
   1.412 +  text = text.trim();
   1.413 +  // Compress remaining whitespace.
   1.414 +  text = text.replace( /\s+/g, " " );
   1.415 +  return text;
   1.416 +}
   1.417 +
   1.418 +function getShellService()
   1.419 +{
   1.420 +  var shell = null;
   1.421 +  try {
   1.422 +    shell = Components.classes["@mozilla.org/browser/shell-service;1"]
   1.423 +      .getService(Components.interfaces.nsIShellService);
   1.424 +  } catch (e) {
   1.425 +  }
   1.426 +  return shell;
   1.427 +}
   1.428 +
   1.429 +function isBidiEnabled() {
   1.430 +  // first check the pref.
   1.431 +  if (getBoolPref("bidi.browser.ui", false))
   1.432 +    return true;
   1.433 +
   1.434 +  // then check intl.uidirection.<locale>
   1.435 +  var chromeReg = Components.classes["@mozilla.org/chrome/chrome-registry;1"].
   1.436 +                  getService(Components.interfaces.nsIXULChromeRegistry);
   1.437 +  if (chromeReg.isLocaleRTL("global"))
   1.438 +    return true;
   1.439 +
   1.440 +  // now see if the system locale is an RTL one.
   1.441 +  var rv = false;
   1.442 +
   1.443 +  try {
   1.444 +    var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"]
   1.445 +                                  .getService(Components.interfaces.nsILocaleService);
   1.446 +    var systemLocale = localeService.getSystemLocale().getCategory("NSILOCALE_CTYPE").substr(0,3);
   1.447 +
   1.448 +    switch (systemLocale) {
   1.449 +      case "ar-":
   1.450 +      case "he-":
   1.451 +      case "fa-":
   1.452 +      case "ug-":
   1.453 +      case "ur-":
   1.454 +      case "syr":
   1.455 +        rv = true;
   1.456 +        Services.prefs.setBoolPref("bidi.browser.ui", true);
   1.457 +    }
   1.458 +  } catch (e) {}
   1.459 +
   1.460 +  return rv;
   1.461 +}
   1.462 +
   1.463 +function openAboutDialog() {
   1.464 +  var enumerator = Services.wm.getEnumerator("Browser:About");
   1.465 +  while (enumerator.hasMoreElements()) {
   1.466 +    // Only open one about window (Bug 599573)
   1.467 +    let win = enumerator.getNext();
   1.468 +    if (win.closed) {
   1.469 +      continue;
   1.470 +    }
   1.471 +    win.focus();
   1.472 +    return;
   1.473 +  }
   1.474 +
   1.475 +#ifdef XP_WIN
   1.476 +  var features = "chrome,centerscreen,dependent";
   1.477 +#elifdef XP_MACOSX
   1.478 +  var features = "chrome,resizable=no,minimizable=no";
   1.479 +#else
   1.480 +  var features = "chrome,centerscreen,dependent,dialog=no";
   1.481 +#endif
   1.482 +  window.openDialog("chrome://browser/content/aboutDialog.xul", "", features);
   1.483 +}
   1.484 +
   1.485 +function openPreferences(paneID, extraArgs)
   1.486 +{
   1.487 +  function switchToAdvancedSubPane(doc) {
   1.488 +    if (extraArgs && extraArgs["advancedTab"]) {
   1.489 +      let advancedPaneTabs = doc.getElementById("advancedPrefs");
   1.490 +      advancedPaneTabs.selectedTab = doc.getElementById(extraArgs["advancedTab"]);
   1.491 +    }
   1.492 +  }
   1.493 +
   1.494 +  if (getBoolPref("browser.preferences.inContent")) {
   1.495 +    let win = Services.wm.getMostRecentWindow("navigator:browser");
   1.496 +    if (!win) {
   1.497 +      return;
   1.498 +    }
   1.499 +
   1.500 +    let newLoad = !win.switchToTabHavingURI("about:preferences", true);
   1.501 +    let browser = win.gBrowser.selectedBrowser;
   1.502 +
   1.503 +    function switchToPane() {
   1.504 +      if (paneID) {
   1.505 +        browser.contentWindow.selectCategory(paneID);
   1.506 +      }
   1.507 +      switchToAdvancedSubPane(browser.contentDocument);
   1.508 +    }
   1.509 +
   1.510 +    if (newLoad) {
   1.511 +      browser.addEventListener("load", function onload() {
   1.512 +        browser.removeEventListener("load", onload, true);
   1.513 +        switchToPane();
   1.514 +      }, true);
   1.515 +    } else {
   1.516 +      switchToPane();
   1.517 +    }
   1.518 +  } else {
   1.519 +    var instantApply = getBoolPref("browser.preferences.instantApply", false);
   1.520 +    var features = "chrome,titlebar,toolbar,centerscreen" + (instantApply ? ",dialog=no" : ",modal");
   1.521 +
   1.522 +    var win = Services.wm.getMostRecentWindow("Browser:Preferences");
   1.523 +    if (win) {
   1.524 +      win.focus();
   1.525 +      if (paneID) {
   1.526 +        var pane = win.document.getElementById(paneID);
   1.527 +        win.document.documentElement.showPane(pane);
   1.528 +      }
   1.529 +
   1.530 +      switchToAdvancedSubPane(win.document);
   1.531 +    } else {
   1.532 +      openDialog("chrome://browser/content/preferences/preferences.xul",
   1.533 +                 "Preferences", features, paneID, extraArgs);
   1.534 +    }
   1.535 +  }
   1.536 +}
   1.537 +
   1.538 +function openAdvancedPreferences(tabID)
   1.539 +{
   1.540 +  openPreferences("paneAdvanced", { "advancedTab" : tabID });
   1.541 +}
   1.542 +
   1.543 +/**
   1.544 + * Opens the troubleshooting information (about:support) page for this version
   1.545 + * of the application.
   1.546 + */
   1.547 +function openTroubleshootingPage()
   1.548 +{
   1.549 +  openUILinkIn("about:support", "tab");
   1.550 +}
   1.551 +
   1.552 +#ifdef MOZ_SERVICES_HEALTHREPORT
   1.553 +/**
   1.554 + * Opens the troubleshooting information (about:support) page for this version
   1.555 + * of the application.
   1.556 + */
   1.557 +function openHealthReport()
   1.558 +{
   1.559 +  openUILinkIn("about:healthreport", "tab");
   1.560 +}
   1.561 +#endif
   1.562 +
   1.563 +/**
   1.564 + * Opens the feedback page for this version of the application.
   1.565 + */
   1.566 +function openFeedbackPage()
   1.567 +{
   1.568 +  var url = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
   1.569 +                      .getService(Components.interfaces.nsIURLFormatter)
   1.570 +                      .formatURLPref("app.feedback.baseURL");
   1.571 +  openUILinkIn(url, "tab");
   1.572 +}
   1.573 +
   1.574 +function openTourPage()
   1.575 +{
   1.576 +  let scope = {}
   1.577 +  Components.utils.import("resource:///modules/UITour.jsm", scope);
   1.578 +  openUILinkIn(scope.UITour.url, "tab");
   1.579 +}
   1.580 +
   1.581 +function buildHelpMenu()
   1.582 +{
   1.583 +  // Enable/disable the "Report Web Forgery" menu item.
   1.584 +  if (typeof gSafeBrowsing != "undefined")
   1.585 +    gSafeBrowsing.setReportPhishingMenu();
   1.586 +}
   1.587 +
   1.588 +function isElementVisible(aElement)
   1.589 +{
   1.590 +  if (!aElement)
   1.591 +    return false;
   1.592 +
   1.593 +  // If aElement or a direct or indirect parent is hidden or collapsed,
   1.594 +  // height, width or both will be 0.
   1.595 +  var bo = aElement.boxObject;
   1.596 +  return (bo.height > 0 && bo.width > 0);
   1.597 +}
   1.598 +
   1.599 +function makeURLAbsolute(aBase, aUrl)
   1.600 +{
   1.601 +  // Note:  makeURI() will throw if aUri is not a valid URI
   1.602 +  return makeURI(aUrl, null, makeURI(aBase)).spec;
   1.603 +}
   1.604 +
   1.605 +/**
   1.606 + * openNewTabWith: opens a new tab with the given URL.
   1.607 + *
   1.608 + * @param aURL
   1.609 + *        The URL to open (as a string).
   1.610 + * @param aDocument
   1.611 + *        Note this parameter is now ignored. There is no security check & no
   1.612 + *        referrer header derived from aDocument (null case).
   1.613 + * @param aPostData
   1.614 + *        Form POST data, or null.
   1.615 + * @param aEvent
   1.616 + *        The triggering event (for the purpose of determining whether to open
   1.617 + *        in the background), or null.
   1.618 + * @param aAllowThirdPartyFixup
   1.619 + *        If true, then we allow the URL text to be sent to third party services
   1.620 + *        (e.g., Google's I Feel Lucky) for interpretation. This parameter may
   1.621 + *        be undefined in which case it is treated as false.
   1.622 + * @param [optional] aReferrer
   1.623 + *        This will be used as the referrer. There will be no security check.
   1.624 + */ 
   1.625 +function openNewTabWith(aURL, aDocument, aPostData, aEvent,
   1.626 +                        aAllowThirdPartyFixup, aReferrer) {
   1.627 +
   1.628 +  // As in openNewWindowWith(), we want to pass the charset of the
   1.629 +  // current document over to a new tab.
   1.630 +  let originCharset = null;
   1.631 +  if (document.documentElement.getAttribute("windowtype") == "navigator:browser")
   1.632 +    originCharset = gBrowser.selectedBrowser.characterSet;
   1.633 +
   1.634 +  openLinkIn(aURL, aEvent && aEvent.shiftKey ? "tabshifted" : "tab",
   1.635 +             { charset: originCharset,
   1.636 +               postData: aPostData,
   1.637 +               allowThirdPartyFixup: aAllowThirdPartyFixup,
   1.638 +               referrerURI: aReferrer });
   1.639 +}
   1.640 +
   1.641 +/**
   1.642 + * @param aDocument
   1.643 + *        Note this parameter is ignored. See openNewTabWith()
   1.644 + */
   1.645 +function openNewWindowWith(aURL, aDocument, aPostData, aAllowThirdPartyFixup, aReferrer) {
   1.646 +  // Extract the current charset menu setting from the current document and
   1.647 +  // use it to initialize the new browser window...
   1.648 +  let originCharset = null;
   1.649 +  if (document.documentElement.getAttribute("windowtype") == "navigator:browser")
   1.650 +    originCharset = gBrowser.selectedBrowser.characterSet;
   1.651 +
   1.652 +  openLinkIn(aURL, "window",
   1.653 +             { charset: originCharset,
   1.654 +               postData: aPostData,
   1.655 +               allowThirdPartyFixup: aAllowThirdPartyFixup,
   1.656 +               referrerURI: aReferrer });
   1.657 +}
   1.658 +
   1.659 +// aCalledFromModal is optional
   1.660 +function openHelpLink(aHelpTopic, aCalledFromModal, aWhere) {
   1.661 +  var url = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
   1.662 +                      .getService(Components.interfaces.nsIURLFormatter)
   1.663 +                      .formatURLPref("app.support.baseURL");
   1.664 +  url += aHelpTopic;
   1.665 +
   1.666 +  var where = aWhere;
   1.667 +  if (!aWhere)
   1.668 +    where = aCalledFromModal ? "window" : "tab";
   1.669 +
   1.670 +  openUILinkIn(url, where);
   1.671 +}
   1.672 +
   1.673 +function openPrefsHelp() {
   1.674 +  // non-instant apply prefwindows are usually modal, so we can't open in the topmost window, 
   1.675 +  // since its probably behind the window.
   1.676 +  var instantApply = getBoolPref("browser.preferences.instantApply");
   1.677 +
   1.678 +  var helpTopic = document.getElementsByTagName("prefwindow")[0].currentPane.helpTopic;
   1.679 +  openHelpLink(helpTopic, !instantApply);
   1.680 +}
   1.681 +
   1.682 +function trimURL(aURL) {
   1.683 +  // This function must not modify the given URL such that calling
   1.684 +  // nsIURIFixup::createFixupURI with the result will produce a different URI.
   1.685 +  return aURL /* remove single trailing slash for http/https/ftp URLs */
   1.686 +             .replace(/^((?:http|https|ftp):\/\/[^/]+)\/$/, "$1")
   1.687 +              /* remove http:// unless the host starts with "ftp\d*\." or contains "@" */
   1.688 +             .replace(/^http:\/\/((?!ftp\d*\.)[^\/@]+(?:\/|$))/, "$1");
   1.689 +}

mercurial