From 9a7dd0a8cd9fe010a9eb65f6984329e081478e3c Mon Sep 17 00:00:00 2001 From: Gene Lian Date: Thu, 12 Jun 2014 16:33:56 +0800 Subject: [PATCH] Bug 1024362 - Correct the Alarm API's coding style. r=henry --- dom/alarm/AlarmDB.jsm | 105 +++++++++++++++------------------- dom/alarm/AlarmService.jsm | 112 ++++++++++++++++++------------------- dom/alarm/AlarmsManager.js | 42 +++++++------- 3 files changed, 117 insertions(+), 142 deletions(-) diff --git a/dom/alarm/AlarmDB.jsm b/dom/alarm/AlarmDB.jsm index e36cc52c9cb..1cf645a8896 100644 --- a/dom/alarm/AlarmDB.jsm +++ b/dom/alarm/AlarmDB.jsm @@ -36,17 +36,20 @@ AlarmDB.prototype = { this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, [ALARMSTORE_NAME]); }, - upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) { + upgradeSchema: function upgradeSchema(aTransaction, aDb, + aOldVersion, aNewVersion) { debug("upgradeSchema()"); - let objectStore = aDb.createObjectStore(ALARMSTORE_NAME, { keyPath: "id", autoIncrement: true }); + let objStore = + aDb.createObjectStore(ALARMSTORE_NAME, + { keyPath: "id", autoIncrement: true }); - objectStore.createIndex("date", "date", { unique: false }); - objectStore.createIndex("ignoreTimezone", "ignoreTimezone", { unique: false }); - objectStore.createIndex("timezoneOffset", "timezoneOffset", { unique: false }); - objectStore.createIndex("data", "data", { unique: false }); - objectStore.createIndex("pageURL", "pageURL", { unique: false }); - objectStore.createIndex("manifestURL", "manifestURL", { unique: false }); + objStore.createIndex("date", "date", { unique: false }); + objStore.createIndex("ignoreTimezone", "ignoreTimezone", { unique: false }); + objStore.createIndex("timezoneOffset", "timezoneOffset", { unique: false }); + objStore.createIndex("data", "data", { unique: false }); + objStore.createIndex("pageURL", "pageURL", { unique: false }); + objStore.createIndex("manifestURL", "manifestURL", { unique: false }); debug("Created object stores and indexes"); }, @@ -62,19 +65,13 @@ AlarmDB.prototype = { add: function add(aAlarm, aSuccessCb, aErrorCb) { debug("add()"); - this.newTxn( - "readwrite", - ALARMSTORE_NAME, - function txnCb(aTxn, aStore) { - debug("Going to add " + JSON.stringify(aAlarm)); - aStore.put(aAlarm).onsuccess = function setTxnResult(aEvent) { - aTxn.result = aEvent.target.result; - debug("Request successful. New record ID: " + aTxn.result); - }; - }, - aSuccessCb, - aErrorCb - ); + this.newTxn("readwrite", ALARMSTORE_NAME, function txnCb(aTxn, aStore) { + debug("Going to add " + JSON.stringify(aAlarm)); + aStore.put(aAlarm).onsuccess = function setTxnResult(aEvent) { + aTxn.result = aEvent.target.result; + debug("Request successful. New record ID: " + aTxn.result); + }; + }, aSuccessCb, aErrorCb); }, /** @@ -92,33 +89,27 @@ AlarmDB.prototype = { remove: function remove(aId, aManifestURL, aSuccessCb, aErrorCb) { debug("remove()"); - this.newTxn( - "readwrite", - ALARMSTORE_NAME, - function txnCb(aTxn, aStore) { - debug("Going to remove " + aId); + this.newTxn("readwrite", ALARMSTORE_NAME, function txnCb(aTxn, aStore) { + debug("Going to remove " + aId); - // Look up the existing record and compare the manifestURL - // to see if the alarm to be removed belongs to this app. - aStore.get(aId).onsuccess = function doRemove(aEvent) { - let alarm = aEvent.target.result; + // Look up the existing record and compare the manifestURL + // to see if the alarm to be removed belongs to this app. + aStore.get(aId).onsuccess = function doRemove(aEvent) { + let alarm = aEvent.target.result; - if (!alarm) { - debug("Alarm doesn't exist. No need to remove it."); - return; - } + if (!alarm) { + debug("Alarm doesn't exist. No need to remove it."); + return; + } - if (aManifestURL && aManifestURL != alarm.manifestURL) { - debug("Cannot remove the alarm added by other apps."); - return; - } + if (aManifestURL && aManifestURL != alarm.manifestURL) { + debug("Cannot remove the alarm added by other apps."); + return; + } - aStore.delete(aId); - }; - }, - aSuccessCb, - aErrorCb - ); + aStore.delete(aId); + }; + }, aSuccessCb, aErrorCb); }, /** @@ -134,22 +125,16 @@ AlarmDB.prototype = { getAll: function getAll(aManifestURL, aSuccessCb, aErrorCb) { debug("getAll()"); - this.newTxn( - "readonly", - ALARMSTORE_NAME, - function txnCb(aTxn, aStore) { - if (!aTxn.result) { - aTxn.result = []; - } + this.newTxn("readonly", ALARMSTORE_NAME, function txnCb(aTxn, aStore) { + if (!aTxn.result) { + aTxn.result = []; + } - let index = aStore.index("manifestURL"); - index.mozGetAll(aManifestURL).onsuccess = function setTxnResult(aEvent) { - aTxn.result = aEvent.target.result; - debug("Request successful. Record count: " + aTxn.result.length); - }; - }, - aSuccessCb, - aErrorCb - ); + let index = aStore.index("manifestURL"); + index.mozGetAll(aManifestURL).onsuccess = function setTxnResult(aEvent) { + aTxn.result = aEvent.target.result; + debug("Request successful. Record count: " + aTxn.result.length); + }; + }, aSuccessCb, aErrorCb); } }; diff --git a/dom/alarm/AlarmService.jsm b/dom/alarm/AlarmService.jsm index 3be2a13ef12..2cc80d21ffd 100644 --- a/dom/alarm/AlarmService.jsm +++ b/dom/alarm/AlarmService.jsm @@ -29,11 +29,13 @@ XPCOMUtils.defineLazyServiceGetter(this, "ppmm", "nsIMessageListenerManager"); XPCOMUtils.defineLazyGetter(this, "messenger", function() { - return Cc["@mozilla.org/system-message-internal;1"].getService(Ci.nsISystemMessagesInternal); + return Cc["@mozilla.org/system-message-internal;1"] + .getService(Ci.nsISystemMessagesInternal); }); XPCOMUtils.defineLazyGetter(this, "powerManagerService", function() { - return Cc["@mozilla.org/power/powermanagerservice;1"].getService(Ci.nsIPowerManagerService); + return Cc["@mozilla.org/power/powermanagerservice;1"] + .getService(Ci.nsIPowerManagerService); }); /** @@ -50,14 +52,14 @@ XPCOMUtils.defineLazyGetter(this, "powerManagerService", function() { this.AlarmService = { init: function init() { debug("init()"); + Services.obs.addObserver(this, "profile-change-teardown", false); Services.obs.addObserver(this, "webapps-clear-data",false); this._currentTimezoneOffset = (new Date()).getTimezoneOffset(); - let alarmHalService = - this._alarmHalService = Cc["@mozilla.org/alarmHalService;1"] - .getService(Ci.nsIAlarmHalService); + let alarmHalService = this._alarmHalService = + Cc["@mozilla.org/alarmHalService;1"].getService(Ci.nsIAlarmHalService); alarmHalService.setAlarmFiredCb(this._onAlarmFired.bind(this)); alarmHalService.setTimezoneChangedCb(this._onTimezoneChanged.bind(this)); @@ -109,6 +111,7 @@ this.AlarmService = { debug("Got message from a child process with no 'alarms' permission."); return null; } + if (!aMessage.target.assertContainApp(json.manifestURL)) { debug("Got message from a child process containing illegal manifest URL."); return null; @@ -116,37 +119,34 @@ this.AlarmService = { } let mm = aMessage.target.QueryInterface(Ci.nsIMessageSender); + switch (aMessage.name) { case "AlarmsManager:GetAll": - this._db.getAll( - json.manifestURL, + this._db.getAll(json.manifestURL, function getAllSuccessCb(aAlarms) { debug("Callback after getting alarms from database: " + JSON.stringify(aAlarms)); + this._sendAsyncMessage(mm, "GetAll", true, json.requestId, aAlarms); }.bind(this), function getAllErrorCb(aErrorMsg) { this._sendAsyncMessage(mm, "GetAll", false, json.requestId, aErrorMsg); - }.bind(this) - ); + }.bind(this)); break; case "AlarmsManager:Add": // Prepare a record for the new alarm to be added. - let newAlarm = { - date: json.date, - ignoreTimezone: json.ignoreTimezone, - data: json.data, - pageURL: json.pageURL, - manifestURL: json.manifestURL - }; + let newAlarm = { date: json.date, + ignoreTimezone: json.ignoreTimezone, + data: json.data, + pageURL: json.pageURL, + manifestURL: json.manifestURL }; this.add(newAlarm, null, // Receives the alarm ID as the last argument. this._sendAsyncMessage.bind(this, mm, "Add", true, json.requestId), // Receives the error message as the last argument. - this._sendAsyncMessage.bind(this, mm, "Add", false, json.requestId) - ); + this._sendAsyncMessage.bind(this, mm, "Add", false, json.requestId)); break; case "AlarmsManager:Remove": @@ -169,8 +169,7 @@ this.AlarmService = { } let json = null; - switch (aMessageName) - { + switch (aMessageName) { case "Add": json = aSuccess ? { requestId: aRequestId, id: aData } : @@ -189,7 +188,8 @@ this.AlarmService = { } aMessageManager.sendAsyncMessage("AlarmsManager:" + aMessageName + - ":Return:" + (aSuccess ? "OK" : "KO"), json); + ":Return:" + (aSuccess ? "OK" : "KO"), + json); }, _removeAlarmFromDb: function _removeAlarmFromDb(aId, aManifestURL, @@ -204,14 +204,10 @@ this.AlarmService = { }; } - this._db.remove( - aId, - aManifestURL, - aRemoveSuccessCb, - function removeErrorCb(aErrorMsg) { - throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - } - ); + this._db.remove(aId, aManifestURL, aRemoveSuccessCb, + function removeErrorCb(aErrorMsg) { + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + }); }, /** @@ -220,13 +216,11 @@ this.AlarmService = { * boolean |ignoreTimezone| field. */ _publicAlarm: function _publicAlarm(aAlarm) { - let alarm = { - "id": aAlarm.id, - "date": aAlarm.date, - "respectTimezone": aAlarm.ignoreTimezone ? - "ignoreTimezone" : "honorTimezone", - "data": aAlarm.data - }; + let alarm = { "id": aAlarm.id, + "date": aAlarm.date, + "respectTimezone": aAlarm.ignoreTimezone ? + "ignoreTimezone" : "honorTimezone", + "data": aAlarm.data }; return alarm; }, @@ -237,8 +231,10 @@ this.AlarmService = { let manifestURI = Services.io.newURI(aAlarm.manifestURL, null, null); let pageURI = Services.io.newURI(aAlarm.pageURL, null, null); - messenger.sendMessage("alarm", this._publicAlarm(aAlarm), - pageURI, manifestURI); + messenger.sendMessage("alarm", + this._publicAlarm(aAlarm), + pageURI, + manifestURI); }, _notifyAlarmObserver: function _notifyAlarmObserver(aAlarm) { @@ -276,6 +272,7 @@ this.AlarmService = { break; } } + this._debugCurrentAlarm(); }, @@ -289,8 +286,7 @@ this.AlarmService = { _restoreAlarmsFromDb: function _restoreAlarmsFromDb() { debug("_restoreAlarmsFromDb()"); - this._db.getAll( - null, + this._db.getAll(null, function getAllSuccessCb(aAlarms) { debug("Callback after getting alarms from database: " + JSON.stringify(aAlarms)); @@ -311,7 +307,7 @@ this.AlarmService = { } }.bind(this)); - // Set the next alarm from queue. + // Set the next alarm from the queue. if (alarmQueue.length) { alarmQueue.sort(this._sortAlarmByTimeStamps.bind(this)); this._currentAlarm = alarmQueue.shift(); @@ -321,8 +317,7 @@ this.AlarmService = { }.bind(this), function getAllErrorCb(aErrorMsg) { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - } - ); + }); }, _getAlarmTime: function _getAlarmTime(aAlarm) { @@ -341,7 +336,8 @@ this.AlarmService = { // is located at Paris. We can adjust the alarm UTC time by calculating // the difference of the orginal timezone and the current timezone. if (aAlarm.ignoreTimezone) { - alarmTime += (this._currentTimezoneOffset - aAlarm.timezoneOffset) * 60000; + alarmTime += + (this._currentTimezoneOffset - aAlarm.timezoneOffset) * 60000; } return alarmTime; }, @@ -402,8 +398,7 @@ this.AlarmService = { aNewAlarm['timezoneOffset'] = this._currentTimezoneOffset; - this._db.add( - aNewAlarm, + this._db.add(aNewAlarm, function addSuccessCb(aNewId) { debug("Callback after adding alarm in database."); @@ -422,7 +417,7 @@ this.AlarmService = { } // If the new alarm is earlier than the current alarm, swap them and - // push the previous alarm back to queue. + // push the previous alarm back to the queue. let alarmQueue = this._alarmQueue; let aNewAlarmTime = this._getAlarmTime(aNewAlarm); let currentAlarmTime = this._getAlarmTime(this._currentAlarm); @@ -442,8 +437,7 @@ this.AlarmService = { }.bind(this), function addErrorCb(aErrorMsg) { aErrorCb(aErrorMsg); - }.bind(this) - ); + }.bind(this)); }, /* @@ -457,9 +451,8 @@ this.AlarmService = { */ remove: function(aAlarmId, aManifestURL) { debug("remove(" + aAlarmId + ", " + aManifestURL + ")"); - this._removeAlarmFromDb( - aAlarmId, - aManifestURL, + + this._removeAlarmFromDb(aAlarmId, aManifestURL, function removeSuccessCb() { debug("Callback after removing alarm from database."); @@ -488,7 +481,7 @@ this.AlarmService = { } // The alarm to be removed is the current alarm reset the next alarm - // from queue if any. + // from the queue if any. if (alarmQueue.length) { this._currentAlarm = alarmQueue.shift(); this._debugCurrentAlarm(); @@ -498,15 +491,17 @@ this.AlarmService = { // No alarm waiting to be set in the queue. this._currentAlarm = null; this._debugCurrentAlarm(); - }.bind(this) - ); + }.bind(this)); }, observe: function(aSubject, aTopic, aData) { + debug("observe(): " + aTopic); + switch (aTopic) { case "profile-change-teardown": this.uninit(); break; + case "webapps-clear-data": let params = aSubject.QueryInterface(Ci.mozIApplicationClearPrivateDataParams); @@ -526,8 +521,7 @@ this.AlarmService = { return; } - this._db.getAll( - manifestURL, + this._db.getAll(manifestURL, function getAllSuccessCb(aAlarms) { aAlarms.forEach(function removeAlarm(aAlarm) { this.remove(aAlarm.id, manifestURL); @@ -535,14 +529,14 @@ this.AlarmService = { }.bind(this), function getAllErrorCb(aErrorMsg) { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - } - ); + }); break; } }, uninit: function uninit() { debug("uninit()"); + Services.obs.removeObserver(this, "profile-change-teardown"); Services.obs.removeObserver(this, "webapps-clear-data"); diff --git a/dom/alarm/AlarmsManager.js b/dom/alarm/AlarmsManager.js index 23daab1ff0c..edb60ab55ac 100644 --- a/dom/alarm/AlarmsManager.js +++ b/dom/alarm/AlarmsManager.js @@ -18,13 +18,11 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); -function AlarmsManager() -{ +function AlarmsManager() { debug("Constructor"); } AlarmsManager.prototype = { - __proto__: DOMRequestIpcHelper.prototype, contractID : "@mozilla.org/alarmsManager;1", @@ -48,6 +46,7 @@ AlarmsManager.prototype = { } let isIgnoreTimezone = true; + switch (aRespectTimezone) { case "honorTimezone": isIgnoreTimezone = false; @@ -63,35 +62,30 @@ AlarmsManager.prototype = { } let request = this.createRequest(); - this._cpmm.sendAsyncMessage( - "AlarmsManager:Add", - { requestId: this.getRequestId(request), - date: aDate, - ignoreTimezone: isIgnoreTimezone, - data: aData, - pageURL: this._pageURL, - manifestURL: this._manifestURL } - ); + this._cpmm.sendAsyncMessage("AlarmsManager:Add", + { requestId: this.getRequestId(request), + date: aDate, + ignoreTimezone: isIgnoreTimezone, + data: aData, + pageURL: this._pageURL, + manifestURL: this._manifestURL }); return request; }, remove: function remove(aId) { debug("remove()"); - this._cpmm.sendAsyncMessage( - "AlarmsManager:Remove", - { id: aId, manifestURL: this._manifestURL } - ); + this._cpmm.sendAsyncMessage("AlarmsManager:Remove", + { id: aId, manifestURL: this._manifestURL }); }, getAll: function getAll() { debug("getAll()"); let request = this.createRequest(); - this._cpmm.sendAsyncMessage( - "AlarmsManager:GetAll", - { requestId: this.getRequestId(request), manifestURL: this._manifestURL } - ); + this._cpmm.sendAsyncMessage("AlarmsManager:GetAll", + { requestId: this.getRequestId(request), + manifestURL: this._manifestURL }); return request; }, @@ -115,13 +109,14 @@ AlarmsManager.prototype = { // We don't need to expose everything to the web content. let alarms = []; json.alarms.forEach(function trimAlarmInfo(aAlarm) { - let alarm = { "id": aAlarm.id, - "date": aAlarm.date, + let alarm = { "id": aAlarm.id, + "date": aAlarm.date, "respectTimezone": aAlarm.ignoreTimezone ? "ignoreTimezone" : "honorTimezone", - "data": aAlarm.data }; + "data": aAlarm.data }; alarms.push(alarm); }); + Services.DOMRequest.fireSuccess(request, Cu.cloneInto(alarms, this._window)); break; @@ -138,6 +133,7 @@ AlarmsManager.prototype = { debug("Wrong message: " + aMessage.name); break; } + this.removeRequest(json.requestId); },