Verified Commit 52400134 authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

fixup! Bug 7494: Create local home page for TBB.

Populate the page with real data.
parent 20e6e921
Loading
Loading
Loading
Loading
+83 −14
Changes for browser/components/abouttor/AboutTorParent.sys.mjs: 83 added lines, 14 removed lines.
Original line number Diff line number Diff line
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";

const kTorCheckFailedTopic = "Torbutton:TorCheckFailed";

class TorCheckObserver {
  #callback = null;

  constructor(callback) {
    this.#callback = callback;
    Services.obs.addObserver(this, kTorCheckFailedTopic);
  }

  stop() {
    Services.obs.removeObserver(this, kTorCheckFailedTopic);
  }

  observe(subject, topic, data) {
    if (topic === kTorCheckFailedTopic && this.#callback) {
      this.#callback();
    }
  }
}

export class AboutTorParent extends JSWindowActorParent {
  async receiveMessage(message) {
  #observer = null;

  constructor() {
    super();
    this.#observer = new TorCheckObserver(() => {
      this.#sendChromeData(false);
    });
  }

  didDestroy() {
    if (this.#observer) {
      this.#observer.stop();
      this.#observer = null;
    }
  }

  receiveMessage(message) {
    if (message.name === "AboutTor:ContentLoaded") {
      this.#sendChromeData();
      this.#sendChromeData(true);
      this.#sendLocale();
    }
  }

  #sendChromeData() {
    const tbbVersion = Services.prefs.getCharPref("torbrowser.version");
    this.sendAsyncMessage("AboutTor:ChromeData", {
      tbbVersion,
      torOn: true,
      updateChannel: "alpha",
      hasBeenUpdated: true,
    });
  async #sendChromeData(isRespondingToPageLoad) {
    const data = {
      updateChannel: AppConstants.MOZ_UPDATE_CHANNEL,
    };

    const checkSvc = Cc[
      "@torproject.org/torbutton-torCheckService;1"
    ].getService(Ci.nsISupports).wrappedJSObject;
    await checkSvc.runTorCheck();
    if (checkSvc.statusOfTorCheck !== checkSvc.kCheckFailed) {
      data.torOn = true;
    }

    if (isRespondingToPageLoad) {
      const kShouldNotifyPref = "torbrowser.post_update.shouldNotify";
      if (Services.prefs.getBoolPref(kShouldNotifyPref, false)) {
        Services.prefs.clearUserPref(kShouldNotifyPref);
        data.hasBeenUpdated = true;
        data.updateMoreInfoURL = Services.prefs.getCharPref(
          "torbrowser.post_update.url",
          ""
        );
        if (!data.updateMoreInfoURL) {
          // Use the default URL as a fallback.
          data.updateMoreInfoURL = Services.urlFormatter.formatURLPref(
            "startup.homepage_override_url"
          );
        }
      }
    }

    this.sendAsyncMessage("AboutTor:ChromeData", data);
  }

  #sendLocale() {
    try {
      const kBrandBundle = "chrome://branding/locale/brand.properties";
      const brandBundle = Services.strings.createBundle(kBrandBundle);
      const productName = brandBundle.GetStringFromName("brandFullName");
      let locale = Services.locale.appLocaleAsBCP47;
      if (locale === "ja-JP-macos") {
        locale = "ja";
      }
      this.sendAsyncMessage("AboutTor:LocaleData", { locale, productName });
      const kBrandBundle = "chrome://branding/locale/brand.properties";
      const brandBundle = Services.strings.createBundle(kBrandBundle);
      const productName = brandBundle.GetStringFromName("brandFullName");
      const productVersion = Services.prefs.getCharPref("torbrowser.version");
      this.sendAsyncMessage("AboutTor:LocaleData", {
        locale,
        // FIXME: This is probably wrong! But that's what we've done until now.
        // I think we can wait the about:tor refactor to fix it.
        productInfo: `${productName} ${productVersion}`,
      });
    } catch (e) {}
  }
}
+2 −10
Changes for browser/components/abouttor/content/aboutTor.js: 2 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -6,8 +6,6 @@
 *************************************************************************/

const AboutTorListener = {
  tbbVersion: "",

  init() {
    window.addEventListener("ChromeData", this.onChromeData.bind(this));
    window.addEventListener("LocaleData", this.onLocaleData.bind(this));
@@ -17,10 +15,6 @@ const AboutTorListener = {
    const body = document.body;
    const data = e.detail;

    if (data.tbbVersion) {
      this.tbbVersion = data.tbbVersion;
    }

    // Update status: tor on/off, Tor Browser manual shown.
    if (data.torOn) {
      body.setAttribute("toron", "yes");
@@ -46,16 +40,14 @@ const AboutTorListener = {
  },

  onLocaleData(e) {
    const { locale, productName } = e.detail;
    const { locale, productInfo } = e.detail;
    // Set localized "Get Involved" link.
    document.getElementById(
      "getInvolvedLink"
    ).href = `https://community.torproject.org/${locale}`;

    // Display the Tor Browser product name and version.
    document.getElementById(
      "torbrowser-version"
    ).textContent = `${productName} ${this.tbbVersion}`;
    document.getElementById("torbrowser-version").textContent = productInfo;
  },
};