Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/testing/cspviolationobserver.js
4506 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
goog.provide('goog.testing.CspViolationObserver');
8
goog.setTestOnly('goog.testing.CspViolationObserver');
9
10
/**
11
* A class for watching Content Security Policy violation reports.
12
*
13
* @constructor
14
*/
15
goog.testing.CspViolationObserver = function() {
16
if (!window.ReportingObserver) {
17
return;
18
}
19
20
/** @private {?ReportingObserver} */
21
this.reportingObserver_ = null;
22
23
/** @private {boolean} */
24
this.enabled_ = true;
25
26
/** @private {!Array<!Report>} */
27
this.reports_ = [];
28
};
29
30
31
/**
32
* Starts listening for CSP reports.
33
*/
34
goog.testing.CspViolationObserver.prototype.start = function() {
35
if (!window.ReportingObserver || !this.enabled_) {
36
return;
37
}
38
39
if (this.reportingObserver_) {
40
throw new Error('CspViolationObserver already started');
41
}
42
43
this.reportingObserver_ = new ReportingObserver(this.onReport_.bind(this), {
44
types: ['csp-violation'],
45
buffered: false,
46
});
47
48
this.reports_ = [];
49
this.reportingObserver_.observe();
50
};
51
52
53
/**
54
* Returns CSP reports collected so far and empties the collection buffer.
55
*
56
* @return {!Array<!Report>}
57
* @private
58
*/
59
goog.testing.CspViolationObserver.prototype.take_ = function() {
60
const newReports = this.reportingObserver_.takeRecords();
61
this.reports_.push(...newReports);
62
63
const results = this.reports_;
64
this.reports_ = [];
65
66
return results;
67
};
68
69
70
/**
71
* Stops listening for violation reports and returns all reports captured so
72
* far.
73
*
74
* @return {!Array<!Report>}
75
*/
76
goog.testing.CspViolationObserver.prototype.stop = function() {
77
if (!window.ReportingObserver) {
78
return [];
79
}
80
81
if (!this.reportingObserver_) {
82
return [];
83
}
84
85
const results = this.take_();
86
this.reportingObserver_.disconnect();
87
this.reportingObserver_ = null;
88
89
return results;
90
};
91
92
93
/**
94
* If called with false, violation reports will no longer be captured and empty
95
* arrays will be returned from stop().
96
*
97
* @param {boolean} enabled
98
*/
99
goog.testing.CspViolationObserver.prototype.setEnabled = function(enabled) {
100
if (!window.ReportingObserver) {
101
return;
102
}
103
if (enabled === this.enabled_) {
104
return;
105
}
106
107
this.reports_ = [];
108
109
if (this.reportingObserver_) {
110
if (enabled) {
111
this.reportingObserver_.observe();
112
} else {
113
this.reportingObserver_.disconnect();
114
}
115
}
116
this.enabled_ = enabled;
117
};
118
119
120
/**
121
* ReportingObserver callback.
122
*
123
* @param {!Array<!Report>} reports
124
* @param {!ReportingObserver} observer
125
* @private
126
*/
127
goog.testing.CspViolationObserver.prototype.onReport_ = function(
128
reports, observer) {
129
this.reports_.push(...reports);
130
};
131
132
133
/**
134
* Returns previously generated reports.
135
*
136
* @return {!Array<!Report>}
137
*/
138
goog.testing.CspViolationObserver.getBufferedReports = function() {
139
if (!window.ReportingObserver) {
140
return [];
141
}
142
143
const reportingObserver = new ReportingObserver(function() {}, {
144
types: ['csp-violation'],
145
buffered: true,
146
});
147
148
// Calling .observe() is necessary otherwise takeRecords() will not return
149
// results.
150
reportingObserver.observe();
151
const reports = reportingObserver.takeRecords();
152
reportingObserver.disconnect();
153
return reports;
154
};
155
156
157
/**
158
* Formats the given list of reports as a string.
159
*
160
* @param {!Array<!Report>} reports
161
* @return {string}
162
*/
163
goog.testing.CspViolationObserver.formatReports = function(reports) {
164
return reports
165
.map(function(report) {
166
return JSON.stringify(report.body.toJSON(), null, ' ');
167
})
168
.join(', ');
169
};
170
171