-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrulesManager.js
96 lines (80 loc) · 3.17 KB
/
rulesManager.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
/*
* Copyright (C) 2019 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
let rulesManager = new RulesManager();
function RulesManager() {
let rulesId = [];
let rulesChecker = new Map();
let eventListeners = new Map();
let notCompatibleRules = [];
eventListeners.set("harReceived", []);
eventListeners.set("frameMeasuresReceived", []);
eventListeners.set("resourceContentReceived", []);
this.registerRule = function (ruleChecker, eventListener) {
rulesId.push(ruleChecker.id);
if ((eventListener==="resourceContentReceived") && ((!chrome.devtools) || (!chrome.devtools.inspectedWindow.getResources))) notCompatibleRules.push(ruleChecker.id);
else {
rulesChecker.set(ruleChecker.id, ruleChecker);
let event = eventListeners.get(eventListener);
if (event) event.push(ruleChecker.id);
}
}
this.getRulesId = function () {
return rulesId;
}
this.getRulesNotCompatibleWithCurrentBrowser = function () {
return notCompatibleRules;
}
this.getNewRulesChecker = function () {
return new RulesChecker();
}
function RulesChecker() {
let rules = new Map();
rulesChecker.forEach((ruleChecker, ruleId) => {
let ruleCheckerInstance = Object.create(ruleChecker)
// for certain rules need an initialization , method not implemented in all rules
if (ruleCheckerInstance.initialize) ruleCheckerInstance.initialize();
rules.set(ruleId, ruleCheckerInstance);
});
this.sendEvent = function (event, measures, resource) {
const eventListener = eventListeners.get(event);
if (eventListener) {
eventListener.forEach(ruleID => {
this.checkRule(ruleID, measures, resource);
// not used yet , see https://github.com/cnumr/GreenIT-Analysis/pull/22
// this.manageExport(ruleID, measures);
});
}
}
this.checkRule = function (rule, measures, resource) {
rules.get(rule).check(measures, resource);
}
this.manageExport = function (rule, measures) {
let myRule = rules.get(rule);
measures.bestPracticeDetails[rule] = {};
measures.bestPracticeDetails[rule].comment = myRule.comment;
measures.bestPracticeDetails[rule].detailComment = myRule.detailComment;
measures.bestPracticeDetails[rule].complianceLevel = myRule.complianceLevel;
measures.bestPracticeDetails[rule].specificMeasures = myRule.getSpecificMeasures();
}
this.getRule = function (rule) {
return rules.get(rule);
}
this.getAllRules = function () {
return rules;
}
}
}