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 (b6d82b1a6b02)

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
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* 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 = ["ReaderParent"];

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

ChromeUtils.defineModuleGetter(
  this,
  "PlacesUtils",
  "resource://gre/modules/PlacesUtils.jsm"
);
ChromeUtils.defineModuleGetter(
  this,
  "ReaderMode",
  "resource://gre/modules/ReaderMode.jsm"
);

const gStringBundle = Services.strings.createBundle(
  "chrome://global/locale/aboutReader.properties"
);

var ReaderParent = {
  // Listeners are added in BrowserGlue.jsm
  receiveMessage(message) {
    switch (message.name) {
      case "Reader:FaviconRequest": {
        if (message.target.messageManager) {
          try {
            let preferredWidth = message.data.preferredWidth || 0;
            let uri = Services.io.newURI(message.data.url);
            PlacesUtils.favicons.getFaviconURLForPage(
              uri,
              iconUri => {
                if (iconUri) {
                  iconUri = PlacesUtils.favicons.getFaviconLinkForIcon(iconUri);
                  message.target.messageManager.sendAsyncMessage(
                    "Reader:FaviconReturn",
                    {
                      url: message.data.url,
                      faviconUrl: iconUri.pathQueryRef.replace(/^favicon:/, ""),
                    }
                  );
                }
              },
              preferredWidth
            );
          } catch (ex) {
            Cu.reportError(
              "Error requesting favicon URL for about:reader content: " + ex
            );
          }
        }
        break;
      }

      case "Reader:UpdateReaderButton": {
        let browser = message.target;
        if (message.data && message.data.isArticle !== undefined) {
          browser.isArticle = message.data.isArticle;
        }
        this.updateReaderButton(browser);
        break;
      }
    }
  },

  updateReaderButton(browser) {
    let win = browser.ownerGlobal;
    if (browser != win.gBrowser.selectedBrowser) {
      return;
    }

    let button = win.document.getElementById("reader-mode-button");
    let menuitem = win.document.getElementById("menu_readerModeItem");
    let key = win.document.getElementById("key_toggleReaderMode");
    // aria-reader is not a real ARIA attribute. However, this will cause
    // Gecko accessibility to expose the "reader" object attribute. We do this
    // so that the reader state is easy for accessibility clients to access
    // programmatically.
    if (browser.currentURI.spec.startsWith("about:reader")) {
      let closeText = gStringBundle.GetStringFromName("readerView.close");

      button.setAttribute("readeractive", true);
      button.hidden = false;
      button.setAttribute("aria-label", closeText);

      menuitem.setAttribute("label", closeText);
      menuitem.setAttribute("hidden", false);
      menuitem.setAttribute(
        "accesskey",
        gStringBundle.GetStringFromName("readerView.close.accesskey")
      );

      key.setAttribute("disabled", false);

      browser.setAttribute("aria-reader", "active");
      Services.obs.notifyObservers(null, "reader-mode-available");
    } else {
      let enterText = gStringBundle.GetStringFromName("readerView.enter");

      button.removeAttribute("readeractive");
      button.hidden = !browser.isArticle;
      button.setAttribute("aria-label", enterText);

      menuitem.setAttribute("label", enterText);
      menuitem.setAttribute("hidden", !browser.isArticle);
      menuitem.setAttribute(
        "accesskey",
        gStringBundle.GetStringFromName("readerView.enter.accesskey")
      );

      key.setAttribute("disabled", !browser.isArticle);

      if (browser.isArticle) {
        browser.setAttribute("aria-reader", "available");
        Services.obs.notifyObservers(null, "reader-mode-available");
      } else {
        browser.removeAttribute("aria-reader");
      }
    }
  },

  forceShowReaderIcon(browser) {
    browser.isArticle = true;
    this.updateReaderButton(browser);
  },

  buttonClick(event) {
    if (event.button != 0) {
      return;
    }
    this.toggleReaderMode(event);
  },

  toggleReaderMode(event) {
    let win = event.target.ownerGlobal;
    let browser = win.gBrowser.selectedBrowser;
    browser.messageManager.sendAsyncMessage("Reader:ToggleReaderMode");
  },

  /**
   * Gets an article for a given URL. This method will download and parse a document.
   *
   * @param url The article URL.
   * @param browser The browser where the article is currently loaded.
   * @return {Promise}
   * @resolves JS object representing the article, or null if no article is found.
   */
  async _getArticle(url, browser) {
    return ReaderMode.downloadAndParseDocument(url).catch(e => {
      if (e && e.newURL) {
        // Pass up the error so we can navigate the browser in question to the new URL:
        throw e;
      }
      Cu.reportError("Error downloading and parsing document: " + e);
      return null;
    });
  },
};