summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/recently-closed-tabs.mjs
blob: 295fc03d27503b09c2e2718742e98648bc0a4903 (plain)
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
/* 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/. */

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
});

import {
  formatURIForDisplay,
  convertTimestamp,
  getImageUrl,
  onToggleContainer,
  NOW_THRESHOLD_MS,
} from "./helpers.mjs";

import {
  html,
  ifDefined,
  styleMap,
} from "chrome://global/content/vendor/lit.all.mjs";
import { MozLitElement } from "chrome://global/content/lit-utils.mjs";

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

const SS_NOTIFY_CLOSED_OBJECTS_CHANGED = "sessionstore-closed-objects-changed";
const SS_NOTIFY_BROWSER_SHUTDOWN_FLUSH = "sessionstore-browser-shutdown-flush";
const UI_OPEN_STATE =
  "browser.tabs.firefox-view.ui-state.recently-closed-tabs.open";

function getWindow() {
  return window.browsingContext.embedderWindowGlobal.browsingContext.window;
}

class RecentlyClosedTabsList extends MozLitElement {
  constructor() {
    super();
    this.maxTabsLength = 25;
    this.recentlyClosedTabs = [];
    this.lastFocusedIndex = -1;

    // The recency timestamp update period is stored in a pref to allow tests to easily change it
    XPCOMUtils.defineLazyPreferenceGetter(
      lazy,
      "timeMsPref",
      "browser.tabs.firefox-view.updateTimeMs",
      NOW_THRESHOLD_MS,
      timeMsPref => {
        clearInterval(this.intervalID);
        this.intervalID = setInterval(() => this.requestUpdate(), timeMsPref);
        this.requestUpdate();
      }
    );
  }

  createRenderRoot() {
    return this;
  }

  static queries = {
    tabsList: "ol",
    timeElements: { all: "span.closed-tab-li-time" },
  };

  get fluentStrings() {
    if (!this._fluentStrings) {
      this._fluentStrings = new Localization(["browser/firefoxView.ftl"], true);
    }
    return this._fluentStrings;
  }

  connectedCallback() {
    super.connectedCallback();
    this.intervalID = setInterval(() => this.requestUpdate(), lazy.timeMsPref);
  }

  disconnectedCallback() {
    clearInterval(this.intervalID);
  }

  getTabStateValue(tab, key) {
    let value = "";
    const tabEntries = tab.state.entries;
    const activeIndex = tab.state.index - 1;

    if (activeIndex >= 0 && tabEntries[activeIndex]) {
      value = tabEntries[activeIndex][key];
    }

    return value;
  }

  openTabAndUpdate(event) {
    if (
      (event.type == "click" && !event.altKey) ||
      (event.type == "keydown" && event.code == "Enter") ||
      (event.type == "keydown" && event.code == "Space")
    ) {
      const item = event.target.closest(".closed-tab-li");
      // only used for telemetry
      const position = [...this.tabsList.children].indexOf(item) + 1;
      const closedId = item.dataset.tabid;

      lazy.SessionStore.undoCloseById(closedId);

      // record telemetry
      let tabClosedAt = parseInt(
        item.querySelector(".closed-tab-li-time").getAttribute("data-timestamp")
      );

      let now = Date.now();
      let deltaSeconds = (now - tabClosedAt) / 1000;
      Services.telemetry.recordEvent(
        "firefoxview",
        "recently_closed",
        "tabs",
        null,
        {
          position: position.toString(),
          delta: deltaSeconds.toString(),
        }
      );
    }
  }

  dismissTabAndUpdate(event) {
    event.preventDefault();
    const item = event.target.closest(".closed-tab-li");
    this.dismissTabAndUpdateForElement(item);
  }

  dismissTabAndUpdateForElement(item) {
    let recentlyClosedList = lazy.SessionStore.getClosedTabDataForWindow(
      getWindow()
    );
    let closedTabIndex = recentlyClosedList.findIndex(closedTab => {
      return closedTab.closedId === parseInt(item.dataset.tabid, 10);
    });
    if (closedTabIndex < 0) {
      // Tab not found in recently closed list
      return;
    }
    lazy.SessionStore.forgetClosedTab(getWindow(), closedTabIndex);

    // record telemetry
    let tabClosedAt = parseInt(
      item.querySelector(".closed-tab-li-time").dataset.timestamp
    );

    let now = Date.now();
    let deltaSeconds = (now - tabClosedAt) / 1000;
    Services.telemetry.recordEvent(
      "firefoxview",
      "dismiss_closed_tab",
      "tabs",
      null,
      {
        delta: deltaSeconds.toString(),
      }
    );
  }

