| 
									
										
										
										
											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/. */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const {Helper} = ChromeUtils.import('chrome://juggler/content/Helper.js'); | 
					
						
							|  |  |  | const {FrameTree} = ChromeUtils.import('chrome://juggler/content/content/FrameTree.js'); | 
					
						
							|  |  |  | const {SimpleChannel} = ChromeUtils.import('chrome://juggler/content/SimpleChannel.js'); | 
					
						
							|  |  |  | const {PageAgent} = ChromeUtils.import('chrome://juggler/content/content/PageAgent.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  | const browsingContextToAgents = new Map(); | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  | const helper = new Helper(); | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function initialize(browsingContext, docShell, actor) { | 
					
						
							|  |  |  |   if (browsingContext.parent) { | 
					
						
							|  |  |  |     // For child frames, return agents from the main frame.
 | 
					
						
							|  |  |  |     return browsingContextToAgents.get(browsingContext.top); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let data = browsingContextToAgents.get(browsingContext); | 
					
						
							|  |  |  |   if (data) { | 
					
						
							|  |  |  |     // Rebind from one main frame actor to another one.
 | 
					
						
							|  |  |  |     data.channel.bindToActor(actor); | 
					
						
							|  |  |  |     return data; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   data = { channel: undefined, pageAgent: undefined, frameTree: undefined, failedToOverrideTimezone: false }; | 
					
						
							|  |  |  |   browsingContextToAgents.set(browsingContext, data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const applySetting = { | 
					
						
							|  |  |  |     geolocation: (geolocation) => { | 
					
						
							|  |  |  |       if (geolocation) { | 
					
						
							|  |  |  |         docShell.setGeolocationOverride({ | 
					
						
							|  |  |  |           coords: { | 
					
						
							|  |  |  |             latitude: geolocation.latitude, | 
					
						
							|  |  |  |             longitude: geolocation.longitude, | 
					
						
							|  |  |  |             accuracy: geolocation.accuracy, | 
					
						
							|  |  |  |             altitude: NaN, | 
					
						
							|  |  |  |             altitudeAccuracy: NaN, | 
					
						
							|  |  |  |             heading: NaN, | 
					
						
							|  |  |  |             speed: NaN, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           address: null, | 
					
						
							|  |  |  |           timestamp: Date.now() | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         docShell.setGeolocationOverride(null); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bypassCSP: (bypassCSP) => { | 
					
						
							|  |  |  |       docShell.bypassCSPEnabled = bypassCSP; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     timezoneId: (timezoneId) => { | 
					
						
							|  |  |  |       data.failedToOverrideTimezone = !docShell.overrideTimezone(timezoneId); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     locale: (locale) => { | 
					
						
							|  |  |  |       docShell.languageOverride = locale; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scrollbarsHidden: (hidden) => { | 
					
						
							|  |  |  |       data.frameTree.setScrollbarsHidden(hidden); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     javaScriptDisabled: (javaScriptDisabled) => { | 
					
						
							|  |  |  |       data.frameTree.setJavaScriptDisabled(javaScriptDisabled); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const contextCrossProcessCookie = Services.cpmm.sharedData.get('juggler:context-cookie-' + browsingContext.originAttributes.userContextId) || { initScripts: [], bindings: [], settings: {} }; | 
					
						
							|  |  |  |   const pageCrossProcessCookie = Services.cpmm.sharedData.get('juggler:page-cookie-' + browsingContext.browserId) || { initScripts: [], bindings: [], interceptFileChooserDialog: false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |   // Enforce focused state for all top level documents.
 | 
					
						
							|  |  |  |   docShell.overrideHasFocus = true; | 
					
						
							| 
									
										
										
										
											2020-11-05 17:53:22 -08:00
										 |  |  |   docShell.forceActiveState = true; | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |   docShell.disallowBFCache = true; | 
					
						
							| 
									
										
										
										
											2023-04-24 21:28:08 +00:00
										 |  |  |   data.frameTree = new FrameTree(browsingContext); | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |   for (const [name, value] of Object.entries(contextCrossProcessCookie.settings)) { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     if (value !== undefined) | 
					
						
							|  |  |  |       applySetting[name](value); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |   for (const { worldName, name, script } of [...contextCrossProcessCookie.bindings, ...pageCrossProcessCookie.bindings]) | 
					
						
							|  |  |  |     data.frameTree.addBinding(worldName, name, script); | 
					
						
							|  |  |  |   data.frameTree.setInitScripts([...contextCrossProcessCookie.initScripts, ...pageCrossProcessCookie.initScripts]); | 
					
						
							| 
									
										
										
										
											2023-09-11 18:16:33 -07:00
										 |  |  |   data.channel = new SimpleChannel('', 'process-' + Services.appinfo.processID); | 
					
						
							|  |  |  |   data.channel.bindToActor(actor); | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |   data.pageAgent = new PageAgent(data.channel, data.frameTree); | 
					
						
							|  |  |  |   docShell.fileInputInterceptionEnabled = !!pageCrossProcessCookie.interceptFileChooserDialog; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   data.channel.register('', { | 
					
						
							| 
									
										
										
										
											2022-02-14 20:32:12 -08:00
										 |  |  |     setInitScripts(scripts) { | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |       data.frameTree.setInitScripts(scripts); | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 16:27:39 +00:00
										 |  |  |     addBinding({worldName, name, script}) { | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |       data.frameTree.addBinding(worldName, name, script); | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     applyContextSetting({name, value}) { | 
					
						
							|  |  |  |       applySetting[name](value); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |     setInterceptFileChooserDialog(enabled) { | 
					
						
							|  |  |  |       docShell.fileInputInterceptionEnabled = !!enabled; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     ensurePermissions() { | 
					
						
							|  |  |  |       // noop, just a rountrip.
 | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hasFailedToOverrideTimezone() { | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |       return data.failedToOverrideTimezone; | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-06 15:08:23 -07:00
										 |  |  |     async awaitViewportDimensions({width, height}) { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  |       const win = docShell.domWindow; | 
					
						
							|  |  |  |       if (win.innerWidth === width && win.innerHeight === height) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       await new Promise(resolve => { | 
					
						
							|  |  |  |         const listener = helper.addEventListener(win, 'resize', () => { | 
					
						
							|  |  |  |           if (win.innerWidth === width && win.innerHeight === height) { | 
					
						
							|  |  |  |             helper.removeListeners([listener]); | 
					
						
							|  |  |  |             resolve(); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dispose() { | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  |   return data; | 
					
						
							| 
									
										
										
										
											2020-06-02 16:51:13 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-23 11:29:48 -08:00
										 |  |  | var EXPORTED_SYMBOLS = ['initialize']; | 
					
						
							|  |  |  | this.initialize = initialize; |