forked from mirrors/gecko-dev
		
	 e930b89c34
			
		
	
	
		e930b89c34
		
	
	
	
	
		
			
			***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8
This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:
  ChromeUtils.import("resource://gre/modules/Services.jsm");
is approximately the same as the following, in the new model:
  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs
This was done using the followng script:
https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16750
--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
		
	
			
		
			
				
	
	
		
			179 lines
		
	
	
	
		
			6.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			179 lines
		
	
	
	
		
			6.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* 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";
 | |
| 
 | |
| var EXPORTED_SYMBOLS = ["HeadlessShell"];
 | |
| 
 | |
| const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 | |
| const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
 | |
| 
 | |
| // Refrences to the progress listeners to keep them from being gc'ed
 | |
| // before they are called.
 | |
| const progressListeners = new Map();
 | |
| 
 | |
| function loadContentWindow(webNavigation, url, principal) {
 | |
|   let uri;
 | |
|   try {
 | |
|     uri = Services.io.newURI(url);
 | |
|   } catch (e) {
 | |
|     let msg = `Invalid URL passed to loadContentWindow(): ${url}`;
 | |
|     Cu.reportError(msg);
 | |
|     return Promise.reject(new Error(msg));
 | |
|   }
 | |
|   return new Promise((resolve, reject) => {
 | |
|     let loadURIOptions = {
 | |
|       triggeringPrincipal: principal,
 | |
|     };
 | |
|     webNavigation.loadURI(uri.spec, loadURIOptions);
 | |
|     let docShell = webNavigation.QueryInterface(Ci.nsIInterfaceRequestor)
 | |
|                                 .getInterface(Ci.nsIDocShell);
 | |
|     let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
 | |
|                               .getInterface(Ci.nsIWebProgress);
 | |
|     let progressListener = {
 | |
|       onLocationChange(progress, request, location, flags) {
 | |
|         // Ignore inner-frame events
 | |
|         if (progress != webProgress) {
 | |
|           return;
 | |
|         }
 | |
|         // Ignore events that don't change the document
 | |
|         if (flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT) {
 | |
|           return;
 | |
|         }
 | |
|         // Ignore the initial about:blank
 | |
|         if (uri.spec != location.spec) {
 | |
|           return;
 | |
|         }
 | |
|         let contentWindow = docShell.domWindow;
 | |
|         progressListeners.delete(progressListener);
 | |
|         webProgress.removeProgressListener(progressListener);
 | |
|         contentWindow.addEventListener("load", (event) => {
 | |
|           resolve(contentWindow);
 | |
|         }, { once: true });
 | |
|       },
 | |
|       QueryInterface: ChromeUtils.generateQI(["nsIWebProgressListener",
 | |
|                                               "nsISupportsWeakReference"]),
 | |
|     };
 | |
|     progressListeners.set(progressListener, progressListener);
 | |
|     webProgress.addProgressListener(progressListener,
 | |
|                                     Ci.nsIWebProgress.NOTIFY_LOCATION);
 | |
|   });
 | |
| }
 | |
| 
 | |
| async function takeScreenshot(fullWidth, fullHeight, contentWidth, contentHeight, path, url) {
 | |
|   try {
 | |
|     var windowlessBrowser = Services.appShell.createWindowlessBrowser(false);
 | |
|     // nsIWindowlessBrowser inherits from nsIWebNavigation.
 | |
|     let contentWindow = await loadContentWindow(windowlessBrowser, url, Services.scriptSecurityManager.getSystemPrincipal());
 | |
|     contentWindow.resizeTo(contentWidth, contentHeight);
 | |
| 
 | |
|     let canvas = contentWindow.document.createElementNS("http://www.w3.org/1999/xhtml", "html:canvas");
 | |
|     let context = canvas.getContext("2d");
 | |
|     let width = fullWidth ? contentWindow.innerWidth + contentWindow.scrollMaxX - contentWindow.scrollMinX
 | |
|                           : contentWindow.innerWidth;
 | |
|     let height = fullHeight ? contentWindow.innerHeight + contentWindow.scrollMaxY - contentWindow.scrollMinY
 | |
|                             : contentWindow.innerHeight;
 | |
|     canvas.width = width;
 | |
|     canvas.height = height;
 | |
|     context.drawWindow(contentWindow, 0, 0, width, height, "rgb(255, 255, 255)");
 | |
| 
 | |
|     function getBlob() {
 | |
|       return new Promise(resolve => canvas.toBlob(resolve));
 | |
|     }
 | |
| 
 | |
|     function readBlob(blob) {
 | |
|       return new Promise(resolve => {
 | |
|         let reader = new FileReader();
 | |
|         reader.onloadend = () => resolve(reader);
 | |
|         reader.readAsArrayBuffer(blob);
 | |
|       });
 | |
|     }
 | |
| 
 | |
|     let blob = await getBlob();
 | |
|     let reader = await readBlob(blob);
 | |
|     await OS.File.writeAtomic(path, new Uint8Array(reader.result), {flush: true});
 | |
|     dump("Screenshot saved to: " + path + "\n");
 | |
|   } catch (e) {
 | |
|     dump("Failure taking screenshot: " + e + "\n");
 | |
|   } finally {
 | |
|     if (windowlessBrowser) {
 | |
|       windowlessBrowser.close();
 | |
|     }
 | |
|   }
 | |
| }
 | |
