[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBW: Audit for notification permissions on start #1064

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ <h2>Do better web tester page</h2>
});
}

function notificationOnStartTest() {
Notification.requestPermission().then(function () {
// noop
});
}

function oldCSSFlexboxTest() {
stampTemplate('old-flexbox-tmpl', document.body);
}
Expand All @@ -243,6 +249,7 @@ <h2>Do better web tester page</h2>
mutationEvenTest();
passiveEventsListenerTest();
geolocationOnStartTest();
notificationOnStartTest();
linksBlockingFirstPaintTest();
noRelOpenLinksTest();
oldCSSFlexboxTest();
Expand All @@ -268,6 +275,9 @@ <h2>Do better web tester page</h2>
if (params.has('geolocation')) {
geolocationOnStartTest();
}
if (params.has('notifications')) {
notificationOnStartTest();
}
if (params.has('linksblockfp')) {
linksBlockingFirstPaintTest();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = [
'no-mutation-events': false,
'no-old-flexbox': false,
'no-websql': false,
'notification-on-start': false,
'script-blocking-first-paint': false,
'uses-passive-event-listeners': false
}
Expand Down
80 changes: 80 additions & 0 deletions lighthouse-core/audits/dobetterweb/notification-on-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @license
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview Audits a page to see if it is requesting usage of the notification API on
* page load. This is often a sign of poor user experience because it lacks context.
*/

'use strict';

const Audit = require('../audit');
const Formatter = require('../../formatters/formatter');

class NotificationOnStart extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'UX',
name: 'notification-on-start',
description: 'Page does not automatically request notification permissions on page load',
helpText: 'Using notifications without context is a poor user experience. Always tie API ' +
'permissions to user interactions.',
requiredArtifacts: ['NotificationOnStart']
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
if (typeof artifacts.NotificationOnStart === 'undefined' ||
artifacts.NotificationOnStart.value === -1) {
let debugString = 'Unknown error with the NotificationOnStart gatherer';
if (typeof artifacts.NotificationOnStart === 'undefined') {
debugString = 'NotificationOnStart gatherer did not run';
} else if (artifacts.NotificationOnStart.debugString) {
debugString = artifacts.NotificationOnStart.debugString;
}

return NotificationOnStart.generateAuditResult({
rawValue: -1,
debugString
});
}

const results = artifacts.NotificationOnStart.usage.map(err => {
return Object.assign({
label: `line: ${err.line}, col: ${err.col}`
}, err);
});

return NotificationOnStart.generateAuditResult({
rawValue: results.length === 0,
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.URLLIST,
value: results
}
});
}

}

module.exports = NotificationOnStart;
5 changes: 5 additions & 0 deletions lighthouse-core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dobetterweb/datenow",
"dobetterweb/document-write",
"dobetterweb/geolocation-on-start",
"dobetterweb/notification-on-start",
"dobetterweb/tags-blocking-first-paint",
"dobetterweb/websql"
]
Expand Down Expand Up @@ -84,6 +85,7 @@
"dobetterweb/no-mutation-events",
"dobetterweb/no-old-flexbox",
"dobetterweb/no-websql",
"dobetterweb/notification-on-start",
"dobetterweb/script-blocking-first-paint",
"dobetterweb/uses-http2",
"dobetterweb/uses-passive-event-listeners"
Expand Down Expand Up @@ -320,6 +322,9 @@
},
"geolocation-on-start": {
"expectedValue": false
},
"notification-on-start": {
"expectedValue": false
}
}
}, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview Captures calls to the notification API on page load.
*/

'use strict';

const Gatherer = require('../gatherer');

/* global navigator */

/* istanbul ignore next */
function queryNotificationPermission() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it would be great to pull this out into driver.queryPermissionStatus or whatever

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

return navigator.permissions.query({name: 'notifications'}).then(result => {
return result.state;
});
}

class NotificationOnStart extends Gatherer {

beforePass(options) {
this.collectNotificationUsage = options.driver.captureFunctionCallSites(
'Notification.requestPermission');
}

afterPass(options) {
return options.driver.evaluateAsync(`(${queryNotificationPermission.toString()}())`)
.then(state => {
if (state === 'granted' || state === 'denied') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add || state === 'denied' to the geolocation audit too? It only checks state === 'granted' atm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

this.artifact = {
value: -1,
debugString: 'Unable to determine if this permission was requested ' +
'on page load because it had already been set. ' +
'Try resetting the permission and run Lighthouse again.'
};
return;
}

return this.collectNotificationUsage().then(results => {
this.artifact.usage = results;
});
}).catch(err => {
this.artifact = {
value: -1,
debugString: err && err.toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geolocation uses e.message. Can we make them consistent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, do we want to standardize on message and not include the error name then? would have benefit of accepting non-Error objects I suppose too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err.toString() might be more noise in the html report. Not sure how that will look.

};
});
}
}

module.exports = NotificationOnStart;