diff --git a/services/aitc/modules/storage.js b/services/aitc/modules/storage.js index efe99fbef7b..152bebebc1a 100644 --- a/services/aitc/modules/storage.js +++ b/services/aitc/modules/storage.js @@ -16,6 +16,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://services-common/log4moz.js"); Cu.import("resource://services-common/preferences.js"); Cu.import("resource://services-common/rest.js"); +Cu.import("resource://services-common/utils.js"); /** * Provides a file-backed queue. Currently used by manager.js as persistent @@ -43,23 +44,17 @@ function AitcQueue(filename, cb) { this._queue = []; this._writeLock = false; - this._file = FileUtils.getFile("ProfD", ["webapps", filename], true); + this._filePath = "webapps/" + filename; this._log.info("AitcQueue instance loading"); - let self = this; - if (this._file.exists()) { - this._getFile(function gotFile(data) { - if (data && Array.isArray(data)) { - self._queue = data; - } - self._log.info("AitcQueue instance created"); - cb(true); - }); - } else { - self._log.info("AitcQueue instance created"); + CommonUtils.jsonLoad(this._filePath, this, function jsonLoaded(data) { + if (data && Array.isArray(data)) { + this._queue = data; + } + this._log.info("AitcQueue instance created"); cb(true); - } + }); } AitcQueue.prototype = { /** @@ -144,36 +139,6 @@ AitcQueue.prototype = { return this._queue.length; }, - /** - * Get contents of cache file and parse it into an array. Will throw an - * exception if there is an error while reading the file. - */ - _getFile: function _getFile(cb) { - let channel = NetUtil.newChannel(this._file); - channel.contentType = "application/json"; - - let self = this; - NetUtil.asyncFetch(channel, function _asyncFetched(stream, res) { - if (!Components.isSuccessCode(res)) { - self._log.error("Could not read from json file " + this._file.path); - cb(null); - return; - } - - let data = []; - try { - data = JSON.parse( - NetUtil.readInputStreamToString(stream, stream.available()) - ); - stream.close(); - cb(data); - } catch (e) { - self._log.error("Could not parse JSON " + e); - cb(null); - } - }); - }, - /** * Put an array into the cache file. Will throw an exception if there is * an error while trying to write to the file. @@ -184,32 +149,18 @@ AitcQueue.prototype = { } this._writeLock = true; - try { - let ostream = FileUtils.openSafeFileOutputStream(this._file); - - let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. - createInstance(Ci.nsIScriptableUnicodeConverter); - converter.charset = "UTF-8"; - let istream = converter.convertToInputStream(JSON.stringify(value)); - - // Asynchronously copy the data to the file. - let self = this; - this._log.info("Writing queue to disk"); - NetUtil.asyncCopy(istream, ostream, function _asyncCopied(result) { - self._writeLock = false; - if (Components.isSuccessCode(result)) { - self._log.info("asyncCopy succeeded"); - cb(null); - } else { - let msg = new Error("asyncCopy failed with " + result); - self._log.info(msg); - cb(msg); - } - }); - } catch (e) { + this._log.info("Writing queue to disk"); + CommonUtils.jsonSave(this._filePath, this, value, function jsonSaved(err) { + if (err) { + let msg = new Error("_putFile failed with " + err); + this._writeLock = false; + cb(msg); + return; + } + this._log.info("_putFile succeeded"); this._writeLock = false; - cb(msg); - } + cb(null); + }); }, }; diff --git a/services/aitc/tests/unit/test_storage_queue.js b/services/aitc/tests/unit/test_storage_queue.js index 21ddf41708d..3b7849f3da9 100644 --- a/services/aitc/tests/unit/test_storage_queue.js +++ b/services/aitc/tests/unit/test_storage_queue.js @@ -7,6 +7,7 @@ Cu.import("resource://services-common/async.js"); let queue = null; function run_test() { + initTestLogging(); queue = new AitcQueue("test", run_next_test); } @@ -97,7 +98,6 @@ add_test(function test_queue_multiaddremove() { }); }); -/* TODO Bug 760905 - Temporarily disabled for orange. add_test(function test_queue_writelock() { // Queue should not enqueue or dequeue if lock is enabled. queue._writeLock = true; @@ -114,4 +114,3 @@ add_test(function test_queue_writelock() { }); }); }); -*/