Commit e7f550c5 authored by Greg Tatum's avatar Greg Tatum
Browse files

Bug 1829687 - Add isRestrictedPage methods; r=nordzilla

This abstracts out the restricted page check. I duplicated the logic in
both the parent and the child since a later patch needs it. It also
adds another check when translating the page that we're not actually
attempting to translate a restricted page. This guards against cases
where the translation is mistakenly invoked on a page that shouldn't be
translated.

Differential Revision: https://phabricator.services.mozilla.com/D179083
parent bdc2b494
Loading
Loading
Loading
Loading
+20 −6
Original line number Diff line number Diff line
@@ -544,6 +544,20 @@ export class TranslationsChild extends JSWindowActorChild {
    );
  }

  /**
   * Only translate pages that match certain protocols, that way internal pages like
   * about:* pages will not be translated.
   */
  #isRestrictedPage() {
    const { href } = this.contentWindow.location;
    // Keep this logic up to date with TranslationsParent.isRestrictedPage.
    return !(
      href.startsWith("http://") ||
      href.startsWith("https://") ||
      href.startsWith("file:///")
    );
  }

  /**
   * Determine if the page should be translated by checking the App's languages and
   * comparing it to the reported language of the page. If we can translate the page,
@@ -556,12 +570,7 @@ export class TranslationsChild extends JSWindowActorChild {
      return this.#langTags;
    }

    const { href } = this.contentWindow.location;
    if (
      !href.startsWith("http://") &&
      !href.startsWith("https://") &&
      !href.startsWith("file:///")
    ) {
    if (this.#isRestrictedPage()) {
      return null;
    }

@@ -710,6 +719,11 @@ export class TranslationsChild extends JSWindowActorChild {
      lazy.console.warn("This page was already translated.");
      return;
    }
    if (this.#isRestrictedPage()) {
      lazy.console.warn("Attempting to translate a restricted page.");
      return;
    }

    try {
      const engineLoadStart = this.docShell.now();
      // Create a function to get an engine. These engines are pretty heavy in terms
+14 −0
Original line number Diff line number Diff line
@@ -266,6 +266,20 @@ export class TranslationsParent extends JSWindowActorParent {
    return TranslationsParent.#isTranslationsEngineSupported;
  }

  /**
   * Only translate pages that match certain protocols, that way internal pages like
   * about:* pages will not be translated.
   * @param {string} url
   */
  static isRestrictedPage(url) {
    // Keep this logic up to date with TranslationsChild.prototype.#isRestrictedPage.
    return !(
      url.startsWith("http://") ||
      url.startsWith("https://") ||
      url.startsWith("file:///")
    );
  }

  async receiveMessage({ name, data }) {
    switch (name) {
      case "Translations:GetTranslationsEnginePayload": {