This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
newTab.js
107 lines (97 loc) · 3.71 KB
/
newTab.js
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
/* 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 XPCOMUtils, Components, sendAsyncMessage, addMessageListener, removeMessageListener,
Services, PrivateBrowsingUtils*/
"use strict";
const {utils: Cu, interfaces: Ci} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
(function() {
let remoteNewTabLocation;
let remoteIFrame;
/**
* Attempts to handle commands sent from the remote IFrame within this content frame.
* Expected commands below, with data types explained.
*
* @returns {Boolean} whether or not the command was handled
* @param {String} command
* The command passed from the remote IFrame
* @param {Object} data
* Parameters to the command
*/
function handleCommand(command, data) {
let commandHandled = true;
switch (command) {
case "NewTab:UpdateTelemetryProbe":
/**
* Update a given Telemetry histogram
*
* @param {String} data.probe
* Probe name to update
* @param {Number} data.value
* Value to update histogram by
*/
Services.telemetry.getHistogramById(data.probe).add(data.value);
break;
case "NewTab:Register":
registerEvent(data.type);
break;
case "NewTab:GetInitialState":
data = {};
data.windowID = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
data.privateBrowsingMode = PrivateBrowsingUtils.isContentWindowPrivate(window);
// Fallthrough - more handling required.
default:
commandHandled = false;
}
return {commandHandled, data};
}
function initRemotePage(initData) {
// Messages that the iframe sends the browser will be passed onto
// the privileged parent process
remoteNewTabLocation = initData;
remoteIFrame = document.querySelector("#remotedoc");
let loadHandler = () => {
if (remoteIFrame.src !== remoteNewTabLocation.href) {
return;
}
remoteIFrame.removeEventListener("load", loadHandler);
remoteIFrame.contentDocument.addEventListener("NewTabCommand", (e) => {
// If the commands are not handled within this content frame, the command will be
// passed on to main process, in RemoteAboutNewTab.jsm
let {handled, data} = handleCommand(e.detail.command, e.detail.data);
if (!handled) {
sendAsyncMessage(e.detail.command, data);
}
});
registerEvent("NewTab:Observe");
let ev = new CustomEvent("NewTabCommandReady");
remoteIFrame.contentDocument.dispatchEvent(ev);
};
remoteIFrame.src = remoteNewTabLocation.href;
remoteIFrame.addEventListener("load", loadHandler);
}
/**
* Allows the content IFrame to register a listener to an event sent by
* the privileged parent process, in RemoteAboutNewTab.jsm
*
* @param {String} eventName
* Event name to listen to
*/
function registerEvent(eventName) {
addMessageListener(eventName, (message) => {
remoteIFrame.contentWindow.postMessage(message, remoteNewTabLocation.origin);
});
}
addMessageListener("NewTabFrame:Init", function loadHandler(message) {
// Everything is loaded. Initialize the New Tab Page.
removeMessageListener("NewTabFrame:Init", loadHandler);
initRemotePage(message.data);
});
sendAsyncMessage("NewTabFrame:GetInit");
}());