Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
abstracting away chrome/firefox push notifications differences
Browse files Browse the repository at this point in the history
  • Loading branch information
mvayngrib committed Jun 30, 2013
1 parent 91710c8 commit ec5a35e
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 173 deletions.
Binary file added ChromeApp/NursMe/NursMe.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions ChromeApp/NursMe/index.html
Expand Up @@ -28,7 +28,7 @@
<!--webview id="webview" src="http://mark.obval.com/urbien/app/NursMe#chatLobby/http%3A%2F%2Fmark.obval.com%2Furbien%2Fsql%2Fwww.hudsonfog.com%2Fvoc%2Fsystem%2Fdesigner%2FWebClass%3Fid%3D34663?-webview=y" style="width:640px; height:480px;"></webview-->
<!--webview id="webview" src="http://urbien.com/app/NursMe1" style="width:640px; height:480px;"></webview-->
<!--webview id="webview" src="http://mark.obval.com/urbien/app/NursMe#chatPrivate/_1?-webview=y" style="width:640px; height:480px;"></webview-->
<!--webview id="webview" src="http://mark.obval.com/urbien/app/NursMe" style="width:640px; height:480px;"></webview-->
<webview id="webview" src="http://urbien.com/app/NursMe1#home/?-webview=y&-min=n" style="width:640px; height:480px;"></webview>
<webview id="webview" src="http://mark.obval.com/urbien/app/NursMe#home/?-webiew=y&-min=n" style="width:640px; height:480px;"></webview>
<!--webview id="webview" src="http://urbien.com/app/NursMe1#home/?-webview=y&-min=n" style="width:640px; height:480px;"></webview-->
</body>
</html>
6 changes: 2 additions & 4 deletions ChromeApp/NursMe/js/index.js
Expand Up @@ -172,10 +172,8 @@
last = path.lastIndexOf('.'),
context = last == -1 ? chrome : leaf(chrome, path.slice(0, last));

if (callback) {
var eventName = callback;
args[callbackIdx] = getCallback(eventName);
}
if (callback)
args[callbackIdx] = getCallback(callback);
else
[].push.call(args, doNothing);

Expand Down
1 change: 0 additions & 1 deletion ChromeApp/NursMe/manifest.json
Expand Up @@ -4,7 +4,6 @@
"minimum_chrome_version": "24.0.1307.0",
"version": "0.0.0.22",
"manifest_version": 2,
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIz7JLrymjKpyixwY26nCWHUEJ8QaDmeGuD9DfjkARr8s+npqL0+nwnwiUYcaP6y1uVe/D7mM30nMo6UoIQ5F0zGbuJDNdlGZqRdeHCG/+B24yfWRPnoGCQcJYVWQ+brM5QJhr3B1oM8Vfwes5fpqpVsqNbGVX8PnIl7eGO6jztwIDAQAB",
"icons": {
"16": "icon_16.png",
"128": "icon_128.png"
Expand Down
29 changes: 25 additions & 4 deletions FirefoxApp/NursMe/js/index.js
Expand Up @@ -52,6 +52,12 @@

notification.show();
}
},
register: getPushReqHandler(),
unregister: getPushReqHandler(),
registrations: getPushReqHandler(),
setMessageHandler: function(messageType, callbackEvent) {
navigator.mozSetMessageHandler(messageType, getCallback(event));
}
},
$ = function() {
Expand All @@ -69,7 +75,6 @@
if (req.result) {
// we're installed
console.log('already installed');
initPush();
} else {
// not installed
navigator.mozApps.install('/manifest.webapp');
Expand All @@ -86,11 +91,27 @@

req.onerror = function(err) {
console('Error checking installation status: ' + err.message);
};
};
};

