[CRX] Replace localStorage in telemetry logic
In MV3, the background script is a service worker. localStorage is not supported in service workers. Switch to storage.local instead. Data migration is not implemented because it is not needed due to the privacy-friendly design of the telemetry: In practice the data is reset about every 4 weeks, when the major version of Chrome is updated.
This commit is contained in:
parent
bc4890d4d4
commit
7017d8246b
@ -10,6 +10,7 @@
|
|||||||
"16": "icon16.png"
|
"16": "icon16.png"
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
|
"alarms",
|
||||||
"declarativeNetRequestWithHostAccess",
|
"declarativeNetRequestWithHostAccess",
|
||||||
"webRequest",
|
"webRequest",
|
||||||
"webRequestBlocking",
|
"webRequestBlocking",
|
||||||
|
|||||||
@ -42,8 +42,35 @@ limitations under the License.
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
maybeSendPing();
|
// The localStorage API is unavailable in service workers. We store data in
|
||||||
setInterval(maybeSendPing, 36e5);
|
// chrome.storage.local and use this "localStorage" object to enable
|
||||||
|
// synchronous access in the logic.
|
||||||
|
const localStorage = {
|
||||||
|
telemetryLastTime: 0,
|
||||||
|
telemetryDeduplicationId: "",
|
||||||
|
telemetryLastVersion: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
chrome.alarms.onAlarm.addListener(alarm => {
|
||||||
|
if (alarm.name === "maybeSendPing") {
|
||||||
|
maybeSendPing();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
chrome.storage.session.get({ didPingCheck: false }, async items => {
|
||||||
|
if (items?.didPingCheck) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
maybeSendPing();
|
||||||
|
await chrome.alarms.clear("maybeSendPing");
|
||||||
|
await chrome.alarms.create("maybeSendPing", { periodInMinutes: 60 });
|
||||||
|
chrome.storage.session.set({ didPingCheck: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
function updateLocalStorage(key, value) {
|
||||||
|
localStorage[key] = value;
|
||||||
|
// Note: We mirror the data in localStorage because the following is async.
|
||||||
|
chrome.storage.local.set({ [key]: value });
|
||||||
|
}
|
||||||
|
|
||||||
function maybeSendPing() {
|
function maybeSendPing() {
|
||||||
getLoggingPref(function (didOptOut) {
|
getLoggingPref(function (didOptOut) {
|
||||||
@ -61,12 +88,20 @@ limitations under the License.
|
|||||||
// send more pings.
|
// send more pings.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
doSendPing();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function doSendPing() {
|
||||||
|
chrome.storage.local.get(localStorage, items => {
|
||||||
|
Object.assign(localStorage, items);
|
||||||
|
|
||||||
var lastTime = parseInt(localStorage.telemetryLastTime) || 0;
|
var lastTime = parseInt(localStorage.telemetryLastTime) || 0;
|
||||||
var wasUpdated = didUpdateSinceLastCheck();
|
var wasUpdated = didUpdateSinceLastCheck();
|
||||||
if (!wasUpdated && Date.now() - lastTime < MINIMUM_TIME_BETWEEN_PING) {
|
if (!wasUpdated && Date.now() - lastTime < MINIMUM_TIME_BETWEEN_PING) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
localStorage.telemetryLastTime = Date.now();
|
updateLocalStorage("telemetryLastTime", Date.now());
|
||||||
|
|
||||||
var deduplication_id = getDeduplicationId(wasUpdated);
|
var deduplication_id = getDeduplicationId(wasUpdated);
|
||||||
var extension_version = chrome.runtime.getManifest().version;
|
var extension_version = chrome.runtime.getManifest().version;
|
||||||
@ -104,7 +139,7 @@ limitations under the License.
|
|||||||
for (const c of buf) {
|
for (const c of buf) {
|
||||||
id += (c >>> 4).toString(16) + (c & 0xf).toString(16);
|
id += (c >>> 4).toString(16) + (c & 0xf).toString(16);
|
||||||
}
|
}
|
||||||
localStorage.telemetryDeduplicationId = id;
|
updateLocalStorage("telemetryDeduplicationId", id);
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@ -119,7 +154,7 @@ limitations under the License.
|
|||||||
if (!chromeVersion || localStorage.telemetryLastVersion === chromeVersion) {
|
if (!chromeVersion || localStorage.telemetryLastVersion === chromeVersion) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
localStorage.telemetryLastVersion = chromeVersion;
|
updateLocalStorage("telemetryLastVersion", chromeVersion);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user