  updateRecentlyClosedTabs() {
    let recentlyClosedTabsData = lazy.SessionStore.getClosedTabDataForWindow(
      getWindow()
    );
    this.recentlyClosedTabs = recentlyClosedTabsData.slice(
      0,
      this.maxTabsLength
    );
    this.requestUpdate();
  }

  render() {
    let { recentlyClosedTabs } = this;
    let closedTabsContainer = document.getElementById(
      "recently-closed-tabs-container"
    );

    if (!recentlyClosedTabs.length) {
      // Show empty message if no recently closed tabs
      closedTabsContainer.toggleContainerStyleForEmptyMsg(true);
      return html` ${this.emptyMessageTemplate()} `;
    }

    closedTabsContainer.toggleContainerStyleForEmptyMsg(false);

    return html`
      <ol class="closed-tabs-list">
        ${recentlyClosedTabs.map((tab, i) =>
          this.recentlyClosedTabTemplate(tab, !i)
        )}
      </ol>
    `;
  }

  willUpdate() {
    if (this.tabsList && this.tabsList.contains(document.activeElement)) {
      let activeLi = document.activeElement.closest(".closed-tab-li");
      this.lastFocusedIndex = [...this.tabsList.children].indexOf(activeLi);
    } else {
      this.lastFocusedIndex = -1;
    }
  }

  updated() {
    let focusRestored = false;
    if (
      this.lastFocusedIndex >= 0 &&
      (!this.tabsList || this.lastFocusedIndex >= this.tabsList.children.length)
    ) {
      if (this.tabsList) {
        let items = [...this.tabsList.children];
        let newFocusIndex = items.length - 1;
        let newFocus = items[newFocusIndex];
        if (newFocus) {
          focusRestored = true;
          newFocus.querySelector(".closed-tab-li-main").focus();
        }
      }
      if (!focusRestored) {
        document.getElementById("recently-closed-tabs-header-section").focus();
      }
    }
    this.lastFocusedIndex = -1;
  }

  emptyMessageTemplate() {
    return html`
      <div
        id="recently-closed-tabs-placeholder"
        class="placeholder-content"
        role="presentation"
      >
        <img
          id="recently-closed-empty-image"
          src="chrome://browser/content/firefoxview/recently-closed-empty.svg"
          role="presentation"
          alt=""
        />
        <div class="placeholder-text">
          <h4
            data-l10n-id="firefoxview-closed-tabs-placeholder-header"
            class="placeholder-header"
          ></h4>
          <p
            data-l10n-id="firefoxview-closed-tabs-placeholder-body"
            class="placeholder-body"
          ></p>
        </div>
      </div>
    `;
  }

  recentlyClosedTabTemplate(tab, primary) {
    const targetURI = this.getTabStateValue(tab, "url");
    const convertedTime = convertTimestamp(
      tab.closedAt,
      this.fluentStrings,
      lazy.timeMsPref
    );
    return html`
      <li
        class="closed-tab-li"
        data-tabid=${tab.closedId}
        data-targeturi=${targetURI}
        tabindex=${ifDefined(primary ? null : "-1")}
        @contextmenu=${e => (this.contextTriggerNode = e.currentTarget)}
      >
        <span
          class="closed-tab-li-main"
          role="button"
          tabindex="0"
          @click=${e => this.openTabAndUpdate(e)}
          @keydown=${e => this.openTabAndUpdate(e)}
        >
          <div
            class="favicon"
            style=${styleMap({
              backgroundImage: `url(${getImageUrl(tab.icon, targetURI)})`,
            })}
          ></div>
          <a
            href=${targetURI}
            class="closed-tab-li-title"
            tabindex="-1"
            @click=${e => e.preventDefault()}
          >
            ${tab.title}
          </a>
          <a
            href=${targetURI}
            class="closed-tab-li-url"
            data-l10n-id="firefoxview-tabs-list-tab-button"
            data-l10n-args=${JSON.stringify({ targetURI })}
            tabindex="-1"
            @click=${e => e.preventDefault()}
          >
            ${formatURIForDisplay(targetURI)}
          </a>
          <span class="closed-tab-li-time" data-timestamp=${tab.closedAt}>
            ${convertedTime}
          </span>
        </span>
        <button
          class="closed-tab-li-dismiss"
          data-l10n-id="firefoxview-closed-tabs-dismiss-tab"
          data-l10n-args=${JSON.stringify({ tabTitle: tab.title })}
          @click=${e => this.dismissTabAndUpdate(e)}
        ></button>
      </li>
    `;
  }

