From 5babe72585cae8e99427fc1378c1e1c9a7add049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabrice=20Desr=C3=A9?= Date: Sat, 6 Oct 2012 22:15:25 -0700 Subject: [PATCH] Bug 790849 - Don't store information in /data about built-in apps before first startup - Move settings db out of prebuild files. [r=gwagner] --- dom/settings/SettingsDB.jsm | 62 +++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/dom/settings/SettingsDB.jsm b/dom/settings/SettingsDB.jsm index 812ae0b1965..fd6f3b7e7c0 100644 --- a/dom/settings/SettingsDB.jsm +++ b/dom/settings/SettingsDB.jsm @@ -2,21 +2,23 @@ * 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/. */ +let Cc = Components.classes; +let Ci = Components.interfaces; +let Cu = Components.utils; + let EXPORTED_SYMBOLS = ["SettingsDB", "SETTINGSDB_NAME", "SETTINGSSTORE_NAME"]; -/* static functions */ -let DEBUG = 0; -if (DEBUG) { - debug = function (s) { dump("-*- SettingsDB: " + s + "\n"); } -} else { - debug = function (s) {} +function debug(s) { + //dump("-*- SettingsDB: " + s + "\n"); } const SETTINGSDB_NAME = "settings"; const SETTINGSDB_VERSION = 1; const SETTINGSSTORE_NAME = "settings"; -Components.utils.import("resource://gre/modules/IndexedDBHelper.jsm"); +Cu.import("resource://gre/modules/IndexedDBHelper.jsm"); +Cu.import("resource://gre/modules/FileUtils.jsm"); +Cu.import("resource://gre/modules/NetUtil.jsm"); function SettingsDB() {} @@ -25,12 +27,54 @@ SettingsDB.prototype = { __proto__: IndexedDBHelper.prototype, upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) { - let objectStore = aDb.createObjectStore(SETTINGSSTORE_NAME, { keyPath: "settingName" }); + let objectStore = aDb.createObjectStore(SETTINGSSTORE_NAME, + { keyPath: "settingName" }); objectStore.createIndex("settingValue", "settingValue", { unique: false }); debug("Created object stores and indexes"); + + if (aOldVersion != 0) { + return; + } + + // Loading resource://app/defaults/settings.json doesn't work because + // settings.json is not in the omnijar. + // So we look for the app dir instead and go from here... + let settingsFile = FileUtils.getFile("DefRt", ["settings.json"], false); + if (!settingsFile || (settingsFile && !settingsFile.exists())) { + // On b2g desktop builds the settings.json file is moved in the + // profile directory by the build system. + settingsFile = FileUtils.getFile("ProfD", ["settings.json"], false); + if (!settingsFile || (settingsFile && !settingsFile.exists())) { + return; + } + } + + let chan = NetUtil.newChannel(settingsFile); + let stream = chan.open(); + // Obtain a converter to read from a UTF-8 encoded input stream. + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Ci.nsIScriptableUnicodeConverter); + converter.charset = "UTF-8"; + let rawstr = converter.ConvertToUnicode(NetUtil.readInputStreamToString( + stream, + stream.available()) || ""); + let settings; + try { + settings = JSON.parse(rawstr); + } catch(e) { + debug("Error parsing " + settingsFile.path + " : " + e); + return; + } + + for (let setting in settings) { + debug("Adding setting " + setting); + objectStore.put({ settingName: setting, + settingValue: settings[setting] }); + } }, init: function init(aGlobal) { - this.initDBHelper(SETTINGSDB_NAME, SETTINGSDB_VERSION, SETTINGSSTORE_NAME, aGlobal); + this.initDBHelper(SETTINGSDB_NAME, SETTINGSDB_VERSION, + SETTINGSSTORE_NAME, aGlobal); } }