michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.CC = Components.Constructor; michael@0: this.Cc = Components.classes; michael@0: this.Ci = Components.interfaces; michael@0: this.Cu = Components.utils; michael@0: michael@0: const MARIONETTE_CONTRACTID = "@mozilla.org/marionette;1"; michael@0: const MARIONETTE_CID = Components.ID("{786a1369-dca5-4adc-8486-33d23c88010a}"); michael@0: const MARIONETTE_ENABLED_PREF = 'marionette.defaultPrefs.enabled'; michael@0: const MARIONETTE_FORCELOCAL_PREF = 'marionette.force-local'; michael@0: const MARIONETTE_LOG_PREF = 'marionette.logging'; michael@0: michael@0: this.ServerSocket = CC("@mozilla.org/network/server-socket;1", michael@0: "nsIServerSocket", michael@0: "initSpecialConnection"); michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Log.jsm"); michael@0: michael@0: let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Ci.mozIJSSubScriptLoader); michael@0: michael@0: function MarionetteComponent() { michael@0: this._loaded = false; michael@0: this.observerService = Services.obs; michael@0: michael@0: // set up the logger michael@0: this.logger = Log.repository.getLogger("Marionette"); michael@0: this.logger.level = Log.Level["Trace"]; michael@0: let dumper = false; michael@0: #ifdef DEBUG michael@0: dumper = true; michael@0: #endif michael@0: #ifdef MOZ_B2G michael@0: dumper = true; michael@0: #endif michael@0: try { michael@0: if (dumper || Services.prefs.getBoolPref(MARIONETTE_LOG_PREF)) { michael@0: let formatter = new Log.BasicFormatter(); michael@0: this.logger.addAppender(new Log.DumpAppender(formatter)); michael@0: } michael@0: } michael@0: catch(e) {} michael@0: } michael@0: michael@0: MarionetteComponent.prototype = { michael@0: classDescription: "Marionette component", michael@0: classID: MARIONETTE_CID, michael@0: contractID: MARIONETTE_CONTRACTID, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler, Ci.nsIObserver]), michael@0: _xpcom_categories: [{category: "command-line-handler", entry: "b-marionette"}, michael@0: {category: "profile-after-change", service: true}], michael@0: appName: Services.appinfo.name, michael@0: enabled: false, michael@0: finalUiStartup: false, michael@0: _marionetteServer: null, michael@0: michael@0: onSocketAccepted: function mc_onSocketAccepted(aSocket, aTransport) { michael@0: this.logger.info("onSocketAccepted for Marionette dummy socket"); michael@0: }, michael@0: michael@0: onStopListening: function mc_onStopListening(aSocket, status) { michael@0: this.logger.info("onStopListening for Marionette dummy socket, code " + status); michael@0: aSocket.close(); michael@0: }, michael@0: michael@0: // Check cmdLine argument for --marionette michael@0: handle: function mc_handle(cmdLine) { michael@0: // If the CLI is there then lets do work otherwise nothing to see michael@0: if (cmdLine.handleFlag("marionette", false)) { michael@0: this.enabled = true; michael@0: this.logger.info("marionette enabled via command-line"); michael@0: this.init(); michael@0: } michael@0: }, michael@0: michael@0: observe: function mc_observe(aSubject, aTopic, aData) { michael@0: switch (aTopic) { michael@0: case "profile-after-change": michael@0: // Using final-ui-startup as the xpcom category doesn't seem to work, michael@0: // so we wait for that by adding an observer here. michael@0: this.observerService.addObserver(this, "final-ui-startup", false); michael@0: #ifdef ENABLE_MARIONETTE michael@0: let enabledPref = false; michael@0: try { michael@0: enabledPref = Services.prefs.getBoolPref(MARIONETTE_ENABLED_PREF); michael@0: } catch(e) {} michael@0: if (enabledPref) { michael@0: this.enabled = true; michael@0: this.logger.info("marionette enabled via build flag and pref"); michael@0: michael@0: // We want to suppress the modal dialog that's shown michael@0: // when starting up in safe-mode to enable testing. michael@0: if (Services.appinfo.inSafeMode) { michael@0: this.observerService.addObserver(this, "domwindowopened", false); michael@0: } michael@0: } michael@0: #endif michael@0: break; michael@0: case "final-ui-startup": michael@0: this.finalUiStartup = true; michael@0: this.observerService.removeObserver(this, aTopic); michael@0: this.observerService.addObserver(this, "xpcom-shutdown", false); michael@0: this.init(); michael@0: break; michael@0: case "domwindowopened": michael@0: this.observerService.removeObserver(this, aTopic); michael@0: this._suppressSafeModeDialog(aSubject); michael@0: break; michael@0: case "xpcom-shutdown": michael@0: this.observerService.removeObserver(this, "xpcom-shutdown"); michael@0: this.uninit(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: _suppressSafeModeDialog: function mc_suppressSafeModeDialog(aWindow) { michael@0: // Wait for the modal dialog to finish loading. michael@0: aWindow.addEventListener("load", function onLoad() { michael@0: aWindow.removeEventListener("load", onLoad); michael@0: michael@0: if (aWindow.document.getElementById("safeModeDialog")) { michael@0: aWindow.setTimeout(() => { michael@0: // Accept the dialog to start in safe-mode. michael@0: aWindow.document.documentElement.getButton("accept").click(); michael@0: }); michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: init: function mc_init() { michael@0: if (!this._loaded && this.enabled && this.finalUiStartup) { michael@0: this._loaded = true; michael@0: michael@0: let marionette_forcelocal = this.appName == 'B2G' ? false : true; michael@0: try { michael@0: marionette_forcelocal = Services.prefs.getBoolPref(MARIONETTE_FORCELOCAL_PREF); michael@0: } michael@0: catch(e) {} michael@0: Services.prefs.setBoolPref(MARIONETTE_FORCELOCAL_PREF, marionette_forcelocal); michael@0: michael@0: if (!marionette_forcelocal) { michael@0: // See bug 800138. Because the first socket that opens with michael@0: // force-local=false fails, we open a dummy socket that will fail. michael@0: // keepWhenOffline=true so that it still work when offline (local). michael@0: // This allows the following attempt by Marionette to open a socket michael@0: // to succeed. michael@0: let insaneSacrificialGoat = new ServerSocket(666, Ci.nsIServerSocket.KeepWhenOffline, 4); michael@0: insaneSacrificialGoat.asyncListen(this); michael@0: } michael@0: michael@0: let port; michael@0: try { michael@0: port = Services.prefs.getIntPref('marionette.defaultPrefs.port'); michael@0: } michael@0: catch(e) { michael@0: port = 2828; michael@0: } michael@0: try { michael@0: loader.loadSubScript("chrome://marionette/content/marionette-server.js"); michael@0: let forceLocal = Services.prefs.getBoolPref(MARIONETTE_FORCELOCAL_PREF); michael@0: this._marionetteServer = new MarionetteServer(port, forceLocal); michael@0: this.logger.info("Marionette server ready"); michael@0: } michael@0: catch(e) { michael@0: this.logger.error('exception: ' + e.name + ', ' + e.message); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: uninit: function mc_uninit() { michael@0: if (this._marionetteServer) { michael@0: this._marionetteServer.closeListener(); michael@0: } michael@0: this._loaded = false; michael@0: }, michael@0: michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MarionetteComponent]);