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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/* 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/. */
import {
html,
ifDefined,
when,
} from "chrome://global/content/vendor/lit.all.mjs";
import {
escapeHtmlEntities,
isSearchEnabled,
navigateToLink,
} from "./helpers.mjs";
import { ViewPage } from "./viewpage.mjs";
// eslint-disable-next-line import/no-unassigned-import
import "chrome://browser/content/migration/migration-wizard.mjs";
// eslint-disable-next-line import/no-unassigned-import
import "chrome://global/content/elements/moz-button.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
HistoryController: "resource:///modules/HistoryController.sys.mjs",
ProfileAge: "resource://gre/modules/ProfileAge.sys.mjs",
});
let XPCOMUtils = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
).XPCOMUtils;
const NEVER_REMEMBER_HISTORY_PREF = "browser.privatebrowsing.autostart";
const HAS_IMPORTED_HISTORY_PREF = "browser.migrate.interactions.history";
const IMPORT_HISTORY_DISMISSED_PREF =
"browser.tabs.firefox-view.importHistory.dismissed";
const SEARCH_RESULTS_LIMIT = 300;
class HistoryInView extends ViewPage {
constructor() {
super();
this._started = false;
// Setting maxTabsLength to -1 for no max
this.maxTabsLength = -1;
this.profileAge = 8;
this.fullyUpdated = false;
this.cumulativeSearches = 0;
}
controller = new lazy.HistoryController(this, {
searchResultsLimit: SEARCH_RESULTS_LIMIT,
});
start() {
if (this._started) {
return;
}
this._started = true;
this.controller.updateCache();
this.toggleVisibilityInCardContainer();
}
async connectedCallback() {
super.connectedCallback();
XPCOMUtils.defineLazyPreferenceGetter(
this,
"importHistoryDismissedPref",
IMPORT_HISTORY_DISMISSED_PREF,
false,
() => {
this.requestUpdate();
}
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
"hasImportedHistoryPref",
HAS_IMPORTED_HISTORY_PREF,
false,
() => {
this.requestUpdate();
}
);
if (!this.importHistoryDismissedPref && !this.hasImportedHistoryPrefs) {
let profileAccessor = await lazy.ProfileAge();
let profileCreateTime = await profileAccessor.created;
let timeNow = new Date().getTime();
let profileAge = timeNow - profileCreateTime;
// Convert milliseconds to days
this.profileAge = profileAge / 1000 / 60 / 60 / 24;
}
}
stop() {
if (!this._started) {
return;
}
this._started = false;
this.toggleVisibilityInCardContainer();
}
disconnectedCallback() {
super.disconnectedCallback();
this.stop();
this.migrationWizardDialog?.removeEventListener(
"MigrationWizard:Close",
this.migrationWizardDialog
);
}
viewVisibleCallback() {
this.start();
}
viewHiddenCallback() {
this.stop();
}
static queries = {
cards: { all: "card-container:not([hidden])" },
migrationWizardDialog: "#migrationWizardDialog",
emptyState: "fxview-empty-state",
lists: { all: "fxview-tab-list" },
showAllHistoryBtn: ".show-all-history-button",
searchTextbox: "fxview-search-textbox",
sortInputs: { all: "input[name=history-sort-option]" },
panelList: "panel-list",
};
static properties = {
// Making profileAge a reactive property for testing
profileAge: { type: Number },
};
async getUpdateComplete() {
await super.getUpdateComplete();
await Promise.all(Array.from(this.cards).map(card => card.updateComplete));
}
onPrimaryAction(e) {
navigateToLink(e);
// Record telemetry
Services.telemetry.recordEvent(
"firefoxview_next",
"history",
"visits",
null,
{}
);
if (this.controller.searchQuery) {
const searchesHistogram = Services.telemetry.getKeyedHistogramById(
"FIREFOX_VIEW_CUMULATIVE_SEARCHES"
);
searchesHistogram.add("history", this.cumulativeSearches);
this.cumulativeSearches = 0;
}
}
onSecondaryAction(e) {
this.triggerNode = e.originalTarget;
e.target.querySelector("panel-list").toggle(e.detail.originalEvent);
}
deleteFromHistory(e) {
this.controller.deleteFromHistory();
this.recordContextMenuTelemetry("delete-from-history", e);
}
onChangeSortOption(e) {
this.controller.onChangeSortOption(e);
Services.telemetry.recordEvent(
"firefoxview_next",
"sort_history",
"tabs",
null,
{
sort_type: this.controller.sortOption,
search_start: this.controller.searchQuery ? "true" : "false",
}
);
}
onSearchQuery(e) {
this.controller.onSearchQuery(e);
this.cumulativeSearches = this.controller.searchQuery
? this.cumulativeSearches + 1
: 0;
}
showAllHistory() {
// Record telemetry
Services.telemetry.recordEvent(
"firefoxview_next",
"show_all_history",
"tabs",
null,
{}
);
// Open History view in Library window
this.getWindow().PlacesCommandHook.showPlacesOrganizer("History");
}
async openMigrationWizard() {
let migrationWizardDialog = this.migrationWizardDialog;
if (migrationWizardDialog.open) {
return;
}
await customElements.whenDefined("migration-wizard");
// If we've been opened before, remove the old wizard and insert a
// new one to put it back into its starting state.
if (!migrationWizardDialog.firstElementChild) {
let wizard = document.createElement("migration-wizard");
wizard.toggleAttribute("dialog-mode", true);
migrationWizardDialog.appendChild(wizard);
}
migrationWizardDialog.firstElementChild.requestState();
this.migrationWizardDialog.addEventListener(
"MigrationWizard:Close",
function (e) {
e.currentTarget.close();
}
);
migrationWizardDialog.showModal();
}
shouldShowImportBanner() {
return (
this.profileAge < 8 &&
!this.hasImportedHistoryPref &&
!this.importHistoryDismissedPref
);
}
dismissImportHistory() {
Services.prefs.setBoolPref(IMPORT_HISTORY_DISMISSED_PREF, true);
}
updated() {
this.fullyUpdated = true;
if (this.lists?.length) {
this.toggleVisibilityInCardContainer();
}
}
panelListTemplate() {
return html`
<panel-list slot="menu" data-tab-type="history">
<panel-item
@click=${this.deleteFromHistory}
data-l10n-id="firefoxview-history-context-delete"
data-l10n-attrs="accesskey"
></panel-item>
<hr />
<panel-item
@click=${this.openInNewWindow}
data-l10n-id="fxviewtabrow-open-in-window"
data-l10n-attrs="accesskey"
></panel-item>
<panel-item
@click=${this.openInNewPrivateWindow}
data-l10n-id="fxviewtabrow-open-in-private-window"
data-l10n-attrs="accesskey"
></panel-item>
<hr />
<panel-item
@click=${this.copyLink}
data-l10n-id="fxviewtabrow-copy-link"
data-l10n-attrs="accesskey"
></panel-item>
</panel-list>
`;
}
/**
* The template to use for cards-container.
*/
get cardsTemplate() {
if (this.controller.searchResults) {
return this.#searchResultsTemplate();
} else if (!this.controller.isHistoryEmpty) {
return this.#historyCardsTemplate();
}
return this.#emptyMessageTemplate();
}
#historyCardsTemplate() {
let cardsTemplate = [];
switch (this.controller.sortOption) {
case "date":
cardsTemplate = this.controller.historyVisits.map(historyItem => {
let dateArg = JSON.stringify({ date: historyItem.items[0].time });
return html`<card-container>
<h3
slot="header"
data-l10n-id=${historyItem.l10nId}
data-l10n-args=${dateArg}
></h3>
<fxview-tab-list
slot="main"
secondaryActionClass="options-button"
dateTimeFormat=${historyItem.l10nId.includes("prev-month")
? "dateTime"
: "time"}
hasPopup="menu"
maxTabsLength=${this.maxTabsLength}
.tabItems=${historyItem.items}
@fxview-tab-list-primary-action=${this.onPrimaryAction}
@fxview-tab-list-secondary-action=${this.onSecondaryAction}
>
${this.panelListTemplate()}
</fxview-tab-list>
</card-container>`;
});
break;
case "site":
cardsTemplate = this.controller.historyVisits.map(historyItem => {
return html`<card-container>
<h3 slot="header" data-l10n-id="${ifDefined(historyItem.l10nId)}">
${historyItem.domain}
</h3>
<fxview-tab-list
slot="main"
secondaryActionClass="options-button"
dateTimeFormat="dateTime"
hasPopup="menu"
maxTabsLength=${this.maxTabsLength}
.tabItems=${historyItem.items}
@fxview-tab-list-primary-action=${this.onPrimaryAction}
@fxview-tab-list-secondary-action=${this.onSecondaryAction}
>
${this.panelListTemplate()}
</fxview-tab-list>
</card-container>`;
});
break;
}
return cardsTemplate;
}
#emptyMessageTemplate() {
let descriptionHeader;
let descriptionLabels;
let descriptionLink;
if (Services.prefs.getBoolPref(NEVER_REMEMBER_HISTORY_PREF, false)) {
// History pref set to never remember history
descriptionHeader = "firefoxview-dont-remember-history-empty-header";
descriptionLabels = [
"firefoxview-dont-remember-history-empty-description",
"firefoxview-dont-remember-history-empty-description-two",
];
descriptionLink = {
url: "about:preferences#privacy",
name: "history-settings-url-two",
};
} else {
descriptionHeader = "firefoxview-history-empty-header";
descriptionLabels = [
"firefoxview-history-empty-description",
"firefoxview-history-empty-description-two",
];
descriptionLink = {
url: "about:preferences#privacy",
name: "history-settings-url",
};
}
return html`
<fxview-empty-state
headerLabel=${descriptionHeader}
.descriptionLabels=${descriptionLabels}
.descriptionLink=${descriptionLink}
class="empty-state history"
?isSelectedTab=${this.selectedTab}
mainImageUrl="chrome://browser/content/firefoxview/history-empty.svg"
>
</fxview-empty-state>
`;
}
#searchResultsTemplate() {
return html` <card-container toggleDisabled>
<h3
slot="header"
data-l10n-id="firefoxview-search-results-header"
data-l10n-args=${JSON.stringify({
query: escapeHtmlEntities(this.controller.searchQuery),
})}
></h3>
${when(
this.controller.searchResults.length,
() =>
html`<h3
slot="secondary-header"
data-l10n-id="firefoxview-search-results-count"
data-l10n-args="${JSON.stringify({
count: this.controller.searchResults.length,
})}"
></h3>`
)}
<fxview-tab-list
slot="main"
secondaryActionClass="options-button"
dateTimeFormat="dateTime"
hasPopup="menu"
maxTabsLength="-1"
.searchQuery=${this.controller.searchQuery}
.tabItems=${this.controller.searchResults}
@fxview-tab-list-primary-action=${this.onPrimaryAction}
@fxview-tab-list-secondary-action=${this.onSecondaryAction}
>
${this.panelListTemplate()}
</fxview-tab-list>
</card-container>`;
}
render() {
if (!this.selectedTab) {
return null;
}
return html`
<link
rel="stylesheet"
href="chrome://browser/content/firefoxview/firefoxview.css"
/>
<link
rel="stylesheet"
href="chrome://browser/content/firefoxview/history.css"
/>
<dialog id="migrationWizardDialog"></dialog>
<div class="sticky-container bottom-fade">
<h2 class="page-header" data-l10n-id="firefoxview-history-header"></h2>
<div class="history-sort-options">
${when(
isSearchEnabled(),
() => html` <div class="history-sort-option">
<fxview-search-textbox
data-l10n-id="firefoxview-search-text-box-history"
data-l10n-attrs="placeholder"
.size=${this.searchTextboxSize}
pageName=${this.recentBrowsing ? "recentbrowsing" : "history"}
@fxview-search-textbox-query=${this.onSearchQuery}
></fxview-search-textbox>
</div>`
)}
<div class="history-sort-option">
<input
type="radio"
id="sort-by-date"
name="history-sort-option"
value="date"
?checked=${this.controller.sortOption === "date"}
@click=${this.onChangeSortOption}
/>
<label
for="sort-by-date"
data-l10n-id="firefoxview-sort-history-by-date-label"
></label>
</div>
<div class="history-sort-option">
<input
type="radio"
id="sort-by-site"
name="history-sort-option"
value="site"
?checked=${this.controller.sortOption === "site"}
@click=${this.onChangeSortOption}
/>
<label
for="sort-by-site"
data-l10n-id="firefoxview-sort-history-by-site-label"
></label>
</div>
</div>
</div>
<div class="cards-container">
<card-container
class="import-history-banner"
hideHeader="true"
?hidden=${!this.shouldShowImportBanner()}
role="group"
aria-labelledby="header"
aria-describedby="description"
>
<div slot="main">
<div class="banner-text">
<span
data-l10n-id="firefoxview-import-history-header"
id="header"
></span>
<span
data-l10n-id="firefoxview-import-history-description"
id="description"
></span>
</div>
<div class="buttons">
<button
class="primary choose-browser"
data-l10n-id="firefoxview-choose-browser-button"
@click=${this.openMigrationWizard}
></button>
<moz-button
class="close"
type="icon ghost"
data-l10n-id="firefoxview-import-history-close-button"
@click=${this.dismissImportHistory}
></moz-button>
</div>
</div>
</card-container>
${this.cardsTemplate}
</div>
<div
class="show-all-history-footer"
?hidden=${this.controller.isHistoryEmpty}
>
<button
class="show-all-history-button"
data-l10n-id="firefoxview-show-all-history"
@click=${this.showAllHistory}
?hidden=${this.controller.searchResults}
></button>
</div>
`;
}
willUpdate() {
this.fullyUpdated = false;
}
}
customElements.define("view-history", HistoryInView);
|