Continuation

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2020-09-28 10:50:31 +01:00
parent e3b0bf1915
commit 30d2e61a0d
3 changed files with 46 additions and 40 deletions

View file

@ -259,6 +259,7 @@ export default class FromWidgetPostMessageApi {
sendResponse(event, res) { sendResponse(event, res) {
const data = objectClone(event.data); const data = objectClone(event.data);
data.response = res; data.response = res;
if (!event.source) return; // source may have gone away since
event.source.postMessage(data, event.origin); event.source.postMessage(data, event.origin);
} }

View file

@ -83,9 +83,11 @@ export default class ModalWidgetDialog extends React.PureComponent<IProps, IStat
public render() { public render() {
// TODO: Don't violate every single security principle // TODO: Don't violate every single security principle
const widgetId = encodeURIComponent(this.getWidgetId()); const widgetUrl = new URL(this.props.widgetDefinition.url);
const parentUrl = encodeURIComponent(window.location.href); // TODO: Replace these with proper widget params
const widgetUrl = `${this.props.widgetDefinition.url}?widgetId=${widgetId}&parentUrl=${parentUrl}`; // See https://github.com/matrix-org/matrix-doc/pull/1958/files#r405714833
widgetUrl.searchParams.set("widgetId", this.getWidgetId());
widgetUrl.searchParams.set("parentUrl", window.location.href);
let buttons; let buttons;
if (this.props.widgetDefinition.buttons) { if (this.props.widgetDefinition.buttons) {
@ -124,10 +126,10 @@ export default class ModalWidgetDialog extends React.PureComponent<IProps, IStat
<div> <div>
<iframe <iframe
ref={this.appFrame} ref={this.appFrame}
sandbox="allow-forms allow-scripts" // sandbox="allow-forms allow-scripts"
width={700} // TODO width={700} // TODO
height={450} // TODO height={450} // TODO
src={widgetUrl} src={widgetUrl.toString()}
onLoad={this.onLoad} onLoad={this.onLoad}
/> />
</div> </div>

View file

@ -126,44 +126,47 @@ export class WidgetApi extends EventEmitter {
if (payload.api === WidgetApiType.ToWidget && payload.action) { if (payload.api === WidgetApiType.ToWidget && payload.action) {
console.log(`[WidgetAPI] Got request: ${JSON.stringify(payload)}`); console.log(`[WidgetAPI] Got request: ${JSON.stringify(payload)}`);
if (payload.action === KnownWidgetActions.GetCapabilities) { switch (payload.action) {
this.onCapabilitiesRequest(<ToWidgetRequest>payload); case KnownWidgetActions.GetCapabilities:
if (!this.expectingExplicitReady) { this.onCapabilitiesRequest(<ToWidgetRequest>payload);
this.readyPromiseResolve(); if (!this.expectingExplicitReady) {
} this.readyPromiseResolve();
} else if (payload.action === KnownWidgetActions.ClientReady) { }
this.readyPromiseResolve(); break;
// Automatically acknowledge so we can move on case KnownWidgetActions.ClientReady:
this.replyToRequest(<ToWidgetRequest>payload, {}); this.readyPromiseResolve();
} else if (payload.action === KnownWidgetActions.Terminate) {
// Finalization needs to be async, so postpone with a promise // Automatically acknowledge so we can move on
let finalizePromise = Promise.resolve();
const wait = (promise) => {
finalizePromise = finalizePromise.then(() => promise);
};
this.emit('terminate', wait);
Promise.resolve(finalizePromise).then(() => {
// Acknowledge that we're shut down now
this.replyToRequest(<ToWidgetRequest>payload, {}); this.replyToRequest(<ToWidgetRequest>payload, {});
}); break;
} else if (payload.action === KnownWidgetActions.ReceiveOpenIDCredentials) {
// Save OpenID credentials case KnownWidgetActions.ReceiveOpenIDCredentials:
this.setOpenIDCredentials(<ToWidgetRequest>payload); // Save OpenID credentials
this.replyToRequest(<ToWidgetRequest>payload, {}); this.setOpenIDCredentials(<ToWidgetRequest>payload);
} else if (payload.action === KnownWidgetActions.GetWidgetConfig) {
// Finalization needs to be async, so postpone with a promise
let finalizePromise = Promise.resolve();
const wait = (promise) => {
finalizePromise = finalizePromise.then(() => promise);
};
this.emit(payload.action, payload, wait);
Promise.resolve(finalizePromise).then(() => {
// Acknowledge that we're shut down now
this.replyToRequest(<ToWidgetRequest>payload, {}); this.replyToRequest(<ToWidgetRequest>payload, {});
}); break;
} else {
console.warn(`[WidgetAPI] Got unexpected action: ${payload.action}`); // Ack, handle by caller
case KnownWidgetActions.Terminate:
case KnownWidgetActions.ButtonClicked:
case KnownWidgetActions.GetWidgetConfig:
case KnownWidgetActions.CloseModalWidget: {
// Finalization needs to be async, so postpone with a promise
let finalizePromise = Promise.resolve();
const wait = (promise) => {
finalizePromise = finalizePromise.then(() => promise);
};
this.emit(payload.action, payload, wait);
Promise.resolve(finalizePromise).then(() => {
// Acknowledge that we're shut down now
this.replyToRequest(<ToWidgetRequest>payload, {});
});
break;
}
default:
console.warn(`[WidgetAPI] Got unexpected action: ${payload.action}`);
} }
} else if (payload.api === WidgetApiType.FromWidget && this.inFlightRequests[payload.requestId]) { } else if (payload.api === WidgetApiType.FromWidget && this.inFlightRequests[payload.requestId]) {
console.log(`[WidgetAPI] Got reply: ${JSON.stringify(payload)}`); console.log(`[WidgetAPI] Got reply: ${JSON.stringify(payload)}`);