public
anonymous / push-b2g-system.txt
Created

push-b2g-system

  • Download Gist
push-b2g-system.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.