DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
// This is redefined below, for strange and unfortunate reasons.
var { PromptUtils } = ChromeUtils.import(
  "resource://gre/modules/SharedPromptUtils.jsm"
);

function Prompter() {
  // Note that EmbedPrompter clones this implementation.
}

Prompter.prototype = {
  classID: Components.ID("{1c978d25-b37f-43a8-a2d6-0c7a239ead87}"),
  QueryInterface: ChromeUtils.generateQI([
    Ci.nsIPromptFactory,
    Ci.nsIPromptService,
  ]),

  /* ----------  private members  ---------- */

  pickPrompter(domWin) {
    return new ModalPrompter(domWin);
  },

  /* ----------  nsIPromptFactory  ---------- */

  getPrompt(domWin, iid) {
    // This is still kind of dumb; the C++ code delegated to login manager
    // here, which in turn calls back into us via nsIPromptService.
    if (iid.equals(Ci.nsIAuthPrompt2) || iid.equals(Ci.nsIAuthPrompt)) {
      try {
        let pwmgr = Cc[
          "@mozilla.org/passwordmanager/authpromptfactory;1"
        ].getService(Ci.nsIPromptFactory);
        return pwmgr.getPrompt(domWin, iid);
      } catch (e) {
        Cu.reportError(
          "nsPrompter: Delegation to password manager failed: " + e
        );
      }
    }

    let p = new ModalPrompter(domWin);
    p.QueryInterface(iid);
    return p;
  },

  /* ----------  nsIPromptService  ---------- */

  alert(domWin, title, text) {
    let p = this.pickPrompter(domWin);
    p.alert(title, text);
  },

  alertCheck(domWin, title, text, checkLabel, checkValue) {
    let p = this.pickPrompter(domWin);
    p.alertCheck(title, text, checkLabel, checkValue);
  },

  confirm(domWin, title, text) {
    let p = this.pickPrompter(domWin);
    return p.confirm(title, text);
  },

  confirmCheck(domWin, title, text, checkLabel, checkValue) {
    let p = this.pickPrompter(domWin);
    return p.confirmCheck(title, text, checkLabel, checkValue);
  },

  confirmEx(
    domWin,
    title,
    text,
    flags,
    button0,
    button1,
    button2,
    checkLabel,
    checkValue
  ) {
    let p = this.pickPrompter(domWin);
    return p.confirmEx(
      title,
      text,
      flags,
      button0,
      button1,
      button2,
      checkLabel,
      checkValue
    );
  },

  prompt(domWin, title, text, value, checkLabel, checkValue) {
    let p = this.pickPrompter(domWin);
    return p.nsIPrompt_prompt(title, text, value, checkLabel, checkValue);
  },

  promptUsernameAndPassword(
    domWin,
    title,
    text,
    user,
    pass,
    checkLabel,
    checkValue
  ) {
    let p = this.pickPrompter(domWin);
    return p.nsIPrompt_promptUsernameAndPassword(
      title,
      text,
      user,
      pass,
      checkLabel,
      checkValue
    );
  },

  promptPassword(domWin, title, text, pass, checkLabel, checkValue) {
    let p = this.pickPrompter(domWin);
    return p.nsIPrompt_promptPassword(
      title,
      text,
      pass,
      checkLabel,
      checkValue
    );
  },

  select(domWin, title, text, list, selected) {
    let p = this.pickPrompter(domWin);
    return p.select(title, text, list, selected);
  },

  promptAuth(domWin, channel, level, authInfo, checkLabel, checkValue) {
    let p = this.pickPrompter(domWin);
    return p.promptAuth(channel, level, authInfo, checkLabel, checkValue);
  },

  asyncPromptAuth(
    domWin,
    channel,
    callback,
    context,
    level,
    authInfo,
    checkLabel,
    checkValue
  ) {
    let p = this.pickPrompter(domWin);
    return p.asyncPromptAuth(
      channel,
      callback,
      context,
      level,
      authInfo,
      checkLabel,
      checkValue
    );
  },
};

