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 (9f1f25699e99)

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
/* 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";

var EXPORTED_SYMBOLS = ["PluginChild"];

const { AppConstants } = ChromeUtils.import(
  "resource://gre/modules/AppConstants.jsm"
);
const { BrowserUtils } = ChromeUtils.import(
  "resource://gre/modules/BrowserUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);

XPCOMUtils.defineLazyGetter(this, "gNavigatorBundle", function() {
  const url = "chrome://browser/locale/browser.properties";
  return Services.strings.createBundle(url);
});

XPCOMUtils.defineLazyServiceGetter(
  this,
  "gPluginHost",
  "@mozilla.org/plugin/host;1",
  "nsIPluginHost"
);

const OVERLAY_DISPLAY = {
  HIDDEN: 0, // The overlay will be transparent
  BLANK: 1, // The overlay will be just a grey box
  TINY: 2, // The overlay with a 16x16 plugin icon
  REDUCED: 3, // The overlay with a 32x32 plugin icon
  NOTEXT: 4, // The overlay with a 48x48 plugin icon and the close button
  FULL: 5, // The full overlay: 48x48 plugin icon, close button and label
};

// This gets sent through the content process message manager because the parent
// can't know exactly which child needs to hear about the progress of the
// submission, so we listen "manually" on the CPMM instead of through the actor
// definition.
const kSubmitMsg = "PluginParent:NPAPIPluginCrashReportSubmitted";

class PluginChild extends JSWindowActorChild {
  constructor() {
    super();

    // Cache of plugin crash information sent from the parent
    this.pluginCrashData = new Map();
    Services.cpmm.addMessageListener(kSubmitMsg, this);
  }

  willDestroy() {
    Services.cpmm.removeMessageListener(kSubmitMsg, this);
    if (this._addedListeners) {
      this.contentWindow.removeEventListener("pagehide", this, {
        capture: true,
        mozSystemGroup: true,
      });
      this.contentWindow.removeEventListener("pageshow", this, {
        capture: true,
        mozSystemGroup: true,
      });
    }
  }

  receiveMessage(msg) {
    switch (msg.name) {
      case "PluginParent:ActivatePlugins":
        this.activatePlugins(msg.data.activationInfo, msg.data.newState);
        break;
      case "PluginParent:NPAPIPluginCrashReportSubmitted":
        this.NPAPIPluginCrashReportSubmitted({
          runID: msg.data.runID,
          state: msg.data.state,
        });
        break;
      case "PluginParent:Test:ClearCrashData":
        // This message should ONLY ever be sent by automated tests.
        if (Services.prefs.getBoolPref("plugins.testmode")) {
          this.pluginCrashData.clear();
        }
    }
  }

  observe(aSubject, aTopic, aData) {
    switch (aTopic) {
      case "decoder-doctor-notification":
        let data = JSON.parse(aData);
        let type = data.type.toLowerCase();
        if (
          type == "cannot-play" &&
          this.haveShownNotification &&
          aSubject.top.document == this.document &&
          data.formats.toLowerCase().includes("application/x-mpegurl", 0)
        ) {
          this.contentWindow.pluginRequiresReload = true;
        }
    }
  }

  onPageShow(event) {
    // Ignore events that aren't from the main document.
    if (!this.contentWindow || event.target != this.document) {
      return;
    }

    // The PluginClickToPlay events are not fired when navigating using the
    // BF cache. |persisted| is true when the page is loaded from the
    // BF cache, so this code reshows the notification if necessary.
    if (event.persisted) {
      this.reshowClickToPlayNotification();
    }
  }

  onPageHide(event) {
    // Ignore events that aren't from the main document.
    if (!this.contentWindow || event.target != this.document) {
      return;
    }

    this.clearPluginCaches();
    this.haveShownNotification = false;
  }

  getPluginUI(pluginElement, anonid) {
    if (
      pluginElement.openOrClosedShadowRoot &&
      pluginElement.openOrClosedShadowRoot.isUAWidget()
    ) {
      return pluginElement.openOrClosedShadowRoot.getElementById(anonid);
    }
    return null;
  }

  _getPluginInfo(pluginElement) {
    if (this.isKnownPlugin(pluginElement)) {
      let pluginTag = gPluginHost.getPluginTagForType(pluginElement.actualType);
      let pluginName = BrowserUtils.makeNicePluginName(pluginTag.name);
      let fallbackType = pluginElement.defaultFallbackType;
      let permissionString = gPluginHost.getPermissionStringForType(
        pluginElement.actualType
      );
      return { pluginTag, pluginName, fallbackType, permissionString };
    }
    return {
      fallbackType: null,
      permissionString: null,
      pluginName: gNavigatorBundle.GetStringFromName(
        "pluginInfo.unknownPlugin"
      ),
      pluginTag: null,
    };
  }

  /**
   * _getPluginInfoForTag is called when iterating the plugins for a document,
   * and what we get from nsIDOMWindowUtils is an nsIPluginTag, and not an
   * nsIObjectLoadingContent. This only should happen if the plugin is
   * click-to-play (see bug 1186948).
   */
  _getPluginInfoForTag(pluginTag) {
    // Since we should only have entered _getPluginInfoForTag when
    // examining a click-to-play plugin, we can safely hard-code
    // this fallback type, since we don't actually have an
    // nsIObjectLoadingContent to check.
    let fallbackType = Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY;
    if (pluginTag) {
      let pluginName = BrowserUtils.makeNicePluginName(pluginTag.name);
      let permissionString = gPluginHost.getPermissionStringForTag(pluginTag);
      return { pluginTag, pluginName, permissionString, fallbackType };
    }
    return {
      fallbackType,
      permissionString: null,
      pluginName: gNavigatorBundle.GetStringFromName(
        "pluginInfo.unknownPlugin"
      ),
      pluginTag: null,
    };
  }

  /**
   * Update the visibility of the plugin overlay.
   */
  setVisibility(plugin, overlay, overlayDisplayState) {
    overlay.classList.toggle(
      "visible",
      overlayDisplayState != OVERLAY_DISPLAY.HIDDEN
    );
    if (overlayDisplayState != OVERLAY_DISPLAY.HIDDEN) {
      overlay.removeAttribute("dismissed");
    }
  }

  /**
   * Adjust the style in which the overlay will be displayed. It might be adjusted
   * based on its size, or if there's some other element covering all corners of
   * the overlay.
   *
   * This function will handle adjusting the style of the overlay, but will
   * not handle hiding it. That is done by setVisibility with the return value
   * from this function.
   *
   * @param {Element} plugin  The plug-in element
   * @param {Element} overlay The overlay element inside the UA Shadow DOM of
   *                          the plug-in element
   * @param {boolean} flushLayout Allow flush layout during computation and
   *                              adjustment.
   * @returns A value from OVERLAY_DISPLAY.
   */
  computeAndAdjustOverlayDisplay(plugin, overlay, flushLayout) {
    let fallbackType = plugin.pluginFallbackType;
    if (plugin.pluginFallbackTypeOverride !== undefined) {
      fallbackType = plugin.pluginFallbackTypeOverride;
    }
    if (fallbackType == Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY_QUIET) {
      return OVERLAY_DISPLAY.HIDDEN;
    }

    // If the overlay size is 0, we haven't done layout yet. Presume that
    // plugins are visible until we know otherwise.
    if (flushLayout && overlay.scrollWidth == 0) {
      return OVERLAY_DISPLAY.FULL;
    }

    let overlayDisplay = OVERLAY_DISPLAY.FULL;
    let cwu = plugin.ownerGlobal.windowUtils;

    // Is the <object>'s size too small to hold what we want to show?
    let pluginRect = flushLayout
      ? plugin.getBoundingClientRect()
      : cwu.getBoundsWithoutFlushing(plugin);
    let pluginWidth = Math.ceil(pluginRect.width);
    let pluginHeight = Math.ceil(pluginRect.height);

    let layoutNeedsFlush =
      !flushLayout &&
      cwu.needsFlush(cwu.FLUSH_STYLE) &&
      cwu.needsFlush(cwu.FLUSH_LAYOUT);

    // We must set the attributes while here inside this function in order
    // for a possible re-style to occur, which will make the scrollWidth/Height
    // checks below correct. Otherwise, we would be requesting e.g. a TINY
    // overlay here, but the default styling would be used, and that would make
    // it overflow, causing it to change to BLANK instead of remaining as TINY.

    if (layoutNeedsFlush) {
      // Set the content to be oversized when we the overlay size is 0,
      // so that we could receive an overflow event afterwards when there is
      // a layout.
      overlayDisplay = OVERLAY_DISPLAY.FULL;
      overlay.setAttribute("sizing", "oversized");
      overlay.removeAttribute("notext");
    } else if (pluginWidth <= 32 || pluginHeight <= 32) {
      overlay.setAttribute("sizing", "blank");
      overlayDisplay = OVERLAY_DISPLAY.BLANK;
    } else if (pluginWidth <= 80 || pluginHeight <= 60) {
      overlayDisplay = OVERLAY_DISPLAY.TINY;
      overlay.setAttribute("sizing", "tiny");
      overlay.setAttribute("notext", "notext");
    } else if (pluginWidth <= 120 || pluginHeight <= 80) {
      overlayDisplay = OVERLAY_DISPLAY.REDUCED;
      overlay.setAttribute("sizing", "reduced");
      overlay.setAttribute("notext", "notext");
    } else if (pluginWidth <= 240 || pluginHeight <= 160) {
      overlayDisplay = OVERLAY_DISPLAY.NOTEXT;
      overlay.removeAttribute("sizing");
      overlay.setAttribute("notext", "notext");
    } else {
      overlayDisplay = OVERLAY_DISPLAY.FULL;
      overlay.removeAttribute("sizing");
      overlay.removeAttribute("notext");
    }

    // The hit test below only works with correct layout information,
    // don't do it if layout needs flush.
    // We also don't want to access scrollWidth/scrollHeight if
    // the layout needs flush.
    if (layoutNeedsFlush) {
      return overlayDisplay;
    }

    // XXX bug 446693. The text-shadow on the submitted-report text at
    //     the bottom causes scrollHeight to be larger than it should be.
    let overflows =
      overlay.scrollWidth > pluginWidth ||
      overlay.scrollHeight - 5 > pluginHeight;
    if (overflows) {
      overlay.setAttribute("sizing", "blank");
      return OVERLAY_DISPLAY.BLANK;
    }

    // Is the plugin covered up by other content so that it is not clickable?
    // Floating point can confuse .elementFromPoint, so inset just a bit
    let left = pluginRect.left + 2;
    let right = pluginRect.right - 2;
    let top = pluginRect.top + 2;
    let bottom = pluginRect.bottom - 2;
    let centerX = left + (right - left) / 2;
    let centerY = top + (bottom - top) / 2;
    let points = [
      [left, top],
      [left, bottom],
      [right, top],
      [right, bottom],
      [centerX, centerY],
    ];

    for (let [x, y] of points) {
      if (x < 0 || y < 0) {
        continue;
      }
      let el = cwu.elementFromPoint(x, y, true, true);
      if (el === plugin) {
        return overlayDisplay;
      }
    }

    overlay.setAttribute("sizing", "blank");
    return OVERLAY_DISPLAY.BLANK;
  }

  addLinkClickCallback(linkNode, callbackName /* callbackArgs...*/) {
    // XXX just doing (callback)(arg) was giving a same-origin error. bug?
    let self = this;
    let callbackArgs = Array.prototype.slice.call(arguments).slice(2);
    linkNode.addEventListener(
      "click",
      function(evt) {
        if (!evt.isTrusted) {
          return;
        }
        evt.preventDefault();
        if (!callbackArgs.length) {
          callbackArgs = [evt];
        }
        self[callbackName].apply(self, callbackArgs);
      },
      true
    );

    linkNode.addEventListener(
      "keydown",
      function(evt) {
        if (!evt.isTrusted) {
          return;
        }
        if (evt.keyCode == evt.DOM_VK_RETURN) {
          evt.preventDefault();
          if (!callbackArgs.length) {
            callbackArgs = [evt];
          }
          evt.preventDefault();
          self[callbackName].apply(self, callbackArgs);
        }
      },
      true
    );
  }

  // Helper to get the binding handler type from a plugin object
  _getBindingType(plugin) {
    if (!(plugin instanceof Ci.nsIObjectLoadingContent)) {
      return null;
    }

    switch (plugin.pluginFallbackType) {
      case Ci.nsIObjectLoadingContent.PLUGIN_UNSUPPORTED:
        return "PluginNotFound";
      case Ci.nsIObjectLoadingContent.PLUGIN_DISABLED:
        return "PluginDisabled";
      case Ci.nsIObjectLoadingContent.PLUGIN_BLOCKLISTED:
        return "PluginBlocklisted";
      case Ci.nsIObjectLoadingContent.PLUGIN_OUTDATED:
        return "PluginOutdated";
      case Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY:
      case Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY_QUIET:
        return "PluginClickToPlay";
      case Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_UPDATABLE:
        return "PluginVulnerableUpdatable";
      case Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_NO_UPDATE:
        return "PluginVulnerableNoUpdate";
      default:
        // Not all states map to a handler
        return null;
    }
  }

  handleEvent(event) {
    // Ignore events for other frames.
    let eventDoc = event.target.ownerDocument || event.target.document;
    if (eventDoc && eventDoc != this.document) {
      return;
    }
    if (!this._addedListeners) {
      // Only add pageshow/pagehide listeners here. We don't want this actor
      // to be instantiated for every frame, we only care if a plugin actually
      // gets used in a frame. So we don't add these listeners in the actor
      // specification, but only at runtime once one of our Plugin* events
      // fires.
      this.contentWindow.addEventListener("pagehide", this, {
        capture: true,
        mozSystemGroup: true,
      });
      this.contentWindow.addEventListener("pageshow", this, {
        capture: true,
        mozSystemGroup: true,
      });
      this._addedListeners = true;
    }

    let eventType = event.type;

    if (eventType == "pagehide") {
      this.onPageHide(event);
      return;
    }

    if (eventType == "pageshow") {
      this.onPageShow(event);
      return;
    }

    if (eventType == "click") {
      this.onOverlayClick(event);
      return;
    }

    if (
      eventType == "PluginCrashed" &&
      !(event.target instanceof Ci.nsIObjectLoadingContent)
    ) {
      // If the event target is not a plugin object (i.e., an <object> or
      // <embed> element), this call is for a window-global plugin.
      this.onPluginCrashed(event.target, event);
      return;
    }

    if (eventType == "HiddenPlugin") {
      let pluginTag = event.tag.QueryInterface(Ci.nsIPluginTag);
      this.showClickToPlayNotification(pluginTag, false);
    }

    let pluginElement = event.target;

    if (!(pluginElement instanceof Ci.nsIObjectLoadingContent)) {
      return;
    }

    if (eventType == "PluginBindingAttached") {
      // The plugin binding fires this event when it is created.
      // As an untrusted event, ensure that this object actually has a binding
      // and make sure we don't handle it twice
      let overlay = this.getPluginUI(pluginElement, "main");
      if (!overlay || overlay._bindingHandled) {
        return;
      }
      overlay._bindingHandled = true;

      // Lookup the handler for this binding
      eventType = this._getBindingType(pluginElement);
      if (!eventType) {
        // Not all bindings have handlers
        return;
      }
    }

    let shouldShowNotification = false;
    switch (eventType) {
      case "PluginCrashed":
        this.onPluginCrashed(pluginElement, event);
        break;

      case "PluginNotFound": {
        /* NOP */
        break;
      }

      case "PluginBlocklisted":
      case "PluginOutdated":
        shouldShowNotification = true;
        break;

      case "PluginVulnerableUpdatable":
        let updateLink = this.getPluginUI(pluginElement, "checkForUpdatesLink");
        let { pluginTag } = this._getPluginInfo(pluginElement);
        this.addLinkClickCallback(
          updateLink,
          "forwardCallback",
          "openPluginUpdatePage",
          pluginTag.id
        );

      /* FALLTHRU */
      case "PluginVulnerableNoUpdate":
      case "PluginClickToPlay":
        this._handleClickToPlayEvent(pluginElement);
        let { pluginName } = this._getPluginInfo(pluginElement);
        let messageString = gNavigatorBundle.formatStringFromName(
          "PluginClickToActivate2",
          [pluginName]
        );
        let overlayText = this.getPluginUI(pluginElement, "clickToPlay");
        overlayText.textContent = messageString;
        if (
          eventType == "PluginVulnerableUpdatable" ||
          eventType == "PluginVulnerableNoUpdate"
        ) {
          let vulnerabilityString = gNavigatorBundle.GetStringFromName(
            eventType
          );
          let vulnerabilityText = this.getPluginUI(
            pluginElement,
            "vulnerabilityStatus"
          );
          vulnerabilityText.textContent = vulnerabilityString;
        }
        shouldShowNotification = true;
        break;

      case "PluginDisabled":
        let manageLink = this.getPluginUI(pluginElement, "managePluginsLink");
        this.addLinkClickCallback(
          manageLink,
          "forwardCallback",
          "managePlugins"
        );
        shouldShowNotification = true;
        break;

      case "PluginInstantiated":
        shouldShowNotification = true;
        break;
    }

    // Show the in-content UI if it's not too big. The crashed plugin handler already did this.
    let overlay = this.getPluginUI(pluginElement, "main");
    if (eventType != "PluginCrashed") {
      if (overlay != null) {
        this.setVisibility(
          pluginElement,
          overlay,
          this.computeAndAdjustOverlayDisplay(pluginElement, overlay, false)
        );

        let resizeListener = () => {
          this.setVisibility(
            pluginElement,
            overlay,
            this.computeAndAdjustOverlayDisplay(pluginElement, overlay, true)
          );
        };
        pluginElement.addEventListener("overflow", resizeListener);
        pluginElement.addEventListener("underflow", resizeListener);
      }
    }

    let closeIcon = this.getPluginUI(pluginElement, "closeIcon");
    if (closeIcon) {
      closeIcon.addEventListener(
        "click",
        clickEvent => {
          if (clickEvent.button == 0 && clickEvent.isTrusted) {
            this.hideClickToPlayOverlay(pluginElement);
            overlay.setAttribute("dismissed", "true");
          }
        },
        true
      );
    }

    if (shouldShowNotification) {
      this.showClickToPlayNotification(pluginElement, false);
    }
  }

  isKnownPlugin(objLoadingContent) {
    return (
      objLoadingContent.getContentTypeForMIMEType(
        objLoadingContent.actualType
      ) == Ci.nsIObjectLoadingContent.TYPE_PLUGIN
    );
  }

  canActivatePlugin(objLoadingContent) {
    // if this isn't a known plugin, we can't activate it
    // (this also guards pluginHost.getPermissionStringForType against
    // unexpected input)
    if (!this.isKnownPlugin(objLoadingContent)) {
      return false;
    }

    let permissionString = gPluginHost.getPermissionStringForType(
      objLoadingContent.actualType
    );
    let principal = objLoadingContent.ownerGlobal.top.document.nodePrincipal;
    let pluginPermission = Services.perms.testPermissionFromPrincipal(
      principal,
      permissionString
    );

    let isFallbackTypeValid =
      objLoadingContent.pluginFallbackType >=
        Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY &&
      objLoadingContent.pluginFallbackType <=
        Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY_QUIET;

    return (
      !objLoadingContent.activated &&
      pluginPermission != Ci.nsIPermissionManager.DENY_ACTION &&
      isFallbackTypeValid
    );
  }

  hideClickToPlayOverlay(pluginElement) {
    let overlay = this.getPluginUI(pluginElement, "main");
    if (overlay) {
      overlay.classList.remove("visible");
    }
  }

  // Forward a link click callback to the chrome process.
  forwardCallback(name, pluginId) {
    this.sendAsyncMessage("PluginContent:LinkClickCallback", {
      name,
      pluginId,
    });
  }

  submitReport(plugin) {
    if (!AppConstants.MOZ_CRASHREPORTER) {
      return;
    }
    if (!plugin) {
      Cu.reportError(
        "Attempted to submit crash report without an associated plugin."
      );
      return;
    }
    if (!(plugin instanceof Ci.nsIObjectLoadingContent)) {
      Cu.reportError(
        "Attempted to submit crash report on plugin that does not" +
          "implement nsIObjectLoadingContent."
      );
      return;
    }

    let runID = plugin.runID;
    let submitURLOptIn = this.getPluginUI(plugin, "submitURLOptIn").checked;
    let keyVals = {};
    let userComment = this.getPluginUI(plugin, "submitComment").value.trim();
    if (userComment) {
      keyVals.PluginUserComment = userComment;
    }
    if (submitURLOptIn) {
      keyVals.PluginContentURL = plugin.ownerDocument.URL;
    }

    this.sendAsyncMessage("PluginContent:SubmitReport", {
      runID,
      keyVals,
      submitURLOptIn,
    });
  }

  reloadPage() {
    this.contentWindow.location.reload();
  }

  // Event listener for click-to-play plugins.
  _handleClickToPlayEvent(plugin) {
    let doc = plugin.ownerDocument;
    // guard against giving pluginHost.getPermissionStringForType a type
    // not associated with any known plugin
    if (!this.isKnownPlugin(plugin)) {
      return;
    }
    let permissionString = gPluginHost.getPermissionStringForType(
      plugin.actualType
    );
    let principal = doc.defaultView.top.document.nodePrincipal;
    let pluginPermission = Services.perms.testPermissionFromPrincipal(
      principal,
      permissionString
    );

    let overlay = this.getPluginUI(plugin, "main");

    if (
      pluginPermission == Ci.nsIPermissionManager.DENY_ACTION ||
      pluginPermission ==
        Ci.nsIObjectLoadingContent.PLUGIN_PERMISSION_PROMPT_ACTION_QUIET
    ) {
      if (overlay) {
        overlay.classList.remove("visible");
      }
      return;
    }

    if (overlay) {
      overlay.addEventListener("click", this, true);
    }
  }

  onOverlayClick(event) {
    let document = event.target.ownerDocument;
    let plugin = document.getBindingParent(event.target);
    let overlay = this.getPluginUI(plugin, "main");
    // Have to check that the target is not the link to update the plugin
    if (
      !(
        ChromeUtils.getClassName(event.originalTarget) === "HTMLAnchorElement"
      ) &&
      event.originalTarget.getAttribute("anonid") != "closeIcon" &&
      event.originalTarget.id != "closeIcon" &&
      !overlay.hasAttribute("dismissed") &&
      event.button == 0 &&
      event.isTrusted
    ) {
      this.showClickToPlayNotification(plugin, true);
      event.stopPropagation();
      event.preventDefault();
    }
  }

  reshowClickToPlayNotification() {
    let { plugins } = this.contentWindow.windowUtils;
    for (let plugin of plugins) {
      let overlay = this.getPluginUI(plugin, "main");
      if (overlay) {
        overlay.removeEventListener("click", this, true);
      }
      if (this.canActivatePlugin(plugin)) {
        this._handleClickToPlayEvent(plugin);
      }
    }
    this.showClickToPlayNotification(null, false);
  }

  /**
   * Activate the plugins that the user has specified.
   */
  activatePlugins(activationInfo, newState) {
    let { plugins } = this.contentWindow.windowUtils;

    let pluginFound = false;
    for (let plugin of plugins) {
      if (!this.isKnownPlugin(plugin)) {
        continue;
      }
      if (
        activationInfo.permissionString ==
        gPluginHost.getPermissionStringForType(plugin.actualType)
      ) {
        let overlay = this.getPluginUI(plugin, "main");
        pluginFound = true;
        if (
          newState == "block" ||
          newState == "blockalways" ||
          newState == "continueblocking"
        ) {
          if (overlay) {
            overlay.addEventListener("click", this, true);
          }
          plugin.pluginFallbackTypeOverride = activationInfo.fallbackType;
          plugin.reload(true);
        } else if (this.canActivatePlugin(plugin)) {
          if (overlay) {
            overlay.removeEventListener("click", this, true);
          }
          plugin.playPlugin();
        }
      }
    }

    // If there are no instances of the plugin on the page any more or if we've
    // noted that the content needs to be reloaded due to replacing HLS, what the
    // user probably needs is for us to allow and then refresh.
    if (
      newState != "block" &&
      newState != "blockalways" &&
      newState != "continueblocking" &&
      (!pluginFound || this.contentWindow.pluginRequiresReload)
    ) {
      this.reloadPage();
    }
  }

  showClickToPlayNotification(pluginElOrTag, showNow) {
    let plugins = [];

    // If pluginElOrTag is null, that means the user has navigated back to a page with
    // plugins, and we need to collect all the plugins.
    if (pluginElOrTag === null) {
      // cwu.plugins may contain non-plugin <object>s, filter them out
      plugins = this.contentWindow.windowUtils.plugins.filter(
        p =>
          p.getContentTypeForMIMEType(p.actualType) ==
          Ci.nsIObjectLoadingContent.TYPE_PLUGIN
      );

      if (!plugins.length) {
        this.removeNotification();
        return;
      }
    } else {
      plugins = [pluginElOrTag];
    }

    // Iterate over the plugins and ensure we have one value for each
    // permission string - though in principle there should only be 1 anyway
    // (for flash), in practice there are still some automated tests where we
    // could encounter other ones.
    let permissionMap = new Map();
    for (let p of plugins) {
      let pluginInfo;
      if (p instanceof Ci.nsIPluginTag) {
        pluginInfo = this._getPluginInfoForTag(p);
      } else {
        pluginInfo = this._getPluginInfo(p);
      }
      if (pluginInfo.permissionString === null) {
        Cu.reportError("No permission string for active plugin.");
        continue;
      }
      if (!permissionMap.has(pluginInfo.permissionString)) {
        permissionMap.set(pluginInfo.permissionString, pluginInfo);
        continue;
      }
    }
    if (permissionMap.size > 1) {
      Cu.reportError(
        "Err, we're not meant to have more than 1 plugin anymore!"
      );
    }
    if (!permissionMap.size) {
      return;
    }

    this.haveShownNotification = true;

    let permissionItem = permissionMap.values().next().value;
    let plugin = {
      id: permissionItem.pluginTag.id,
      fallbackType: permissionItem.fallbackType,
    };

    let msg = "PluginContent:ShowClickToPlayNotification";
    this.sendAsyncMessage(msg, { plugin, showNow });
  }

  removeNotification() {
    this.sendAsyncMessage("PluginContent:RemoveNotification");
  }

  clearPluginCaches() {
    this.pluginCrashData.clear();
  }

  /**
   * Determines whether or not the crashed plugin is contained within current
   * full screen DOM element.
   * @param fullScreenElement (DOM element)
   *   The DOM element that is currently full screen, or null.
   * @param domElement
   *   The DOM element which contains the crashed plugin, or the crashed plugin
   *   itself.
   * @returns bool
   *   True if the plugin is a descendant of the full screen DOM element, false otherwise.
   **/
  isWithinFullScreenElement(fullScreenElement, domElement) {
    /**
     * Traverses down iframes until it find a non-iframe full screen DOM element.
     * @param fullScreenIframe
     *  Target iframe to begin searching from.
     * @returns DOM element
     *  The full screen DOM element contained within the iframe (could be inner iframe), or the original iframe if no inner DOM element is found.
     **/
    let getTrueFullScreenElement = fullScreenIframe => {
      if (
        typeof fullScreenIframe.contentDocument !== "undefined" &&
        fullScreenIframe.contentDocument.mozFullScreenElement
      ) {
        return getTrueFullScreenElement(
          fullScreenIframe.contentDocument.mozFullScreenElement
        );
      }
      return fullScreenIframe;
    };

    if (fullScreenElement.tagName === "IFRAME") {
      fullScreenElement = getTrueFullScreenElement(fullScreenElement);
    }

    if (fullScreenElement.contains(domElement)) {
      return true;
    }
    let parentIframe = domElement.ownerGlobal.frameElement;
    if (parentIframe) {
      return this.isWithinFullScreenElement(fullScreenElement, parentIframe);
    }
    return false;
  }

  /**
   * The PluginCrashed event handler. Note that the PluginCrashed event is
   * fired for both NPAPI and Gecko Media plugins. In the latter case, the
   * target of the event is the document that the GMP is being used in.
   */
  async onPluginCrashed(target, aEvent) {
    if (!(aEvent instanceof this.contentWindow.PluginCrashedEvent)) {
      return;
    }

    let fullScreenElement = this.contentWindow.top.document
      .mozFullScreenElement;
    if (fullScreenElement) {
      if (this.isWithinFullScreenElement(fullScreenElement, target)) {
        this.contentWindow.top.document.mozCancelFullScreen();
      }
    }

    if (aEvent.gmpPlugin) {
      this.GMPCrashed(aEvent);
      return;
    }

    if (!(target instanceof Ci.nsIObjectLoadingContent)) {
      return;
    }

    let crashData = this.pluginCrashData.get(target.runID);
    if (!crashData) {
      // We haven't received information from the parent yet about
      // this crash, so go get it:
      crashData = await this.sendQuery("PluginContent:GetCrashData", {
        runID: target.runID,
      });
      this.pluginCrashData.set(target.runID, crashData);
    }

    this.setCrashedNPAPIPluginState({
      pluginElement: target,
      state: crashData.state,
      pluginName: crashData.pluginName,
    });
  }

  setCrashedNPAPIPluginState({ pluginElement, state, pluginName }) {
    // Force a layout flush so the binding is attached.
    pluginElement.clientTop;
    let overlay = this.getPluginUI(pluginElement, "main");
    let statusDiv = this.getPluginUI(pluginElement, "submitStatus");
    let optInCB = this.getPluginUI(pluginElement, "submitURLOptIn");

    this.getPluginUI(pluginElement, "submitButton").addEventListener(
      "click",
      event => {
        if (event.button != 0 || !event.isTrusted) {
          return;
        }
        this.submitReport(pluginElement);
      }
    );

    optInCB.checked = Services.prefs.getBoolPref(
      "dom.ipc.plugins.reportCrashURL",
      true
    );

    statusDiv.setAttribute("status", state);

    let helpIcon = this.getPluginUI(pluginElement, "helpIcon");
    this.addLinkClickCallback(helpIcon, "openHelpPage");

    let crashText = this.getPluginUI(pluginElement, "crashedText");

    let message = gNavigatorBundle.formatStringFromName(
      "crashedpluginsMessage.title",
      [pluginName]
    );
    crashText.textContent = message;

    let link = this.getPluginUI(pluginElement, "reloadLink");
    this.addLinkClickCallback(link, "reloadPage");

    // This might trigger force reflow, but plug-in crashing code path shouldn't be hot.
    let overlayDisplayState = this.computeAndAdjustOverlayDisplay(
      pluginElement,
      overlay,
      true
    );

    // Is the <object>'s size too small to hold what we want to show?
    if (overlayDisplayState != OVERLAY_DISPLAY.FULL) {
      // First try hiding the crash report submission UI.
      statusDiv.removeAttribute("status");

      overlayDisplayState = this.computeAndAdjustOverlayDisplay(
        pluginElement,
        overlay,
        true
      );
    }
    this.setVisibility(pluginElement, overlay, overlayDisplayState);

    let doc = pluginElement.ownerDocument;
    let runID = pluginElement.runID;

    if (overlayDisplayState == OVERLAY_DISPLAY.FULL) {
      doc.mozNoPluginCrashedNotification = true;

      // Notify others that the crash reporter UI is now ready.
      // Currently, this event is only used by tests.
      let winUtils = this.contentWindow.windowUtils;
      let event = new this.contentWindow.CustomEvent(
        "PluginCrashReporterDisplayed",
        {
          bubbles: true,
        }
      );
      winUtils.dispatchEventToChromeOnly(pluginElement, event);
    } else if (!doc.mozNoPluginCrashedNotification) {
      // If another plugin on the page was large enough to show our UI, we don't
      // want to show a notification bar.
      this.sendAsyncMessage("PluginContent:ShowPluginCrashedNotification", {
        pluginCrashID: { runID },
      });
    }
  }

  NPAPIPluginCrashReportSubmitted({ runID, state }) {
    this.pluginCrashData.delete(runID);
    let { plugins } = this.contentWindow.windowUtils;

    for (let pluginElement of plugins) {
      if (
        pluginElement instanceof Ci.nsIObjectLoadingContent &&
        pluginElement.runID == runID
      ) {
        let statusDiv = this.getPluginUI(pluginElement, "submitStatus");
        statusDiv.setAttribute("status", state);
      }
    }
  }

  GMPCrashed(aEvent) {
    let { target, gmpPlugin, pluginID } = aEvent;
    if (!gmpPlugin || !target.document) {
      // TODO: Throw exception? How did we get here?
      return;
    }

    this.sendAsyncMessage("PluginContent:ShowPluginCrashedNotification", {
      pluginCrashID: { pluginID },
    });
  }
}