Skip to content

Commit 8bb84dc

Browse files
committed
#19 | Asynchronous status refresh after clicking the Reload button
1 parent 83d67be commit 8bb84dc

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

chromeipass/background/keepass.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@ keepass.getDatabaseHash = function (tab, triggerUnlock) {
577577
return keepass.databaseHash;
578578
};
579579

580+
keepass.refreshStatus = function (cb) {
581+
const work = keepass.isAssociated()
582+
? keepass._testAssociationAsync(null, false)
583+
: keepass._getDatabaseHashAsync(null, false);
584+
work.finally(() => { if (cb) cb(); });
585+
};
586+
580587
keepass.setVerifier = function (request, inputKey) {
581588
var key = inputKey || null;
582589
var id = null;

chromeipass/background/service_worker.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
7676
const tab = sender.tab;
7777

7878
// helpers
79-
const async = () => true;
8079
const respondNoTab = (payload) => {
8180
try { sendResponse(payload); } catch (_) { }
8281
};
@@ -175,20 +174,28 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
175174
}, { ok: false });
176175
}
177176
case 'get_status': {
178-
const tId = tab && tab.id;
179-
const error = (tId && page.tabs[tId] && page.tabs[tId].errorMessage) || undefined;
180-
sendResponse({
181-
configured: keepass.isConfigured(),
182-
associated: keepass.isAssociated(),
183-
identifier: (keepass.databaseHash in keepass.keyRing)
184-
? keepass.keyRing[keepass.databaseHash].id
185-
: null,
186-
keePassHttpAvailable: keepass.isKeePassHttpAvailable,
187-
databaseClosed: keepass.isDatabaseClosed,
188-
encryptionKeyUnrecognized: keepass.isEncryptionKeyUnrecognized,
189-
error
177+
// Clear any stale error messages before computing status
178+
if (typeof page !== 'undefined' && page.tabs) {
179+
for (const t of Object.values(page.tabs)) {
180+
if (t && 'errorMessage' in t) delete t.errorMessage;
181+
}
182+
}
183+
refreshStatus(async statusData => {
184+
if (!statusData.associated && statusData.configured && !statusData.databaseClosed) {
185+
// Attempt lightweight bootstrap / reassociation
186+
try {
187+
await keepass.bootstrapAssociation();
188+
} catch (e) {
189+
console.warn('Reassociation attempt failed', e);
190+
}
191+
// Refresh after attempt
192+
return refreshStatus(newStatus => {
193+
try { sendResponse(newStatus); } catch (_) { }
194+
});
195+
}
196+
try { sendResponse(statusData); } catch (_) { }
190197
});
191-
return;
198+
return true;
192199
}
193200
case 'check_update_keepasshttp': {
194201
keepass.checkForNewKeePassHttpVersion()
@@ -290,4 +297,22 @@ chrome.runtime.onInstalled.addListener(details => {
290297
});
291298

292299
// Lazy on-demand init (in case worker was cold-started)
293-
chrome.runtime.onStartup && chrome.runtime.onStartup.addListener(() => safeInit({ reason: 'startup' }));
300+
chrome.runtime.onStartup && chrome.runtime.onStartup.addListener(() => safeInit({ reason: 'startup' }));
301+
302+
function buildStatus() {
303+
return {
304+
configured: keepass.isConfigured(),
305+
associated: keepass.isAssociated(),
306+
identifier: (keepass.databaseHash in keepass.keyRing)
307+
? keepass.keyRing[keepass.databaseHash].id
308+
: null,
309+
keePassHttpAvailable: keepass.isKeePassHttpAvailable,
310+
databaseClosed: keepass.isDatabaseClosed,
311+
encryptionKeyUnrecognized: keepass.isEncryptionKeyUnrecognized,
312+
error: (page.tabs && Object.values(page.tabs).find(t => t.errorMessage) || {}).errorMessage
313+
};
314+
}
315+
316+
function refreshStatus(cb) {
317+
keepass.refreshStatus(() => cb(buildStatus()));
318+
}

chromeipass/popups/popup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ document.addEventListener('DOMContentLoaded', () => {
106106
if (tabs && tabs[0]) {
107107
chrome.tabs.sendMessage(tabs[0].id, { action: "redetect_fields" });
108108
}
109+
window.close();
109110
});
110111
});
111112
}

0 commit comments

Comments
 (0)