-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
432 lines (368 loc) · 13.2 KB
/
Copy pathbackground.js
File metadata and controls
432 lines (368 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* Background script for Thunderbird Sync Labels extension
*/
// Import modules (loaded in manifest)
// Note: In real setup, use proper module loading
let syncEngine = null;
const SYNC_ALARM_NAME = 'periodic-sync';
const SYNC_TIMEOUT = 30000; // 30 seconds timeout for sync operations
// MV3 event pages get terminated after ~30s idle, and Thunderbird 128 does NOT
// wake a terminated page when browser.alarms fire. To make auto-sync reliable we
// keep the page alive with a lightweight API ping while sync is enabled.
const KEEPALIVE_INTERVAL_MS = 20000;
let keepAliveTimer = null;
// Default to filesystem backend for actual file syncing
const DEFAULT_BACKEND_TYPE = 'filesystem';
// Listen for extension installation or update
browser.runtime.onInstalled.addListener(async (details) => {
console.log("Extension installed:", details);
if (details.reason === "install") {
console.log("First time installation - initializing default settings...");
// No default path: the user must configure it in settings.
await browser.storage.local.set({
syncEnabled: false, // Disabled until user configures
lastSync: null,
syncInterval: 3600000, // 1 hour default
backendConfig: {
type: 'filesystem',
filePath: ''
},
tagDefinitions: {},
lastSyncedTags: null, // Snapshot for three-way merge (deletion tracking)
tagModificationTimes: {} // Track tag modification timestamps
});
console.log('[Install] Configure path in settings to enable syncing');
} else if (details.reason === "update") {
console.log("Extension updated to version:", browser.runtime.getManifest().version);
// Ensure a backendConfig object exists (path stays empty until configured)
const storage = await browser.storage.local.get(['backendConfig', 'lastSyncedTags', 'tagModificationTimes']);
if (!storage.backendConfig) {
await browser.storage.local.set({
syncEnabled: false,
syncInterval: 3600000,
backendConfig: {
type: 'filesystem',
filePath: ''
}
});
}
// Migration: Add deletion tracking support for existing users
if (storage.lastSyncedTags === undefined || storage.tagModificationTimes === undefined) {
console.log("[Migration] Adding deletion tracking support (three-way merge)");
await browser.storage.local.set({
lastSyncedTags: null,
tagModificationTimes: {}
});
console.log("[Migration] Next sync will establish baseline snapshot");
}
}
// Initialize sync engine
await initializeSyncEngine();
// Auto-start sync if enabled
const storage = await browser.storage.local.get('syncEnabled');
if (storage.syncEnabled) {
await startSyncScheduler();
}
});
/**
* Initialize the sync engine with configured backend
*/
async function initializeSyncEngine() {
try {
const storage = await browser.storage.local.get(['backendConfig']);
const backendConfig = storage.backendConfig;
if (!backendConfig || !backendConfig.type) {
console.warn('[Background] Backend not configured');
return;
}
console.log('[Background] Initializing sync engine with backend:', backendConfig.type);
console.log('[Background] Backend config:', JSON.stringify(backendConfig));
// Create appropriate backend adapter
let backend;
let requestedType = backendConfig.type;
switch (backendConfig.type) {
case 'storage':
if (typeof StorageBackend !== 'undefined') {
backend = new StorageBackend(backendConfig);
console.log('[Background] Using StorageBackend');
} else {
console.error('[Background] StorageBackend not loaded');
return;
}
break;
case 'filesystem':
if (typeof FilesystemBackend !== 'undefined') {
backend = new FilesystemBackend(backendConfig);
console.log('[Background] Using FilesystemBackend');
} else {
console.error('[Background] FilesystemBackend not loaded');
return;
}
break;
default:
console.error('[Background] Unknown backend type:', backendConfig.type);
return;
}
// Test backend connection
const connected = await backend.testConnection();
if (!connected) {
console.warn(`[Background] Backend connection test failed for type: ${requestedType}`);
// Fallback to StorageBackend if filesystem fails
if (requestedType === 'filesystem' && typeof StorageBackend !== 'undefined') {
console.warn('[Background] Falling back to StorageBackend (filesystem not available)');
backend = new StorageBackend({});
const storageConnected = await backend.testConnection();
if (!storageConnected) {
console.error('[Background] Even StorageBackend failed');
return;
}
} else {
return;
}
}
// Initialize tag manager and sync engine
if (typeof TagManager === 'undefined' || typeof SyncEngine === 'undefined') {
console.error('[Background] Tag manager or sync engine not loaded');
return;
}
const tagManager = new TagManager();
await tagManager.init();
syncEngine = new SyncEngine(backend, tagManager);
console.log('[Background] Sync engine initialized and ready');
} catch (error) {
console.error('[Background] Failed to initialize sync engine:', error.message);
}
}
/**
* Handle messages from popup or other parts of the extension
* IMPORTANT: Must return true to indicate async response
*/
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('[Background] Message received:', request.action);
// Handle async operations
(async () => {
try {
if (request.action === 'performSync') {
const result = await performSync();
sendResponse({ success: true, result });
}
else if (request.action === 'getStatus') {
const status = await getExtensionStatus();
sendResponse({ success: true, status });
}
else if (request.action === 'updateBackendConfig') {
await browser.storage.local.set({
backendConfig: request.config
});
await initializeSyncEngine();
sendResponse({ success: true });
}
else if (request.action === 'getLogs') {
if (typeof errorHandler !== 'undefined') {
const logs = errorHandler.getHistory(200);
sendResponse({ success: true, logs });
} else {
sendResponse({ success: true, logs: [] });
}
}
else if (request.action === 'clearLogs') {
if (typeof errorHandler !== 'undefined') {
errorHandler.clearHistory();
}
sendResponse({ success: true });
}
else if (request.action === 'detectSyncthing') {
try {
const discovery = new PathDiscovery();
const accessible = await discovery.discoverPaths();
const folders = accessible.map(entry => ({
path: entry.path,
label: entry.label,
type: entry.type,
suggestedFile: discovery.suggestTagsPath(entry.path)
}));
sendResponse({ success: true, folders });
} catch (err) {
sendResponse({ success: false, folders: [], error: err.message });
}
}
else if (request.action === 'testPath') {
const testBackend = new FilesystemBackend({ filePath: request.path });
const connected = await testBackend.testConnection();
sendResponse({ success: connected, message: connected ? 'Path is accessible' : 'Path not accessible' });
}
else {
sendResponse({ success: false, error: 'Unknown action: ' + request.action });
}
} catch (error) {
console.error('[Background] Message handler error:', error);
sendResponse({ success: false, error: error.message });
}
})();
// Return true to indicate we will respond asynchronously
return true;
});
/**
* Perform sync with backend
*/
async function performSync() {
try {
console.log('[Background] Starting sync...');
// Lazy init: when an alarm wakes a suspended event page, the startup
// IIFE that sets syncEngine may not have finished yet. Ensure it's ready.
if (!syncEngine) {
console.log('[Background] Sync engine not ready, initializing on demand...');
await initializeSyncEngine();
}
if (!syncEngine) {
throw new Error('Sync engine not initialized. Please configure backend in settings.');
}
// Perform actual sync
const result = await syncEngine.sync();
// Always store the result; only update lastSync on success so the
// popup's "Last Sync" reflects the last SUCCESSFUL sync.
const update = { lastSyncResult: result };
if (result.status === 'success') {
update.lastSync = Date.now();
}
await browser.storage.local.set(update);
return result;
} catch (error) {
console.error('[Background] Sync failed:', error.message);
// Persist the failure so the popup does not keep showing a stale success
await browser.storage.local.set({
lastSyncResult: {
status: 'error',
imported: 0,
exported: 0,
deleted: 0,
errors: [error.message]
}
});
throw error;
}
}
/**
* Get current extension status
*/
async function getExtensionStatus() {
const storage = await browser.storage.local.get([
'syncEnabled',
'lastSync',
'backendConfig',
'lastSyncResult'
]);
return {
syncEnabled: storage.syncEnabled || false,
lastSync: storage.lastSync || null,
backendConfigured: !!(storage.backendConfig?.filePath),
backendType: storage.backendConfig?.type || 'none',
lastSyncResult: storage.lastSyncResult || null
};
}
/**
* Start periodic sync scheduler using browser.alarms (survives background script restarts)
*/
async function startSyncScheduler() {
try {
stopSyncScheduler();
const storage = await browser.storage.local.get(['syncInterval', 'syncEnabled']);
const syncInterval = storage.syncInterval || 3600000; // Default 1 hour
const syncEnabled = storage.syncEnabled;
if (!syncEnabled || syncInterval === 0) {
console.log('[Background] Sync scheduler disabled');
return;
}
const periodInMinutes = syncInterval / 60000;
console.log(`[Background] Sync scheduler starting with interval: ${periodInMinutes} minutes`);
// Keep the event page awake so the alarm actually fires (see KEEPALIVE note)
startKeepAlive();
// Perform initial sync immediately
await performSync().catch(err => {
console.error('[Background] Initial sync failed:', err.message);
});
// Schedule periodic syncs via alarms (persists across background script restarts)
browser.alarms.create(SYNC_ALARM_NAME, { periodInMinutes });
console.log('[Background] Sync scheduler started (alarms)');
} catch (error) {
console.error('[Background] Failed to start sync scheduler:', error.message);
}
}
/**
* Stop periodic sync scheduler
*/
function stopSyncScheduler() {
browser.alarms.clear(SYNC_ALARM_NAME);
stopKeepAlive();
console.log('[Background] Sync scheduler stopped');
}
/**
* Keep the MV3 event page alive so alarms fire reliably.
* A trivial API call every KEEPALIVE_INTERVAL_MS resets the idle-shutdown timer.
*/
function startKeepAlive() {
if (keepAliveTimer !== null) return;
keepAliveTimer = setInterval(() => {
browser.runtime.getPlatformInfo().catch(() => {});
}, KEEPALIVE_INTERVAL_MS);
console.log('[Background] Keep-alive started (event page kept awake)');
}
function stopKeepAlive() {
if (keepAliveTimer !== null) {
clearInterval(keepAliveTimer);
keepAliveTimer = null;
console.log('[Background] Keep-alive stopped');
}
}
// Handle alarm firing
browser.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === SYNC_ALARM_NAME) {
console.log('[Background] Running scheduled sync (alarm)...');
try {
await performSync();
} catch (error) {
console.error('[Background] Scheduled sync failed:', error.message);
}
}
});
/**
* Listen for storage changes to trigger actions
*/
browser.storage.onChanged.addListener(async (changes, areaName) => {
if (areaName === 'local') {
let needsSchedulerRestart = false;
if (changes.syncEnabled) {
console.log('[Background] Sync enabled changed to:', changes.syncEnabled.newValue);
needsSchedulerRestart = true;
}
if (changes.syncInterval) {
console.log('[Background] Sync interval changed to:', changes.syncInterval.newValue);
needsSchedulerRestart = true;
}
if (changes.backendConfig) {
console.log('[Background] Backend config changed');
await initializeSyncEngine();
// Don't restart scheduler, let current interval continue
}
if (needsSchedulerRestart) {
if (changes.syncEnabled?.newValue === false) {
stopSyncScheduler();
} else {
await startSyncScheduler();
}
}
}
});
// Initialize on load
console.log("Background script loaded");
(async () => {
if (typeof errorHandler !== 'undefined') {
await errorHandler.init();
}
await initializeSyncEngine();
// Start scheduler if enabled
const storage = await browser.storage.local.get('syncEnabled');
if (storage.syncEnabled) {
await startSyncScheduler();
}
})();