KMARKS,
      true
    );
    //and then send the message
    lazy.BookmarksBarButton.showBookmarksBarButton(browser, message);
  }

  showCFR(message, browser) {
    lazy.CFRPageActions.forceRecommendation(
      browser,
      message,
      dispatchCFRAction
    );
  }

  showPrivateBrowsingMessage(message, browser) {
    ASRouter.forcePBWindow(browser, message);
  }

  async showFeatureCallout(message, browser) {
    // Clear the Feature Tour prefs used by some callouts, to ensure
    // the behaviour of the message is correct
    let tourPref = message.content.tour_pref_name;
    if (tourPref) {
      Services.prefs.clearUserPref(tourPref);
    }
    // For messagePreview, force the trigger && targeting to be something we can show.
    message.trigger = { id: "nthTabClosed" };
    message.targeting = "true";
    // Check whether or not the callout is showing already, then
    // modify the anchor property of the feature callout to
    // ensure it's something we can show.
    let showing = await lazy.FeatureCalloutBroker.showFeatureCallout(
      browser,
      message
    );
    if (!showing) {
      for (const screen of message.content.screens) {
        let existingAnchors = screen.anchors;
        let fallbackAnchor = { selector: "#star-button-box" };

        if (existingAnchors[0].hasOwnProperty("arrow_position")) {
          fallbackAnchor.arrow_position = "top-center-arrow-end";
        } else {
          fallbackAnchor.panel_position = {
            anchor_attachment: "bottomcenter",
            callout_attachment: "topright",
          };
        }

        screen.anchors = [...existingAnchors, fallbackAnchor];
        lazy.log.debug("ANCHORS: ", screen.anchors);
      }
      // Try showing again
      await lazy.FeatureCalloutBroker.showFeatureCallout(browser, message);
    }
  }

  /**
   * Chooses the appropriate messaging system function for showing
   * the message, based on the template passed in data
   *
   * @param {string} data - a string containing the message JSON
   * @param {boolean} validationEnabled - whether or not to run
   * schema validation on the message JSON. Should be false in
   * tests so that we don't have to pass real messages or call
   * the validation function.
   */

  async showMessage(data, validationEnabled = true) {
    let message;
    try {
      message = JSON.parse(data);
    } catch (e) {
      lazy.log.error("Could not parse message", e);
      return;
    }

    if (validationEnabled) {
      const schema = await fetch(
        "chrome://browser/content/asrouter/schemas/MessagingExperiment.schema.json",
        { credentials: "omit" }
      ).then(rsp => rsp.json());
      const result = JsonSchema.validate(message, schema);
      if (!result.valid) {
        lazy.log.error(
          `Invalid message: ${JSON.stringify(result.errors, undefined, 2)}`
        );
      }
    }

    const browser =
      this.browsingContext.topChromeWindow.gBrowser.selectedBrowser;
    switch (message.template) {
      case "infobar":
        this.showInfoBar(message, browser);
        return;
      case "spotlight":
        this.showSpotlight(message, browser);
        return;
      case "cfr_doorhanger":
        this.showCFR(message, browser);
        return;
      case "feature_callout":
        this.showFeatureCallout(message, browser);
        return;
      case "bookmarks_bar_button":
        this.showBookmarksBarButton(message, browser);
        return;
      case "pb_newtab":
        this.showPrivateBrowsingMessage(message, browser);
        return;
      default:
        lazy.log.error(`Unsupported message template ${message.template}`);
    }
  }

  async receiveMessage(message) {
    // validationEnabled is used for testing
    const { name, data, validationEnabled } = message;

    switch (name) {
      case "MessagePreview:SHOW_MESSAGE":
        await this.showMessage(data, validationEnabled);
        return;
      case "MessagePreview:CHANGE_THEME": {
        const theme = data.isDark ? SWITCH_THEMES.LIGHT : SWITCH_THEMES.DARK;
        await lazy.AddonManager.getAddonByID(theme).then(addon =>
          addon.enable()
        );
        return;
      }
      default:
        lazy.log.debug(`Unexpected event ${name} was not handled.`);
    }
  }
}
PK