From 0522ab8fcd2d5e91f91fd698a023d399189d6d3d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sat, 12 May 2018 13:18:43 -0600 Subject: [PATCH 1/3] Expose the requestId fully in the toWidget postMessage API This field is flagged as required in the proposal. Addresses part of https://github.com/vector-im/riot-web/issues/6708 Signed-off-by: Travis Ralston --- src/ToWidgetPostMessageApi.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ToWidgetPostMessageApi.js b/src/ToWidgetPostMessageApi.js index ccaa0207c1..def4af56ae 100644 --- a/src/ToWidgetPostMessageApi.js +++ b/src/ToWidgetPostMessageApi.js @@ -51,11 +51,11 @@ export default class ToWidgetPostMessageApi { if (payload.response === undefined) { return; } - const promise = this._requestMap[payload._id]; + const promise = this._requestMap[payload.requestId]; if (!promise) { return; } - delete this._requestMap[payload._id]; + delete this._requestMap[payload.requestId]; promise.resolve(payload); } @@ -64,21 +64,21 @@ export default class ToWidgetPostMessageApi { targetWindow = targetWindow || window.parent; // default to parent window targetOrigin = targetOrigin || "*"; this._counter += 1; - action._id = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter; + action.requestId = Date.now() + "-" + Math.random().toString(36) + "-" + this._counter; return new Promise((resolve, reject) => { - this._requestMap[action._id] = {resolve, reject}; + this._requestMap[action.requestId] = {resolve, reject}; targetWindow.postMessage(action, targetOrigin); if (this._timeoutMs > 0) { setTimeout(() => { - if (!this._requestMap[action._id]) { + if (!this._requestMap[action.requestId]) { return; } console.error("postMessage request timed out. Sent object: " + JSON.stringify(action), this._requestMap); - this._requestMap[action._id].reject(new Error("Timed out")); - delete this._requestMap[action._id]; + this._requestMap[action.requestId].reject(new Error("Timed out")); + delete this._requestMap[action.requestId]; }, this._timeoutMs); } }); From 1515ca11a8be49e9a000afcc1c94e778faae57b5 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sat, 12 May 2018 13:21:27 -0600 Subject: [PATCH 2/3] Add a warning for widget developers when their postMessage is missing a requestId Signed-off-by: Travis Ralston --- src/FromWidgetPostMessageApi.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/FromWidgetPostMessageApi.js b/src/FromWidgetPostMessageApi.js index 13ffb4a74b..792fd73733 100644 --- a/src/FromWidgetPostMessageApi.js +++ b/src/FromWidgetPostMessageApi.js @@ -116,6 +116,12 @@ export default class FromWidgetPostMessageApi { return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise } + // Although the requestId is required, we don't use it. We'll be nice and process the message + // if the property is missing, but with a warning for widget developers. + if (!event.data.requestId) { + console.warn("fromWidget action '" + event.data.action + "' does not have a requestId"); + } + const action = event.data.action; const widgetId = event.data.widgetId; if (action === 'content_loaded') { From 98da8b35755e2bf00115f3b194de803808795746 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sat, 12 May 2018 13:49:43 -0600 Subject: [PATCH 3/3] Send the widgetId as part of all toWidget requests Addresses part of https://github.com/vector-im/riot-web/issues/6708 Signed-off-by: Travis Ralston --- src/WidgetMessaging.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/WidgetMessaging.js b/src/WidgetMessaging.js index 86eaa0b59b..5b722df65f 100644 --- a/src/WidgetMessaging.js +++ b/src/WidgetMessaging.js @@ -44,6 +44,8 @@ export default class WidgetMessaging { } messageToWidget(action) { + action.widgetId = this.widgetId; // Required to be sent for all outbound requests + return this.toWidget.exec(action, this.target).then((data) => { // Check for errors and reject if found if (data.response === undefined) { // null is valid