// Common utils not specific to a particular prompter style.
var PromptUtilsTemp = {
  __proto__: PromptUtils,

  getLocalizedString(key, formatArgs) {
    if (formatArgs) {
      return this.strBundle.formatStringFromName(key, formatArgs);
    }
    return this.strBundle.GetStringFromName(key);
  },

  confirmExHelper(flags, button0, button1, button2) {
    const BUTTON_DEFAULT_MASK = 0x03000000;
    let defaultButtonNum = (flags & BUTTON_DEFAULT_MASK) >> 24;
    let isDelayEnabled = flags & Ci.nsIPrompt.BUTTON_DELAY_ENABLE;

    // Flags can be used to select a specific pre-defined button label or
    // a caller-supplied string (button0/button1/button2). If no flags are
    // set for a button, then the button won't be shown.
    let argText = [button0, button1, button2];
    let buttonLabels = [null, null, null];
    for (let i = 0; i < 3; i++) {
      let buttonLabel;
      switch (flags & 0xff) {
        case Ci.nsIPrompt.BUTTON_TITLE_OK:
          buttonLabel = PromptUtils.getLocalizedString("OK");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_CANCEL:
          buttonLabel = PromptUtils.getLocalizedString("Cancel");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_YES:
          buttonLabel = PromptUtils.getLocalizedString("Yes");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_NO:
          buttonLabel = PromptUtils.getLocalizedString("No");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_SAVE:
          buttonLabel = PromptUtils.getLocalizedString("Save");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_DONT_SAVE:
          buttonLabel = PromptUtils.getLocalizedString("DontSave");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_REVERT:
          buttonLabel = PromptUtils.getLocalizedString("Revert");
          break;
        case Ci.nsIPrompt.BUTTON_TITLE_IS_STRING:
          buttonLabel = argText[i];
          break;
      }
      if (buttonLabel) {
        buttonLabels[i] = buttonLabel;
      }
      flags >>= 8;
    }

    return [
      buttonLabels[0],
      buttonLabels[1],
      buttonLabels[2],
      defaultButtonNum,
      isDelayEnabled,
    ];
  },

  getAuthInfo(authInfo) {
    let username, password;

    let flags = authInfo.flags;
    if (flags & Ci.nsIAuthInformation.NEED_DOMAIN && authInfo.domain) {
      username = authInfo.domain + "\\" + authInfo.username;
    } else {
      username = authInfo.username;
    }

    password = authInfo.password;

    return [username, password];
  },

  setAuthInfo(authInfo, username, password) {
    let flags = authInfo.flags;
    if (flags & Ci.nsIAuthInformation.NEED_DOMAIN) {
      // Domain is separated from username by a backslash
      let idx = username.indexOf("\\");
      if (idx == -1) {
        authInfo.username = username;
      } else {
        authInfo.domain = username.substring(0, idx);
        authInfo.username = username.substring(idx + 1);
      }
    } else {
      authInfo.username = username;
    }
    authInfo.password = password;
  },

  /**
   * Strip out things like userPass and path for display.
   */
  getFormattedHostname(uri) {
    return uri.scheme + "://" + uri.hostPort;
  },

  // Copied from login manager
  getAuthTarget(aChannel, aAuthInfo) {
    let hostname, realm;

    // If our proxy is demanding authentication, don't use the
    // channel's actual destination.
    if (aAuthInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) {
      if (!(aChannel instanceof Ci.nsIProxiedChannel)) {
        throw new Error("proxy auth needs nsIProxiedChannel");
      }

      let info = aChannel.proxyInfo;
      if (!info) {
        throw new Error("proxy auth needs nsIProxyInfo");
      }

      // Proxies don't have a scheme, but we'll use "moz-proxy://"
      // so that it's more obvious what the login is for.
      let idnService = Cc["@mozilla.org/network/idn-service;1"].getService(
        Ci.nsIIDNService
      );
      hostname =
        "moz-proxy://" +
        idnService.convertUTF8toACE(info.host) +
        ":" +
        info.port;
      realm = aAuthInfo.realm;
      if (!realm) {
        realm = hostname;
      }

      return [hostname, realm];
    }

    hostname = this.getFormattedHostname(aChannel.URI);

    // If a HTTP WWW-Authenticate header specified a realm, that value
    // will be available here. If it wasn't set or wasn't HTTP, we'll use
    // the formatted hostname instead.
    realm = aAuthInfo.realm;
    if (!realm) {
      realm = hostname;
    }

    return [hostname, realm];
  },

  makeAuthMessage(channel, authInfo) {
    let isProxy = authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY;
    let isPassOnly = authInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD;
    let isCrossOrig =
      authInfo.flags & Ci.nsIAuthInformation.CROSS_ORIGIN_SUB_RESOURCE;

    let username = authInfo.username;
    let [displayHost, realm] = this.getAuthTarget(channel, authInfo);

    // Suppress "the site says: $realm" when we synthesized a missing realm.
    if (!authInfo.realm && !isProxy) {
      realm = "";
    }

    // Trim obnoxiously long realms.
    if (realm.length > 150) {
      realm = realm.substring(0, 150);
      // Append "..." (or localized equivalent).
      realm += this.ellipsis;
    }

    let text;
    if (isProxy) {
      text = PromptUtils.getLocalizedString("EnterLoginForProxy3", [
        realm,
        displayHost,
      ]);
    } else if (isPassOnly) {
      text = PromptUtils.getLocalizedString("EnterPasswordFor", [
        username,
        displayHost,
      ]);
    } else if (isCrossOrig) {
      text = PromptUtils.getLocalizedString(
        "EnterUserPasswordForCrossOrigin2",
        [displayHost]
      );
    } else if (!realm) {
      text = PromptUtils.getLocalizedString("EnterUserPasswordFor2", [
        displayHost,
      ]);
    } else {
      text = PromptUtils.getLocalizedString("EnterLoginForRealm3", [
        realm,
        displayHost,
      ]);
    }

    return text;
  },

  getTabModalPrompt(domWin) {
    var promptBox = null;

    try {
      // Get the topmost window, in case we're in a frame.
      var promptWin = domWin.top;

      // Get the chrome window for the content window we're using.
      // (Unwrap because we need a non-IDL property below.)
      var chromeWin =
        promptWin.docShell.chromeEventHandler.ownerGlobal.wrappedJSObject;

      if (chromeWin) {
        if (chromeWin.gBrowser && chromeWin.gBrowser.getTabModalPromptBox) {
          let browser = promptWin.docShell.chromeEventHandler;
          promptBox = chromeWin.gBrowser.getTabModalPromptBox(browser);
        } else if (chromeWin.getTabModalPromptBox) {
          chromeWin.getTabModalPromptBox(promptWin);
        }
      }
    } catch (e) {
      // If any errors happen, just assume no tabmodal prompter.
    }

    return promptBox;
  },
};

