Skip to content

Commit

Permalink
Merge pull request #39 from leor-gh/master
Browse files Browse the repository at this point in the history
Add support for automatic persistent data migration when upgrading from MV2 to MV3
  • Loading branch information
henices committed Aug 26, 2024
2 parents 0a54878 + 9426645 commit 7a62c2e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
40 changes: 32 additions & 8 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ chrome.webRequest.onAuthRequired.addListener(
{urls: ["<all_urls>"]},
['asyncBlocking'] );

chrome.runtime.onMessage.addListener(async function(msg, sender, res) {
chrome.runtime.onMessage.addListener((msg, sender, res) => {
if (msg.action != "authUpdate")
return;

console.log("%s onMessage listener", new Date(Date.now()).toISOString());
if (localStorage.proxySetting == undefined)
await getLocalStorage();
(async () => {
console.log("%s receive authUpdate", new Date(Date.now()).toISOString());
if (localStorage.proxySetting == undefined)
await getLocalStorage();

var proxySetting = JSON.parse(localStorage.proxySetting);
proxySetting['auth'] = msg.data;
localStorage.proxySetting = JSON.stringify(proxySetting);
chrome.storage.local.set(localStorage);
var proxySetting = JSON.parse(localStorage.proxySetting);
proxySetting['auth'] = msg.data;
localStorage.proxySetting = JSON.stringify(proxySetting);
await chrome.storage.local.set(localStorage);

console.log("%s sending authUpdate response", new Date(Date.now()).toISOString());
res('done');
})();

return true;
});

var proxySetting = {
Expand Down Expand Up @@ -116,6 +123,23 @@ chrome.runtime.onInstalled.addListener(async details => {
if (store.proxySetting == undefined) {
localStorage.proxySetting = JSON.stringify(proxySetting);
await chrome.storage.local.set(localStorage);

if (details.reason == "update") {
chrome.runtime.onMessage.addListener((msg, sender, res) => {
if (msg.action != "migrationDone")
return;

console.log("%s data migration done", new Date(Date.now()).toISOString());
chrome.offscreen.closeDocument();
});

console.log("%s starting data migration", new Date(Date.now()).toISOString());
chrome.offscreen.createDocument({
url: 'migration.html',
reasons: ['LOCAL_STORAGE'],
justification: 'Migrate storage data for MV2 to MV3',
});
}
}
if(details.reason == "install") {
gotoPage('options.html');
Expand Down
15 changes: 7 additions & 8 deletions doc/migration
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ During normal operation:
- Service worker comes and goes, and credential information is
lazy-loaded only when needed

After migration of installed MV2 extension to MV3, on the first run:
During update migration of installed MV2 extension to MV3:
- localStorage is populated with valid data
- chrome.storage is empty, and the service worker populates the
template proxySetting into chrome.storage, but this does not have
valid proxy authorization credentials
- proxy authorization will fail, and the browser presents a popup
dialog to prompt user to enter proxy credentials
- the option page still displays the correct credentials
- this migration issue can be fixed by deleting and re-entering the
credential information (both the username and password) in the
options page to force the service worker receive and persist the
correct data for its use
- the onInstall handler sees a reason of "update", which means this
is the MV2 to MV3 migration, and it kicks off the migration
offscreen page
- the offscreen page JS code checks localStorage to see if it has
non-empty credentials, and sends a message to the service worker
to update the credentials in chrome.storage
16 changes: 16 additions & 0 deletions javascripts/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(async () => {
console.log("%s data migration. localStorage =",
new Date(Date.now()).toISOString(),
JSON.parse(JSON.stringify(localStorage)));

let resp;
if (localStorage.proxySetting != undefined) {
auth = JSON.parse(localStorage.proxySetting).auth;
if (auth.user != '' || auth.pass != '') {
resp = chrome.runtime.sendMessage({ action: 'authUpdate', data: auth });
}
}
await resp;

chrome.runtime.sendMessage({ action: 'migrationDone' });
})();
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"tabs",
"storage",
"webRequest",
"webRequestAuthProvider"
"webRequestAuthProvider",
"offscreen"
],
"host_permissions": [
"*://*/*"
Expand Down
5 changes: 5 additions & 0 deletions migration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<head>
<script src="javascripts/migration.js"></script>
</head>
</html>

0 comments on commit 7a62c2e

Please sign in to comment.