Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Bug 686320 - Allow Mozmill to probe the OS for a free port instead of hardcoded value in JSBridge. r=jhammel #45

Merged
merged 1 commit into from
Jun 14, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions jsbridge/jsbridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
extension_path = os.path.join(parent, 'extension')
wait_to_create_timeout = 60


def find_port():
free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
free_socket.bind(('127.0.0.1', 0))
port = free_socket.getsockname()[1]
free_socket.close()

return port


def wait_and_create_network(host, port, timeout=wait_to_create_timeout):
ttl = 0
while ttl < timeout:
Expand All @@ -28,16 +38,16 @@ def wait_and_create_network(host, port, timeout=wait_to_create_timeout):
sleep(.25)
ttl += .25
if ttl == timeout:
raise Exception("Sorry, cannot connect to jsbridge extension, port %s" % port)
raise Exception("Cannot connect to jsbridge extension, port %s" % port)

back_channel, bridge = create_network(host, port)
sleep(.5)

while back_channel.registered is False:
back_channel.close()
bridge.close()
asyncore.socket_map = {}
sleep(1)
back_channel, bridge = create_network(host, port)

return back_channel, bridge
15 changes: 3 additions & 12 deletions jsbridge/jsbridge/extension/chrome.manifest
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
resource jsbridge resource/

content jsbridge chrome/content/
component {2872d428-14f6-11de-ac86-001f5bd9235c} components/jsbridge.js
contract @mozilla.org/jsbridge;1 {2872d428-14f6-11de-ac86-001f5bd9235c}
category profile-after-change JSBridge @mozilla.org/jsbridge;1

overlay chrome://browser/content/browser.xul chrome://jsbridge/content/overlay.xul
overlay chrome://messenger/content/mailWindowOverlay.xul chrome://jsbridge/content/overlay.xul

overlay chrome://calendar/content/calendar.xul chrome://jsbridge/content/overlay.xul

overlay windowtype:Songbird:Main chrome://jsbridge/content/overlay.xul

component {2872d428-14f6-11de-ac86-001f5bd9235c} components/cmdarg.js
contract @mozilla.org/commandlinehandler/general-startup;1?type=jsbridge {2872d428-14f6-11de-ac86-001f5bd9235c}
category profile-after-change jsbridge @mozilla.org/commandlinehandler/general-startup;1?type=jsbridge
category command-line-handler jsbridge @mozilla.org/commandlinehandler/general-startup;1?type=jsbridge
7 changes: 0 additions & 7 deletions jsbridge/jsbridge/extension/chrome/content/overlay.js

This file was deleted.

9 changes: 0 additions & 9 deletions jsbridge/jsbridge/extension/chrome/content/overlay.xul

This file was deleted.

145 changes: 0 additions & 145 deletions jsbridge/jsbridge/extension/components/cmdarg.js

This file was deleted.

82 changes: 82 additions & 0 deletions jsbridge/jsbridge/extension/components/jsbridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* 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 Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

// Import global modules
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

// Include local modules
Cu.import("resource://jsbridge/modules/Log.jsm");


// Constants for preferences names
const PREF_JSBRIDGE_PORT = "extensions.jsbridge.port";


/**
* XPCOM component to observe different application states and to
* handle the JSBridge server.
*/
function JSBridge() {
this._server = null;
}

JSBridge.prototype = {
classDescription: "JSBridge",
classID: Components.ID("{2872d428-14f6-11de-ac86-001f5bd9235c}"),
contractID: "@mozilla.org/jsbridge;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),

_xpcom_categories: [{category: "profile-after-change"}],

/**
* Handler for registered observer notifications.
*
* @param {String} aSubject
* Subject of the observer message (not used)
* @param {String} aTopic
* Topic of the observer message
* @param {Object} aData
* Data of the observer message (not used)
*/
observe: function JSB_observe(aSubject, aTopic, aData) {
Log.dump("Observer topic", aTopic);

switch (aTopic) {
// The server cannot be started before the ui is shown. That means
// we also have to register for the final-ui-startup notification.
case "profile-after-change":
Services.obs.addObserver(this, "final-ui-startup", false);
Services.obs.addObserver(this, "quit-application", false);
break;

case "final-ui-startup":
Services.obs.removeObserver(this, "final-ui-startup", false);

// The port the server has to be started on is set via a preference
let port = Services.prefs.getIntPref(PREF_JSBRIDGE_PORT);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the behaviour is if the preference is not set. What happens in that case? IMHO, we should just not start the server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getIntPref will throw an exception if the preference cannot be found. Given that all the code below will not be executed and we bail out. As you said, we do NOT start the server in such a case.

// Start the server
Cu.import('resource://jsbridge/modules/Server.jsm');
this._server = new Server.Server(port);
this._server.start();

break;

case "quit-application":
Services.obs.removeObserver(this, "quit-application", false);

// Stop the server
this._server.stop();
this._server = null;
break;
}
}
}

const NSGetFactory = XPCOMUtils.generateNSGetFactory([JSBridge]);
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

var EXPORTED_SYMBOLS = ["Bridge"];


const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

var events = {}; Cu.import("resource://jsbridge/modules/events.js", events);

var uuidgen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
// Include local modules
Cu.import("resource://jsbridge/modules/Events.jsm");
Cu.import("resource://jsbridge/modules/Log.jsm");


globalRegistry = {};
var globalRegistry = {};
var uuidgen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);


function Bridge(session) {
this.session = session;
Expand All @@ -23,11 +27,13 @@ function Bridge(session) {
Bridge.prototype._register = function (_type) {
this.bridgeType = _type;

if (_type == "backchannel")
events.addBackChannel(this);
if (_type === "backchannel")
Events.addBackChannel(this);
};

Bridge.prototype.register = function (uuid, _type) {
Log.dump("Register", uuid + " (" + _type + ")");

try {
this._register(_type);
var passed = true;
Expand Down Expand Up @@ -74,6 +80,8 @@ Bridge.prototype._describe = function (obj) {
};

Bridge.prototype.describe = function (uuid, obj) {
Log.dump("Describe", uuid + ", " + obj);

var response = this._describe(obj);
response.uuid = uuid;
response.result = true;
Expand All @@ -90,6 +98,8 @@ Bridge.prototype._set = function (obj) {
};

Bridge.prototype.set = function (uuid, obj) {
Log.dump("Set", uuid);

var ruuid = this._set(obj);

this.session.encodeOut({'result': true,
Expand All @@ -104,7 +114,8 @@ Bridge.prototype._setAttribute = function (obj, name, value) {
};

Bridge.prototype.setAttribute = function (uuid, obj, name, value) {
// log(uuid, String(obj), name, String(value))
Log.dump("Set attribute", uuid + " (" + name + "=" + value + ")");

try {
var result = this._setAttribute(obj, name, value);
} catch (e) {
Expand All @@ -130,6 +141,8 @@ Bridge.prototype._execFunction = function (func, args) {
};

Bridge.prototype.execFunction = function (uuid, func, args) {
Log.dump("Exec function", uuid + " (" + func.name + ")");

try {
var data = this._execFunction(func, args);
var result = true;
Expand All @@ -155,3 +168,4 @@ Bridge.prototype.execFunction = function (uuid, func, args) {
else
throw 'jsbridge could not execute function ' + func;
};

Loading