Verified Commit bf71bd68 authored by Kathleen Brade's avatar Kathleen Brade Committed by Pier Angelo Vendrame
Browse files

Bug 7494: Create local home page for TBB.

parent d60d4929
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -715,6 +715,7 @@ var gPageIcons = {
};

var gInitialPages = [
  "about:tor",
  "about:torconnect",
  "about:blank",
  "about:home",
+18 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
  UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
  WebChannel: "resource://gre/modules/WebChannel.sys.mjs",
  WindowsRegistry: "resource://gre/modules/WindowsRegistry.sys.mjs",
  checkHomepageOverride: "resource:///modules/HomepageOverride.sys.mjs",
  clearTimeout: "resource://gre/modules/Timer.sys.mjs",
  setTimeout: "resource://gre/modules/Timer.sys.mjs",
});
@@ -329,6 +330,21 @@ let JSWINDOWACTORS = {
    matches: ["about:tabcrashed*"],
  },

  AboutTor: {
    parent: {
      esModuleURI: "resource:///actors/AboutTorParent.sys.mjs",
    },
    child: {
      esModuleURI: "resource:///actors/AboutTorChild.sys.mjs",

      events: {
        DOMContentLoaded: {},
      },
    },

    matches: ["about:tor"],
  },

  AboutWelcome: {
    parent: {
      moduleURI: "resource:///actors/AboutWelcomeParent.jsm",
@@ -1272,6 +1288,8 @@ BrowserGlue.prototype = {
      lazy.Normandy.init();
    }

    lazy.checkHomepageOverride();

    AboutHomeStartupCache.init();

    Services.obs.notifyObservers(null, "browser-ui-startup-complete");
+26 −0
Original line number Diff line number Diff line
export class AboutTorChild extends JSWindowActorChild {
  handleEvent(e) {
    switch (e.type) {
      case "DOMContentLoaded":
        this.sendAsyncMessage("AboutTor:ContentLoaded");
    }
  }

  receiveMessage(message) {
    switch (message.name) {
      case "AboutTor:ChromeData":
        this.#sendToContent("ChromeData", message.data);
        break;
      case "AboutTor:LocaleData":
        this.#sendToContent("LocaleData", message.data);
        break;
    }
  }

  #sendToContent(eventName, detail) {
    let event = new this.contentWindow.CustomEvent(eventName, {
      detail: Cu.cloneInto(detail, this.contentWindow),
    });
    this.contentWindow.dispatchEvent(event);
  }
}
+111 −0
Original line number Diff line number Diff line
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  TorCheckService: "resource:///modules/TorCheckService.sys.mjs",
});

const kTorCheckTopic = "TorCheckService:StatusChanged";

class TorCheckObserver {
  #callback = null;

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

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

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

export class AboutTorParent extends JSWindowActorParent {
  #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(true);
      this.#sendLocale();
    }
  }

  async #sendChromeData(isRespondingToPageLoad) {
    const data = {
      updateChannel: AppConstants.MOZ_UPDATE_CHANNEL,
    };

    if (lazy.TorCheckService.hasFailed || !isRespondingToPageLoad) {
      // If it has failed, check again, but do not wait for it.
      // An observer will call us if we need to change the page status.
      // Same if we are invoked by an observer, since now it can be called only
      // by the TorServiceCheck observer.
      lazy.TorCheckService.runTorCheck();
    } else {
      await lazy.TorCheckService.runTorCheck();
    }
    if (!lazy.TorCheckService.hasFailed) {
      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 {
      let locale = Services.locale.appLocaleAsBCP47;
      if (locale === "ja-JP-macos") {
        locale = "ja";
      }
      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) {}
  }
}
+15 −0
Original line number Diff line number Diff line
export function checkHomepageOverride() {
  // tor-browser#13835: Allow overriding the default homepage by setting a
  // custom environment variable.
  if (Services.env.exists("TOR_DEFAULT_HOMEPAGE")) {
    const prefName = "browser.startup.homepage";
    // if the user has set this value in a previous installation, don't
    // override it
    if (!Services.prefs.prefHasUserValue(prefName)) {
      Services.prefs.setCharPref(
        prefName,
        Services.env.get("TOR_DEFAULT_HOMEPAGE")
      );
    }
  }
}
Loading