2017-11-30 02:11:03 +08:00
|
|
|
/*
|
2017-11-30 18:20:29 +08:00
|
|
|
Copyright 2017 New Vector Ltd
|
2017-11-30 02:11:03 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-12-01 22:44:14 +08:00
|
|
|
/*
|
2018-01-04 17:53:26 +08:00
|
|
|
POSTMESSAGE API
|
|
|
|
===============
|
|
|
|
|
2017-12-01 22:44:14 +08:00
|
|
|
Listens for incoming postMessage requests from embedded widgets. The following API is exposed:
|
|
|
|
{
|
2017-12-01 23:56:30 +08:00
|
|
|
api: "widget",
|
|
|
|
action: "content_loaded",
|
2017-12-05 02:06:05 +08:00
|
|
|
widgetId: $WIDGET_ID,
|
|
|
|
data: {}
|
2017-12-01 23:56:30 +08:00
|
|
|
// additional request fields
|
2017-12-01 22:44:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
The complete request object is returned to the caller with an additional "response" key like so:
|
|
|
|
{
|
2017-12-01 23:56:30 +08:00
|
|
|
api: "widget",
|
|
|
|
action: "content_loaded",
|
2017-12-05 02:06:05 +08:00
|
|
|
widgetId: $WIDGET_ID,
|
|
|
|
data: {},
|
2017-12-01 23:56:30 +08:00
|
|
|
// additional request fields
|
2017-12-01 22:44:14 +08:00
|
|
|
response: { ... }
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:56:30 +08:00
|
|
|
The "api" field is required to use this API, and must be set to "widget" in all requests.
|
|
|
|
|
2017-12-01 22:44:14 +08:00
|
|
|
The "action" determines the format of the request and response. All actions can return an error response.
|
2017-12-01 22:56:27 +08:00
|
|
|
|
2018-01-04 17:53:26 +08:00
|
|
|
Additional data can be sent as abritrary fields. However, typically the data object should be used.
|
2017-12-05 02:06:05 +08:00
|
|
|
|
2017-12-01 22:56:27 +08:00
|
|
|
A success response is an object with zero or more keys.
|
|
|
|
|
2017-12-01 22:44:14 +08:00
|
|
|
An error response is a "response" object which consists of a sole "error" key to indicate an error.
|
|
|
|
They look like:
|
|
|
|
{
|
|
|
|
error: {
|
|
|
|
message: "Unable to invite user into room.",
|
|
|
|
_error: <Original Error Object>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
The "message" key should be a human-friendly string.
|
|
|
|
|
2018-01-04 17:53:26 +08:00
|
|
|
ACTIONS
|
|
|
|
=======
|
2017-12-20 01:11:36 +08:00
|
|
|
** All actions must include an "api" field with value of "widget".**
|
2017-12-01 22:44:14 +08:00
|
|
|
All actions can return an error response instead of the response outlined below.
|
|
|
|
|
|
|
|
content_loaded
|
|
|
|
--------------
|
|
|
|
Indicates that widget contet has fully loaded
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- widgetId is the unique ID of the widget instance in riot / matrix state.
|
|
|
|
- No additional fields.
|
|
|
|
Response:
|
|
|
|
{
|
2017-12-20 01:11:36 +08:00
|
|
|
success: "true"
|
2017-12-01 22:44:14 +08:00
|
|
|
}
|
|
|
|
Example:
|
|
|
|
{
|
2017-12-01 23:56:30 +08:00
|
|
|
api: "widget",
|
|
|
|
action: "content_loaded",
|
2017-12-01 22:44:14 +08:00
|
|
|
widgetId: $WIDGET_ID
|
|
|
|
}
|
2017-12-01 23:56:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
api_version
|
|
|
|
-----------
|
|
|
|
Get the current version of the widget postMessage API
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- No additional fields.
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
api_version: "0.0.1"
|
|
|
|
}
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
api: "widget",
|
|
|
|
action: "api_version",
|
|
|
|
}
|
|
|
|
|
|
|
|
supported_api_versions
|
|
|
|
----------------------
|
|
|
|
Get versions of the widget postMessage API that are currently supported
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- No additional fields.
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
api: "widget"
|
|
|
|
supported_versions: ["0.0.1"]
|
|
|
|
}
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
api: "widget",
|
|
|
|
action: "supported_api_versions",
|
|
|
|
}
|
|
|
|
|
2017-12-16 00:39:04 +08:00
|
|
|
|
2018-01-04 17:53:26 +08:00
|
|
|
OUTBOUND POSTMESSAGE API
|
|
|
|
========================
|
|
|
|
|
|
|
|
This API can be used to initiate actions in remote widget instances.
|
2017-12-16 00:39:04 +08:00
|
|
|
|
2018-01-04 17:53:26 +08:00
|
|
|
ACTIONS
|
|
|
|
=======
|
2017-12-16 00:39:04 +08:00
|
|
|
|
|
|
|
Outbound actions use the "widget_client" API key / name, which must be included
|
|
|
|
on all requests.
|
|
|
|
|
|
|
|
{
|
|
|
|
api: "widget_client",
|
|
|
|
action: "screenshot",
|
|
|
|
widgetId: $WIDGET_ID,
|
|
|
|
data: {}
|
|
|
|
// additional request fields
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
screenshot
|
|
|
|
----------
|
|
|
|
|
|
|
|
Request a screenshot from the widget (if supported).
|
2017-12-29 01:08:50 +08:00
|
|
|
This can currently only be supported by widgets that have access to all of their DOM tree.
|
2018-01-04 17:53:26 +08:00
|
|
|
For example, widgets that nest further levels of iframes can not support this capability.
|
2017-12-16 00:39:04 +08:00
|
|
|
|
2017-12-20 01:11:36 +08:00
|
|
|
The screenshot is returned as a Blob object.
|
|
|
|
|
2017-12-16 00:39:04 +08:00
|
|
|
Request:
|
|
|
|
- No additional fields.
|
|
|
|
Response:
|
|
|
|
{
|
2017-12-20 01:11:36 +08:00
|
|
|
screenshot: {<Blob>data...}
|
2017-12-16 00:39:04 +08:00
|
|
|
}
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
api: "widget_client",
|
|
|
|
action: "screenshot",
|
|
|
|
widgetId: $WIDGET_ID
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:44:14 +08:00
|
|
|
*/
|
|
|
|
|
2017-12-05 08:08:17 +08:00
|
|
|
import URL from 'url';
|
2017-12-15 23:24:22 +08:00
|
|
|
import dis from './dispatcher';
|
|
|
|
import MatrixPostMessageApi from './MatrixPostMessageApi';
|
2017-12-16 05:36:02 +08:00
|
|
|
import Promise from 'bluebird';
|
2017-12-05 08:08:17 +08:00
|
|
|
|
2017-12-01 23:56:30 +08:00
|
|
|
const WIDGET_API_VERSION = '0.0.1'; // Current API version
|
|
|
|
const SUPPORTED_WIDGET_API_VERSIONS = [
|
|
|
|
'0.0.1',
|
|
|
|
];
|
|
|
|
|
2017-12-05 01:54:00 +08:00
|
|
|
if (!global.mxWidgetMessagingListenerCount) {
|
|
|
|
global.mxWidgetMessagingListenerCount = 0;
|
|
|
|
}
|
|
|
|
if (!global.mxWidgetMessagingMessageEndpoints) {
|
|
|
|
global.mxWidgetMessagingMessageEndpoints = [];
|
|
|
|
}
|
2017-11-30 19:30:30 +08:00
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
export default class WidgetMessaging extends MatrixPostMessageApi {
|
2017-12-29 22:34:52 +08:00
|
|
|
constructor(widgetId, targetWindow) {
|
2017-12-15 23:24:22 +08:00
|
|
|
super(targetWindow);
|
2017-12-29 22:34:52 +08:00
|
|
|
this.widgetId = widgetId;
|
2017-12-02 00:17:18 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
exec(action) {
|
|
|
|
return super.exec(action).then((data) => {
|
|
|
|
// check for errors and reject if found
|
|
|
|
if (data.response === undefined) { // null is valid
|
|
|
|
throw new Error("Missing 'response' field");
|
|
|
|
}
|
|
|
|
if (data.response && data.response.error) {
|
|
|
|
const err = data.response.error;
|
|
|
|
const msg = String(err.message ? err.message : "An error was returned");
|
|
|
|
if (err._error) {
|
|
|
|
console.error(err._error);
|
|
|
|
}
|
|
|
|
// Potential XSS attack if 'msg' is not appropriately sanitized,
|
|
|
|
// as it is untrusted input by our parent window (which we assume is Riot).
|
|
|
|
// We can't aggressively sanitize [A-z0-9] since it might be a translation.
|
|
|
|
throw new Error(msg);
|
|
|
|
}
|
|
|
|
// return the response field for the request
|
|
|
|
return data.response;
|
|
|
|
});
|
2017-12-02 00:17:18 +08:00
|
|
|
}
|
2017-12-15 23:24:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register widget message event listeners
|
|
|
|
*/
|
|
|
|
startListening() {
|
2017-12-16 05:36:02 +08:00
|
|
|
this.start();
|
2017-12-15 23:24:22 +08:00
|
|
|
if (global.mxWidgetMessagingListenerCount === 0) {
|
2018-01-05 02:43:08 +08:00
|
|
|
window.addEventListener("message", (evt) => this.onMessage(evt), false);
|
2017-12-15 23:24:22 +08:00
|
|
|
}
|
|
|
|
global.mxWidgetMessagingListenerCount += 1;
|
2017-12-02 00:17:18 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* De-register widget message event listeners
|
|
|
|
*/
|
|
|
|
stopListening() {
|
2017-12-16 05:36:02 +08:00
|
|
|
this.stop();
|
2017-12-15 23:24:22 +08:00
|
|
|
global.mxWidgetMessagingListenerCount -= 1;
|
|
|
|
if (global.mxWidgetMessagingListenerCount === 0) {
|
2017-12-16 00:55:33 +08:00
|
|
|
window.removeEventListener("message", () => this.onMessage);
|
2017-12-15 23:24:22 +08:00
|
|
|
}
|
|
|
|
if (global.mxWidgetMessagingListenerCount < 0) {
|
|
|
|
// Make an error so we get a stack trace
|
|
|
|
const e = new Error(
|
|
|
|
"WidgetMessaging: mismatched startListening / stopListening detected." +
|
|
|
|
" Negative count",
|
|
|
|
);
|
|
|
|
console.error(e);
|
|
|
|
}
|
2017-12-05 08:08:17 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* Register a widget endpoint for trusted postMessage communication
|
|
|
|
* @param {string} widgetId Unique widget identifier
|
|
|
|
* @param {string} endpointUrl Widget wurl origin (protocol + (optional port) + host)
|
|
|
|
*/
|
|
|
|
addEndpoint(widgetId, endpointUrl) {
|
|
|
|
const u = URL.parse(endpointUrl);
|
|
|
|
if (!u || !u.protocol || !u.host) {
|
|
|
|
console.warn("Invalid origin:", endpointUrl);
|
2017-12-02 00:17:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
const origin = u.protocol + '//' + u.host;
|
|
|
|
const endpoint = new WidgetMessageEndpoint(widgetId, origin);
|
|
|
|
if (global.mxWidgetMessagingMessageEndpoints) {
|
|
|
|
if (global.mxWidgetMessagingMessageEndpoints.some(function(ep) {
|
|
|
|
return (ep.widgetId === widgetId && ep.endpointUrl === endpointUrl);
|
|
|
|
})) {
|
|
|
|
// Message endpoint already registered
|
|
|
|
console.warn("Endpoint already registered");
|
|
|
|
return;
|
2018-01-05 02:41:47 +08:00
|
|
|
} else {
|
2018-01-05 06:38:52 +08:00
|
|
|
// console.warn(`Adding widget messaging endpoint for ${widgetId}`);
|
2018-01-05 02:41:47 +08:00
|
|
|
global.mxWidgetMessagingMessageEndpoints.push(endpoint);
|
2017-12-15 23:24:22 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-05 08:08:17 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* De-register a widget endpoint from trusted communication sources
|
|
|
|
* @param {string} widgetId Unique widget identifier
|
|
|
|
* @param {string} endpointUrl Widget wurl origin (protocol + (optional port) + host)
|
|
|
|
* @return {boolean} True if endpoint was successfully removed
|
|
|
|
*/
|
|
|
|
removeEndpoint(widgetId, endpointUrl) {
|
|
|
|
const u = URL.parse(endpointUrl);
|
|
|
|
if (!u || !u.protocol || !u.host) {
|
|
|
|
console.warn("Invalid origin");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const origin = u.protocol + '//' + u.host;
|
|
|
|
if (global.mxWidgetMessagingMessageEndpoints && global.mxWidgetMessagingMessageEndpoints.length > 0) {
|
|
|
|
const length = global.mxWidgetMessagingMessageEndpoints.length;
|
|
|
|
global.mxWidgetMessagingMessageEndpoints = global.mxWidgetMessagingMessageEndpoints.
|
|
|
|
filter(function(endpoint) {
|
|
|
|
return (endpoint.widgetId != widgetId || endpoint.endpointUrl != origin);
|
|
|
|
});
|
|
|
|
return (length > global.mxWidgetMessagingMessageEndpoints.length);
|
|
|
|
}
|
|
|
|
return false;
|
2017-12-02 00:17:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* Handle widget postMessage events
|
|
|
|
* @param {Event} event Event to handle
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
onMessage(event) {
|
|
|
|
if (!event.origin) { // Handle chrome
|
|
|
|
event.origin = event.originalEvent.origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event origin is empty string if undefined
|
|
|
|
if (
|
|
|
|
event.origin.length === 0 ||
|
|
|
|
!this.trustedEndpoint(event.origin) ||
|
|
|
|
event.data.api !== "widget" ||
|
|
|
|
!event.data.widgetId
|
|
|
|
) {
|
|
|
|
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
|
|
|
|
}
|
2017-11-30 02:11:03 +08:00
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
const action = event.data.action;
|
|
|
|
const widgetId = event.data.widgetId;
|
|
|
|
if (action === 'content_loaded') {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'widget_content_loaded',
|
|
|
|
widgetId: widgetId,
|
|
|
|
});
|
|
|
|
this.sendResponse(event, {success: true});
|
|
|
|
} else if (action === 'supported_api_versions') {
|
|
|
|
this.sendResponse(event, {
|
|
|
|
api: "widget",
|
|
|
|
supported_versions: SUPPORTED_WIDGET_API_VERSIONS,
|
|
|
|
});
|
|
|
|
} else if (action === 'api_version') {
|
|
|
|
this.sendResponse(event, {
|
|
|
|
api: "widget",
|
|
|
|
version: WIDGET_API_VERSION,
|
|
|
|
});
|
2018-01-05 02:41:47 +08:00
|
|
|
} else if (action === 'sticker_message') {
|
2018-01-05 02:58:55 +08:00
|
|
|
dis.dispatch({action: 'sticker_message', data: event.data.widgetData, widgetId: event.data.widgetId});
|
2017-12-15 23:24:22 +08:00
|
|
|
} else {
|
|
|
|
console.warn("Widget postMessage event unhandled");
|
|
|
|
this.sendError(event, {message: "The postMessage was unhandled"});
|
|
|
|
}
|
2017-11-30 18:20:29 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* Check if message origin is registered as trusted
|
|
|
|
* @param {string} origin PostMessage origin to check
|
|
|
|
* @return {boolean} True if trusted
|
|
|
|
*/
|
|
|
|
trustedEndpoint(origin) {
|
|
|
|
if (!origin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return global.mxWidgetMessagingMessageEndpoints.some((endpoint) => {
|
|
|
|
return endpoint.endpointUrl === origin;
|
2017-12-01 23:56:30 +08:00
|
|
|
});
|
2017-11-30 20:26:40 +08:00
|
|
|
}
|
2017-11-30 18:20:29 +08:00
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* Send a postmessage response to a postMessage request
|
|
|
|
* @param {Event} event The original postMessage request event
|
|
|
|
* @param {Object} res Response data
|
|
|
|
*/
|
|
|
|
sendResponse(event, res) {
|
|
|
|
const data = JSON.parse(JSON.stringify(event.data));
|
|
|
|
data.response = res;
|
|
|
|
event.source.postMessage(data, event.origin);
|
2017-11-30 19:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
/**
|
|
|
|
* Send an error response to a postMessage request
|
|
|
|
* @param {Event} event The original postMessage request event
|
|
|
|
* @param {string} msg Error message
|
|
|
|
* @param {Error} nestedError Nested error event (optional)
|
|
|
|
*/
|
|
|
|
sendError(event, msg, nestedError) {
|
|
|
|
console.error("Action:" + event.data.action + " failed with message: " + msg);
|
|
|
|
const data = JSON.parse(JSON.stringify(event.data));
|
|
|
|
data.response = {
|
|
|
|
error: {
|
|
|
|
message: msg,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (nestedError) {
|
|
|
|
data.response.error._error = nestedError;
|
|
|
|
}
|
|
|
|
event.source.postMessage(data, event.origin);
|
2017-12-01 22:56:27 +08:00
|
|
|
}
|
2017-12-16 00:39:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request a screenshot from a widget
|
2017-12-16 05:36:02 +08:00
|
|
|
* @return {Promise} To be resolved when screenshot has been generated
|
2017-12-16 00:39:04 +08:00
|
|
|
*/
|
|
|
|
getScreenshot() {
|
2017-12-16 05:36:02 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.exec({
|
|
|
|
api: "widget_client",
|
|
|
|
action: "screenshot",
|
|
|
|
}).then(function(response) {
|
|
|
|
// console.warn("got screenshot", response.screenshot);
|
|
|
|
resolve(response.screenshot);
|
|
|
|
}).catch((error) => {
|
|
|
|
reject(Error("Failed to get screenshot: " + error.message));
|
|
|
|
});
|
2017-12-16 00:39:04 +08:00
|
|
|
});
|
|
|
|
}
|
2017-12-16 17:16:24 +08:00
|
|
|
|
|
|
|
getCapabilities() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.exec({
|
|
|
|
api: "widget_client",
|
|
|
|
action: "capabilities",
|
|
|
|
}).then(function(response) {
|
|
|
|
// console.warn("got capabilities", response.capabilities);
|
|
|
|
resolve(response.capabilities);
|
|
|
|
}).catch((error) => {
|
|
|
|
reject(Error("Failed to get capabilities: " + error.message));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-12-01 22:56:27 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:24:22 +08:00
|
|
|
|
2017-11-30 19:30:30 +08:00
|
|
|
/**
|
|
|
|
* Represents mapping of widget instance to URLs for trusted postMessage communication.
|
|
|
|
*/
|
|
|
|
class WidgetMessageEndpoint {
|
|
|
|
/**
|
|
|
|
* Mapping of widget instance to URL for trusted postMessage communication.
|
|
|
|
* @param {string} widgetId Unique widget identifier
|
|
|
|
* @param {string} endpointUrl Widget wurl origin.
|
|
|
|
*/
|
|
|
|
constructor(widgetId, endpointUrl) {
|
|
|
|
if (!widgetId) {
|
|
|
|
throw new Error("No widgetId specified in widgetMessageEndpoint constructor");
|
|
|
|
}
|
|
|
|
if (!endpointUrl) {
|
|
|
|
throw new Error("No endpoint specified in widgetMessageEndpoint constructor");
|
|
|
|
}
|
|
|
|
this.widgetId = widgetId;
|
|
|
|
this.endpointUrl = endpointUrl;
|
|
|
|
}
|
|
|
|
}
|