Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 1 | /** |
| 2 | * The plugin system is based |
| 3 | * on registered widgets (iframes) from |
| 4 | * foreign services. |
| 5 | * The server component spawns new iframes and |
| 6 | * listens to them. |
| 7 | * |
| 8 | * @author Nils Diewald |
| 9 | */ |
| 10 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 11 | define(["plugin/widget", "util"], function (widgetClass) { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 12 | "use strict"; |
| 13 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 14 | // Contains all widgets to address with |
| 15 | // messages to them |
| 16 | var widgets = {}; |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 17 | var plugins = {}; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 18 | |
| 19 | // TODO: |
| 20 | // These should better be panels and every panel |
| 21 | // has a buttonGroup |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 22 | var buttons = { |
| 23 | match : [] |
| 24 | }; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 25 | var panels = { |
| 26 | match : 1 |
| 27 | }; |
| 28 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 29 | // This is a counter to limit acceptable incoming messages |
| 30 | // to a certain amount. For every message, this counter will |
| 31 | // be decreased (down to 0), for every second this will be |
| 32 | // increased (up to 100). |
| 33 | // Once a widget surpasses the limit, it will be killed |
| 34 | // and called suspicious. |
| 35 | var maxMessages = 100; |
| 36 | var limits = {}; |
| 37 | |
| 38 | // TODO: |
| 39 | // It may be useful to establish a watcher that pings |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 40 | // all widgets every second to see if it is still alive, |
| 41 | // otherwise kill |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 42 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 43 | // Load Plugin server |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 44 | return { |
| 45 | |
| 46 | /** |
| 47 | * Create new plugin management system |
| 48 | */ |
| 49 | create : function () { |
| 50 | return Object.create(this)._init(); |
| 51 | }, |
| 52 | |
| 53 | /* |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 54 | * Initialize the plugin manager |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 55 | */ |
| 56 | _init : function () { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 57 | return this; |
| 58 | }, |
| 59 | |
| 60 | /** |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 61 | * Register a plugin described as a JSON object. |
| 62 | * |
| 63 | * This is work in progress. |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 64 | * |
| 65 | * Example: |
| 66 | * |
| 67 | * KorAP.Plugin.register({ |
| 68 | * 'name' : 'CatContent', |
| 69 | * 'desc' : 'Some content about cats', |
| 70 | * 'about' : 'https://localhost:5678/', |
| 71 | * 'embed' : [{ |
| 72 | * 'panel' : 'match', |
| 73 | * 'title' : loc.TRANSLATE, |
| 74 | * 'classes' : ['translate'] |
| 75 | * 'onClick' : { |
| 76 | * 'action' : 'addWidget', |
| 77 | * 'template' : 'https://localhost:5678/?match={matchid}', |
| 78 | * } |
| 79 | * }] |
| 80 | * }); |
| 81 | * |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 82 | */ |
| 83 | register : function (obj) { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 84 | // TODO: |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 85 | // These fields need to be localized for display by a structure like |
| 86 | // { de : { name : '..' }, en : { .. } } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 87 | var name = obj["name"]; |
| 88 | |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 89 | if (!name) |
| 90 | throw new Error("Missing name of plugin"); |
| 91 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 92 | // Register plugin by name |
| 93 | var plugin = plugins[name] = { |
| 94 | name : name, |
| 95 | desc : obj["desc"], |
| 96 | about : obj["about"], |
| 97 | widgets : [] |
| 98 | }; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 99 | |
| 100 | if (typeof obj["embed"] !== 'object') |
| 101 | throw new Error("Embedding of plugin is no list"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 102 | |
| 103 | // Embed all embeddings of the plugin |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 104 | var that = this; |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 105 | for (var i in obj["embed"]) { |
| 106 | var embed = obj["embed"][i]; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 107 | |
| 108 | if (typeof embed !== 'object') |
| 109 | throw new Error("Embedding of plugin is no object"); |
| 110 | |
| 111 | var panel = embed["panel"]; |
| 112 | |
| 113 | if (!panel || !buttons[panel]) |
| 114 | throw new Error("Panel for plugin is invalid"); |
| 115 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 116 | var onClick = embed["onClick"]; |
| 117 | |
| 118 | // Needs to be localized as well |
| 119 | var title = embed["title"]; |
| 120 | |
| 121 | // The embedding will open a widget |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 122 | if (!onClick["action"] || onClick["action"] == "addWidget") { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 123 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 124 | var cb = function (e) { |
| 125 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 126 | // "this" is bind to the panel |
| 127 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 128 | // Get the URL of the widget |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 129 | var url = onClick["template"]; |
| 130 | // that._interpolateURI(onClick["template"], this.match); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 131 | |
| 132 | // Add the widget to the panel |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 133 | var id = that.addWidget(this, name, url); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 134 | plugin["widgets"].push(id); |
| 135 | }; |
| 136 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 137 | buttons[panel].push([title, embed["classes"], cb]); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 138 | }; |
| 139 | }; |
| 140 | }, |
| 141 | |
| 142 | |
| 143 | // TODO: |
| 144 | // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570 |
| 145 | // but as simple as possible |
| 146 | _interpolateURI : function (uri, obj) { |
| 147 | // ... |
| 148 | }, |
| 149 | |
| 150 | |
| 151 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 152 | * Get named button group - better rename to "action" |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 153 | */ |
| 154 | buttonGroup : function (name) { |
| 155 | return buttons[name]; |
| 156 | }, |
| 157 | |
| 158 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 159 | * Open a new widget view in a certain panel and return |
| 160 | * the id. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 161 | */ |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 162 | addWidget : function (panel, name, src) { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 163 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 164 | if (!src) |
| 165 | return; |
| 166 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 167 | // Is it the first widget? |
| 168 | if (!this._listener) { |
| 169 | |
| 170 | /* |
| 171 | * Establish the global 'message' hook. |
| 172 | */ |
| 173 | this._listener = this._receiveMsg.bind(this); |
| 174 | window.addEventListener("message", this._listener); |
| 175 | |
| 176 | // Every second increase the limits of all registered widgets |
| 177 | this._timer = window.setInterval(function () { |
| 178 | for (var i in limits) { |
| 179 | if (limits[i]++ >= maxMessages) { |
| 180 | limits[i] = maxMessages; |
| 181 | } |
| 182 | } |
| 183 | }, 1000); |
| 184 | }; |
| 185 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 186 | // Create a unique random ID per widget |
| 187 | var id = 'id-' + this._randomID(); |
| 188 | |
| 189 | // Create a new widget |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 190 | var widget = widgetClass.create(name, src, id); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 191 | |
| 192 | // Store the widget based on the identifier |
| 193 | widgets[id] = widget; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 194 | limits[id] = maxMessages; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 195 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 196 | widget._mgr = this; |
| 197 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 198 | // Add widget to panel |
| 199 | panel.add(widget); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 200 | |
| 201 | return id; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 202 | }, |
| 203 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 204 | |
| 205 | /** |
| 206 | * Get widget by identifier |
| 207 | */ |
| 208 | widget : function (id) { |
| 209 | return widgets[id]; |
| 210 | }, |
| 211 | |
| 212 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 213 | // Receive a call from an embedded iframe. |
| 214 | // The handling needs to be very careful, |
| 215 | // as this can easily become a security nightmare. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 216 | _receiveMsg : function (e) { |
| 217 | // Get event data |
| 218 | var d = e.data; |
| 219 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 220 | // If no data given - fail |
| 221 | // (probably check that it's an assoc array) |
| 222 | if (!d) |
| 223 | return; |
| 224 | |
| 225 | // e.origin is probably set and okay - CHECK! |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 226 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 227 | // Get origin ID |
| 228 | var id = d["originID"]; |
| 229 | |
| 230 | // If no origin ID given - fail |
| 231 | if (!id) |
| 232 | return; |
| 233 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 234 | // Get the widget |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 235 | var widget = widgets[id]; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 236 | |
| 237 | // If the addressed widget does not exist - fail |
| 238 | if (!widget) |
| 239 | return; |
| 240 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 241 | // Check for message limits |
| 242 | if (limits[id]-- < 0) { |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 243 | |
| 244 | // Kill widget |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 245 | KorAP.log(0, 'Suspicious action by widget', widget.src); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 246 | |
| 247 | // TODO: |
| 248 | // Potentially kill the whole plugin! |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 249 | |
| 250 | // This removes all connections before closing the widget |
| 251 | this._closeWidget(widget.id); |
| 252 | widget.close(); |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 253 | return; |
| 254 | }; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 255 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 256 | // Resize the iframe |
| 257 | if (d.action === 'resize') { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 258 | widget.resize(d); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Log message from iframe |
| 262 | else if (d.action === 'log') { |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 263 | KorAP.log(d.code, d.msg, widget.src); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | // TODO: |
| 267 | // Close |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 268 | }, |
| 269 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 270 | // Close the widget |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 271 | _closeWidget : function (id) { |
| 272 | delete limits[id]; |
| 273 | delete widgets[id]; |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 274 | |
| 275 | // Remove listeners in case no widget |
| 276 | // is available any longer |
| 277 | if (Object.keys(limits).length == 0) |
| 278 | this._removeListener(); |
| 279 | }, |
| 280 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 281 | // Get a random identifier |
| 282 | _randomID : function () { |
| 283 | return randomID(20); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 284 | }, |
| 285 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 286 | // Remove the listener |
| 287 | _removeListener : function () { |
| 288 | window.clearInterval(this._timer); |
| 289 | this._timer = undefined; |
| 290 | window.removeEventListener("message", this._listener); |
| 291 | this._listener = undefined; |
| 292 | }, |
| 293 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 294 | // Destructor, just for testing scenarios |
| 295 | destroy : function () { |
| 296 | limits = {}; |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame^] | 297 | for (let w in widgets) { |
| 298 | widgets[w].close(); |
| 299 | }; |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 300 | widgets = {}; |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 301 | this._removeListener(); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 302 | } |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 303 | }; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 304 | }); |