Path: blob/trunk/third_party/closure/goog/testing/cspviolationobserver.js
4506 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56goog.provide('goog.testing.CspViolationObserver');7goog.setTestOnly('goog.testing.CspViolationObserver');89/**10* A class for watching Content Security Policy violation reports.11*12* @constructor13*/14goog.testing.CspViolationObserver = function() {15if (!window.ReportingObserver) {16return;17}1819/** @private {?ReportingObserver} */20this.reportingObserver_ = null;2122/** @private {boolean} */23this.enabled_ = true;2425/** @private {!Array<!Report>} */26this.reports_ = [];27};282930/**31* Starts listening for CSP reports.32*/33goog.testing.CspViolationObserver.prototype.start = function() {34if (!window.ReportingObserver || !this.enabled_) {35return;36}3738if (this.reportingObserver_) {39throw new Error('CspViolationObserver already started');40}4142this.reportingObserver_ = new ReportingObserver(this.onReport_.bind(this), {43types: ['csp-violation'],44buffered: false,45});4647this.reports_ = [];48this.reportingObserver_.observe();49};505152/**53* Returns CSP reports collected so far and empties the collection buffer.54*55* @return {!Array<!Report>}56* @private57*/58goog.testing.CspViolationObserver.prototype.take_ = function() {59const newReports = this.reportingObserver_.takeRecords();60this.reports_.push(...newReports);6162const results = this.reports_;63this.reports_ = [];6465return results;66};676869/**70* Stops listening for violation reports and returns all reports captured so71* far.72*73* @return {!Array<!Report>}74*/75goog.testing.CspViolationObserver.prototype.stop = function() {76if (!window.ReportingObserver) {77return [];78}7980if (!this.reportingObserver_) {81return [];82}8384const results = this.take_();85this.reportingObserver_.disconnect();86this.reportingObserver_ = null;8788return results;89};909192/**93* If called with false, violation reports will no longer be captured and empty94* arrays will be returned from stop().95*96* @param {boolean} enabled97*/98goog.testing.CspViolationObserver.prototype.setEnabled = function(enabled) {99if (!window.ReportingObserver) {100return;101}102if (enabled === this.enabled_) {103return;104}105106this.reports_ = [];107108if (this.reportingObserver_) {109if (enabled) {110this.reportingObserver_.observe();111} else {112this.reportingObserver_.disconnect();113}114}115this.enabled_ = enabled;116};117118119/**120* ReportingObserver callback.121*122* @param {!Array<!Report>} reports123* @param {!ReportingObserver} observer124* @private125*/126goog.testing.CspViolationObserver.prototype.onReport_ = function(127reports, observer) {128this.reports_.push(...reports);129};130131132/**133* Returns previously generated reports.134*135* @return {!Array<!Report>}136*/137goog.testing.CspViolationObserver.getBufferedReports = function() {138if (!window.ReportingObserver) {139return [];140}141142const reportingObserver = new ReportingObserver(function() {}, {143types: ['csp-violation'],144buffered: true,145});146147// Calling .observe() is necessary otherwise takeRecords() will not return148// results.149reportingObserver.observe();150const reports = reportingObserver.takeRecords();151reportingObserver.disconnect();152return reports;153};154155156/**157* Formats the given list of reports as a string.158*159* @param {!Array<!Report>} reports160* @return {string}161*/162goog.testing.CspViolationObserver.formatReports = function(reports) {163return reports164.map(function(report) {165return JSON.stringify(report.body.toJSON(), null, ' ');166})167.join(', ');168};169170171