forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amp-web-push-config.js
272 lines (244 loc) · 8.21 KB
/
amp-web-push-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Copyright 2017 The AMP HTML Authors. 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.
*/
import {CONFIG_TAG, SERVICE_TAG, TAG} from './vars';
import {dev, user, userAssert} from '../../../src/log';
import {escapeCssSelectorIdent} from '../../../src/css';
import {getServiceForDoc} from '../../../src/service';
import {parseUrlDeprecated} from '../../../src/url';
/** @enum {string} */
export const WebPushConfigAttributes = {
HELPER_FRAME_URL: 'helper-iframe-url',
PERMISSION_DIALOG_URL: 'permission-dialog-url',
SERVICE_WORKER_URL: 'service-worker-url',
};
/** @enum {string} */
export const WebPushWidgetActions = {
SUBSCRIBE: 'subscribe',
UNSUBSCRIBE: 'unsubscribe',
};
/** @typedef {{
* 'helper-iframe-url': (?string|undefined),
* 'permission-dialog-url': (?string|undefined),
* 'service-worker-url': (?string|undefined),
* }}
*/
export let AmpWebPushConfig;
/**
* @fileoverview
* The element that exposes attributes for publishers to configure the web push
* service.
*
* On buildCallback(), the element starts the web push service.
*
* Only a single element of this kind is allowed in the document, and it must
* have the ID "amp-web-push". Subscribe and unsubscribe actions dispatched from
* various widget elements are all processed by this element which then forwards
* the event to the web push service.
*/
export class WebPushConfig extends AMP.BaseElement {
/** @param {!AmpElement} element */
constructor(element) {
super(element);
}
/**
* Validates that this element instance has an ID attribute of 'amp-web-push'
* and that there are no other elements of the same tag name.
*/
validate() {
this.ensureSpecificElementId_();
this.ensureUniqueElement_();
const config = {
'helper-iframe-url': null,
'permission-dialog-url': null,
'service-worker-url': null,
};
for (const attribute in WebPushConfigAttributes) {
const value = WebPushConfigAttributes[attribute];
userAssert(
this.element.getAttribute(value),
`The ${value} attribute is required for <${CONFIG_TAG}>`
);
config[value] = this.element.getAttribute(value);
}
if (
!this.isValidHelperOrPermissionDialogUrl_(config['helper-iframe-url'])
) {
throw user().createError(
`<${CONFIG_TAG}> must have a valid ` +
'helper-iframe-url attribute. It should begin with ' +
'the https:// protocol and point to the provided lightweight ' +
'template page provided for AMP messaging.'
);
}
if (
!this.isValidHelperOrPermissionDialogUrl_(config['permission-dialog-url'])
) {
throw user().createError(
`<${CONFIG_TAG}> must have a valid ` +
'permission-dialog-url attribute. It should begin with ' +
'the https:// protocol and point to the provided template page ' +
'for showing the permission prompt.'
);
}
if (parseUrlDeprecated(config['service-worker-url']).protocol
!== 'https:') {
throw user().createError(
`<${CONFIG_TAG}> must have a valid ` +
'service-worker-url attribute. It should begin with the ' +
'https:// protocol and point to the service worker JavaScript file ' +
'to be installed.'
);
}
if (
parseUrlDeprecated(config['service-worker-url']).origin !==
parseUrlDeprecated(config['permission-dialog-url']).origin ||
parseUrlDeprecated(config['permission-dialog-url']).origin !==
parseUrlDeprecated(config['helper-iframe-url']).origin
) {
throw user().createError(
`<${CONFIG_TAG}> URL attributes ` +
'service-worker-url, permission-dialog-url, and ' +
'helper-iframe-url must all share the same origin.'
);
}
}
/**
* Parses the JSON configuration and returns a JavaScript object.
* @return {AmpWebPushConfig}
*/
parseConfig() {
const config = {};
for (const attribute in WebPushConfigAttributes) {
const value = WebPushConfigAttributes[attribute];
config[value] = this.element.getAttribute(value);
}
return config;
}
/** @override */
buildCallback() {
this.validate();
const config = this.parseConfig();
const webPushService = getServiceForDoc(this.getAmpDoc(), SERVICE_TAG);
webPushService.start(config).catch(() => {});
this.registerAction(
WebPushWidgetActions.SUBSCRIBE,
this.onSubscribe_.bind(this)
);
this.registerAction(
WebPushWidgetActions.UNSUBSCRIBE,
this.onUnsubscribe_.bind(this)
);
}
/**
* Ensures this element is defined with TAG id.
* @private
*/
ensureSpecificElementId_() {
if (this.element.getAttribute('id') !== TAG) {
throw user().createError(
`<${CONFIG_TAG}> must have an id ` +
"attribute with value '" +
TAG +
"'."
);
}
}
/**
* Ensures there isn't another page element with the same id.
* @private
*/
ensureUniqueElement_() {
const webPushConfigElements = this.getAmpDoc()
.getRootNode()
.querySelectorAll(`#${escapeCssSelectorIdent(CONFIG_TAG)}`);
if (webPushConfigElements.length > 1) {
throw user().createError(
`Only one <${CONFIG_TAG}> element may exist on a page.`
);
}
}
/**
* @param {!../../../src/service/action-impl.ActionInvocation} invocation
* @private
*/
onSubscribe_(invocation) {
// Disable the widget temporarily to prevent multiple clicks The widget will
// be re-enabled when the popup is closed, or the user interacts with the
// prompt
const widget = dev().assertElement(invocation.event.target);
this.setWidgetDisabled_(widget, true);
const webPushService = getServiceForDoc(this.getAmpDoc(), SERVICE_TAG);
webPushService
.subscribe(() => {
// On popup closed
this.setWidgetDisabled_(widget, false);
})
.then(() => {
// On browser notification permission granted, denied, or dismissed
this.setWidgetDisabled_(widget, false);
});
}
/**
*
* @param {!Element} widget
* @param {boolean} isDisabled
* @private
*/
setWidgetDisabled_(widget, isDisabled) {
widget.disabled = isDisabled;
}
/**
* @param {!../../../src/service/action-impl.ActionInvocation} invocation
* @private
*/
onUnsubscribe_(invocation) {
const widget = dev().assertElement(invocation.event.target);
this.setWidgetDisabled_(widget, true);
const webPushService = getServiceForDoc(this.getAmpDoc(), SERVICE_TAG);
webPushService.unsubscribe().then(() => {
this.setWidgetDisabled_(widget, false);
});
}
/**
* @private
* @param {string} url
* @return {boolean}
*/
isValidHelperOrPermissionDialogUrl_(url) {
try {
const parsedUrl = parseUrlDeprecated(url);
/*
The helper-iframe-url must be to a specific lightweight page on the
user's site for handling AMP postMessage calls without loading push
vendor-specific SDKs or other resources. It should not be the site root.
The permission-dialog-url can load push vendor-specific SDKs, but it
should still not be the site root and should be a dedicated page for
subscribing.
*/
const isNotRootUrl = parsedUrl.pathname.length > 1;
/*
Similar to <amp-form> and <amp-iframe>, the helper and subscribe URLs
must be HTTPS. This is because most AMP caches serve pages over HTTPS,
and an HTTP iframe URL would not load due to insecure resources being
blocked on a secure page.
*/
const isSecureUrl = parsedUrl.protocol === 'https:';
return isSecureUrl && isNotRootUrl;
} catch (e) {
return false;
}
}
}