@@ -18,7 +18,12 @@ export class FrameManager {
18
18
private frameAllowAttrObserver : MutationObserver ;
19
19
private frames = new Map <
20
20
HTMLIFrameElement ,
21
- { frameId : string | null ; requestIds : string [ ] }
21
+ {
22
+ frameId : string | null ;
23
+ requestIds : string [ ] ;
24
+ allowAttribute : string ;
25
+ origin : string ;
26
+ }
22
27
> ( ) ;
23
28
24
29
constructor ( { window, document, logger, message } : Cradle ) {
@@ -78,9 +83,21 @@ export class FrameManager {
78
83
}
79
84
const hasTarget = this . frames . has ( target ) ;
80
85
const typeSpecified =
81
- target instanceof HTMLIFrameElement && target . allow === 'monetization' ;
86
+ target instanceof HTMLIFrameElement &&
87
+ target . allow . includes ( 'monetization' ) ;
82
88
89
+ let allowedByPermissionsPolicy = false ;
83
90
if ( ! hasTarget && typeSpecified ) {
91
+ const res = await this . message . send ( 'IS_MONETIZATION_ALLOWED' , {
92
+ allowAttribute : target . allow ,
93
+ origin : new URL ( target . src , location . origin ) . origin ,
94
+ } ) ;
95
+ if ( res . success && res . payload ) {
96
+ allowedByPermissionsPolicy = true ;
97
+ }
98
+ }
99
+
100
+ if ( ! hasTarget && typeSpecified && allowedByPermissionsPolicy ) {
84
101
await this . onAddedFrame ( target ) ;
85
102
handledTags . add ( target ) ;
86
103
} else if ( hasTarget && ! typeSpecified ) {
@@ -97,6 +114,8 @@ export class FrameManager {
97
114
this . frames . set ( frame , {
98
115
frameId : null ,
99
116
requestIds : [ ] ,
117
+ allowAttribute : frame . allow ,
118
+ origin : new URL ( frame . src , location . origin ) . origin ,
100
119
} ) ;
101
120
}
102
121
@@ -181,7 +200,7 @@ export class FrameManager {
181
200
private bindMessageHandler ( ) {
182
201
this . window . addEventListener (
183
202
'message' ,
184
- ( event : MessageEvent < ContentToContentMessage > ) => {
203
+ async ( event : MessageEvent < ContentToContentMessage > ) => {
185
204
const { message, payload, id } = event . data ;
186
205
if ( ! HANDLED_MESSAGES . includes ( message ) ) {
187
206
return ;
@@ -201,15 +220,26 @@ export class FrameManager {
201
220
this . frames . set ( frame , {
202
221
frameId : id ,
203
222
requestIds : [ ] ,
223
+ allowAttribute : frame . allow ,
224
+ origin : new URL ( frame . src , location . origin ) . origin ,
204
225
} ) ;
205
226
return ;
206
227
207
- case 'IS_MONETIZATION_ALLOWED_ON_START' :
228
+ case 'IS_MONETIZATION_ALLOWED_ON_START' : {
208
229
event . stopPropagation ( ) ;
209
- if ( frame . allow === 'monetization' ) {
230
+ const permissionsPolicyPayload = {
231
+ allowAttribute : frame . allow ,
232
+ origin : new URL ( frame . src , location . origin ) . origin ,
233
+ } ;
234
+ const res = await this . message . send (
235
+ 'IS_MONETIZATION_ALLOWED' ,
236
+ permissionsPolicyPayload ,
237
+ ) ;
238
+ if ( res . success && res . payload ) {
210
239
this . frames . set ( frame , {
211
240
frameId : id ,
212
241
requestIds : payload . map ( ( p ) => p . requestId ) ,
242
+ ...permissionsPolicyPayload ,
213
243
} ) ;
214
244
eventSource . postMessage (
215
245
{ message : 'START_MONETIZATION' , id, payload } ,
@@ -218,20 +248,31 @@ export class FrameManager {
218
248
}
219
249
220
250
return ;
251
+ }
221
252
222
- case 'IS_MONETIZATION_ALLOWED_ON_RESUME' :
253
+ case 'IS_MONETIZATION_ALLOWED_ON_RESUME' : {
223
254
event . stopPropagation ( ) ;
224
- if ( frame . allow === 'monetization' ) {
255
+ const permissionsPolicyPayload = {
256
+ allowAttribute : frame . allow ,
257
+ origin : new URL ( frame . src , location . origin ) . origin ,
258
+ } ;
259
+ const res = await this . message . send (
260
+ 'IS_MONETIZATION_ALLOWED' ,
261
+ permissionsPolicyPayload ,
262
+ ) ;
263
+ if ( res . success && res . payload ) {
225
264
this . frames . set ( frame , {
226
265
frameId : id ,
227
266
requestIds : payload . map ( ( p ) => p . requestId ) ,
267
+ ...permissionsPolicyPayload ,
228
268
} ) ;
229
269
eventSource . postMessage (
230
270
{ message : 'RESUME_MONETIZATION' , id, payload } ,
231
271
'*' ,
232
272
) ;
233
273
}
234
274
return ;
275
+ }
235
276
236
277
default :
237
278
return ;
0 commit comments