Skip to content

Commit

Permalink
make senders register themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
mykmelez committed Oct 15, 2014
1 parent 056beb7 commit 4737421
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 87 deletions.
169 changes: 86 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,25 @@ function loadScript(path) {
});
})();

function getAllContacts(message, sender) {
var req = navigator.mozContacts.getAll();

req.onsuccess = function() {
var contact = req.result;
sender(contact);
if (contact) {
req.continue();
}
}

req.onerror = function() {
console.error("Error while reading contacts");
}
}

function getMobileInfo(message, sender) {
// Initialize the object with the URL params and fallback placeholders
// for testing/debugging on a desktop.
var mobileInfo = {
network: {
mcc: urlParams.network_mcc || "310", // United States
mnc: urlParams.network_mnc || "001",
},
icc: {
mcc: urlParams.icc_mcc || "310", // United States
mnc: urlParams.icc_mnc || "001",
msisdn: urlParams.icc_msisdn || "10005551212",
},
};

var mobileConnections = window.navigator.mozMobileConnections;
if (!mobileConnections && window.navigator.mozMobileConnection) {
mobileConnections = [ window.navigator.mozMobileConnection ];
}

// If we have access to the Mobile Connection API, then we use it to get
// the actual values.
if (mobileConnections) {
// Then the only part of the Mobile Connection API that is accessible
// to privileged apps is lastKnownNetwork and lastKnownHomeNetwork, which
// is fortunately all we need. lastKnownNetwork is a string of format
// "<mcc>-<mnc>", while lastKnownHomeNetwork is "<mcc>-<mnc>[-<spn>]".
// Use only the info about the first SIM for the time being.
var lastKnownNetwork = mobileConnections[0].lastKnownNetwork.split("-");
mobileInfo.network.mcc = lastKnownNetwork[0];
mobileInfo.network.mnc = lastKnownNetwork[1];

var lastKnownHomeNetwork = mobileConnections[0].lastKnownHomeNetwork.split("-");
mobileInfo.icc.mcc = lastKnownHomeNetwork[0];
mobileInfo.icc.mnc = lastKnownHomeNetwork[1];
}

sender(mobileInfo);
}

var DumbPipe = {
// Functions that handle requests to open a pipe, indexed by type.
openers: {},

// Functions that receive messages from the other side for active pipes.
recipients: {},

// Queue of messages to send to the other side. Retrieved by the other side
// via a "get" message.
outgoingMessages: [],

// Every time we want to make the other side retrieve messages, the hash has
// to change, so we set it to an ever-incrementing value.
// Every time we want to make the other side retrieve messages, the hash
// of the other side's web page has to change, so we increment it.
nextHashID: 0,

registerOpener: function(type, opener) {
this.openers[type] = opener;
},

handleEvent: function(event) {
if (event.detail.promptType == "custom-prompt") {
console.warn("unresponsive script warning; figure out how to handle");
Expand Down Expand Up @@ -132,28 +86,21 @@ var DumbPipe = {
},

openPipe: function(pipeID, type, message) {
// Create a function that the object on this spipeIDe of the boundary
// can use to send a message to the object on the other spipeIDe.
var sender = this.sender.bind(this, pipeID);

// Call the appropriate function to initialize the sender.
switch (type) {
case "echo":
this.recipients[pipeID] = openEchoPipe(message, sender);
break;
case "socket":
this.recipients[pipeID] = openSocketPipe(message, sender);
break;
case "mobileInfo":
this.recipients[pipeID] = getMobileInfo(message, sender);
break;
case "contacts":
this.recipients[pipeID] = getAllContacts(message, sender);
break;
var opener = this.openers[type];

if (!opener) {
console.error("no opener for pipe type " + type);
return;
}

// Create a function that this side of the boundary can use to send
// a message to the other side.
var sender = this.sendMessage.bind(this, pipeID);

this.recipients[pipeID] = opener(message, sender);
},

sender: function(pipeID, message) {
sendMessage: function(pipeID, message) {
// Sadly, we have no way to send a message to the other side directly.
// Instead, we change the hash part of the other side's URL, which triggers
// a hashchange event on the other side. A listener on the other side
Expand Down Expand Up @@ -204,7 +151,67 @@ document.getElementById("mozbrowser").addEventListener("mozbrowsershowmodalpromp
DumbPipe.handleEvent.bind(DumbPipe),
true);

var openSocketPipe = function(message, sender) {
DumbPipe.registerOpener("echo", function(message, sender) {
sender(message);
});

DumbPipe.registerOpener("mobileInfo", function(message, sender) {
// Initialize the object with the URL params and fallback placeholders
// for testing/debugging on a desktop.
var mobileInfo = {
network: {
mcc: urlParams.network_mcc || "310", // United States
mnc: urlParams.network_mnc || "001",
},
icc: {
mcc: urlParams.icc_mcc || "310", // United States
mnc: urlParams.icc_mnc || "001",
msisdn: urlParams.icc_msisdn || "10005551212",
},
};

var mobileConnections = window.navigator.mozMobileConnections;
if (!mobileConnections && window.navigator.mozMobileConnection) {
mobileConnections = [ window.navigator.mozMobileConnection ];
}

// If we have access to the Mobile Connection API, then we use it to get
// the actual values.
if (mobileConnections) {
// Then the only part of the Mobile Connection API that is accessible
// to privileged apps is lastKnownNetwork and lastKnownHomeNetwork, which
// is fortunately all we need. lastKnownNetwork is a string of format
// "<mcc>-<mnc>", while lastKnownHomeNetwork is "<mcc>-<mnc>[-<spn>]".
// Use only the info about the first SIM for the time being.
var lastKnownNetwork = mobileConnections[0].lastKnownNetwork.split("-");
mobileInfo.network.mcc = lastKnownNetwork[0];
mobileInfo.network.mnc = lastKnownNetwork[1];

var lastKnownHomeNetwork = mobileConnections[0].lastKnownHomeNetwork.split("-");
mobileInfo.icc.mcc = lastKnownHomeNetwork[0];
mobileInfo.icc.mnc = lastKnownHomeNetwork[1];
}

sender(mobileInfo);
});

DumbPipe.registerOpener("contacts", function(message, sender) {
var req = navigator.mozContacts.getAll();

req.onsuccess = function() {
var contact = req.result;
sender(contact);
if (contact) {
req.continue();
}
}

req.onerror = function() {
console.error("Error while reading contacts");
}
});

DumbPipe.registerOpener("socket", function(message, sender) {
var socket;
try {
socket = navigator.mozTCPSocket.open(message.host, message.port, { binaryType: "arraybuffer" });
Expand Down Expand Up @@ -256,8 +263,4 @@ var openSocketPipe = function(message, sender) {
break;
}
};
};

var openEchoPipe = function(message, sender) {
sender(message);
};
});
9 changes: 5 additions & 4 deletions tests/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ var fakeContacts = [{
},],
}];

function getAllContacts(message, pipe) {
// Override the regular "contacts" registration with our fake one.
DumbPipe.registerOpener("contacts", function(message, sender) {
fakeContacts.forEach(function(contact) {
window.setTimeout(function() {
pipe(contact);
sender(contact);
}, 0);
});
window.setTimeout(function() {
pipe(null);
sender(null);
}, 0);
}
});

0 comments on commit 4737421

Please sign in to comment.