function initPush() {

function getPushReqHandler(obj, method) {
return function() {
return handleDomReqRPC.apply(null, [obj[method], obj].concat(args));
}
};

function handleDomReqRPC(fn, context) {
var args = arguments,
successEvent = args[args.length - 1],
errorEvent = args[args.length - 1];

args = [].slice.call(args, 2, args.length - 2);
var req = fn.apply(context, args);
req.onsuccess = function() {
getCallback(successEvent)(req.result);
};

req.onerror = getCallback(errorEvent);
};

function doNothing() {};
Expand Down
2 changes: 1 addition & 1 deletion FirefoxApp/NursMe/manifest.webapp
@@ -1,6 +1,6 @@
{
"name": "Nurse Me",
"type": "certified",
"type": "privileged",
"developer": {
"name": "Urbien",
"url": "http://urbien.com"
Expand Down
10 changes: 1 addition & 9 deletions js/chrome.js
Expand Up @@ -60,14 +60,6 @@ define('chrome', ['globals', 'underscore', 'events', 'utils', 'collections/Resou
}
};

function sendMessageToApp(msg) {
var appWin = G.appWindow;
if (appWin && G.appOrigin)
appWin.postMessage(msg, G.appOrigin);
else
console.debug("can't send message to app, don't know app's window & origin");
};

function onpush(msg) {
var subchannelId = msg.subchannelId,
payload = msg.payload,
Expand All @@ -91,6 +83,7 @@ define('chrome', ['globals', 'underscore', 'events', 'utils', 'collections/Resou
chrome.notifications.onClicked(function(notificationId) {
console.log('clicked notification, id:', notificationId);
if (notificationId == id) {
chrome.notifications.clear(id);
U.rpc('focus');
Events.trigger('navigate', G.tabs[0].hash);
ringtone.remove();
Expand Down Expand Up @@ -129,7 +122,6 @@ define('chrome', ['globals', 'underscore', 'events', 'utils', 'collections/Resou
}
},
_setup: function() {
Events.on('messageToApp', sendMessageToApp);
Events.on('messageFromApp:push', onpush);
var installedApps = G.currentUser.installedApps,
currentApp = G.currentApp,
Expand Down
163 changes: 91 additions & 72 deletions js/firefox.js
@@ -1,56 +1,39 @@
define('firefox', ['globals', 'jquery', 'Events', 'utils'], function(G, $, Events, U) {
var gManifestName = "/manifest.webapp",
registrations = [];

function initPush() {
define('firefox', ['globals', 'jquery', 'Events', 'utils', 'cache'], function(G, $, Events, U, C) {
var gManifestName = "/manifest.webapp";
function onpush(message) {
debugger;
if (!navigator.push)
return;
console.log('got push message');
var endpoints = C.getResourceList(U.getModel(G.commonTypes.PushEndpoint)),
endpoint = message.endpoint;

U.domReq(navigator.registrations()).done(function(regs) {
registrations = regs;
if (regs && regs.indexOf('default')) {
attachPush();
return;
}

U.domReq(navigator.register()).done(function(endpoint) {
Events.trigger('newPushEndpoint', endpoint);
attachPush();
});
}).fail(function(err) {
if (!endpoints || !endpoints.length) {
debugger;
});
return;
}

Events.on('logout', function() {
debugger;
for (var i = 0; i < registrations.length; i++) {
navigator.push.unregister(registrations[i]);
}
});
var match = endpoints.where({
endpoint: endpoint
}, true);

navigator.mozSetMessageHandler('push-register', function(e) {
if (!match) {
debugger;
if (!G.currentUser.guest)
initPush();
});
};

function attachPush() {
navigator.mozSetMessageHandler('push', onpush);
};

function onpush() {
console.log('got push message');
firefox.notifications.create('Client Waiting', "There's a client waiting to be assisted in the lobby", 'icon_128.png', {
onclick: function() {
console.log('push message notification clicked');
Events.trigger('navigate', G.tabs[0].hash);
},
onclose: function() {
console.log('push message notification closed');
}
});
return;
}

if (/^https?\:/.test(match.channelName))
window.location.href = match.channelName;
else
Events.trigger('navigate', match.channelName);

// firefox.notifications.create('Client Waiting', "There's a client waiting to be assisted in the lobby", 'icon_128.png', {
// onclick: function() {
// console.log('push message notification clicked');
// Events.trigger('navigate', G.tabs[0].hash);
// },
// onclose: function() {
// console.log('push message notification closed');
// }
// });
};

/**
Expand Down Expand Up @@ -88,27 +71,48 @@ define('firefox', ['globals', 'jquery', 'Events', 'utils'], function(G, $, Event
}
}
};

var firefox = {
_setup: function() {
U.domReq(navigator.mozApps.getSelf()).done(function(result) {
debugger;
if (result) {
// we're installed
console.log('already installed');
initPush();
} else {
// not installed
U.domReq(navigator.mozApps.install(gManifestName)).done(function() {
console.log("app installed successfully");
initPush();
}).fail(function(errObj) {
console.log("Couldn't install app (" + errObj.code + ") " + errObj.message);
Events.on('messageFromApp:push', onpush);
var installedApps = G.currentUser.installedApps,
currentApp = G.currentApp,
channelId = G.pushChannelId,
appInstall = G.currentAppInstall,
channels = G.currentApp.pushChannels,
endpointList = new ResourceList(G.currentUser.pushEndpoints, {
model: U.getModel(G.commonTypes.PushEndpoint),
query: $.param({
appInstall: appInstall
})
});

if (!pushChannels || !pushChannels.length)
return;

for (var i = 0; i < pushChannels.length; i++) {
var channel = pushChannels[i].channel;
if (endpointList.where({endpoint: channel}).length) {
console.log('PUSH ENDPOINT ALREADY EXISTS FOR CHANNEL:', channel);
return;
}
}).fail(function(err) {
console('Error checking installation status: ' + err.message);
else {
firefox.push.register(function(endpoint) {
Events.trigger('newPushEndpoint', endpoint, channel);
}, function() {
console.log("FAILED TO REGISTER PUSH CHANNEL");
});
}
}

firefox.setMessageHandler('push', onpush);
firefox.setMessageHandler('push-register', function(e) {
debugger;
if (!G.currentUser.guest)
firefox._setup();
});

Events.trigger('newPushEndpoint', channelId);
},
mozNotification: {
/**
Expand All @@ -132,21 +136,36 @@ define('firefox', ['globals', 'jquery', 'Events', 'utils'], function(G, $, Event
[].unshift.call(args, this._path);
U.rpc.apply(this._path, args);
}
},
push: {
register: function(success, error) {
U.rpc.call(this._path, createCallbackEvent(success || function() {}), createCallbackEvent(error || function() {}));
},
unregister: function(endpoint) {
U.rpc.call(this._path, endpoint, createCallbackEvent(success || function() {}), createCallbackEvent(error || function() {}));
},
registrations: function() {
U.rpc.call(this._path, createCallbackEvent(success || function() {}), createCallbackEvent(error || function() {}));
}
},
setMessageHandler: function(messageType, callback) {
U.rpc.call(this._path, messageType, createCallbackEvent(callback));
}
};


setPaths(firefox);

// TESTING //
console.log("creating notification");
firefox.mozNotification.createNotification("Hello Mark", "This is for your eyes only", "icon_128.png", {
onclose: function() {
console.log("closed notification");
},
onclick: function() {
console.log("clicked notification");
}
});
// console.log("creating notification");
// firefox.mozNotification.createNotification("Hello Mark", "This is for your eyes only", "icon_128.png", {
// onclose: function() {
// console.log("closed notification");
// },
// onclick: function() {
// console.log("clicked notification");
// }
// });
// END TESTING //
return firefox;
});
2 changes: 1 addition & 1 deletion js/views/ChatPage.js
Expand Up @@ -202,7 +202,7 @@ define('views/ChatPage', [
this.isPrivate = U.isPrivateChat();
this.isAgent = this.hashParams['-agent'] === 'y';
this.isClient = !this.isAgent;
this.textOnly = /^chat\//.test(this.hash);
this.textOnly = /^chat\//.test(this.hash) || !G.canWebcam;
this.hasVideo = !this.textOnly && (this.isPrivate || this.isClient); // HACK, waiting room might not have video
this.hasAudio = !this.textOnly && (this.hasVideo || this.isPrivate);
this.config = {
Expand Down
2 changes: 1 addition & 1 deletion js/views/EditView.js
Expand Up @@ -918,7 +918,7 @@ define('views/EditView', [
resource = null;
}

this.getInputs().attr('disabled', fale);
this.getInputs().attr('disabled', false);
var code = xhr ? xhr.code || xhr.status : 0;
if (!code || xhr.statusText === 'error') {
Errors.errDialog({msg: 'There was en error with your request, please try again', delay: 100});
Expand Down

0 comments on commit ec5a35e

Please sign in to comment.