-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
614 lines (588 loc) · 21.2 KB
/
background.js
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// SPDX-License-Identifier: (GPL-3.0-or-later AND Apache-2.0)
import _ from 'underscore'
import { spdxkey, defaultoptions, urls } from './const.js'
var version = browser.runtime.getManifest().version
var list = {}
var options
var lastupdate
var updating = false
var runningworkers = 0
var workers = []
var workqueue = {}
var workqueuelength = 0
var pendingcompare = false
var comparequeue = []
var activeTabId = null
var unsorted = {}
var selection = ''
var status = {}
var diffcount = {}
var licensesLoaded = 0
var pendingload = false
var filtered = {}
var total = 0
var completedcompares = 0
chrome.browserAction.setBadgeText({
text: `Diff`
})
function handleClick (tab) {
// Send a message to the active tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tab) {
var activeTab = tab[0]
activeTabId = activeTab.id
console.log('Click detected', status[activeTabId])
if (!status[activeTabId]) {
injectContentScript(activeTabId)
} else {
chrome.tabs.sendMessage(activeTabId, { 'command': 'clicked_browser_action' }, messageResponse)
}
})
}
function injectContentScript (activeTabId) {
chrome.tabs.insertCSS(activeTabId, { file: '/styles/contentscript.css' })
chrome.tabs.executeScript(activeTabId, { file: '/scripts/contentscript.js' },
function (result) {
chrome.tabs.sendMessage(activeTabId, { 'command': 'clicked_browser_action' }, messageResponse)
})
}
function messageResponse (response) {
console.log('Processing message response from ' + activeTabId, response)
if (!response) {
console.log(activeTabId + ' failed to respond; assuming not injected in tab')
status[activeTabId] = null
}
}
function handleActivate (activeinfo) {
// Set the active tab
activeTabId = activeinfo.tabId
// console.log("ActiveTabId changed", activeTabId)
if (status[activeTabId]) {
chrome.tabs.sendMessage(activeTabId, { command: 'alive?' }, messageResponse)
}
}
function handleUpdated (tabId, changeInfo, tabInfo) {
// console.log(tabId + " updated.");
if (status[tabId]) { // reset status so we will inject content script again
status[tabId] = null
}
}
function handleFocusChanged (windowid) {
// Set the active tab
chrome.tabs.query({ active: true, currentWindow: true }, function (queryinfo) {
if (queryinfo.length > 0) { activeTabId = queryinfo[0].id }
// console.log("ActiveTabId changed", activeTabId)
})
}
// This function responds to changes to storage
function handleStorageChange (changes, area) {
if (area === 'local' && 'options' in changes) {
console.log('Detected changed options; reloading')
restoreOptions()
}
}
// respond to content script
function handleMessage (request, sender, sendResponse) {
switch (request.command) {
case 'focused':
activeTabId = sender.tab.id
console.log('activeTabId', activeTabId)
break
case 'updatelicenselist':
updateList()
break
case 'compareselection':
selection = request.selection
activeTabId = sender.tab.id
if (updating || list.licenses === undefined || licensesLoaded < list.licenses.length) {
pendingcompare = true
comparequeue.push({ 'selection': selection, 'tabId': activeTabId })
status[activeTabId] = 'Pending'
if (updating) {
console.log('Update pending; queing compare for tab %s; %s queued', activeTabId, comparequeue.length)
} else {
console.log('License load needed; queing compare for tab %s; %s queued', activeTabId, comparequeue.length)
loadList()
}
break
}
if (status[activeTabId] !== 'Comparing') {
console.log('tab %s: Starting compare: %s', activeTabId, selection.substring(0, 25))
status[activeTabId] = 'Comparing'
compareSelection(selection, activeTabId)
} else {
console.log('tab %s: Ignoring redundant compare: %s', activeTabId, selection.substring(0, 25))
}
break
case 'generateDiff':
activeTabId = sender.tab.id
request.tabId = activeTabId
console.log('tab %s: Generating diff:', activeTabId, request)
status[activeTabId] = 'Diffing'
diffcount[activeTabId] = diffcount[activeTabId] + 1
dowork(request)
break
default:
// console.log("Proxying to worker", request);
// chrome.tabs.sendMessage(activeTab.id, request);
break
}
}
// Workerqueue functions
// These functions are for allowing multiple workers.
function workeronmessage (event) {
processqueue((status[activeTabId] && status[activeTabId] !== 'Done' &&
activeTabId)
? activeTabId : 0) // Message received so see if queue can be cleared.
var result
var threadid
var type
var item
switch (event.data.command) {
case 'progressbarmax':
var tabId = (event.data.tabId !== undefined) ? event.data.tabId : activeTabId
if (pendingcompare) { // broadcast to all
for (var i = 0; i < comparequeue.length; i++) { chrome.tabs.sendMessage(comparequeue[i].tabId, event.data) }
} else if (tabId !== undefined && tabId) {
chrome.tabs.sendMessage(tabId, event.data)
}
break
case 'progressbarvalue':
tabId = (event.data.tabId !== undefined) ? event.data.tabId : activeTabId
if (pendingcompare) { // broadcast to all
for (i = 0; i < comparequeue.length; i++) { chrome.tabs.sendMessage(comparequeue[i].tabId, event.data) }
} else if (tabId !== undefined && tabId) {
chrome.tabs.sendMessage(tabId, event.data)
}
break
case 'next':
tabId = (event.data.tabId !== undefined) ? event.data.tabId : activeTabId
if (pendingcompare) { // broadcast to all
for (i = 0; i < comparequeue.length; i++) { chrome.tabs.sendMessage(comparequeue[i].tabId, event.data) }
} else if (tabId !== undefined && tabId) {
chrome.tabs.sendMessage(tabId, event.data)
}
break
case 'store':
// This path is intended to store a hash of a comparison. TODO: Complete
// updateProgressBar(-1, -1)
var obj = {}
obj[event.data.spdxid] = {
hash: event.data.hash,
raw: event.data.raw,
processed: event.data.processed,
patterns: event.data.patterns }
setStorage(obj)
break
case 'license':
// This path is intended to determine if comparison already done. TODO: Complete
var spdxid = event.data.spdxid
// var hash = event.data.hash
// chrome.storage.local.get(spdxid, function(result) {
// if (result[spdxid] && result[spdxid].hash != hash){
// console.log('No match found', spdxid, hash, result);
// worker.postMessage({ 'command':'process', 'license':spdxid,'data':result[spdxid].processed, 'selection': selection});
//
// }else {
// console.log('Found prior computed result', spdxid, hash, result);
//
// }
// });
break
case 'savelicenselist':
type = event.data.type
var externallicenselist = event.data.value
var externalversion = externallicenselist.licenseListVersion
var externalcount = (externallicenselist[type])
? Object.keys(externallicenselist[type]).length : 0
console.log('Trying to save list', externallicenselist)
getStorage('list').then(function (result) {
if (result.list && result.list.licenseListVersion) {
var list = result.list
var version = list.licenseListVersion
var count = (list[type]) ? Object.keys(list[type]).length : 0
console.log('Existing License list version %s with %s %s',
version,
count,
type)
if (version < externalversion) {
console.log('Newer license list version %s found with %s %s',
externalversion,
externalcount,
type)
storeList(externallicenselist)
} else if (count < externalcount) {
list[type] = externallicenselist[type]
console.log('New list version %s found with %s %s more than old list with %s %s',
externalversion,
externalcount,
type,
count,
type)
storeList(list)
} else {
// dowork({ 'command': 'populatelicenselist', 'data': list })
console.log('No new update found; same version %s and with %s %s',
externalversion,
externalcount,
type)
}
} else {
console.log('No existing license list found; storing')
storeList(externallicenselist)
}
})
break
case 'saveitem':
if (updating) {
item = event.data
type = event.data.type
spdxid = item.data[spdxkey[type].id]
if (list[type + 'dict'] === undefined) {
list[type + 'dict'] = {}
}
list[type + 'dict'][spdxid] = item.data[spdxid]
console.log('Saving %s: %s', type, spdxid, item.data)
getStorage(spdxid).then(function (result) {
if (result[spdxid] && item.data && _.isEqual(result[spdxid], item.data)) {
// var license = result[spdxid]
console.log('Ignoring existing %s %s', type, spdxid)
} else {
console.log('Saving new %s %s', type, spdxid)
var obj = {}
obj[spdxid] = item.data
chrome.storage.local.set(obj,
function () {
console.log('Storing', obj)
})
}
})
} else {
console.log('Not supposed to update but received saveitem request; ignoring', event.data)
}
break
case 'updatedone':
workerdone(event.data.id)
type = event.data.type
console.log('Received worker update completion for %s', type)
let types = Object.keys(urls)
if (types.every((type) => {
return (list[type].length === Object.keys(list[type + 'dict']).length)
})) {
console.log('Update completed')
updating = false
launchPendingCompares()
}
break
case 'comparenext':
threadid = event.data.id
workerdone(threadid)
tabId = event.data.tabId
result = event.data.result
spdxid = event.data.spdxid
chrome.tabs.sendMessage(tabId, { 'command': 'next', 'spdxid': spdxid, 'id': threadid })
unsorted[tabId][spdxid] = result
completedcompares++
if (completedcompares >= Object.keys(unsorted[tabId]).length) {
console.log('Requesting final sort of %s for tab %s', Object.keys(unsorted[tabId]).length, tabId)
status[tabId] = 'Sorting'
dowork({ 'command': 'sortlicenses', 'licenses': unsorted[tabId], 'tabId': tabId })
// unsorted[tabId] = {}
diffcount[tabId] = 0
}
break
case 'sortdone':
threadid = event.data.id
workerdone(threadid)
tabId = event.data.tabId
result = event.data.result
chrome.tabs.sendMessage(tabId, { 'command': 'sortdone', 'result': result, 'id': threadid })
break
case 'diffnext':
threadid = event.data.id
workerdone(threadid)
tabId = event.data.tabId
result = event.data.result
spdxid = event.data.spdxid
var record = event.data.record
chrome.tabs.sendMessage(tabId, { 'command': 'diffnext', 'spdxid': spdxid, 'result': result, 'record': record, 'id': threadid })
diffcount[tabId] = diffcount[tabId] - 1
if (diffcount[tabId] === 0) {
status[tabId] = 'Done'
for (let filter of Object.keys(filtered[tabId])) {
for (let item in filtered[tabId][filter]) {
let type = filtered[tabId][filter][item].type
let itemdict = list[type + 'dict'][item]
status[tabId] = 'Background comparing'
console.log('Background compare for %s', item)
dowork({ 'command': 'compare', 'selection': selection, 'maxLengthDifference': options.maxLengthDifference, 'spdxid': item, 'itemdict': itemdict, 'total': total, 'tabId': tabId, 'type': type, 'background': true })
}
}
}
break
case 'backgroundcomparenext':
threadid = event.data.id
workerdone(threadid)
tabId = event.data.tabId
result = event.data.result
spdxid = event.data.spdxid
chrome.tabs.sendMessage(tabId, { 'command': 'next', 'spdxid': spdxid, 'id': threadid })
if (filtered[tabId]['results'] === undefined) { filtered[tabId]['results'] = {} }
filtered[tabId]['results'][spdxid] = result
unsorted[tabId][spdxid] = result
completedcompares++
total = Object.keys(urls).reduce((type) => {
return (Object.keys(list[type + 'dict']).length)
})
if (completedcompares === total) {
console.log('Done with background compare of %s for tab %s', Object.keys(filtered[tabId]['results']).length, tabId)
status[tabId] = 'Done'
dowork({ 'command': 'sortlicenses', 'licenses': unsorted[tabId], 'tabId': tabId })
// unsorted[tabId] = {}
diffcount[tabId] = 0
}
break
default:
}
}
// storage functions
function storeList (externallicenselist) {
var obj = {}
externallicenselist.lastupdate = Date.now()
obj = {
list: externallicenselist
}
setStorage(obj)
}
function loadList () {
if (pendingload) {
console.log('Ignoring redundant loadList request')
} else {
pendingload = true
console.log('Attempting to load list from storage')
getStorage('list').then((result, intspdxkey = spdxkey) => {
if (result.list && result.list.licenseListVersion) {
list = result.list
lastupdate = list.lastupdate
var version = list.licenseListVersion
var lcount = (list.licenses) ? Object.keys(list.licenses).length : 0
var ecount = (list.exceptions) ? Object.keys(list.exceptions).length : 0
console.log('Loading License list version %s from storage with %s licenses %s exceptions last updated %s',
version, lcount, ecount, new Date(lastupdate))
if ((Date.now() - lastupdate) >= (options.updateFrequency * 86400000)) {
console.log('Last update was over %s days ago; update required', options.updateFrequency)
updateList()
} else {
for (let type of Object.keys(urls)) {
if (!list[type]) {
continue
}
if (typeof list[type + 'dict'] === 'undefined') { list[type + 'dict'] = {} }
for (let j = 0; j < list[type].length; j++) {
let line = list[type][j]
let item = line[intspdxkey[type].id]
console.log('Attempting to load %s from storage', item)
getStorage(item).then((result, types = Object.keys(urls)) => {
if (result && !_.isEmpty(result)) {
item = Object.keys(result)[0]
list[type + 'dict'][item] = result[item]
licensesLoaded++
console.log('%s succesfully loaded from storage %s/%s (%s)',
item, Object.keys(list[type + 'dict']).length, list[type].length, type)
} else if (updating) {
console.log('%s not found in storage; update pending', item)
} else {
console.log('%s not found in storage; requesting update', item)
updateList()
}
if (types.every((type) => {
return (list[type] && list[type + 'dict'] &&
list[type].length === Object.keys(list[type + 'dict']).length)
})) {
pendingload = false
launchPendingCompares()
}
})
}
}
}
} else {
pendingload = false
console.log('No license list found in storage; requesting update')
updateList()
}
})
}
}
function updateList () {
if (updating) {
console.log('Ignoring redundant update request')
} else {
updating = true
licensesLoaded = 0
dowork({ 'command': 'updatelicenselist' })
}
}
// processing phase functions (these are called by the workeronmessage in order)
// Compare selection against a fully populated license list (must be loaded in list)
// This is the first phase to determine edit distance and return a sorted list
// for display in spdx
function compareSelection (selection, tabId = activeTabId) {
unsorted[tabId] = {}
if (filtered === undefined) {
filtered = {}
}
filtered[tabId] = {}
completedcompares = 0
for (let type of Object.keys(urls)) {
for (let item of Object.keys(list[type + 'dict'])) {
for (let filter in options.filters) {
if (list[type + 'dict'][item][options.filters[filter]]) {
if (filtered[tabId][filter] === undefined) { filtered[tabId][filter] = {} }
filtered[tabId][filter][item] = {}
filtered[tabId][filter][item].type = type
console.log('Deferring %s %s because its %s', type, item, filter)
}
if (filtered[tabId] === undefined ||
filtered[tabId][filter] === undefined ||
filtered[tabId][filter][item] === undefined) {
unsorted[tabId][item] = list[type + 'dict'][item]
unsorted[tabId][item].type = type
}
}
}
}
total = Object.keys(unsorted[tabId]).length
for (let item in unsorted[tabId]) {
let type = unsorted[tabId][item].type
let itemdict = list[type + 'dict'][item]
dowork({ 'command': 'compare', 'selection': selection, 'maxLengthDifference': options.maxLengthDifference, 'spdxid': item, 'itemdict': itemdict, 'total': total, 'tabId': tabId, 'type': type })
chrome.tabs.sendMessage(tabId, { 'message': 'progressbarmax', 'value': total, 'stage': 'Comparing licenses', 'reset': true })
}
}
function launchPendingCompares () {
if (pendingcompare) { console.log('Processing pending %s compare items', comparequeue.length) }
while (comparequeue.length) {
var compare = comparequeue.shift()
selection = compare.selection
var tabId = compare.tabId
console.log('Processing compare queue: compare for %s of selection length %s', tabId, selection.length)
status[tabId] = 'Comparing'
compareSelection(selection, tabId)
}
pendingcompare = false
}
function restoreOptions () {
getStorage('options').then(function (result) {
options = result.options
if (options === undefined) {
options = defaultoptions
}
loadList()
})
}
// Workerqueue functions
// These functions are for allowing multiple workers.
function spawnworkers () {
if (workers.length >= options.maxworkers) { return }
console.log('Spawning %s workers', options.maxworkers)
for (var i = 0; i < options.maxworkers; i++) {
var worker = new Worker(chrome.runtime.getURL('scripts/worker.js'))
worker.onmessage = workeronmessage
workers[i] = [worker, false]
}
}
// queue and start work
// priority defines a tabId to search for
function processqueue (priority = 0) {
var work = null
while (workqueuelength > 0 && options.maxworkers > runningworkers) {
if ((priority > 0) &&
(workqueue[priority]) &&
(work = workqueue[priority].shift())) {
dowork(work)
workqueuelength--
console.log('Prioritizing tab %s work with %s items, total queue %s items', priority, workqueue[priority].length, workqueuelength)
} else {
for (var tabId in workqueue) {
work = workqueue[tabId].shift()
if (work) {
dowork(work)
workqueuelength--
}
}
}
}
}
function dowork (message) {
spawnworkers()
var offset = options.maxworkers - runningworkers
if (options.maxworkers > runningworkers) {
for (var i = runningworkers % options.maxworkers; i < options.maxworkers + offset - 1; i = (i + 1) % options.maxworkers) {
if (!workers[i][1]) { // worker is available
message.id = i
var worker = workers[i][0]
workers[i][1] = true
worker.postMessage(message)
runningworkers++
break
} else {
continue
}
}
} else { // queue up work
queuework(message)
}
}
function queuework (message) {
var tabId
if (!message.tabId) {
tabId = 0
} else {
tabId = message.tabId
}
if (!workqueue[tabId]) {
workqueue[tabId] = []
}
workqueue[tabId].push(message)
workqueuelength++
}
function workerdone (id) {
workers[id][1] = false
runningworkers--
}
// promisfy gets
const getStorage = function (item) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(item, function (result) {
if (chrome.runtime.lastError) {
reject(Error(chrome.runtime.lastError))
} else {
resolve(result)
}
})
})
}
const setStorage = function (obj) {
return new Promise((resolve, reject) => {
chrome.storage.local.set(obj, function () {
if (chrome.runtime.lastError) {
reject(Error(chrome.runtime.lastError))
} else {
console.log('Storing', obj)
resolve()
}
})
})
}
function init () {
console.log('Initializing spdx-license-diff ' + version)
restoreOptions()
}
init()
chrome.runtime.onStartup.addListener(init)
chrome.runtime.onMessage.addListener(handleMessage)
chrome.browserAction.onClicked.addListener(handleClick)
chrome.tabs.onActivated.addListener(handleActivate)
chrome.windows.onFocusChanged.addListener(handleFocusChanged)
chrome.storage.onChanged.addListener(handleStorageChange)
chrome.tabs.onUpdated.addListener(handleUpdated)