summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/geckoview/GeckoViewSessionStore.sys.mjs
blob: 5c176a17f1cb8f1d6c088c4dc86fa7f3372a425a (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
/* 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 { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  SessionHistory: "resource://gre/modules/sessionstore/SessionHistory.sys.mjs",
});

const { debug, warn } = GeckoViewUtils.initLogging("SessionStore");
const kNoIndex = Number.MAX_SAFE_INTEGER;
const kLastIndex = Number.MAX_SAFE_INTEGER - 1;

class SHistoryListener {
  constructor(browsingContext) {
    this.QueryInterface = ChromeUtils.generateQI([
      "nsISHistoryListener",
      "nsISupportsWeakReference",
    ]);

    this._browserId = browsingContext.browserId;
    this._fromIndex = kNoIndex;
  }

  unregister(permanentKey) {
    const bc = BrowsingContext.getCurrentTopByBrowserId(this._browserId);
    bc?.sessionHistory?.removeSHistoryListener(this);
    GeckoViewSessionStore._browserSHistoryListener?.delete(permanentKey);
  }

  collect(
    permanentKey, // eslint-disable-line no-shadow
    browsingContext, // eslint-disable-line no-shadow
    { collectFull = true, writeToCache = false }
  ) {
    // Don't bother doing anything if we haven't seen any navigations.
    if (!collectFull && this._fromIndex === kNoIndex) {
      return null;
    }

    const fromIndex = collectFull ? -1 : this._fromIndex;
    this._fromIndex = kNoIndex;

    const historychange = lazy.SessionHistory.collectFromParent(
      browsingContext.currentURI?.spec,
      true, // Bug 1704574
      browsingContext.sessionHistory,
      fromIndex
    );

    if (writeToCache) {
      const win =
        browsingContext.embedderElement?.ownerGlobal ||
        browsingContext.currentWindowGlobal?.browsingContext?.window;

      GeckoViewSessionStore.onTabStateUpdate(permanentKey, win, {
        data: { historychange },
      });
    }

    return historychange;
  }

  collectFrom(index) {
    if (this._fromIndex <= index) {
      // If we already know that we need to update history from index N we
      // can ignore any changes that happened with an element with index
      // larger than N.
      //
      // Note: initially we use kNoIndex which is MAX_SAFE_INTEGER which
      // means we don't ignore anything here, and in case of navigation in
      // the history back and forth cases we use kLastIndex which ignores
      // only the subsequent navigations, but not any new elements added.
      return;
    }

    const bc = BrowsingContext.getCurrentTopByBrowserId(this._browserId);
    if (bc?.embedderElement?.frameLoader) {
      this._fromIndex = index;

      // Queue a tab state update on the |browser.sessionstore.interval|
      // timer. We'll call this.collect() when we receive the update.
      bc.embedderElement.frameLoader.requestSHistoryUpdate();
    }
  }

  OnHistoryNewEntry(newURI, oldIndex) {
    // We use oldIndex - 1 to collect the current entry as well. This makes
    // sure to collect any changes that were made to the entry while the
    // document was active.
    this.collectFrom(oldIndex == -1 ? oldIndex : oldIndex - 1);
  }
  OnHistoryGotoIndex() {
    this.collectFrom(kLastIndex);
  }
  OnHistoryPurge() {
    this.collectFrom(-1);
  }
  OnHistoryReload() {
    this.collectFrom(-1);
    return true;
  }
  OnHistoryReplaceEntry() {
    this.collectFrom(-1);
  }
}

export var GeckoViewSessionStore = {
  // For each <browser> element, records the SHistoryListener.
  _browserSHistoryListener: new WeakMap(),

  observe(aSubject, aTopic) {
    debug`observe ${aTopic}`;

    switch (aTopic) {
      case "browsing-context-did-set-embedder": {
        if (aSubject === aSubject.top && aSubject.isContent) {
          const permanentKey = aSubject.embedderElement?.permanentKey;
          if (permanentKey) {
            this.maybeRecreateSHistoryListener(permanentKey, aSubject);
          }
        }
        break;
      }
      case "browsing-context-discarded":
        const permanentKey = aSubject?.embedderElement?.permanentKey;
        if (permanentKey) {
          this._browserSHistoryListener
            .get(permanentKey)
            ?.unregister(permanentKey);
        }
        break;
    }
  },

  onTabStateUpdate(permanentKey, win, data) {
    win.WindowEventDispatcher.sendRequest({
      type: "GeckoView:StateUpdated",
      data: data.data,
    });
  },

  getOrCreateSHistoryListener(permanentKey, browsingContext) {
    if (!permanentKey || browsingContext !== browsingContext.top) {
      return null;
    }

    const listener = this._browserSHistoryListener.get(permanentKey);
    if (listener) {
      return listener;
    }

    return this.createSHistoryListener(permanentKey, browsingContext, false);
  },

  maybeRecreateSHistoryListener(permanentKey, browsingContext) {
    const listener = this._browserSHistoryListener.get(permanentKey);
    if (!listener || listener._browserId != browsingContext.browserId) {
      listener?.unregister(permanentKey);
      this.createSHistoryListener(permanentKey, browsingContext, true);
    }
  },

  createSHistoryListener(permanentKey, browsingContext, collectImmediately) {
    const sessionHistory = browsingContext.sessionHistory;
    if (!sessionHistory) {
      return null;
    }

    const listener = new SHistoryListener(browsingContext);
    sessionHistory.addSHistoryListener(listener);
    this._browserSHistoryListener.set(permanentKey, listener);

    if (
      collectImmediately &&
      (!(browsingContext.currentURI?.spec === "about:blank") ||
        sessionHistory.count !== 0)
    ) {
      listener.collect(permanentKey, browsingContext, { writeToCache: true });
    }

    return listener;
  },

  updateSessionStoreFromTabListener(
    browser,
    browsingContext,
    permanentKey,
    update,
    forStorage = false
  ) {
    permanentKey = browser?.permanentKey ?? permanentKey;
    if (!permanentKey) {
      return;
    }

    if (browsingContext.isReplaced) {
      return;
    }

    const listener = this.getOrCreateSHistoryListener(
      permanentKey,
      browsingContext
    );

    if (listener) {
      const historychange =
        // If it is not the scheduled update (tab closed, window closed etc),
        // try to store the loading non-web-controlled page opened in _blank
        // first.
        (forStorage &&
          lazy.SessionHistory.collectNonWebControlledBlankLoadingSession(
            browsingContext
          )) ||
        listener.collect(permanentKey, browsingContext, {
          collectFull: !!update.sHistoryNeeded,
          writeToCache: false,
        });

      if (historychange) {
        update.data.historychange = historychange;
      }
    }

    const win =
      browsingContext.embedderElement?.ownerGlobal ||
      browsingContext.currentWindowGlobal?.browsingContext?.window;

    this.onTabStateUpdate(permanentKey, win, update);
  },
};