| 
 | |
| let HeadlessShell = {
 | |
|   async handleCmdLineArgs(cmdLine, URLlist) {
 | |
|     try {
 | |
|       // Don't quit even though we don't create a window
 | |
|       Services.startup.enterLastWindowClosingSurvivalArea();
 | |
| 
 | |
|       // Default options
 | |
|       let fullWidth = true;
 | |
|       let fullHeight = true;
 | |
|       // Most common screen resolution of Firefox users
 | |
|       let contentWidth = 1366;
 | |
|       let contentHeight = 768;
 | |
| 
 | |
|       // Parse `window-size`
 | |
|       try {
 | |
|         var dimensionsStr = cmdLine.handleFlagWithParam("window-size", true);
 | |
|       } catch (e) {
 | |
|         dump("expected format: --window-size width[,height]\n");
 | |
|         return;
 | |
|       }
 | |
|       if (dimensionsStr) {
 | |
|         let success;
 | |
|         let dimensions = dimensionsStr.split(",", 2);
 | |
|         if (dimensions.length == 1) {
 | |
|           success = dimensions[0] > 0;
 | |
|           if (success) {
 | |
|             fullWidth = false;
 | |
|             fullHeight = true;
 | |
|             contentWidth = dimensions[0];
 | |
|           }
 | |
|         } else {
 | |
|           success = dimensions[0] > 0 && dimensions[1] > 0;
 | |
|           if (success) {
 | |
|             fullWidth = false;
 | |
|             fullHeight = false;
 | |
|             contentWidth = dimensions[0];
 | |
|             contentHeight = dimensions[1];
 | |
|           }
 | |
|         }
 | |
| 
 | |
|         if (!success) {
 | |
|           dump("expected format: --window-size width[,height]\n");
 | |
|           return;
 | |
|         }
 | |
|       }
 | |
| 
 | |
|       // Only command line argument left should be `screenshot`
 | |
|       // There could still be URLs however
 | |
|       try {
 | |
|         var path = cmdLine.handleFlagWithParam("screenshot", true);
 | |
|         if (!cmdLine.length && !URLlist.length) {
 | |
|           URLlist.push(path); // Assume the user wanted to specify a URL
 | |
|           path = OS.Path.join(cmdLine.workingDirectory.path, "screenshot.png");
 | |
|         }
 | |
|       } catch (e) {
 | |
|         path = OS.Path.join(cmdLine.workingDirectory.path, "screenshot.png");
 | |
|         cmdLine.handleFlag("screenshot", true); // Remove `screenshot`
 | |
|       }
 | |
| 
 | |
|       for (let i = 0; i < cmdLine.length; ++i) {
 | |
|         URLlist.push(cmdLine.getArgument(i)); // Assume that all remaining arguments are URLs
 | |
|       }
 | |
| 
 | |
|       if (URLlist.length == 1) {
 | |
|         await takeScreenshot(fullWidth, fullHeight, contentWidth, contentHeight, path, URLlist[0]);
 | |
|       } else {
 | |
|         dump("expected exactly one URL when using `screenshot`\n");
 | |
|       }
 | |
|     } finally {
 | |
|       Services.startup.exitLastWindowClosingSurvivalArea();
 | |
|       Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
 | |
|     }
 | |
|   },
 | |
| };
 |