Skip to content
Snippets Groups Projects
Unverified Commit e88b9af5 authored by xgui3783's avatar xgui3783 Committed by GitHub
Browse files

Merge pull request #770 from HumanBrainProject/exmp_postMessage

chore: adding more messaging API
parents f601cce6 b381039e
No related branches found
No related tags found
No related merge requests found
import { NgModule } from "@angular/core";
import { NgModule, Optional } from "@angular/core";
import { MatDialog } from "@angular/material/dialog";
import { AtlasViewerAPIServices } from "src/atlasViewer/atlasViewer.apiService.service";
import { ComponentsModule } from "src/components";
import { ConfirmDialogComponent } from "src/components/confirmDialog/confirmDialog.component";
import { AngularMaterialModule } from "src/ui/sharedModules/angularMaterial.module";
......@@ -19,57 +20,88 @@ export class MesssagingModule{
private pendingRequests: Map<string, Promise<boolean>> = new Map()
constructor(
private dialog: MatDialog
private dialog: MatDialog,
@Optional() private apiService: AtlasViewerAPIServices
){
window.addEventListener('message', async ({ data, origin, source }) => {
const { method, id } = data
const { method, id, param } = data
const src = source as Window
if (!method) return
if (method.indexOf(IAV_POSTMESSAGE_NAMESPACE) !== 0) return
const strippedMethod = method.replace(IAV_POSTMESSAGE_NAMESPACE, '')
switch (strippedMethod) {
case 'ping': {
/**
* if ping method, respond pong method
*/
if (strippedMethod === 'ping') {
window.opener.postMessage({
id,
result: 'pong',
jsonrpc: '2.0'
}, origin)
break
return
}
case 'dummyMethod': {
try {
const result = await this.dummyMethod({ data, origin })
src.postMessage({
id,
result
}, origin)
} catch (e) {
/**
* otherwise, check permission
*/
try {
const allow = await this.checkOrigin({ origin })
if (!allow) {
src.postMessage({
jsonrpc: '2.0',
id,
error: e.code
? e
: { code: 500, message: e.toString() }
error: {
code: 403,
message: 'User declined'
}
}, origin)
return
}
break;
}
default: {
const result = await this.processMessage({ method: strippedMethod, param })
src.postMessage({
jsonrpc: '2.0',
id,
error: {
code: 404,
message: 'Method not found'
}
result
}, origin)
} catch (e) {
src.postMessage({
jsonrpc: '2.0',
id,
error: e.code
? e
: { code: 500, message: e.toString() }
}, origin)
}
}
})
}
async processMessage({ method, param }){
console.log({ method, param })
if (method === 'dummyMethod') {
return 'OK'
}
if (method === 'viewerHandle:add3DLandmarks') {
this.apiService.interactiveViewer.viewerHandle.add3DLandmarks(param)
return 'OK'
}
if (method === 'viewerHandle:remove3DLandmarks') {
this.apiService.interactiveViewer.viewerHandle.remove3DLandmarks(param)
return 'OK'
}
throw ({ code: 404, message: 'Method not found' })
}
async checkOrigin({ origin }){
if (this.whiteListedOrigins.has(origin)) return true
if (this.pendingRequests.has(origin)) return this.pendingRequests.get(origin)
......@@ -89,10 +121,4 @@ export class MesssagingModule{
if (response) this.whiteListedOrigins.add(origin)
return response
}
async dummyMethod({ data, origin }){
const allow = await this.checkOrigin({ origin })
if (!allow) throw ({ code: 403, message: 'User declined' })
return 'OK'
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment