From 53be0558db93dce5cdd660a27e92725d76deb7f0 Mon Sep 17 00:00:00 2001 From: James Lal Date: Fri, 27 Jul 2012 14:21:57 -0700 Subject: [PATCH] update tcp-socket shim --- lib/components/event-emitter.jsm | 150 ------------------------------- lib/components/tcp-socket.jsm | 16 ++-- lib/loader.js | 22 +++-- test/tcp-socket-test.js | 30 ++++--- 4 files changed, 38 insertions(+), 180 deletions(-) delete mode 100644 lib/components/event-emitter.jsm diff --git a/lib/components/event-emitter.jsm b/lib/components/event-emitter.jsm deleted file mode 100644 index 53064fe..0000000 --- a/lib/components/event-emitter.jsm +++ /dev/null @@ -1,150 +0,0 @@ -'use strict'; - - -var EXPORTED_SYMBOLS = ['EventEmitter']; - -/** - * Constructor - * - * @param {Object} events of events to add onto responder. - */ -function EventEmitter(events) { - this._$events = {}; - - if (typeof(events) !== 'undefined') { - this.addEventListener(events); - } -}; - -EventEmitter.prototype = { - parse: EventEmitter.parse, - stringify: EventEmitter.stringify, - - /** - * Events on this instance - * - * @type Object - */ - _$events: null, - - /** - * Adds an event listener to this object. - * - * - * @param {String} type event name. - * @param {Function} callback event callback. - */ - addEventListener: function addEventListener(type, callback) { - var event; - - if (typeof(callback) === 'undefined' && typeof(type) === 'object') { - for (event in type) { - if (type.hasOwnProperty(event)) { - this.addEventListener(event, type[event]); - } - } - - return this; - } - - if (!(type in this._$events)) { - this._$events[type] = []; - } - - this._$events[type].push(callback); - - return this; - }, - - /** - * Adds an event listener which will - * only fire once and then remove itself. - * - * - * @param {String} type event name. - * @param {Function} callback fired when event is emitted. - */ - once: function once(type, callback) { - var self = this; - function onceCb() { - self.removeEventListener(type, onceCb); - callback.apply(this, arguments); - } - - this.addEventListener(type, onceCb); - - return this; - }, - - /** - * Emits an event. - * - * Accepts any number of additional arguments to pass unto - * event listener. - * - * @param {String} eventName name of the event to emit. - * @param {Object} [arguments] additional arguments to pass. - */ - emit: function emit() { - var args = Array.prototype.slice.call(arguments), - event = args.shift(), - eventList, - self = this; - - if (event in this._$events) { - eventList = this._$events[event]; - - eventList.forEach(function(callback) { - callback.apply(self, args); - }); - } - - return this; - }, - - /** - * Removes all event listeners for a given event type - * - * - * @param {String} event event type to remove. - */ - removeAllEventListeners: function removeAllEventListeners(name) { - if (name in this._$events) { - //reuse array - this._$events[name].length = 0; - } - - return this; - }, - - /** - * Removes a single event listener from a given event type - * and callback function. - * - * - * @param {String} eventName event name. - * @param {Function} callback same instance of event handler. - */ - removeEventListener: function removeEventListener(name, callback) { - var i, length, events; - - if (!(name in this._$events)) { - return false; - } - - events = this._$events[name]; - - for (i = 0, length = events.length; i < length; i++) { - if (events[i] && events[i] === callback) { - events.splice(i, 1); - return true; - } - } - - return false; - } - -}; - -EventEmitter.prototype.on = EventEmitter.prototype.addEventListener; -EventEmitter.prototype.dispatchEvent = EventEmitter.prototype.emit; diff --git a/lib/components/tcp-socket.jsm b/lib/components/tcp-socket.jsm index ce7ef96..8d1b949 100644 --- a/lib/components/tcp-socket.jsm +++ b/lib/components/tcp-socket.jsm @@ -1,3 +1,4 @@ + /* 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/. */ @@ -10,7 +11,6 @@ const Cu = Components.utils; const Cr = Components.results; const CC = Components.Constructor; -Cu.import('resource://xpcwindow/lib/components/event-emitter.jsm'); Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); @@ -85,17 +85,10 @@ function TCPSocket() { this.host = ""; this.port = 0; this.ssl = false; - - - EventEmitter.call(this); - - this.open.apply(this, arguments); }; TCPSocket.prototype = { - __proto__: EventEmitter.prototype, - // Constants CONNECTING: kCONNECTING, OPEN: kOPEN, @@ -168,9 +161,10 @@ TCPSocket.prototype = { }, callListener: function ts_callListener(type, data) { - type = type.replace(/^on/, ''); - this.dispatchEvent(type, new TCPSocketEvent(type, this, data || "")); + if (!this[type]) + return; + this[type].call(null, new TCPSocketEvent(type, this, data || "")); }, get bufferedAmount() { @@ -194,7 +188,7 @@ TCPSocket.prototype = { if (this._hasPrivileges !== true && this._hasPrivileges !== null) { throw new Error("TCPSocket does not have permission in this context.\n"); } - let that = this; + let that = new TCPSocket(); LOG("startup called\n"); LOG("Host info: " + host + ":" + port + "\n"); diff --git a/lib/loader.js b/lib/loader.js index c7f99a6..4e56721 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -21,7 +21,7 @@ function importLib(file) { //Register xpcwindow // Map resource://xpcwindow/ to lib/ -(function () { +(function() { var fileObj = Cc["@mozilla.org/file/local;1"] .createInstance(Ci.nsILocalFile); @@ -44,13 +44,21 @@ var window = this.window = { xpcComponents: Components, xpcArgv: args, - get MozTCPSocket() { + get TCPSocket() { + var comp = '@mozilla.org/tcp-socket;1'; //lazy get MozTCPSocket - Components.utils.import( - 'resource://xpcwindow/lib/components/tcp-socket.jsm' - ); - delete window.MozTCPSocket; - return window.MozTCPSocket = TCPSocket; + if (comp in Components.classes) { + dump(typeof(Components.classes[comp].open) + '\n'); + delete window.TCPSocket; + return window.TCPSocket = new Components.classes[comp]; + } else { + Components.utils.import( + 'resource://xpcwindow/lib/components/tcp-socket.jsm' + ); + + delete window.TCPSocket; + return window.TCPSocket = new TCPSocket; + } } }; diff --git a/test/tcp-socket-test.js b/test/tcp-socket-test.js index 7f18eaa..6a34947 100644 --- a/test/tcp-socket-test.js +++ b/test/tcp-socket-test.js @@ -1,26 +1,32 @@ describe('tcp socket', function() { - var klass = MozTCPSocket, - subject; - - afterEach(function(done) { - subject.on('close', function() { - done(); - }); - subject.close(); - }); + var subject; describe('initialize', function() { it('should work under xpcom', function(done) { - subject = new MozTCPSocket('localhost', 80, { + this.timeout(5000); + + subject = window.TCPSocket.open('localhost', 80, { verifyCert: false }); - subject.on('open', function() { + subject.onopen = function() { done(); - }); + }; }); + + afterEach(function(done) { + if (!subject) + return done(); + + subject.onclose = function() { + done(); + }; + + subject.close(); + }); + }); });