Skip to content

Commit

Permalink
update tcp-socket shim
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsofapollo committed Jul 27, 2012
1 parent 9613966 commit 53be055
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 180 deletions.
150 changes: 0 additions & 150 deletions lib/components/event-emitter.jsm

This file was deleted.

16 changes: 5 additions & 11 deletions lib/components/tcp-socket.jsm
Original file line number Diff line number Diff line change
@@ -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/. */
Expand All @@ -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");

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand All @@ -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");
Expand Down
22 changes: 15 additions & 7 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

};
Expand Down
30 changes: 18 additions & 12 deletions test/tcp-socket-test.js
Original file line number Diff line number Diff line change
@@ -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();
});

});

});

0 comments on commit 53be055

Please sign in to comment.