PromptUtils = PromptUtilsTemp;

XPCOMUtils.defineLazyGetter(PromptUtils, "strBundle", function() {
  let bundle = Services.strings.createBundle(
    "chrome://global/locale/commonDialogs.properties"
  );
  if (!bundle) {
    throw new Error("String bundle for Prompter not present!");
  }
  return bundle;
});

XPCOMUtils.defineLazyGetter(PromptUtils, "ellipsis", function() {
  let ellipsis = "\u2026";
  try {
    ellipsis = Services.prefs.getComplexValue(
      "intl.ellipsis",
      Ci.nsIPrefLocalizedString
    ).data;
  } catch (e) {}
  return ellipsis;
});

function openModalWindow(domWin, uri, args) {
  // There's an implied contract that says modal prompts should still work
  // when no "parent" window is passed for the dialog (eg, the "Master
  // Password" dialog does this).  These prompts must be shown even if there
  // are *no* visible windows at all.
  // There's also a requirement for prompts to be blocked if a window is
  // passed and that window is hidden (eg, auth prompts are supressed if the
  // passed window is the hidden window).
  // See bug 875157 comment 30 for more...
  if (domWin) {
    // a domWin was passed, so we can apply the check for it being hidden.
    let winUtils = domWin.windowUtils;

    if (winUtils && !winUtils.isParentWindowMainWidgetVisible) {
      throw Components.Exception(
        "Cannot call openModalWindow on a hidden window",
        Cr.NS_ERROR_NOT_AVAILABLE
      );
    }
  } else {
    // We try and find a window to use as the parent, but don't consider
    // if that is visible before showing the prompt.
    domWin = Services.ww.activeWindow;
    // domWin may still be null here if there are _no_ windows open.
  }
  // Note that we don't need to fire DOMWillOpenModalDialog and
  // DOMModalDialogClosed events here, wwatcher's OpenWindowInternal
  // will do that. Similarly for enterModalState / leaveModalState.

  Services.ww.openWindow(
    domWin,
    uri,
    "_blank",
    "centerscreen,chrome,modal,titlebar",
    args
  );
}

function openTabPrompt(domWin, tabPrompt, args) {
  let docShell = domWin.docShell;
  let inPermitUnload =
    docShell.contentViewer && docShell.contentViewer.inPermitUnload;
  let eventDetail = Cu.cloneInto({ tabPrompt: true, inPermitUnload }, domWin);
  PromptUtils.fireDialogEvent(
    domWin,
    "DOMWillOpenModalDialog",
    null,
    eventDetail
  );

  let winUtils = domWin.windowUtils;
  winUtils.enterModalState();

  let frameMM = docShell.messageManager;

  // We provide a callback so the prompt can close itself. We don't want to
  // wait for this event loop to return... Otherwise the presence of other
  // prompts on the call stack would in this dialog appearing unresponsive
  // until the other prompts had been closed.
  let callbackInvoked = false;
  let newPrompt;
  function onPromptClose(forceCleanup) {
    if (!newPrompt && !forceCleanup) {
      return;
    }
    callbackInvoked = true;
    if (newPrompt) {
      tabPrompt.removePrompt(newPrompt);
    }

    frameMM.removeEventListener("pagehide", pagehide, true);

    winUtils.leaveModalState();

    PromptUtils.fireDialogEvent(domWin, "DOMModalDialogClosed");
  }

  frameMM.addEventListener("pagehide", pagehide, true);
  function pagehide(e) {
    // Check whether the event relates to our window or its ancestors
    let window = domWin;
    let eventWindow = e.target.defaultView;
    while (window != eventWindow && window.parent != window) {
      window = window.parent;
    }
    if (window != eventWindow) {
      return;
    }
    frameMM.removeEventListener("pagehide", pagehide, true);

    if (newPrompt) {
      newPrompt.abortPrompt();
    }
  }

  try {
    let topPrincipal = domWin.top.document.nodePrincipal;
    let promptPrincipal = domWin.document.nodePrincipal;
    args.showAlertOrigin = topPrincipal.equals(promptPrincipal);
    args.promptActive = true;

    newPrompt = tabPrompt.appendPrompt(args, onPromptClose);

    // TODO since we don't actually open a window, need to check if
    // there's other stuff in nsWindowWatcher::OpenWindowInternal
    // that we might need to do here as well.

    Services.tm.spinEventLoopUntil(() => !args.promptActive);
    delete args.promptActive;

    if (args.promptAborted) {
      throw Components.Exception(
        "prompt aborted by user",
        Cr.NS_ERROR_NOT_AVAILABLE
      );
    }
  } finally {
    // If the prompt unexpectedly failed to invoke the callback, do so here.
    if (!callbackInvoked) {
      onPromptClose(true);
    }
  }
}

function openRemotePrompt(domWin, args) {
  let windowGlobal = domWin.getWindowGlobalChild();
  let actor = windowGlobal.getActor("Prompt");

  let docShell = domWin.docShell;
  let inPermitUnload =
    docShell.contentViewer && docShell.contentViewer.inPermitUnload;
  let eventDetail = Cu.cloneInto(
    { tabPrompt: args.tabPrompt, inPermitUnload },
    domWin
  );
  PromptUtils.fireDialogEvent(
    domWin,
    "DOMWillOpenModalDialog",
    null,
    eventDetail
  );

  let windowUtils = domWin.windowUtils;
  windowUtils.enterModalState();

  // It is technically possible for multiple prompts to be sent from a single
  // BrowsingContext. See bug 1266353. We use a randomly generated UUID to
  // differentiate between the different prompts.
  let id =
    "id" +
    Cc["@mozilla.org/uuid-generator;1"]
      .getService(Ci.nsIUUIDGenerator)
      .generateUUID()
      .toString();

  let frameMM = docShell.messageManager;
  let closed = false;

  let onPageHide = e => {
    let window = domWin;
    let eventWindow = e.target.defaultView;
    while (window != eventWindow && window.parent != window) {
      window = window.parent;
    }
    if (window != eventWindow) {
      return;
    }

    actor.sendAsyncMessage("Prompt:ForceClose", { _remoteId: id });
    closed = true;
  };

  frameMM.addEventListener("pagehide", onPageHide, true);

  try {
    let promptPrincipal = domWin.document.nodePrincipal;
    args.promptPrincipal = promptPrincipal;
    args.inPermitUnload = inPermitUnload;
    args._remoteId = id;

    let promise = actor.sendQuery("Prompt:Open", args);
    promise.then(returnedArgs => {
      // Copy the response from the closed prompt into our args, it will be
      // read by our caller.
      if (returnedArgs) {
        if (returnedArgs._remoteId !== id) {
          return;
        }

        for (let key in returnedArgs) {
          args[key] = returnedArgs[key];
        }
      }
      closed = true;
    });

    Services.tm.spinEventLoopUntilOrShutdown(() => closed);
  } finally {
    frameMM.removeEventListener("pagehide", onPageHide, true);
    windowUtils.leaveModalState();
    PromptUtils.fireDialogEvent(domWin, "DOMModalDialogClosed");
  }
}

