feat: enable reporting api (#18255)
This commit is contained in:
parent
babe2b68fb
commit
f4c792d014
3 changed files with 96 additions and 6 deletions
|
@ -430,25 +430,25 @@ bool AtomNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
|
|||
return false;
|
||||
}
|
||||
|
||||
// TODO(deepak1556) : Enable after hooking into the reporting service
|
||||
// https://crbug.com/704259
|
||||
bool AtomNetworkDelegate::OnCanQueueReportingReport(
|
||||
const url::Origin& origin) const {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnCanSendReportingReports(
|
||||
std::set<url::Origin> origins,
|
||||
base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {}
|
||||
base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
|
||||
std::move(result_callback).Run(std::move(origins));
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanSetReportingClient(const url::Origin& origin,
|
||||
const GURL& endpoint) const {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanUseReportingClient(const url::Origin& origin,
|
||||
const GURL& endpoint) const {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request,
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
#include "net/url_request/url_request_job_factory_impl.h"
|
||||
#include "services/network/ignore_errors_cert_verifier.h"
|
||||
#include "services/network/network_service.h"
|
||||
#include "services/network/public/cpp/features.h"
|
||||
#include "services/network/public/cpp/network_switches.h"
|
||||
#include "services/network/url_request_context_builder_mojo.h"
|
||||
#include "url/url_constants.h"
|
||||
|
@ -55,6 +56,11 @@
|
|||
#include "net/url_request/ftp_protocol_handler.h"
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_REPORTING)
|
||||
#include "net/reporting/reporting_policy.h"
|
||||
#include "net/reporting/reporting_service.h"
|
||||
#endif // BUILDFLAG(ENABLE_REPORTING)
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace atom {
|
||||
|
@ -265,6 +271,18 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|||
// Enable file:// support.
|
||||
builder->set_file_enabled(true);
|
||||
|
||||
#if BUILDFLAG(ENABLE_REPORTING)
|
||||
if (base::FeatureList::IsEnabled(network::features::kReporting)) {
|
||||
auto reporting_policy = net::ReportingPolicy::Create();
|
||||
builder->set_reporting_policy(std::move(reporting_policy));
|
||||
} else {
|
||||
builder->set_reporting_policy(nullptr);
|
||||
}
|
||||
|
||||
builder->set_network_error_logging_enabled(
|
||||
base::FeatureList::IsEnabled(network::features::kNetworkErrorLogging));
|
||||
#endif // BUILDFLAG(ENABLE_REPORTING)
|
||||
|
||||
auto network_delegate = std::make_unique<AtomNetworkDelegate>();
|
||||
network_delegate_ = network_delegate.get();
|
||||
builder->set_network_delegate(std::move(network_delegate));
|
||||
|
|
72
spec-main/chromium-spec.ts
Normal file
72
spec-main/chromium-spec.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import * as chai from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
import { BrowserWindow, session } from 'electron'
|
||||
import { emittedOnce } from './events-helpers';
|
||||
import * as https from 'https';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
const { expect } = chai
|
||||
|
||||
chai.use(chaiAsPromised)
|
||||
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures')
|
||||
|
||||
describe('reporting api', () => {
|
||||
it('sends a report for a deprecation', async () => {
|
||||
const reports = new EventEmitter
|
||||
|
||||
// The Reporting API only works on https with valid certs. To dodge having
|
||||
// to set up a trusted certificate, hack the validator.
|
||||
session.defaultSession.setCertificateVerifyProc((req, cb) => {
|
||||
cb(0)
|
||||
})
|
||||
const certPath = path.join(fixturesPath, 'certificates')
|
||||
const options = {
|
||||
key: fs.readFileSync(path.join(certPath, 'server.key')),
|
||||
cert: fs.readFileSync(path.join(certPath, 'server.pem')),
|
||||
ca: [
|
||||
fs.readFileSync(path.join(certPath, 'rootCA.pem')),
|
||||
fs.readFileSync(path.join(certPath, 'intermediateCA.pem'))
|
||||
],
|
||||
requestCert: true,
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
|
||||
const server = https.createServer(options, (req, res) => {
|
||||
if (req.url === '/report') {
|
||||
let data = ''
|
||||
req.on('data', (d) => data += d.toString('utf-8'))
|
||||
req.on('end', () => {
|
||||
reports.emit('report', JSON.parse(data))
|
||||
})
|
||||
}
|
||||
res.setHeader('Report-To', JSON.stringify({
|
||||
group: 'default',
|
||||
max_age: 120,
|
||||
endpoints: [ {url: `https://localhost:${(server.address() as any).port}/report`} ],
|
||||
}))
|
||||
res.setHeader('Content-Type', 'text/html')
|
||||
// using the deprecated `webkitRequestAnimationFrame` will trigger a
|
||||
// "deprecation" report.
|
||||
res.end('<script>webkitRequestAnimationFrame(() => {})</script>')
|
||||
})
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const bw = new BrowserWindow({
|
||||
show: false,
|
||||
})
|
||||
try {
|
||||
const reportGenerated = emittedOnce(reports, 'report')
|
||||
const url = `https://localhost:${(server.address() as any).port}/a`
|
||||
await bw.loadURL(url)
|
||||
const [report] = await reportGenerated
|
||||
expect(report).to.be.an('array')
|
||||
expect(report[0].type).to.equal('deprecation')
|
||||
expect(report[0].url).to.equal(url)
|
||||
expect(report[0].body.id).to.equal('PrefixedRequestAnimationFrame')
|
||||
} finally {
|
||||
bw.destroy()
|
||||
server.close()
|
||||
}
|
||||
})
|
||||
})
|
Loading…
Add table
Reference in a new issue