-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbackground.ts
141 lines (117 loc) · 3.88 KB
/
background.ts
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
import { AlarmKey, SCHEDULE_ALARMS } from '@common/constants/alarm-key.constant';
import { ADENA_WALLET_EXTENSION_ID } from '@common/constants/storage.constant';
import { MemoryProvider } from '@common/provider/memory/memory-provider';
import { ChromeLocalStorage } from '@common/storage';
import { CommandHandler } from '@inject/message/command-handler';
import { CommandMessage, isCommandMessageData } from '@inject/message/command-message';
import { clearInMemoryKey } from '@inject/message/commands/encrypt';
import { MessageHandler } from './inject/message';
const inMemoryProvider = new MemoryProvider();
inMemoryProvider.init();
initAlarms();
function existsWallet(): Promise<boolean> {
const storage = new ChromeLocalStorage();
return storage
.get('SERIALIZED')
.then(async (serialized) => typeof serialized === 'string' && serialized.length !== 0)
.catch(() => false);
}
function setupPopup(existWallet: boolean): boolean {
const popupUri = existWallet ? 'popup.html' : '';
chrome.action.setPopup({ popup: popupUri });
return true;
}
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.tabs.create({
url: chrome.runtime.getURL('/register.html'),
});
} else if (details.reason === 'update') {
existsWallet().then((existWallet) => {
setupPopup(existWallet);
});
}
});
chrome.tabs.onCreated.addListener(() => {
existsWallet().then((existWallet) => {
setupPopup(existWallet);
});
});
chrome.action.onClicked.addListener(async () => {
existsWallet().then((existWallet) => {
setupPopup(existWallet);
if (!existWallet) {
chrome.tabs.create({
url: chrome.runtime.getURL('/register.html'),
});
}
});
});
chrome.runtime.onConnect.addListener(async (port) => {
if (port.name !== ADENA_WALLET_EXTENSION_ID) {
return;
}
inMemoryProvider.addConnection();
inMemoryProvider.updateExpiredTimeBy(null);
port.onDisconnect.addListener(async () => {
inMemoryProvider.removeConnection();
if (!inMemoryProvider.isActive()) {
const expiredTime = new Date().getTime() + inMemoryProvider.getExpiredPasswordDurationTime();
inMemoryProvider.updateExpiredTimeBy(expiredTime);
console.info('Password Expired time:', new Date(expiredTime));
}
});
});
chrome.alarms.onAlarm.addListener(async (alarm) => {
try {
const currentTime = new Date().getTime();
chrome.storage.local.set({ SESSION: currentTime });
switch (alarm?.name) {
case AlarmKey.EXPIRED_PASSWORD:
if (!inMemoryProvider.isExpired(currentTime)) {
return;
}
await chrome.storage.session.clear();
await clearInMemoryKey(inMemoryProvider);
inMemoryProvider.updateExpiredTimeBy(null);
console.info('Password Expired');
break;
default:
break;
}
} catch (error) {
console.error(error);
}
return true;
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
// Check metadata when tab is updated
if (changeInfo.status === 'complete') {
chrome.tabs.sendMessage(tabId, CommandMessage.command('checkMetadata')).catch(console.info);
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (isCommandMessageData(message)) {
CommandHandler.createHandler(inMemoryProvider, message, sender, sendResponse);
return true;
}
return MessageHandler.createHandler(inMemoryProvider, message, sender, sendResponse);
});
function initAlarms(): void {
SCHEDULE_ALARMS.map(initAlarmWithDelay);
}
function initAlarmWithDelay(alarm: { key: string; periodInMinutes: number; delay: number }): void {
if (alarm.delay === 0) {
chrome.alarms.create(alarm.key, {
periodInMinutes: alarm.periodInMinutes,
});
return;
}
setTimeout(
() =>
chrome.alarms.create(alarm.key, {
periodInMinutes: alarm.periodInMinutes,
}),
alarm.delay,
);
}