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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/* 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";
ChromeUtils.defineModuleGetter(
this,
"FxAccounts",
"resource://gre/modules/FxAccounts.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"Services",
"resource://gre/modules/Services.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm"
);
class _BookmarkPanelHub {
constructor() {
this._id = "BookmarkPanelHub";
this._trigger = { id: "bookmark-panel" };
this._handleMessageRequest = null;
this._addImpression = null;
this._sendTelemetry = null;
this._initialized = false;
this._response = null;
this._l10n = null;
this.messageRequest = this.messageRequest.bind(this);
this.toggleRecommendation = this.toggleRecommendation.bind(this);
this.sendUserEventTelemetry = this.sendUserEventTelemetry.bind(this);
this.collapseMessage = this.collapseMessage.bind(this);
}
/**
* @param {function} handleMessageRequest
* @param {function} addImpression
* @param {function} sendTelemetry - Used for sending user telemetry information
*/
init(handleMessageRequest, addImpression, sendTelemetry) {
this._handleMessageRequest = handleMessageRequest;
this._addImpression = addImpression;
this._sendTelemetry = sendTelemetry;
this._l10n = new DOMLocalization([]);
this._initialized = true;
}
uninit() {
this._l10n = null;
this._initialized = false;
this._handleMessageRequest = null;
this._addImpression = null;
this._sendTelemetry = null;
this._response = null;
}
/**
* Checks if a similar cached requests exists before forwarding the request
* to ASRouter. Caches only 1 request, unique identifier is `request.url`.
* Caching ensures we don't duplicate requests and telemetry pings.
* Return value is important for the caller to know if a message will be
* shown.
*
* @returns {obj|null} response object or null if no messages matched
*/
async messageRequest(target, win) {
if (!this._initialized) {
return false;
}
if (
this._response &&
this._response.win === win &&
this._response.url === target.url &&
this._response.content
) {
this.showMessage(this._response.content, target, win);
return true;
}
// If we didn't match on a previously cached request then make sure
// the container is empty
this._removeContainer(target);
const response = await this._handleMessageRequest({
triggerId: this._trigger.id,
});
return this.onResponse(response, target, win);
}
/**
* If the response contains a message render it and send an impression.
* Otherwise we remove the message from the container.
*/
onResponse(response, target, win) {
this._response = {
...response,
collapsed: false,
target,
win,
url: target.url,
};
if (response && response.content) {
// Only insert localization files if we need to show a message
win.MozXULElement.insertFTLIfNeeded("browser/newtab/asrouter.ftl");
win.MozXULElement.insertFTLIfNeeded("browser/branding/sync-brand.ftl");
this.showMessage(response.content, target, win);
this.sendImpression();
this.sendUserEventTelemetry("IMPRESSION", win);
} else {
this.hideMessage(target);
}
target.infoButton.disabled = !response;
return !!response;
}
showMessage(message, target, win) {
if (this._response && this._response.collapsed) {
this.toggleRecommendation(false);
return;
}
const createElement = elem =>
target.document.createElementNS("http://www.w3.org/1999/xhtml", elem);
let recommendation = target.container.querySelector("#cfrMessageContainer");
if (!recommendation) {
recommendation = createElement("div");
const headerContainer = createElement("div");
headerContainer.classList.add("cfrMessageHeader");
recommendation.setAttribute("id", "cfrMessageContainer");
recommendation.addEventListener("click", async e => {
target.hidePopup();
const url = await FxAccounts.config.promiseConnectAccountURI(
"bookmark"
);
win.ownerGlobal.openLinkIn(url, "tabshifted", {
private: false,
triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal(
{}
),
csp: null,
});
this.sendUserEventTelemetry("CLICK", win);
});
recommendation.style.color = message.color;
recommendation.style.background = `linear-gradient(135deg, ${message.background_color_1} 0%, ${message.background_color_2} 70%)`;
const close = createElement("button");
close.setAttribute("id", "cfrClose");
close.setAttribute("aria-label", "close");
close.addEventListener("click", e => {
this.sendUserEventTelemetry("DISMISS", win);
this.collapseMessage();
target.close(e);
});
const title = createElement("h1");
title.setAttribute("id", "editBookmarkPanelRecommendationTitle");
const content = createElement("p");
content.setAttribute("id", "editBookmarkPanelRecommendationContent");
const cta = createElement("button");
cta.setAttribute("id", "editBookmarkPanelRecommendationCta");
// If `string_id` is present it means we are relying on fluent for translations
if (message.text.string_id) {
this._l10n.setAttributes(
close,
message.close_button.tooltiptext.string_id
);
this._l10n.setAttributes(title, message.title.string_id);
this._l10n.setAttributes(content, message.text.string_id);
this._l10n.setAttributes(cta, message.cta.string_id);
} else {
close.setAttribute("title", message.close_button.tooltiptext);
title.textContent = message.title;
content.textContent = message.text;
cta.textContent = message.cta;
}
headerContainer.appendChild(title);
headerContainer.appendChild(close);
recommendation.appendChild(headerContainer);
recommendation.appendChild(content);
recommendation.appendChild(cta);
target.container.appendChild(recommendation);
}
this.toggleRecommendation(true);
this._adjustPanelHeight(win, recommendation);
}
/**
* Adjust the size of the container for locales where the message is
* longer than the fixed 150px set for height
*/
async _adjustPanelHeight(window, messageContainer) {
const { document } = window;
// Contains the screenshot of the page we are bookmarking
const screenshotContainer = document.getElementById(
"editBookmarkPanelImage"
);
// Wait for strings to be added which can change element height
await document.l10n.translateElements([messageContainer]);
window.requestAnimationFrame(() => {
let { height } = messageContainer.getBoundingClientRect();
if (height > 150) {
messageContainer.classList.add("longMessagePadding");
// Get the new value with the added padding
height = messageContainer.getBoundingClientRect().height;
// Needs to be adjusted to match the message height
screenshotContainer.style.height = `${height}px`;
}
});
}
/**
* Restore the panel back to the original size so the slide in
* animation can run again
*/
_restorePanelHeight(window) {
const { document } = window;
// Contains the screenshot of the page we are bookmarking
document.getElementById("editBookmarkPanelImage").style.height = "";
}
toggleRecommendation(visible) {
if (!this._response) {
return;
}
const { target } = this._response;
if (visible === undefined) {
// When called from the info button of the bookmark panel
target.infoButton.checked = !target.infoButton.checked;
} else {
target.infoButton.checked = visible;
}
if (target.infoButton.checked) {
// If it was ever collapsed we need to cancel the state
this._response.collapsed = false;
target.container.removeAttribute("disabled");
} else {
target.container.setAttribute("disabled", "disabled");
}
}
collapseMessage() {
this._response.collapsed = true;
this.toggleRecommendation(false);
}
_removeContainer(target) {
if (target || (this._response && this._response.target)) {
const container = (
target || this._response.target
).container.querySelector("#cfrMessageContainer");
if (container) {
this._restorePanelHeight(this._response.win);
container.remove();
}
}
}
hideMessage(target) {
this._removeContainer(target);
this.toggleRecommendation(false);
this._response = null;
}
forceShowMessage(browser, message) {
const doc = browser.ownerGlobal.gBrowser.ownerDocument;
const win = browser.ownerGlobal.window;
const panelTarget = {
container: doc.getElementById("editBookmarkPanelRecommendation"),
infoButton: doc.getElementById("editBookmarkPanelInfoButton"),
document: doc,
close: e => {
e.stopPropagation();
this.toggleRecommendation(false);
},
};
// Remove any existing message
this.hideMessage(panelTarget);
// Reset the reference to the panel elements
this._response = { target: panelTarget, win };
// Required if we want to preview messages that include fluent strings
win.MozXULElement.insertFTLIfNeeded("browser/newtab/asrouter.ftl");
win.MozXULElement.insertFTLIfNeeded("browser/branding/sync-brand.ftl");
this.showMessage(message.content, panelTarget, win);
}
sendImpression() {
this._addImpression(this._response);
}
sendUserEventTelemetry(event, win) {
// Only send pings for non private browsing windows
if (
!PrivateBrowsingUtils.isBrowserPrivate(
win.ownerGlobal.gBrowser.selectedBrowser
)
) {
this._sendPing({
message_id: this._response.id,
bucket_id: this._response.id,
event,
});
}
}
_sendPing(ping) {
this._sendTelemetry({
type: "DOORHANGER_TELEMETRY",
data: { action: "cfr_user_event", source: "CFR", ...ping },
});
}
}
this._BookmarkPanelHub = _BookmarkPanelHub;
/**
* BookmarkPanelHub - singleton instance of _BookmarkPanelHub that can initiate
* message requests and render messages.
*/
this.BookmarkPanelHub = new _BookmarkPanelHub();
const EXPORTED_SYMBOLS = ["BookmarkPanelHub", "_BookmarkPanelHub"];
|