Skip to content

Instantly share code, notes, and snippets.

Created April 3, 2013 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5305425 to your computer and use it in GitHub Desktop.
Save anonymous/5305425 to your computer and use it in GitHub Desktop.
push-b2g-system
diff --git a/apps/system/index.html b/apps/system/index.html
index e82bb3c..95fb8bd 100644
--- a/apps/system/index.html
+++ b/apps/system/index.html
@@ -146,6 +146,9 @@
<link rel="stylesheet" type="text/css" href="style/app_install_manager/app_install_manager.css">
<script defer src="js/app_install_manager.js"></script>
+ <!-- Push Notifications for app installation -->
+ <script defer src="js/push_to_install.js"></script>
+
<!-- Cost Control -->
<link rel="stylesheet" type="text/css" href="style/cost_control/cost_control.css">
<script defer src="js/cost_control.js"></script>
diff --git a/apps/system/js/push_to_install.js b/apps/system/js/push_to_install.js
new file mode 100644
index 0000000..d677f05
--- /dev/null
+++ b/apps/system/js/push_to_install.js
@@ -0,0 +1,110 @@
+/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
+/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
+
+'use strict';
+
+function debug(str) {
+ dump("PushToInstall: " + str);
+}
+
+var PushToInstall = {
+ init: function() {
+ debug("init()");
+ if (!navigator.mozId) {
+ debug("No persona support!");
+ return;
+ }
+ this._email = null;
+ window.addEventListener('load', this._requestLogin.bind(this));
+ },
+
+ _requestLogin: function() {
+ debug("_requestLogin()");
+ navigator.mozId.watch({
+ loggedInUser: 'nsm.nikhil@gmail.com',
+ onlogin: function(assertion) {
+ debug("onlogin()");
+ var xhr = new XMLHttpRequest({mozSystem:true});
+ xhr.open('POST', 'http://pushtophone.nikhilism.com/login', true);
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
+ xhr.onreadystatechange = function() {
+ debug("login assertion XHR status " + xhr.status + " " + xhr.statusText);
+ if (xhr.readyState == 4 && xhr.status == 200) {
+ this._email = xhr.response;
+ this._monitorPush();
+ }
+ else {
+ this._email = null;
+ }
+ }.bind(this);
+ xhr.send('assertion=' + assertion);
+ }.bind(this),
+ onlogout: function() {
+ debug("onlogout called");
+ this._email = null;
+ debug("Requesting login");
+ navigator.mozId.request({siteName: 'Push to Install'});
+ }.bind(this),
+ onready: function() {
+ debug("onready: Requesting login");
+ navigator.mozId.request({siteName: 'Push to Install'});
+ }
+ });
+ },
+
+ _monitorPush: function() {
+ if (!this._email) {
+ debug("No email to associate with push. Not registering");
+ return;
+ }
+ var req = navigator.push.register();
+ req.onsuccess = function() {
+ debug("Endpoint: " + req.result);
+ var xhr = new XMLHttpRequest({mozSystem:true});
+ xhr.open('POST', 'http://pushtophone.nikhilism.com/endpoint', true);
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4) {
+ debug("XHR status " + xhr.status + " " + xhr.statusText);
+ if (xhr.responseText == 'OK') {
+ // now wait for push
+ clearInterval(this.interval);
+ }
+ else {
+ debug("XHR error " + xhr.status);
+ }
+ }
+ }.bind(this);
+ xhr.send('endpoint=' + req.result + '&email=' + this._email);
+ }.bind(this);
+
+ req.onerror = function() {
+ debug("Push registration error! " + arguments);
+ }
+
+ navigator.mozSetMessageHandler('push', this.onPush.bind(this));
+ },
+
+ onPush: function() {
+ debug("onPush()");
+ var xhr = new XMLHttpRequest({mozSystem:true});
+ xhr.open('GET', 'http://pushtophone.nikhilism.com/manifest', true);
+ xhr.responseType = "text";
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4) {
+ if (xhr.status == 200 || xhr.status == 0) {
+ var manifest = xhr.response;
+ if (!manifest) {
+ debug("No manifest!");
+ return;
+ }
+ debug("Got manifest " + manifest);
+ navigator.mozApps.install(manifest);
+ }
+ }
+ }
+ xhr.send();
+ }
+};
+
+PushToInstall.init();
diff --git a/apps/system/manifest.webapp b/apps/system/manifest.webapp
index d332f8a..c1f2867 100644
--- a/apps/system/manifest.webapp
+++ b/apps/system/manifest.webapp
@@ -35,7 +35,9 @@
"audio-channel-notification":{},
"audio-channel-content":{},
"cellbroadcast":{},
- "keyboard":{}
+ "keyboard":{},
+ "push":{},
+ "systemXHR":{}
},
"locales": {
"ar": {
@@ -66,6 +68,8 @@
{ "bluetooth-opp-receiving-file-confirmation": "/index.html" },
{ "bluetooth-opp-transfer-start": "/index.html" },
{ "icc-stkcommand": "/index.html" },
- { "bluetooth-hfp-status-changed": "/index.html" }
+ { "bluetooth-hfp-status-changed": "/index.html" },
+ { "push": "/index.html" },
+ { "push-register": "/index.html" }
]
}
diff --git a/build/payment-prefs.js b/build/payment-prefs.js
index 214fd4e..1e43452 100644
--- a/build/payment-prefs.js
+++ b/build/payment-prefs.js
@@ -3,3 +3,6 @@ pref("dom.payment.provider.0.description", "marketplace.firefox.com");
pref("dom.payment.provider.0.uri", "https://marketplace.firefox.com/mozpay/?req=");
pref("dom.payment.provider.0.type", "mozilla/payments/pay/v1");
pref("dom.payment.provider.0.requestMethod", "GET");
+
+pref("toolkit.identity.debug", true);
+pref("dom.identity.syntheticEventsOk", true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment