2019-07-12 00:50:17 +03:00
|
|
|
/* 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";
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
2019-07-12 00:50:17 +03:00
|
|
|
);
|
2019-08-24 09:35:25 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
Services: "resource://gre/modules/Services.jsm",
|
|
|
|
EveryWindow: "resource:///modules/EveryWindow.jsm",
|
|
|
|
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
|
2019-11-20 22:45:18 +03:00
|
|
|
RemoteL10n: "resource://activity-stream/lib/RemoteL10n.jsm",
|
2019-08-24 09:35:25 +03:00
|
|
|
});
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
2019-07-29 09:55:43 +03:00
|
|
|
this,
|
2019-08-24 09:35:25 +03:00
|
|
|
"TrackingDBService",
|
|
|
|
"@mozilla.org/tracking-db-service;1",
|
|
|
|
"nsITrackingDBService"
|
2019-07-29 09:55:43 +03:00
|
|
|
);
|
2019-07-12 00:50:17 +03:00
|
|
|
|
2019-11-26 18:18:55 +03:00
|
|
|
const idToTextMap = new Map([
|
|
|
|
[Ci.nsITrackingDBService.TRACKERS_ID, "tracker"],
|
|
|
|
[Ci.nsITrackingDBService.TRACKING_COOKIES_ID, "cookie"],
|
|
|
|
[Ci.nsITrackingDBService.CRYPTOMINERS_ID, "cryptominer"],
|
|
|
|
[Ci.nsITrackingDBService.FINGERPRINTERS_ID, "fingerprinter"],
|
|
|
|
[Ci.nsITrackingDBService.SOCIAL_ID, "social"],
|
|
|
|
]);
|
|
|
|
|
2019-07-12 00:50:17 +03:00
|
|
|
const WHATSNEW_ENABLED_PREF = "browser.messaging-system.whatsNewPanel.enabled";
|
2019-08-07 20:57:48 +03:00
|
|
|
const PROTECTIONS_PANEL_INFOMSG_PREF =
|
|
|
|
"browser.protections_panel.infoMessage.seen";
|
2019-07-12 00:50:17 +03:00
|
|
|
|
|
|
|
const TOOLBAR_BUTTON_ID = "whats-new-menu-button";
|
|
|
|
const APPMENU_BUTTON_ID = "appMenu-whatsnew-button";
|
|
|
|
|
|
|
|
const BUTTON_STRING_ID = "cfr-whatsnew-button";
|
2019-09-14 01:57:40 +03:00
|
|
|
const WHATS_NEW_PANEL_SELECTOR = "PanelUI-whatsNew-message-container";
|
2019-07-12 00:50:17 +03:00
|
|
|
|
|
|
|
class _ToolbarPanelHub {
|
|
|
|
constructor() {
|
2019-07-29 09:55:43 +03:00
|
|
|
this.triggerId = "whatsNewPanelOpened";
|
2019-07-12 00:50:17 +03:00
|
|
|
this._showAppmenuButton = this._showAppmenuButton.bind(this);
|
|
|
|
this._hideAppmenuButton = this._hideAppmenuButton.bind(this);
|
|
|
|
this._showToolbarButton = this._showToolbarButton.bind(this);
|
|
|
|
this._hideToolbarButton = this._hideToolbarButton.bind(this);
|
2019-08-07 20:57:48 +03:00
|
|
|
this.insertProtectionPanelMessage = this.insertProtectionPanelMessage.bind(
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
this.state = {};
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
async init(waitForInitialized, { getMessages, dispatch, handleUserAction }) {
|
2019-07-12 00:50:17 +03:00
|
|
|
this._getMessages = getMessages;
|
2019-07-29 09:55:43 +03:00
|
|
|
this._dispatch = dispatch;
|
2019-08-24 09:35:25 +03:00
|
|
|
this._handleUserAction = handleUserAction;
|
2019-07-19 02:53:04 +03:00
|
|
|
// Wait for ASRouter messages to become available in order to know
|
|
|
|
// if we can show the What's New panel
|
|
|
|
await waitForInitialized;
|
2019-07-12 00:50:17 +03:00
|
|
|
if (this.whatsNewPanelEnabled) {
|
2019-07-19 02:53:04 +03:00
|
|
|
// Enable the application menu button so that the user can access
|
|
|
|
// the panel outside of the toolbar button
|
2019-07-12 00:50:17 +03:00
|
|
|
this.enableAppmenuButton();
|
|
|
|
}
|
2019-07-19 02:53:04 +03:00
|
|
|
// Listen for pref changes that could turn off the feature
|
|
|
|
Services.prefs.addObserver(WHATSNEW_ENABLED_PREF, this);
|
2019-08-07 20:57:48 +03:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
protectionPanelMessageSeen: Services.prefs.getBoolPref(
|
|
|
|
PROTECTIONS_PANEL_INFOMSG_PREF,
|
|
|
|
false
|
|
|
|
),
|
|
|
|
};
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uninit() {
|
|
|
|
EveryWindow.unregisterCallback(TOOLBAR_BUTTON_ID);
|
|
|
|
EveryWindow.unregisterCallback(APPMENU_BUTTON_ID);
|
2019-07-19 02:53:04 +03:00
|
|
|
Services.prefs.removeObserver(WHATSNEW_ENABLED_PREF, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
observe(aSubject, aTopic, aPrefName) {
|
|
|
|
switch (aPrefName) {
|
|
|
|
case WHATSNEW_ENABLED_PREF:
|
|
|
|
if (!this.whatsNewPanelEnabled) {
|
|
|
|
this.uninit();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get messages() {
|
|
|
|
return this._getMessages({
|
|
|
|
template: "whatsnew_panel_message",
|
|
|
|
triggerId: "whatsNewPanelOpened",
|
|
|
|
returnAll: true,
|
|
|
|
});
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get whatsNewPanelEnabled() {
|
|
|
|
return Services.prefs.getBoolPref(WHATSNEW_ENABLED_PREF, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
maybeInsertFTL(win) {
|
|
|
|
win.MozXULElement.insertFTLIfNeeded("browser/newtab/asrouter.ftl");
|
2019-08-29 17:12:59 +03:00
|
|
|
win.MozXULElement.insertFTLIfNeeded("browser/branding/brandings.ftl");
|
|
|
|
win.MozXULElement.insertFTLIfNeeded("browser/branding/sync-brand.ftl");
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turns on the Appmenu (hamburger menu) button for all open windows and future windows.
|
2019-07-19 02:53:04 +03:00
|
|
|
async enableAppmenuButton() {
|
|
|
|
if ((await this.messages).length) {
|
|
|
|
EveryWindow.registerCallback(
|
|
|
|
APPMENU_BUTTON_ID,
|
|
|
|
this._showAppmenuButton,
|
|
|
|
this._hideAppmenuButton
|
|
|
|
);
|
|
|
|
}
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turns on the Toolbar button for all open windows and future windows.
|
2019-07-19 02:53:04 +03:00
|
|
|
async enableToolbarButton() {
|
|
|
|
if ((await this.messages).length) {
|
|
|
|
EveryWindow.registerCallback(
|
|
|
|
TOOLBAR_BUTTON_ID,
|
|
|
|
this._showToolbarButton,
|
|
|
|
this._hideToolbarButton
|
|
|
|
);
|
|
|
|
}
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:52:26 +03:00
|
|
|
// When the panel is hidden we want to run some cleanup
|
|
|
|
_onPanelHidden(win) {
|
|
|
|
const panelContainer = win.document.getElementById(
|
|
|
|
"customizationui-widget-panel"
|
|
|
|
);
|
|
|
|
// When the panel is hidden we want to remove any toolbar buttons that
|
|
|
|
// might have been added as an entry point to the panel
|
|
|
|
const removeToolbarButton = () => {
|
|
|
|
EveryWindow.unregisterCallback(TOOLBAR_BUTTON_ID);
|
|
|
|
};
|
|
|
|
if (!panelContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
panelContainer.addEventListener("popuphidden", removeToolbarButton, {
|
|
|
|
once: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
// Newer messages first and use `order` field to decide between messages
|
|
|
|
// with the same timestamp
|
|
|
|
_sortWhatsNewMessages(m1, m2) {
|
|
|
|
// Sort by published_date in descending order.
|
|
|
|
if (m1.content.published_date === m2.content.published_date) {
|
|
|
|
// Ascending order
|
|
|
|
return m1.order - m2.order;
|
|
|
|
}
|
|
|
|
if (m1.content.published_date > m2.content.published_date) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-12 00:50:17 +03:00
|
|
|
// Render what's new messages into the panel.
|
2019-09-14 01:57:40 +03:00
|
|
|
async renderMessages(win, doc, containerId, options = {}) {
|
|
|
|
const messages =
|
|
|
|
(options.force && options.messages) ||
|
|
|
|
(await this.messages).sort(this._sortWhatsNewMessages);
|
2019-07-12 00:50:17 +03:00
|
|
|
const container = doc.getElementById(containerId);
|
|
|
|
|
2019-10-04 20:06:01 +03:00
|
|
|
if (messages) {
|
|
|
|
// Targeting attribute state might have changed making new messages
|
|
|
|
// available and old messages invalid, we need to refresh
|
|
|
|
for (const prevMessageEl of container.querySelectorAll(
|
|
|
|
".whatsNew-message"
|
|
|
|
)) {
|
|
|
|
container.removeChild(prevMessageEl);
|
|
|
|
}
|
2019-07-12 00:50:17 +03:00
|
|
|
let previousDate = 0;
|
2019-08-24 09:35:25 +03:00
|
|
|
// Get and store any variable part of the message content
|
|
|
|
this.state.contentArguments = await this._contentArguments();
|
2019-07-29 09:55:43 +03:00
|
|
|
for (let message of messages) {
|
2019-07-12 00:50:17 +03:00
|
|
|
container.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createMessageElements(win, doc, message, previousDate)
|
2019-07-12 00:50:17 +03:00
|
|
|
);
|
2019-07-29 09:55:43 +03:00
|
|
|
previousDate = message.content.published_date;
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:52:26 +03:00
|
|
|
this._onPanelHidden(win);
|
2019-07-29 09:55:43 +03:00
|
|
|
|
|
|
|
// Panel impressions are not associated with one particular message
|
|
|
|
// but with a set of messages. We concatenate message ids and send them
|
|
|
|
// back for every impression.
|
|
|
|
const eventId = {
|
|
|
|
id: messages
|
|
|
|
.map(({ id }) => id)
|
|
|
|
.sort()
|
|
|
|
.join(","),
|
|
|
|
};
|
|
|
|
// Check `mainview` attribute to determine if the panel is shown as a
|
|
|
|
// subview (inside the application menu) or as a toolbar dropdown.
|
|
|
|
// https://searchfox.org/mozilla-central/rev/07f7390618692fa4f2a674a96b9b677df3a13450/browser/components/customizableui/PanelMultiView.jsm#1268
|
|
|
|
const mainview = win.PanelUI.whatsNewPanel.hasAttribute("mainview");
|
|
|
|
this.sendUserEventTelemetry(win, "IMPRESSION", eventId, {
|
|
|
|
value: { view: mainview ? "toolbar_dropdown" : "application_menu" },
|
|
|
|
});
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
2019-09-14 01:57:40 +03:00
|
|
|
removeMessages(win, containerId) {
|
|
|
|
const doc = win.document;
|
|
|
|
const messageNodes = doc
|
|
|
|
.getElementById(containerId)
|
|
|
|
.querySelectorAll(".whatsNew-message");
|
|
|
|
for (const messageNode of messageNodes) {
|
|
|
|
messageNode.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
/**
|
2019-09-26 23:01:28 +03:00
|
|
|
* Dispatch the action defined in the message and user telemetry event.
|
2019-08-24 09:35:25 +03:00
|
|
|
*/
|
2019-09-26 23:01:28 +03:00
|
|
|
_dispatchUserAction(win, message) {
|
2019-11-23 03:08:00 +03:00
|
|
|
let url;
|
|
|
|
try {
|
|
|
|
// Set platform specific path variables for SUMO articles
|
|
|
|
url = Services.urlFormatter.formatURL(message.content.cta_url);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
url = message.content.cta_url;
|
|
|
|
}
|
2019-09-26 23:01:28 +03:00
|
|
|
this._handleUserAction({
|
|
|
|
target: win,
|
|
|
|
data: {
|
|
|
|
type: message.content.cta_type,
|
2019-08-24 09:35:25 +03:00
|
|
|
data: {
|
2019-11-23 03:08:00 +03:00
|
|
|
args: url,
|
2019-09-26 23:01:28 +03:00
|
|
|
where: "tabshifted",
|
2019-08-24 09:35:25 +03:00
|
|
|
},
|
2019-09-26 23:01:28 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.sendUserEventTelemetry(win, "CLICK", message);
|
|
|
|
}
|
2019-08-24 09:35:25 +03:00
|
|
|
|
2019-09-26 23:01:28 +03:00
|
|
|
/**
|
|
|
|
* Attach event listener to dispatch message defined action.
|
|
|
|
*/
|
|
|
|
_attachClickListener(win, element, message) {
|
|
|
|
// Add event listener for `mouseup` not to overlap with the
|
|
|
|
// `mousedown` & `click` events dispatched from PanelMultiView.jsm
|
|
|
|
// https://searchfox.org/mozilla-central/rev/7531325c8660cfa61bf71725f83501028178cbb9/browser/components/customizableui/PanelMultiView.jsm#1830-1837
|
|
|
|
element.addEventListener("mouseup", () => {
|
|
|
|
this._dispatchUserAction(win, message);
|
2019-08-24 09:35:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
async _createMessageElements(win, doc, message, previousDate) {
|
2019-07-29 09:55:43 +03:00
|
|
|
const { content } = message;
|
2019-11-20 22:45:18 +03:00
|
|
|
const messageEl = await this._createElement(doc, "div");
|
2019-07-12 00:50:17 +03:00
|
|
|
messageEl.classList.add("whatsNew-message");
|
|
|
|
|
2019-08-26 23:14:50 +03:00
|
|
|
// Only render date if it is different from the one rendered before.
|
|
|
|
if (content.published_date !== previousDate) {
|
|
|
|
messageEl.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createElement(doc, "p", {
|
2019-08-26 23:14:50 +03:00
|
|
|
classList: "whatsNew-message-date",
|
|
|
|
content: new Date(content.published_date).toLocaleDateString(
|
|
|
|
"default",
|
|
|
|
{
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
year: "numeric",
|
|
|
|
}
|
|
|
|
),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
const wrapperEl = await this._createElement(doc, "button");
|
2019-09-26 23:01:28 +03:00
|
|
|
wrapperEl.doCommand = () => this._dispatchUserAction(win, message);
|
2019-07-12 00:50:17 +03:00
|
|
|
wrapperEl.classList.add("whatsNew-message-body");
|
|
|
|
messageEl.appendChild(wrapperEl);
|
|
|
|
|
|
|
|
if (content.icon_url) {
|
|
|
|
wrapperEl.classList.add("has-icon");
|
2019-11-20 22:45:18 +03:00
|
|
|
const iconEl = await this._createElement(doc, "img");
|
2019-07-12 00:50:17 +03:00
|
|
|
iconEl.src = content.icon_url;
|
|
|
|
iconEl.classList.add("whatsNew-message-icon");
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._setTextAttribute(iconEl, "alt", content.icon_alt);
|
2019-07-12 00:50:17 +03:00
|
|
|
wrapperEl.appendChild(iconEl);
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
wrapperEl.appendChild(await this._createMessageContent(win, doc, content));
|
2019-07-12 00:50:17 +03:00
|
|
|
|
|
|
|
if (content.link_text) {
|
2019-11-20 22:45:18 +03:00
|
|
|
const anchorEl = await this._createElement(doc, "a", {
|
2019-09-17 21:56:03 +03:00
|
|
|
classList: "text-link",
|
|
|
|
content: content.link_text,
|
|
|
|
});
|
2019-09-26 23:01:28 +03:00
|
|
|
anchorEl.doCommand = () => this._dispatchUserAction(win, message);
|
2019-09-17 21:56:03 +03:00
|
|
|
wrapperEl.appendChild(anchorEl);
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
// Attach event listener on entire message container
|
2019-08-26 23:14:50 +03:00
|
|
|
this._attachClickListener(win, messageEl, message);
|
2019-08-24 09:35:25 +03:00
|
|
|
|
2019-07-12 00:50:17 +03:00
|
|
|
return messageEl;
|
|
|
|
}
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
/**
|
|
|
|
* Return message title (optional subtitle) and body
|
|
|
|
*/
|
2019-11-20 22:45:18 +03:00
|
|
|
async _createMessageContent(win, doc, content) {
|
2019-08-24 09:35:25 +03:00
|
|
|
const wrapperEl = new win.DocumentFragment();
|
|
|
|
|
|
|
|
wrapperEl.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createElement(doc, "h2", {
|
2019-08-24 09:35:25 +03:00
|
|
|
classList: "whatsNew-message-title",
|
|
|
|
content: content.title,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (content.layout) {
|
|
|
|
case "tracking-protections":
|
2019-11-20 22:45:18 +03:00
|
|
|
await wrapperEl.appendChild(
|
|
|
|
await this._createElement(doc, "h4", {
|
2019-08-24 09:35:25 +03:00
|
|
|
classList: "whatsNew-message-subtitle",
|
|
|
|
content: content.subtitle,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
wrapperEl.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createElement(doc, "h2", {
|
2019-08-24 09:35:25 +03:00
|
|
|
classList: "whatsNew-message-title-large",
|
|
|
|
content: this.state.contentArguments.blockedCount,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapperEl.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createElement(doc, "p", { content: content.body })
|
2019-08-24 09:35:25 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return wrapperEl;
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
async _createHeroElement(win, doc, message) {
|
|
|
|
const messageEl = await this._createElement(doc, "div");
|
2019-08-07 20:57:48 +03:00
|
|
|
messageEl.setAttribute("id", "protections-popup-message");
|
|
|
|
messageEl.classList.add("whatsNew-hero-message");
|
2019-11-20 22:45:18 +03:00
|
|
|
const wrapperEl = await this._createElement(doc, "div");
|
2019-08-07 20:57:48 +03:00
|
|
|
wrapperEl.classList.add("whatsNew-message-body");
|
|
|
|
messageEl.appendChild(wrapperEl);
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
wrapperEl.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createElement(doc, "h2", {
|
2019-08-24 09:35:25 +03:00
|
|
|
classList: "whatsNew-message-title",
|
|
|
|
content: message.content.title,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
wrapperEl.appendChild(
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._createElement(doc, "p", { content: message.content.body })
|
2019-08-24 09:35:25 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (message.content.link_text) {
|
2019-11-20 22:45:18 +03:00
|
|
|
let linkEl = await this._createElement(doc, "a", {
|
2019-09-30 14:39:34 +03:00
|
|
|
classList: "text-link",
|
|
|
|
content: message.content.link_text,
|
|
|
|
});
|
|
|
|
wrapperEl.appendChild(linkEl);
|
|
|
|
this._attachClickListener(win, linkEl, message);
|
|
|
|
} else {
|
|
|
|
this._attachClickListener(win, wrapperEl, message);
|
2019-08-07 20:57:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return messageEl;
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
async _createElement(doc, elem, options = {}) {
|
2019-08-24 09:35:25 +03:00
|
|
|
const node = doc.createElementNS("http://www.w3.org/1999/xhtml", elem);
|
|
|
|
if (options.classList) {
|
|
|
|
node.classList.add(options.classList);
|
|
|
|
}
|
|
|
|
if (options.content) {
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._setString(node, options.content);
|
2019-08-24 09:35:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 09:35:25 +03:00
|
|
|
async _contentArguments() {
|
|
|
|
// Between now and 6 weeks ago
|
|
|
|
const dateTo = new Date();
|
|
|
|
const dateFrom = new Date(dateTo.getTime() - 42 * 24 * 60 * 60 * 1000);
|
|
|
|
const eventsByDate = await TrackingDBService.getEventsByDateRange(
|
|
|
|
dateFrom,
|
|
|
|
dateTo
|
|
|
|
);
|
|
|
|
// Count all events in the past 6 weeks
|
2019-11-26 18:18:55 +03:00
|
|
|
// Returns an object with:
|
|
|
|
// `blockedCount` total number of blocked resources
|
|
|
|
// {tracker|cookie|social...} breakdown by event type as defined by `idToTextMap`
|
2019-08-24 09:35:25 +03:00
|
|
|
const totalEvents = eventsByDate.reduce(
|
2019-11-26 18:18:55 +03:00
|
|
|
(acc, day) => {
|
|
|
|
const type = day.getResultByName("type");
|
|
|
|
const count = day.getResultByName("count");
|
|
|
|
acc[idToTextMap.get(type)] = (acc[idToTextMap.get(type)] || 0) + count;
|
|
|
|
acc.blockedCount += count;
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{ blockedCount: 0 }
|
2019-08-24 09:35:25 +03:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
// Keys need to match variable names used in asrouter.ftl
|
|
|
|
// `earliestDate` will be either 6 weeks ago or when tracking recording
|
|
|
|
// started. Whichever is more recent.
|
2019-11-26 18:18:55 +03:00
|
|
|
earliestDate: Math.max(
|
|
|
|
new Date(await TrackingDBService.getEarliestRecordedDate()),
|
|
|
|
dateFrom
|
|
|
|
),
|
|
|
|
...totalEvents,
|
2019-08-24 09:35:25 +03:00
|
|
|
};
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If `string_id` is present it means we are relying on fluent for translations.
|
|
|
|
// Otherwise, we have a vanilla string.
|
2019-11-20 22:45:18 +03:00
|
|
|
async _setString(el, stringObj) {
|
2019-09-14 01:57:40 +03:00
|
|
|
if (stringObj && stringObj.string_id) {
|
2019-11-20 22:45:18 +03:00
|
|
|
const [{ value }] = await RemoteL10n.l10n.formatMessages([
|
|
|
|
{
|
|
|
|
id: stringObj.string_id,
|
|
|
|
// Pass all available arguments to Fluent
|
|
|
|
args: this.state.contentArguments,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
el.textContent = value;
|
2019-07-12 00:50:17 +03:00
|
|
|
} else {
|
|
|
|
el.textContent = stringObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:33:30 +03:00
|
|
|
// If `string_id` is present it means we are relying on fluent for translations.
|
|
|
|
// Otherwise, we have a vanilla string.
|
2019-11-20 22:45:18 +03:00
|
|
|
async _setTextAttribute(el, attr, stringObj) {
|
2019-09-14 01:57:40 +03:00
|
|
|
if (stringObj && stringObj.string_id) {
|
2019-11-20 22:45:18 +03:00
|
|
|
const [{ attributes }] = await RemoteL10n.l10n.formatMessages([
|
|
|
|
{
|
|
|
|
id: stringObj.string_id,
|
|
|
|
// Pass all available arguments to Fluent
|
|
|
|
args: this.state.contentArguments,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
if (attributes) {
|
|
|
|
const { value } = attributes.find(({ name }) => name === attr);
|
|
|
|
el.setAttribute(attr, value);
|
|
|
|
}
|
2019-08-02 21:33:30 +03:00
|
|
|
} else {
|
|
|
|
el.setAttribute(attr, stringObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
async _showAppmenuButton(win) {
|
2019-07-12 00:50:17 +03:00
|
|
|
this.maybeInsertFTL(win);
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._showElement(
|
2019-07-12 00:50:17 +03:00
|
|
|
win.browser.ownerDocument,
|
|
|
|
APPMENU_BUTTON_ID,
|
|
|
|
BUTTON_STRING_ID
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_hideAppmenuButton(win) {
|
|
|
|
this._hideElement(win.browser.ownerDocument, APPMENU_BUTTON_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
_showToolbarButton(win) {
|
|
|
|
const document = win.browser.ownerDocument;
|
|
|
|
this.maybeInsertFTL(win);
|
2019-11-20 22:45:18 +03:00
|
|
|
return this._showElement(document, TOOLBAR_BUTTON_ID, BUTTON_STRING_ID);
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_hideToolbarButton(win) {
|
|
|
|
this._hideElement(win.browser.ownerDocument, TOOLBAR_BUTTON_ID);
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:45:18 +03:00
|
|
|
async _showElement(document, id, string_id) {
|
2019-07-12 00:50:17 +03:00
|
|
|
const el = document.getElementById(id);
|
2019-11-20 22:45:18 +03:00
|
|
|
await this._setTextAttribute(el, "label", { string_id });
|
|
|
|
await this._setTextAttribute(el, "tooltiptext", { string_id });
|
2019-07-12 00:50:17 +03:00
|
|
|
el.removeAttribute("hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
_hideElement(document, id) {
|
|
|
|
document.getElementById(id).setAttribute("hidden", true);
|
|
|
|
}
|
2019-07-29 09:55:43 +03:00
|
|
|
|
|
|
|
_sendTelemetry(ping) {
|
|
|
|
this._dispatch({
|
|
|
|
type: "TOOLBAR_PANEL_TELEMETRY",
|
|
|
|
data: { action: "cfr_user_event", source: "CFR", ...ping },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendUserEventTelemetry(win, event, message, options = {}) {
|
|
|
|
// Only send pings for non private browsing windows
|
|
|
|
if (
|
|
|
|
win &&
|
|
|
|
!PrivateBrowsingUtils.isBrowserPrivate(
|
|
|
|
win.ownerGlobal.gBrowser.selectedBrowser
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
this._sendTelemetry({
|
|
|
|
message_id: message.id,
|
|
|
|
bucket_id: message.id,
|
|
|
|
event,
|
2019-11-06 23:43:58 +03:00
|
|
|
event_context: options.value,
|
2019-07-29 09:55:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 20:57:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a message into the Protections Panel. The message is visible once
|
|
|
|
* and afterwards set in a collapsed state. It can be shown again using the
|
|
|
|
* info button in the panel header.
|
|
|
|
*/
|
|
|
|
async insertProtectionPanelMessage(event) {
|
|
|
|
const win = event.target.ownerGlobal;
|
2019-08-16 08:54:16 +03:00
|
|
|
this.maybeInsertFTL(win);
|
|
|
|
|
2019-08-07 20:57:48 +03:00
|
|
|
const doc = event.target.ownerDocument;
|
|
|
|
const container = doc.getElementById("messaging-system-message-container");
|
|
|
|
const infoButton = doc.getElementById("protections-popup-info-button");
|
|
|
|
const panelContainer = doc.getElementById("protections-popup");
|
|
|
|
const toggleMessage = () => {
|
|
|
|
container.toggleAttribute("disabled");
|
|
|
|
infoButton.toggleAttribute("checked");
|
2019-08-24 22:28:06 +03:00
|
|
|
panelContainer.toggleAttribute("infoMessageShowing");
|
2019-08-07 20:57:48 +03:00
|
|
|
};
|
|
|
|
if (!container.childElementCount) {
|
|
|
|
const message = await this._getMessages({
|
|
|
|
template: "protections_panel",
|
|
|
|
triggerId: "protectionsPanelOpen",
|
|
|
|
});
|
|
|
|
if (message) {
|
2019-11-20 22:45:18 +03:00
|
|
|
const messageEl = await this._createHeroElement(win, doc, message);
|
2019-08-07 20:57:48 +03:00
|
|
|
container.appendChild(messageEl);
|
|
|
|
infoButton.addEventListener("click", toggleMessage);
|
2019-10-19 03:41:03 +03:00
|
|
|
this.sendUserEventTelemetry(win, "IMPRESSION", message);
|
2019-08-07 20:57:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Message is collapsed by default. If it was never shown before we want
|
|
|
|
// to expand it
|
|
|
|
if (
|
|
|
|
!this.state.protectionPanelMessageSeen &&
|
|
|
|
container.hasAttribute("disabled")
|
|
|
|
) {
|
|
|
|
toggleMessage();
|
|
|
|
}
|
|
|
|
// Save state that we displayed the message
|
|
|
|
if (!this.state.protectionPanelMessageSeen) {
|
|
|
|
Services.prefs.setBoolPref(PROTECTIONS_PANEL_INFOMSG_PREF, true);
|
|
|
|
this.state.protectionPanelMessageSeen = true;
|
|
|
|
}
|
|
|
|
// Collapse the message after the panel is hidden so we don't get the
|
|
|
|
// animation when opening the panel
|
|
|
|
panelContainer.addEventListener(
|
|
|
|
"popuphidden",
|
|
|
|
() => {
|
|
|
|
if (
|
|
|
|
this.state.protectionPanelMessageSeen &&
|
|
|
|
!container.hasAttribute("disabled")
|
|
|
|
) {
|
|
|
|
toggleMessage();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
once: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2019-09-14 01:57:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} browser MessageChannel target argument as a response to a user action
|
|
|
|
* @param {object} message Message selected from devtools page
|
|
|
|
*/
|
|
|
|
forceShowMessage(browser, message) {
|
|
|
|
const win = browser.browser.ownerGlobal;
|
|
|
|
const doc = browser.browser.ownerDocument;
|
|
|
|
this.removeMessages(win, WHATS_NEW_PANEL_SELECTOR);
|
|
|
|
this.renderMessages(win, doc, WHATS_NEW_PANEL_SELECTOR, {
|
|
|
|
force: true,
|
|
|
|
messages: [message],
|
|
|
|
});
|
|
|
|
win.PanelUI.panel.addEventListener("popuphidden", event =>
|
|
|
|
this.removeMessages(event.target.ownerGlobal, WHATS_NEW_PANEL_SELECTOR)
|
|
|
|
);
|
|
|
|
}
|
2019-07-12 00:50:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this._ToolbarPanelHub = _ToolbarPanelHub;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ToolbarPanelHub - singleton instance of _ToolbarPanelHub that can initiate
|
|
|
|
* message requests and render messages.
|
|
|
|
*/
|
|
|
|
this.ToolbarPanelHub = new _ToolbarPanelHub();
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ["ToolbarPanelHub", "_ToolbarPanelHub"];
|