Skip to content

Commit

Permalink
Use await in chrome.storage.local.get.
Browse files Browse the repository at this point in the history
Call open cookie database only once.
  • Loading branch information
amovar18 committed May 9, 2024
1 parent 1471df0 commit f6ca935
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 58 deletions.
23 changes: 1 addition & 22 deletions packages/extension/src/contentScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import { TOOLTIP_CLASS } from './constants';
import {
DEVTOOLS_SET_JAVASCSCRIPT_COOKIE,
GET_JS_COOKIES,
SERVICE_WORKER_PORT_NAME,
TABID_STORAGE,
WEBPAGE_PORT_NAME,
} from '../constants';
Expand All @@ -63,11 +62,6 @@ class WebpageContentScript {
*/
port: chrome.runtime.Port | null = null;

/**
* Serviceeworker Connection port
*/
serviceWorkerPort: chrome.runtime.Port | null = null;

/**
* TabId of the current Tab
*/
Expand Down Expand Up @@ -158,13 +152,6 @@ class WebpageContentScript {

if (message?.payload?.type === TABID_STORAGE) {
this.tabId = message.payload.tabId;
chrome.runtime.sendMessage({
setInPage: true,
type: 'PING',
payload: {
tabId: this.tabId,
},
});
}

if (message?.payload?.type === GET_JS_COOKIES) {
Expand All @@ -182,13 +169,6 @@ class WebpageContentScript {
port.onMessage.addListener(this.onMessage);
port.onDisconnect.addListener(this.onDisconnect);
}
if (port.name.startsWith(SERVICE_WORKER_PORT_NAME)) {
this.serviceWorkerPort = port;
this.serviceWorkerPort.onMessage.addListener(noop);
this.serviceWorkerPort.onDisconnect.addListener(() => {
this.serviceWorkerPort = null;
});
}
});
}

Expand All @@ -204,6 +184,7 @@ class WebpageContentScript {
tabUrl: window.location.href,
tabId,
documentCookies: jsCookies,
cookieDB: this.cookieDB ?? {},
});
} catch (error) {
//Fail silently. No logging because sometimes cookieStore.getAll fails to run in some context.
Expand Down Expand Up @@ -319,8 +300,6 @@ class WebpageContentScript {
setInPage: false,
});
}
this.serviceWorkerPort?.disconnect();
this.serviceWorkerPort = null;
}

/**
Expand Down
41 changes: 8 additions & 33 deletions packages/extension/src/serviceWorker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
INITIAL_SYNC,
POPUP_CLOSE,
POPUP_OPEN,
SERVICE_WORKER_PORT_NAME,
SERVICE_WORKER_RELOAD_MESSAGE,
SERVICE_WORKER_TABS_RELOAD_COMMAND,
SET_TAB_TO_READ,
Expand Down Expand Up @@ -165,6 +164,10 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
* @see https://developer.chrome.com/docs/extensions/reference/api/runtime#event-onStartup
*/
chrome.runtime.onStartup.addListener(async () => {
setInterval(async () => {
await chrome.storage.session.get();
}, 20000);

const storage = await chrome.storage.sync.get();

if (!syncCookieStore) {
Expand Down Expand Up @@ -293,6 +296,10 @@ chrome.runtime.onInstalled.addListener(async (details) => {
syncCookieStore = new SynchnorousCookieStore();
syncCookieStore?.clear();

setInterval(async () => {
await chrome.storage.session.get();
}, 20000);

if (details.reason === 'install') {
await chrome.storage.sync.clear();
await chrome.storage.sync.set({
Expand Down Expand Up @@ -606,38 +613,6 @@ chrome.runtime.onMessage.addListener(async (request) => {

const incomingMessageTabId = request.payload.tabId;

if ('PING' === request?.type) {
if (syncCookieStore && syncCookieStore?.tabs[incomingMessageTabId]) {
if (!syncCookieStore?.tabs[incomingMessageTabId]?.portRef) {
syncCookieStore.tabs[incomingMessageTabId].portRef =
chrome.tabs.connect(Number(incomingMessageTabId), {
name: `${SERVICE_WORKER_PORT_NAME}-${incomingMessageTabId}`,
});
}

if (syncCookieStore?.tabs[incomingMessageTabId]?.portRef) {
setInterval(() => {
syncCookieStore?.tabs[incomingMessageTabId]?.portRef?.postMessage({
status: 'ping',
});
}, 10000);
}

syncCookieStore?.tabs[
incomingMessageTabId
]?.portRef.onDisconnect.addListener(() => {
if (
syncCookieStore &&
syncCookieStore?.tabs[incomingMessageTabId]?.portRef
) {
syncCookieStore.tabs[incomingMessageTabId].portRef = null;
}

clearInterval(10000);
});
}
}

if (DEVTOOLS_OPEN === incomingMessageType) {
const dataToSend: { [key: string]: string | boolean } = {};
dataToSend['tabMode'] = tabMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,31 @@ import {
isFirstParty,
findAnalyticsMatch,
type CookieData,
type CookieDatabase,
} from '@ps-analysis-tool/common';
import { type Cookie as ParsedCookie } from 'simple-cookie';

/**
* Internal dependencies.
*/
import { createCookieObject } from '../serviceWorker/createCookieObject';
import { fetchDictionary } from './fetchCookieDictionary';

import { GET_JS_COOKIES } from '../constants';

interface ProcessAndStoreDucmentCookies {
tabUrl: string;
tabId: string;
documentCookies: ParsedCookie[];
cookieDB: CookieDatabase;
}

const processAndStoreDocumentCookies = async ({
tabUrl,
tabId,
documentCookies,
cookieDB,
}: ProcessAndStoreDucmentCookies) => {
try {
const cookieDB = await fetchDictionary();

const parsedCookieData: CookieData[] = documentCookies.map(
(singleCookie: ParsedCookie) => {
let parsedCookie = {
Expand Down

0 comments on commit f6ca935

Please sign in to comment.