-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from 1 commit
3e9ece0
45afadd
07cd99a
cfc1810
4d74a0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
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() { | ||
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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. geolocation uses There was a problem hiding this comment. Choose a reason for hiding this commentThe 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- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = NotificationOnStart; |
There was a problem hiding this comment.
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 whateverThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do