forked from mirrors/gecko-dev
Bug 1735162 - [marionette] Write Marionette port to MarionetteActivePort file in profile directory. r=webdriver-reviewers,jdescottes
Differential Revision: https://phabricator.services.mozilla.com/D128412
This commit is contained in:
parent
71fbec519e
commit
110a77d073
2 changed files with 62 additions and 9 deletions
|
|
@ -25,6 +25,8 @@ XPCOMUtils.defineLazyGetter(this, "logger", () =>
|
||||||
Log.get(Log.TYPES.MARIONETTE)
|
Log.get(Log.TYPES.MARIONETTE)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
XPCOMUtils.defineLazyGetter(this, "textEncoder", () => new TextEncoder());
|
||||||
|
|
||||||
XPCOMUtils.defineLazyServiceGetter(
|
XPCOMUtils.defineLazyServiceGetter(
|
||||||
this,
|
this,
|
||||||
"env",
|
"env",
|
||||||
|
|
@ -68,6 +70,7 @@ const isRemote =
|
||||||
class MarionetteParentProcess {
|
class MarionetteParentProcess {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.server = null;
|
this.server = null;
|
||||||
|
this._marionetteActivePortPath;
|
||||||
|
|
||||||
this.classID = Components.ID("{786a1369-dca5-4adc-8486-33d23c88010a}");
|
this.classID = Components.ID("{786a1369-dca5-4adc-8486-33d23c88010a}");
|
||||||
this.helpInfo = " --marionette Enable remote control server.\n";
|
this.helpInfo = " --marionette Enable remote control server.\n";
|
||||||
|
|
@ -126,7 +129,7 @@ class MarionetteParentProcess {
|
||||||
cmdLine.handleFlag("marionette", false);
|
cmdLine.handleFlag("marionette", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
observe(subject, topic) {
|
async observe(subject, topic) {
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
logger.trace(`Received observer notification ${topic}`);
|
logger.trace(`Received observer notification ${topic}`);
|
||||||
}
|
}
|
||||||
|
|
@ -172,7 +175,7 @@ class MarionetteParentProcess {
|
||||||
Services.obs.addObserver(this, "quit-application");
|
Services.obs.addObserver(this, "quit-application");
|
||||||
|
|
||||||
this.finalUIStartup = true;
|
this.finalUIStartup = true;
|
||||||
this.init();
|
await this.init();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -184,13 +187,13 @@ class MarionetteParentProcess {
|
||||||
case "toplevel-window-ready":
|
case "toplevel-window-ready":
|
||||||
subject.addEventListener(
|
subject.addEventListener(
|
||||||
"load",
|
"load",
|
||||||
ev => {
|
async ev => {
|
||||||
if (ev.target.documentElement.namespaceURI == XMLURI_PARSE_ERROR) {
|
if (ev.target.documentElement.namespaceURI == XMLURI_PARSE_ERROR) {
|
||||||
Services.obs.removeObserver(this, topic);
|
Services.obs.removeObserver(this, topic);
|
||||||
|
|
||||||
let parserError = ev.target.querySelector("parsererror");
|
let parserError = ev.target.querySelector("parsererror");
|
||||||
logger.fatal(parserError.textContent);
|
logger.fatal(parserError.textContent);
|
||||||
this.uninit();
|
await this.uninit();
|
||||||
Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
|
Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -225,14 +228,14 @@ class MarionetteParentProcess {
|
||||||
Services.obs.addObserver(this, "quit-application");
|
Services.obs.addObserver(this, "quit-application");
|
||||||
|
|
||||||
this.finalUIStartup = true;
|
this.finalUIStartup = true;
|
||||||
this.init();
|
await this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "quit-application":
|
case "quit-application":
|
||||||
Services.obs.removeObserver(this, "quit-application");
|
Services.obs.removeObserver(this, "quit-application");
|
||||||
this.uninit();
|
await this.uninit();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -254,7 +257,7 @@ class MarionetteParentProcess {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
init(quit = true) {
|
async init(quit = true) {
|
||||||
if (this.running || !this.enabled || !this.finalUIStartup) {
|
if (this.running || !this.enabled || !this.finalUIStartup) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Init aborted (running=${this.running}, ` +
|
`Init aborted (running=${this.running}, ` +
|
||||||
|
|
@ -282,7 +285,7 @@ class MarionetteParentProcess {
|
||||||
this.server.start();
|
this.server.start();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.fatal("Remote protocol server failed to start", e);
|
logger.fatal("Remote protocol server failed to start", e);
|
||||||
this.uninit();
|
await this.uninit();
|
||||||
if (quit) {
|
if (quit) {
|
||||||
Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
|
Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
|
||||||
}
|
}
|
||||||
|
|
@ -292,14 +295,41 @@ class MarionetteParentProcess {
|
||||||
env.set(ENV_ENABLED, "1");
|
env.set(ENV_ENABLED, "1");
|
||||||
Services.obs.notifyObservers(this, NOTIFY_LISTENING, true);
|
Services.obs.notifyObservers(this, NOTIFY_LISTENING, true);
|
||||||
logger.debug("Marionette is listening");
|
logger.debug("Marionette is listening");
|
||||||
|
|
||||||
|
// Write Marionette port to MarionetteActivePort file within the profile.
|
||||||
|
const profileDir = await PathUtils.getProfileDir();
|
||||||
|
this._marionetteActivePortPath = PathUtils.join(
|
||||||
|
profileDir,
|
||||||
|
"MarionetteActivePort"
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = `${this.server.port}`;
|
||||||
|
try {
|
||||||
|
await IOUtils.write(
|
||||||
|
this._marionetteActivePortPath,
|
||||||
|
textEncoder.encode(data)
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn(
|
||||||
|
`Failed to create ${this._marionetteActivePortPath} (${e.message})`
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
uninit() {
|
async uninit() {
|
||||||
if (this.running) {
|
if (this.running) {
|
||||||
this.server.stop();
|
this.server.stop();
|
||||||
Services.obs.notifyObservers(this, NOTIFY_LISTENING);
|
Services.obs.notifyObservers(this, NOTIFY_LISTENING);
|
||||||
logger.debug("Marionette stopped listening");
|
logger.debug("Marionette stopped listening");
|
||||||
|
|
||||||
|
try {
|
||||||
|
await IOUtils.remove(this._marionetteActivePortPath);
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn(
|
||||||
|
`Failed to remove ${this._marionetteActivePortPath} (${e.message})`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import os
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
@ -32,6 +33,28 @@ class TestMarionette(MarionetteTestCase):
|
||||||
self.assertRaises(socket.timeout, self.marionette.raise_for_port, timeout=5)
|
self.assertRaises(socket.timeout, self.marionette.raise_for_port, timeout=5)
|
||||||
self.assertLess(time.time() - start_time, 5)
|
self.assertLess(time.time() - start_time, 5)
|
||||||
|
|
||||||
|
@run_if_manage_instance("Only runnable if Marionette manages the instance")
|
||||||
|
def test_marionette_active_port_file(self):
|
||||||
|
active_port_file = os.path.join(
|
||||||
|
self.marionette.instance.profile.profile, "MarionetteActivePort"
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
os.path.exists(active_port_file), "MarionetteActivePort file written"
|
||||||
|
)
|
||||||
|
with open(active_port_file, "r") as fp:
|
||||||
|
lines = fp.readlines()
|
||||||
|
self.assertEqual(len(lines), 1, "MarionetteActivePort file contains two lines")
|
||||||
|
self.assertEqual(
|
||||||
|
int(lines[0]),
|
||||||
|
self.marionette.port,
|
||||||
|
"MarionetteActivePort file contains port",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.marionette.quit(in_app=True)
|
||||||
|
self.assertFalse(
|
||||||
|
os.path.exists(active_port_file), "MarionetteActivePort file removed"
|
||||||
|
)
|
||||||
|
|
||||||
def test_single_active_session(self):
|
def test_single_active_session(self):
|
||||||
self.assertEqual(1, self.marionette.execute_script("return 1"))
|
self.assertEqual(1, self.marionette.execute_script("return 1"))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue