summaryrefslogtreecommitdiff
path: root/data/extensions/https-everywhere@eff.org/background-scripts/incognito.js
diff options
context:
space:
mode:
authorRuben Rodriguez <ruben@gnu.org>2018-09-13 20:39:48 -0400
committerRuben Rodriguez <ruben@gnu.org>2018-09-13 21:02:13 -0400
commitd26b319fd6f98517cc3421f10bf18698b953e4d2 (patch)
treebc70c4e472a2eaf514d411dba5067d530e5bbea9 /data/extensions/https-everywhere@eff.org/background-scripts/incognito.js
parentc3b304c51a3386ea09527a479a883253ea35243a (diff)
Updated extensions list for v60
Diffstat (limited to 'data/extensions/https-everywhere@eff.org/background-scripts/incognito.js')
-rw-r--r--data/extensions/https-everywhere@eff.org/background-scripts/incognito.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/data/extensions/https-everywhere@eff.org/background-scripts/incognito.js b/data/extensions/https-everywhere@eff.org/background-scripts/incognito.js
new file mode 100644
index 0000000..ca52177
--- /dev/null
+++ b/data/extensions/https-everywhere@eff.org/background-scripts/incognito.js
@@ -0,0 +1,73 @@
+"use strict";
+
+(function(exports) {
+
+// This file keeps track of incognito sessions, and clears any caches after
+// an entire incognito session is closed (i.e. all incognito windows are closed).
+
+let state = {
+ incognito_session_exists: false,
+};
+
+function Incognito(onIncognitoDestruction) {
+ Object.assign(this, {onIncognitoDestruction});
+ // Listen to window creation, so we can detect if an incognito window is created
+ if (chrome.windows) {
+ chrome.windows.onCreated.addListener(this.detect_incognito_creation);
+ }
+
+ // Listen to window destruction, so we can clear caches if all incognito windows are destroyed
+ if (chrome.windows) {
+ chrome.windows.onRemoved.addListener(this.detect_incognito_destruction);
+ }
+}
+
+Incognito.prototype = {
+ /**
+ * Detect if an incognito session is created, so we can clear caches when it's destroyed.
+ *
+ * @param window: A standard Window object.
+ */
+ detect_incognito_creation: function(window_) {
+ if (window_.incognito === true) {
+ state.incognito_session_exists = true;
+ }
+ },
+
+ // If a window is destroyed, and an incognito session existed, see if it still does.
+ detect_incognito_destruction: async function() {
+ if (state.incognito_session_exists) {
+ if (!(await any_incognito_windows())) {
+ state.incognito_session_exists = false;
+ this.onIncognitoDestruction();
+ }
+ }
+ },
+}
+
+/**
+ * Check if any incognito window still exists
+ */
+function any_incognito_windows() {
+ return new Promise(resolve => {
+ chrome.windows.getAll(arrayOfWindows => {
+ for (let window_ of arrayOfWindows) {
+ if (window_.incognito === true) {
+ return resolve(true);
+ }
+ }
+ resolve(false);
+ });
+ });
+}
+
+function onIncognitoDestruction(callback) {
+ return new Incognito(callback);
+};
+
+Object.assign(exports, {
+ onIncognitoDestruction,
+ state,
+});
+
+})(typeof exports == 'undefined' ? require.scopes.incognito = {} : exports);