Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
siibra-explorer
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Xiao Gui
siibra-explorer
Commits
e88b9af5
Unverified
Commit
e88b9af5
authored
4 years ago
by
xgui3783
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
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
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/messaging/module.ts
+59
-33
59 additions, 33 deletions
src/messaging/module.ts
with
59 additions
and
33 deletions
src/messaging/module.ts
+
59
−
33
View file @
e88b9af5
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
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment