import { MessageListener } from '@wirewheel/cmp/src/entities/MessageListener' import { Message } from '@wirewheel/cmp/src/entities/Message' import { MessageType } from '@wirewheel/cmp/src/entities/MessageType' import { isChildWindow } from '@wirewheel/cmp/src/utils/isChildWindow' import { MessageService } from '@wirewheel/cmp/src/modules/MessageService' import { Subject } from '@wirewheel/cmp/src/entities/Subject' import { Action } from '@wirewheel/cmp/src/entities/Action' import { PersistentStore } from '@wirewheel/cmp/src/modules/PersistentStore' import { PersistentStoreFactory } from '@wirewheel/cmp/src/modules/PersistentStoreFactory' import { CookieConfig } from '@wirewheel/cmp/src/entities/CookieConfig' import { ChildEmbeddedConfig } from '@wirewheel/cmp/src/entities/ChildEmbeddedConfig' /** Interval to wait for the child to be ready, in milliseconds. */ const CHILD_WAIT_INTERVAL = 250 /** Timeout to stop waiting for the child window, in milliseconds. */ const CHILD_WAIT_TIMEOUT = 1000000 export class EmbeddedHandler { private childLoaded = false static createFromParent(targetIframe: HTMLIFrameElement, cookieConfig?: CookieConfig): EmbeddedHandler { const messageService = MessageService.createFromParent(targetIframe) const handler = new EmbeddedHandler(messageService, cookieConfig) handler.initParent() return handler } static createFromChild(childEmbeddedConfig?: ChildEmbeddedConfig): EmbeddedHandler { const messageService = MessageService.createFromChild() const handler = new EmbeddedHandler(messageService, childEmbeddedConfig?.cookieConfig) handler.initChild() return handler } private readonly store: PersistentStore constructor(private readonly messageService: MessageService, private readonly cookieConfig?: CookieConfig) { this.store = PersistentStoreFactory.getStore(cookieConfig) } on(type: MessageType, listener: MessageListener) { this.messageService.on(type, listener) } send(message: Message) { this.messageService.send(message) } login(subject: Subject) { this.messageService.send({ type: MessageType.LOGIN, data: { subject } }) } logout(subject: Subject) { this.messageService.send({ type: MessageType.LOGOUT, data: { subject } }) } createConsent(actions: Action[]) { this.messageService.send({ type: MessageType.CONSENT_WRITE, data: { actions } }) } initParent() { if (isChildWindow()) { console.log('invalid call to initParent(), this is a child window') return } // Listen events in child application this.messageService.on(MessageType.LOGIN, (message) => { const { subject } = message.data this.store.setSubject(subject) }) this.messageService.on(MessageType.LOGOUT, (message) => { const { subject } = message.data this.store.setSubject(subject) }) this.messageService.on(MessageType.CHILD_LOADED, (message) => { if (!this.childLoaded) { this.store.setSubject(message.data.subject) this.childLoaded = true } }) const queryString = window.location.search const params = new URLSearchParams(queryString) const sessionId = params.get('sessionId') setTimeout(this.sendHandshake.bind(this, undefined, sessionId), CHILD_WAIT_INTERVAL) } initChild(): void { if (!isChildWindow()) { console.log('invalid call to initChild(), this is a top level window') return } this.messageService.on(MessageType.CHILD_INIT, (message) => { if (message.data.subject) { this.store.setSubject(message.data.subject) } this.store.setParentDomain(message.data.parentDomain) this.messageService.send({ type: MessageType.CHILD_LOADED, data: { subject: this.store.getSubject(), }, }) }) } getParentDomain(): string | undefined { return this.store.getParentDomain() } private sendHandshake(start: number = Date.now(), sessionId?: string | null) { const elapsed: number = Date.now() - start if (!this.childLoaded) { if (elapsed > CHILD_WAIT_TIMEOUT) { throw new Error('no answer from child window, embedded client initialization failed') } // Overrides the subject in the child app, if any this.messageService.send({ type: MessageType.CHILD_INIT, data: { subject: this.store.getSubject(), parentDomain: window.location.href, sessionId: sessionId ?? undefined, }, }) setTimeout(() => this.sendHandshake(start, sessionId), CHILD_WAIT_INTERVAL) } } }