  // Update the URL for a new or previously-populated list item.
  // This is needed because when tabs get closed we don't necessarily
  // have all the requisite information for them immediately.
  updateURLForListItem(li, targetURI) {
    li.dataset.targetURI = targetURI;
    let urlElement = li.querySelector(".closed-tab-li-url");
    document.l10n.setAttributes(
      urlElement,
      "firefoxview-tabs-list-tab-button",
      {
        targetURI,
      }
    );
    if (targetURI) {
      urlElement.textContent = formatURIForDisplay(targetURI);
      urlElement.title = targetURI;
    } else {
      urlElement.textContent = urlElement.title = "";
    }
  }
}
customElements.define("recently-closed-tabs-list", RecentlyClosedTabsList);

class RecentlyClosedTabsContainer extends HTMLDetailsElement {
  constructor() {
    super();
    this.observerAdded = false;
    this.boundObserve = (...args) => this.observe(...args);
  }

  connectedCallback() {
    this.noTabsElement = this.querySelector(
      "#recently-closed-tabs-placeholder"
    );
    this.list = this.querySelector("recently-closed-tabs-list");
    this.collapsibleContainer = this.querySelector(
      "#collapsible-tabs-container"
    );
    this.addEventListener("toggle", this);
    getWindow().gBrowser.tabContainer.addEventListener("TabSelect", this);
    getWindow().addEventListener("command", this, true);
    getWindow()
      .document.getElementById("contentAreaContextMenu")
      .addEventListener("popuphiding", this);
    this.open = Services.prefs.getBoolPref(UI_OPEN_STATE, true);
  }

  cleanup() {
    getWindow().gBrowser.tabContainer.removeEventListener("TabSelect", this);
    getWindow().removeEventListener("command", this, true);
    getWindow()
      .document.getElementById("contentAreaContextMenu")
      .removeEventListener("popuphiding", this);
    this.removeObserversIfNeeded();
  }

  addObserversIfNeeded() {
    if (!this.observerAdded) {
      Services.obs.addObserver(
        this.boundObserve,
        SS_NOTIFY_CLOSED_OBJECTS_CHANGED
      );
      Services.obs.addObserver(
        this.boundObserve,
        SS_NOTIFY_BROWSER_SHUTDOWN_FLUSH
      );
      this.observerAdded = true;
    }
  }

  removeObserversIfNeeded() {
    if (this.observerAdded) {
      Services.obs.removeObserver(
        this.boundObserve,
        SS_NOTIFY_CLOSED_OBJECTS_CHANGED
      );
      Services.obs.removeObserver(
        this.boundObserve,
        SS_NOTIFY_BROWSER_SHUTDOWN_FLUSH
      );
      this.observerAdded = false;
    }
  }

  // we observe when a tab closes but since this notification fires more frequently and on
  // all windows, we remove the observer when another tab is selected; we check for changes
  // to the session store once the user return to this tab.
  handleObservers(contentDocument) {
    if (contentDocument?.URL == "about:firefoxview") {
      this.addObserversIfNeeded();
      this.list.updateRecentlyClosedTabs();
    } else {
      this.removeObserversIfNeeded();
    }
  }

  observe(subject, topic, data) {
    if (
      topic == SS_NOTIFY_CLOSED_OBJECTS_CHANGED ||
      (topic == SS_NOTIFY_BROWSER_SHUTDOWN_FLUSH &&
        subject.ownerGlobal == getWindow())
    ) {
      this.list.updateRecentlyClosedTabs();
    }
  }

  onLoad() {
    this.list.updateRecentlyClosedTabs();
    this.addObserversIfNeeded();
  }

  handleEvent(event) {
    if (event.type == "toggle") {
      onToggleContainer(this);
    } else if (event.type == "TabSelect") {
      this.handleObservers(event.target.linkedBrowser.contentDocument);
    } else if (
      event.type === "command" &&
      event.target.closest(".context-menu-open-link") &&
      this.list.contextTriggerNode
    ) {
      this.list.dismissTabAndUpdateForElement(this.list.contextTriggerNode);
    } else if (event.type === "popuphiding") {
      delete this.list.contextTriggerNode;
    }
  }

  toggleContainerStyleForEmptyMsg(visible) {
    this.collapsibleContainer.classList.toggle("empty-container", visible);
  }

  getClosedTabCount = () => {
    try {
      return lazy.SessionStore.getClosedTabCountForWindow(getWindow());
    } catch (ex) {
      return 0;
    }
  };
}
customElements.define(
  "recently-closed-tabs-container",
  RecentlyClosedTabsContainer,
  {
    extends: "details",
  }
);