function ModalPrompter(domWin) {
  this.domWin = domWin;
}
ModalPrompter.prototype = {
  domWin: null,
  /*
   * Default to not using a tab-modal prompt, unless the caller opts in by
   * QIing to nsIWritablePropertyBag and setting the value of this property
   * to true.
   */
  allowTabModal: false,

  QueryInterface: ChromeUtils.generateQI([
    Ci.nsIPrompt,
    Ci.nsIAuthPrompt,
    Ci.nsIAuthPrompt2,
    Ci.nsIWritablePropertyBag2,
  ]),

  /* ---------- internal methods ---------- */

  openPrompt(args) {
    // Check pref, if false/missing do not ever allow tab-modal prompts.
    const prefName = "prompts.tab_modal.enabled";
    let prefValue = false;
    if (Services.prefs.getPrefType(prefName) == Services.prefs.PREF_BOOL) {
      prefValue = Services.prefs.getBoolPref(prefName);
    }

    let allowTabModal = this.allowTabModal && prefValue;

    if (allowTabModal && this.domWin) {
      if (
        Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT
      ) {
        args.tabPrompt = true;
        openRemotePrompt(this.domWin, args);
        return;
      }

      let tabPrompt = PromptUtils.getTabModalPrompt(this.domWin);
      if (tabPrompt) {
        openTabPrompt(this.domWin, tabPrompt, args);
        return;
      }
    }

    // If we can't do a tab modal prompt, fallback to using a window-modal dialog.
    if (
      Services.appinfo.processType === Services.appinfo.PROCESS_TYPE_CONTENT
    ) {
      args.tabPrompt = false;
      openRemotePrompt(this.domWin, args);
      return;
    }

    const COMMON_DIALOG = "chrome://global/content/commonDialog.xul";
    const SELECT_DIALOG = "chrome://global/content/selectDialog.xul";

    let uri = args.promptType == "select" ? SELECT_DIALOG : COMMON_DIALOG;
    let propBag = PromptUtils.objectToPropBag(args);
    openModalWindow(this.domWin, uri, propBag);
    PromptUtils.propBagToObject(propBag, args);
  },

  /*
   * ---------- interface disambiguation ----------
   *
   * nsIPrompt and nsIAuthPrompt share 3 method names with slightly
   * different arguments. All but prompt() have the same number of
   * arguments, so look at the arg types to figure out how we're being
   * called. :-(
   */
  prompt() {
    // also, the nsIPrompt flavor has 5 args instead of 6.
    if (typeof arguments[2] == "object") {
      return this.nsIPrompt_prompt.apply(this, arguments);
    }
    return this.nsIAuthPrompt_prompt.apply(this, arguments);
  },

  promptUsernameAndPassword() {
    // Both have 6 args, so use types.
    if (typeof arguments[2] == "object") {
      return this.nsIPrompt_promptUsernameAndPassword.apply(this, arguments);
    }
    return this.nsIAuthPrompt_promptUsernameAndPassword.apply(this, arguments);
  },

  promptPassword() {
    // Both have 5 args, so use types.
    if (typeof arguments[2] == "object") {
      return this.nsIPrompt_promptPassword.apply(this, arguments);
    }
    return this.nsIAuthPrompt_promptPassword.apply(this, arguments);
  },

  /* ----------  nsIPrompt  ---------- */

  alert(title, text) {
    if (!title) {
      title = PromptUtils.getLocalizedString("Alert");
    }

    let args = {
      promptType: "alert",
      title,
      text,
    };

    this.openPrompt(args);
  },

  alertCheck(title, text, checkLabel, checkValue) {
    if (!title) {
      title = PromptUtils.getLocalizedString("Alert");
    }

    let args = {
      promptType: "alertCheck",
      title,
      text,
      checkLabel,
      checked: checkValue.value,
    };

    this.openPrompt(args);

    // Checkbox state always returned, even if cancel clicked.
    checkValue.value = args.checked;
  },

  confirm(title, text) {
    if (!title) {
      title = PromptUtils.getLocalizedString("Confirm");
    }

    let args = {
      promptType: "confirm",
      title,
      text,
      ok: false,
    };

    this.openPrompt(args);

    // Did user click Ok or Cancel?
    return args.ok;
  },

  confirmCheck(title, text, checkLabel, checkValue) {
    if (!title) {
      title = PromptUtils.getLocalizedString("ConfirmCheck");
    }

    let args = {
      promptType: "confirmCheck",
      title,
      text,
      checkLabel,
      checked: checkValue.value,
      ok: false,
    };

    this.openPrompt(args);

    // Checkbox state always returned, even if cancel clicked.
    checkValue.value = args.checked;

    // Did user click Ok or Cancel?
    return args.ok;
  },

  confirmEx(
    title,
    text,
    flags,
    button0,
    button1,
    button2,
    checkLabel,
    checkValue
  ) {
    if (!title) {
      title = PromptUtils.getLocalizedString("Confirm");
    }

    let args = {
      promptType: "confirmEx",
      title,
      text,
      checkLabel,
      checked: checkValue.value,
      ok: false,
      buttonNumClicked: 1,
    };

    let [
      label0,
      label1,
      label2,
      defaultButtonNum,
      isDelayEnabled,
    ] = PromptUtils.confirmExHelper(flags, button0, button1, button2);

    args.defaultButtonNum = defaultButtonNum;
    args.enableDelay = isDelayEnabled;

    if (label0) {
      args.button0Label = label0;
      if (label1) {
        args.button1Label = label1;
        if (label2) {
          args.button2Label = label2;
        }
      }
    }

    this.openPrompt(args);

    // Checkbox state always returned, even if cancel clicked.
    checkValue.value = args.checked;

    // Get the number of the button the user clicked.
    return args.buttonNumClicked;
  },

  nsIPrompt_prompt(title, text, value, checkLabel, checkValue) {
    if (!title) {
      title = PromptUtils.getLocalizedString("Prompt");
    }

    let args = {
      promptType: "prompt",
      title,
      text,
      value: value.value,
      checkLabel,
      checked: checkValue.value,
      ok: false,
    };

    this.openPrompt(args);

    // Did user click Ok or Cancel?
    let ok = args.ok;
    if (ok) {
      checkValue.value = args.checked;
      value.value = args.value;
    }

    return ok;
  },

  nsIPrompt_promptUsernameAndPassword(
    title,
    text,
    user,
    pass,
    checkLabel,
    checkValue
  ) {
    if (!title) {
      title = PromptUtils.getLocalizedString("PromptUsernameAndPassword2");
    }

    let args = {
      promptType: "promptUserAndPass",
      title,
      text,
      user: user.value,
      pass: pass.value,
      checkLabel,
      checked: checkValue.value,
      ok: false,
    };

    this.openPrompt(args);

    // Did user click Ok or Cancel?
    let ok = args.ok;
    if (ok) {
      checkValue.value = args.checked;
      user.value = args.user;
      pass.value = args.pass;
    }

    return ok;
  },

  nsIPrompt_promptPassword(title, text, pass, checkLabel, checkValue) {
    if (!title) {
      title = PromptUtils.getLocalizedString("PromptPassword2");
    }

    let args = {
      promptType: "promptPassword",
      title,
      text,
      pass: pass.value,
      checkLabel,
      checked: checkValue.value,
      ok: false,
    };

    this.openPrompt(args);

    // Did user click Ok or Cancel?
    let ok = args.ok;
    if (ok) {
      checkValue.value = args.checked;
      pass.value = args.pass;
    }

    return ok;
  },

  select(title, text, list, selected) {
    if (!title) {
      title = PromptUtils.getLocalizedString("Select");
    }

    let args = {
      promptType: "select",
      title,
      text,
      list,
      selected: -1,
      ok: false,
    };

    this.openPrompt(args);

    // Did user click Ok or Cancel?
    let ok = args.ok;
    if (ok) {
      selected.value = args.selected;
    }

    return ok;
  },

  /* ----------  nsIAuthPrompt  ---------- */

  nsIAuthPrompt_prompt(
    title,
    text,
    passwordRealm,
    savePassword,
    defaultText,
    result
  ) {
    // The passwordRealm and savePassword args were ignored by nsPrompt.cpp
    if (defaultText) {
      result.value = defaultText;
    }
    return this.nsIPrompt_prompt(title, text,