Bug 914369 - SimplePush: Add debug pref to allow toggling debug settings at runtime. r=dougt
authorNikhil Marathe <nsm.nikhil@gmail.com>
Mon, 16 Sep 2013 16:55:26 -0700
changeset 156053 a0ec683fa04256139c86232d67ec8bab019ad4a2
parent 156052 23de7d509e70cf9f4b2256f23e6d4ac373788257
child 156054 104e23b7018783aabca4f2d1fd5364fedeb0ab0b
push idunknown
push userunknown
push dateunknown
reviewersdougt
bugs914369
milestone26.0a1
Bug 914369 - SimplePush: Add debug pref to allow toggling debug settings at runtime. r=dougt
b2g/app/b2g.js
dom/push/src/Push.js
dom/push/src/PushService.jsm
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -395,16 +395,18 @@ pref("dom.phonenumber.substringmatching.
 pref("dom.phonenumber.substringmatching.CO", 10);
 pref("dom.phonenumber.substringmatching.VE", 7);
 
 // WebAlarms
 pref("dom.mozAlarms.enabled", true);
 
 // SimplePush
 pref("services.push.enabled", true);
+// Debugging enabled.
+pref("services.push.debug", false);
 // Is the network connection allowed to be up?
 // This preference should be used in UX to enable/disable push.
 pref("services.push.connection.enabled", true);
 // serverURL to be assigned by services team
 pref("services.push.serverURL", "wss://push.services.mozilla.com/");
 pref("services.push.userAgentID", "");
 // Exponential back-off start is 5 seconds like in HTTP/1.1.
 // Maximum back-off is pingInterval.
--- a/dom/push/src/Push.js
+++ b/dom/push/src/Push.js
@@ -1,16 +1,20 @@
 /* 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/. */
 
 "use strict";
 
+// Don't modify this, instead set services.push.debug.
+let gDebuggingEnabled = false;
+
 function debug(s) {
-  // dump("-*- Push.js: " + s + "\n");
+  if (gDebuggingEnabled)
+    dump("-*- Push.js: " + s + "\n");
 }
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
@@ -34,16 +38,20 @@ Push.prototype = {
   contractID: "@mozilla.org/push/PushManager;1",
 
   classID : PUSH_CID,
 
   QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
                                           Ci.nsISupportsWeakReference]),
 
   init: function(aWindow) {
+    // Set debug first so that all debugging actually works.
+    // NOTE: We don't add an observer here like in PushService. Flipping the
+    // pref will require a reload of the app/page, which seems acceptable.
+    gDebuggingEnabled = Services.prefs.getBoolPref("services.push.debug");
     debug("init()");
 
     let principal = aWindow.document.nodePrincipal;
     let appsService = Cc["@mozilla.org/AppsService;1"]
                         .getService(Ci.nsIAppsService);
 
     this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
     this._pageURL = principal.URI;
--- a/dom/push/src/PushService.jsm
+++ b/dom/push/src/PushService.jsm
@@ -1,16 +1,20 @@
 /* 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/. */
 
 "use strict";
 
+// Don't modify this, instead set services.push.debug.
+let gDebuggingEnabled = false;
+
 function debug(s) {
-  // dump("-*- PushService.jsm: " + s + "\n");
+  if (gDebuggingEnabled)
+    dump("-*- PushService.jsm: " + s + "\n");
 }
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 const Cr = Components.results;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
@@ -21,16 +25,18 @@ Cu.import("resource://gre/modules/Prefer
 Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
 
 XPCOMUtils.defineLazyModuleGetter(this, "AlarmService",
                                   "resource://gre/modules/AlarmService.jsm");
 
 this.EXPORTED_SYMBOLS = ["PushService"];
 
 const prefs = new Preferences("services.push.");
+// Set debug first so that all debugging actually works.
+gDebuggingEnabled = prefs.get("debug");
 
 const kPUSHDB_DB_NAME = "push";
 const kPUSHDB_DB_VERSION = 1; // Change this if the IndexedDB format changes
 const kPUSHDB_STORE_NAME = "push";
 
 const kUDP_WAKEUP_WS_STATUS_CODE = 4774;  // WebSocket Close status code sent
                                           // by server to signal that it can
                                           // wake client up using UDP.
@@ -313,16 +319,18 @@ this.PushService = {
                 prefs.get("serverURL"));
           this._shutdownWS();
         } else if (aData == "services.push.connection.enabled") {
           if (prefs.get("connection.enabled")) {
             this._startListeningIfChannelsPresent();
           } else {
             this._shutdownWS();
           }
+        } else if (aData == "services.push.debug") {
+          gDebuggingEnabled = prefs.get("debug");
         }
         break;
       case "timer-callback":
         if (aSubject == this._requestTimeoutTimer) {
           if (Object.keys(this._pendingRequests).length == 0)
             this._requestTimeoutTimer.cancel();
 
           for (let channelID in this._pendingRequests) {
@@ -462,16 +470,18 @@ this.PushService = {
     // don't add an observer for offline-status-changed on B2G.
     Services.obs.addObserver(this, this._getNetworkStateChangeEventName(), false);
 
     // This is only used for testing. Different tests require connecting to
     // slightly different URLs.
     prefs.observe("serverURL", this);
     // Used to monitor if the user wishes to disable Push.
     prefs.observe("connection.enabled", this);
+    // Debugging
+    prefs.observe("debug", this);
 
     this._started = true;
   },
 
   _shutdownWS: function() {
     debug("shutdownWS()");
     this._currentState = STATE_SHUT_DOWN;
     this._willBeWokenUpByUDP = false;
@@ -488,16 +498,17 @@ this.PushService = {
   },
 
   uninit: function() {
     if (!this._started)
       return;
 
     debug("uninit()");
 
+    prefs.ignore("debug", this);
     prefs.ignore("connection.enabled", this);
     prefs.ignore("serverURL", this);
     Services.obs.removeObserver(this, this._getNetworkStateChangeEventName());
     Services.obs.removeObserver(this, "webapps-uninstall", false);
     Services.obs.removeObserver(this, "xpcom-shutdown", false);
 
     if (this._db) {
       this._db.close();