mozilla / newtab-dev

Repo following gecko-dev for newtab development. No longer maintained.
Other
18 stars 10 forks source link

Bug 1183206 - Make about:newtab independent of Firefox #3

Closed emtwo closed 9 years ago

emtwo commented 9 years ago
emtwo commented 9 years ago

This addresses review comments in pull request #2.

marcoscaceres commented 9 years ago

So, linting the code with JSHint revealed a whole bunch of redundancies and dead code.

Making JSHint happy, I get:

/* 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/. */
/*globals Components, sendAsyncMessage, addMessageListener*/

"use strict";
(function() {
  const {
    utils: Cu
  } = Components;
  const TRUSTED_ORIGIN = "https://mozilla.github.io";
  const {
    XPCOMUtils
  } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
  const imports = {};
  XPCOMUtils.defineLazyModuleGetter(imports, "PrivateBrowsingUtils",
    "resource://gre/modules/PrivateBrowsingUtils.jsm");
  XPCOMUtils.defineLazyModuleGetter(imports, "Services",
    "resource://gre/modules/Services.jsm");

  let iframe;

  function handleCommand(command, data) {
    let commandHandled = true;
    switch (command) {
    case "NewTab:UpdateTelemetryProbe":
      imports.Services.telemetry.getHistogramById(data.probe).add(data.value);
      break;
    case "NewTab:Register":
      registerEvent(data.type);
      break;
    case "NewTab:GetInitialState":
      getInitialState();
      break;
    default:
      commandHandled = false;
    }
    return commandHandled;
  }

  function initRemotePage() {
    // Messages that the iframe sends the browser will be passed onto
    // the privileged parent process
    let iframe = getIframe();
    let loadHandler = () => {
      iframe.removeEventListener("load", loadHandler);
      iframe.contentDocument.addEventListener("NewTabCommand", (e) => {
        let handled = handleCommand(e.detail.command, e.detail.data);
        if (!handled) {
          sendAsyncMessage(e.detail.command, e.detail.data);
        }
      });
      registerEvent("NewTab:Observe");
      let ev = new CustomEvent("NewTabCommandReady");
      iframe.contentDocument.dispatchEvent(ev);
    };
    // Check if iframe already fired its onload event
    if (iframe.contentDocument.readyState === "complete") {
      loadHandler();
      return;
    }
    iframe.addEventListener("load", loadHandler);
  }

  function registerEvent(event) {
    // Messages that the privileged parent process sends will be passed
    // onto the iframe
    addMessageListener(event, (message) => {
      let iframe = getIframe();
      iframe.contentWindow.postMessage(message, TRUSTED_ORIGIN);
    });
  }

  function getInitialState() {
    let prefs = imports.Services.prefs;
    let isPrivate = imports.PrivateBrowsingUtils.isContentWindowPrivate(window);
    let state = {
      enabled: prefs.getBoolPref("browser.newtabpage.enabled"),
      enhanced: prefs.getBoolPref("browser.newtabpage.enhanced"),
      rows: prefs.getIntPref("browser.newtabpage.rows"),
      columns: prefs.getIntPref("browser.newtabpage.columns"),
      introShown: prefs.getBoolPref("browser.newtabpage.introShown"),
      privateBrowsingMode: isPrivate
    };
    let iframe = getIframe();
    iframe.contentWindow.postMessage({
      name: "NewTab:State",
      data: state
    }, TRUSTED_ORIGIN);
  }

  function getIframe() {
    if (!iframe) {
      iframe = document.getElementById("remotedoc");
    }
    return iframe;
  }

  // Everything is loaded. Initialize the New Tab Page.
  initRemotePage();
}());
marcoscaceres commented 9 years ago

Things to note in newTab.js:

marcoscaceres commented 9 years ago

@emtwo let me know if you agree with the above.

oyiptong commented 9 years ago

kill the dead code. kill it with fire

emtwo commented 9 years ago

@marcoscaceres those changes look good to me. Thanks for linting.

emtwo commented 9 years ago

@marcoscaceres r?

marcoscaceres commented 9 years ago

R+. Could you please squash the commits into a single commit?

marcoscaceres commented 9 years ago

Squashed commit history.

marcoscaceres commented 9 years ago

Note, this was merged to "bug_1021654_unprivileged_about_newtab_convert_about_newtab_to_use_messages_and_a_content_script_in_order_to_work_well_in_e10s"