| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | loadSubScript('chrome://juggler/content/content/Runtime.js'); | 
					
						
							|  |  |  | loadSubScript('chrome://juggler/content/SimpleChannel.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const channel = new SimpleChannel('worker::worker'); | 
					
						
							|  |  |  | const eventListener = event => channel._onMessage(JSON.parse(event.data)); | 
					
						
							|  |  |  | this.addEventListener('message', eventListener); | 
					
						
							| 
									
										
										
										
											2020-11-02 16:21:34 -08:00
										 |  |  | channel.setTransport({ | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |   sendMessage: msg => postMessage(JSON.stringify(msg)), | 
					
						
							|  |  |  |   dispose: () => this.removeEventListener('message', eventListener), | 
					
						
							| 
									
										
										
										
											2020-11-02 16:21:34 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const runtime = new Runtime(true /* isWorker */); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (() => { | 
					
						
							|  |  |  |   // Create execution context in the runtime only when the script
 | 
					
						
							|  |  |  |   // source was actually evaluated in it.
 | 
					
						
							|  |  |  |   const dbg = new Debugger(global); | 
					
						
							|  |  |  |   if (dbg.findScripts({global}).length) { | 
					
						
							|  |  |  |     runtime.createExecutionContext(null /* domWindow */, global, {}); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     dbg.onNewScript = function(s) { | 
					
						
							|  |  |  |       dbg.onNewScript = undefined; | 
					
						
							|  |  |  |       dbg.removeAllDebuggees(); | 
					
						
							|  |  |  |       runtime.createExecutionContext(null /* domWindow */, global, {}); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | })(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RuntimeAgent { | 
					
						
							| 
									
										
										
										
											2020-10-02 04:13:42 -07:00
										 |  |  |   constructor(runtime, channel) { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     this._runtime = runtime; | 
					
						
							| 
									
										
										
										
											2020-10-02 04:13:42 -07:00
										 |  |  |     this._browserRuntime = channel.connect('runtime'); | 
					
						
							| 
									
										
										
										
											2020-10-06 01:53:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const context of this._runtime.executionContexts()) | 
					
						
							|  |  |  |       this._onExecutionContextCreated(context); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     this._eventListeners = [ | 
					
						
							| 
									
										
										
										
											2020-10-06 01:53:25 -07:00
										 |  |  |       this._runtime.events.onConsoleMessage(msg => this._browserRuntime.emit('runtimeConsole', msg)), | 
					
						
							|  |  |  |       this._runtime.events.onExecutionContextCreated(this._onExecutionContextCreated.bind(this)), | 
					
						
							|  |  |  |       this._runtime.events.onExecutionContextDestroyed(this._onExecutionContextDestroyed.bind(this)), | 
					
						
							| 
									
										
										
										
											2020-10-02 04:13:42 -07:00
										 |  |  |       channel.register('runtime', { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |         evaluate: this._runtime.evaluate.bind(this._runtime), | 
					
						
							|  |  |  |         callFunction: this._runtime.callFunction.bind(this._runtime), | 
					
						
							|  |  |  |         getObjectProperties: this._runtime.getObjectProperties.bind(this._runtime), | 
					
						
							|  |  |  |         disposeObject: this._runtime.disposeObject.bind(this._runtime), | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _onExecutionContextCreated(executionContext) { | 
					
						
							|  |  |  |     this._browserRuntime.emit('runtimeExecutionContextCreated', { | 
					
						
							|  |  |  |       executionContextId: executionContext.id(), | 
					
						
							|  |  |  |       auxData: executionContext.auxData(), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _onExecutionContextDestroyed(executionContext) { | 
					
						
							|  |  |  |     this._browserRuntime.emit('runtimeExecutionContextDestroyed', { | 
					
						
							|  |  |  |       executionContextId: executionContext.id(), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   dispose() { | 
					
						
							|  |  |  |     for (const disposer of this._eventListeners) | 
					
						
							|  |  |  |       disposer(); | 
					
						
							|  |  |  |     this._eventListeners = []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 01:53:25 -07:00
										 |  |  | new RuntimeAgent(runtime, channel); | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  | 
 |