|
| 1 | +"use strict"; |
| 2 | +// This file keeps track of incognito sessions, and clears any caches after |
| 3 | +// an entire incognito session is closed (i.e. all incognito windows are closed). |
| 4 | + |
| 5 | +let incognito_session_exists = false; |
| 6 | + |
| 7 | +/** |
| 8 | + * Detect if an incognito session is created, so we can clear caches when it's destroyed. |
| 9 | + * |
| 10 | + * @param window: A standard Window object. |
| 11 | + */ |
| 12 | +function detect_incognito_creation(window) { |
| 13 | + if (window.incognito === true) { |
| 14 | + incognito_session_exists = true; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Clear any caches we have. |
| 20 | + * Called if an incognito session is destroyed. |
| 21 | + */ |
| 22 | +function destroy_caches() { |
| 23 | + log(DBUG, "Destroying caches."); |
| 24 | + all_rules.cookieHostCache.clear(); |
| 25 | + all_rules.ruleCache.clear(); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Check if any incognito window still exists. If not, destroy caches. |
| 30 | + * @param arrayOfWindows: A array of all open Window objects. |
| 31 | + */ |
| 32 | +function check_for_incognito_session(arrayOfWindows) { |
| 33 | + for (let window of arrayOfWindows) { |
| 34 | + if (window.incognito === true) { |
| 35 | + // An incognito window still exists, so don't destroy caches yet. |
| 36 | + return; |
| 37 | + } |
| 38 | + } |
| 39 | + // All incognito windows have been closed. |
| 40 | + incognito_session_exists = false; |
| 41 | + destroy_caches(); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * If a window is destroyed, and an incognito session existed, see if it still does. |
| 46 | + * |
| 47 | + * @param windowId: Ignored. |
| 48 | + */ |
| 49 | +function detect_incognito_destruction(windowId) { |
| 50 | + if (incognito_session_exists) { |
| 51 | + // Are any current windows incognito? |
| 52 | + chrome.windows.getAll(check_for_incognito_session); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +// Listen to window creation, so we can detect if an incognito window is created |
| 58 | +chrome.windows.onCreated.addListener(detect_incognito_creation); |
| 59 | + |
| 60 | +// Listen to window destruction, so we can clear caches if all incognito windows are destroyed |
| 61 | +chrome.windows.onRemoved.addListener(detect_incognito_destruction); |
0 commit comments