Bug 776600 - Allow passing some default prefs from the manfiest to the webapp. r=mfinkle

This commit is contained in:
Wes Johnston 2012-08-08 12:35:15 -07:00
parent 00379c59e1
commit 3301706c7e
2 changed files with 88 additions and 22 deletions

View File

@ -6,6 +6,8 @@ let Ci = Components.interfaces;
let Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
function pref(name, value) {
return {
@ -15,6 +17,8 @@ function pref(name, value) {
}
var WebAppRT = {
DEFAULT_PREFS_FILENAME: "default-prefs.js",
prefs: [
// Disable all add-on locations other than the profile (which can't be disabled this way)
pref("extensions.enabledScopes", 1),
@ -26,13 +30,47 @@ var WebAppRT = {
pref("toolkit.telemetry.prompted", 2)
],
init: function(isUpdate) {
init: function(isUpdate, url) {
this.deck = document.getElementById("browsers");
this.deck.addEventListener("click", this, false, true);
// on first run, update any prefs
if (isUpdate == "new") {
this.prefs.forEach(function(aPref) {
this.getDefaultPrefs().forEach(this.addPref);
// update the blocklist url to use a different app id
let blocklist = Services.prefs.getCharPref("extensions.blocklist.url");
blocklist = blocklist.replace(/%APP_ID%/g, "webapprt-mobile@mozilla.org");
Services.prefs.setCharPref("extensions.blocklist.url", blocklist);
}
},
getDefaultPrefs: function() {
// read default prefs from the disk
try {
let defaultPrefs = [];
try {
defaultPrefs = this.readDefaultPrefs(FileUtils.getFile("ProfD", [this.DEFAULT_PREFS_FILENAME]));
} catch(ex) {
// this can throw if the defaultprefs.js file doesn't exist
}
for (let i = 0; i < defaultPrefs.length; i++) {
this.prefs.push(defaultPrefs[i]);
}
} catch(ex) {
console.log("Error reading defaultPrefs file: " + ex);
}
return this.prefs;
},
readDefaultPrefs: function webapps_readDefaultPrefs(aFile) {
let fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
fstream.init(aFile, -1, 0, 0);
let prefsString = NetUtil.readInputStreamToString(fstream, fstream.available(), {});
return JSON.parse(prefsString);
},
addPref: function(aPref) {
switch (typeof aPref.value) {
case "string":
Services.prefs.setCharPref(aPref.name, aPref.value);
@ -44,13 +82,6 @@ var WebAppRT = {
Services.prefs.setIntPref(aPref.name, aPref.value);
break;
}
});
// update the blocklist url to use a different app id
var blocklist = Services.prefs.getCharPref("extensions.blocklist.url");
blocklist = blocklist.replace(/%APP_ID%/g, "webapprt-mobile@mozilla.org");
Services.prefs.setCharPref("extensions.blocklist.url", blocklist);
}
},
handleEvent: function(event) {

View File

@ -27,6 +27,11 @@ XPCOMUtils.defineLazyGetter(this, "DebuggerServer", function() {
return DebuggerServer;
});
XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
Cu.import("resource://gre/modules/NetUtil.jsm");
return NetUtil;
});
// Lazily-loaded browser scripts:
[
["HelperApps", "chrome://browser/content/HelperApps.js"],
@ -5917,6 +5922,8 @@ var WebappsUI = {
Services.obs.removeObserver(this, "webapps-sync-uninstall");
},
DEFAULT_PREFS_FILENAME: "default-prefs.js",
observe: function observe(aSubject, aTopic, aData) {
let data = JSON.parse(aData);
switch (aTopic) {
@ -6008,8 +6015,8 @@ var WebappsUI = {
// Add a homescreen shortcut -- we can't use createShortcut, since we need to pass
// a unique ID for Android webapp allocation
this.makeBase64Icon(this.getBiggestIcon(manifest.icons, Services.io.newURI(aData.app.origin, null, null)),
function(icon) {
var profilePath = sendMessageToJava({
(function(icon) {
let profilePath = sendMessageToJava({
gecko: {
type: "WebApps:Install",
name: manifest.name,
@ -6020,18 +6027,46 @@ var WebappsUI = {
});
// if java returned a profile path to us, try to use it to pre-populate the app cache
var file = null;
let file = null;
if (profilePath) {
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(profilePath);
// build any app specific default prefs
let prefs = [];
if (manifest.orientation)
prefs.push({name:"app.orientation.default", value: manifest.orientation});
// write them into the app profile
let defaultPrefsFile = file.clone();
defaultPrefsFile.append(this.DEFAULT_PREFS_FILENAME);
this.writeDefaultPrefs(defaultPrefsFile, prefs);
}
DOMApplicationRegistry.confirmInstall(aData, false, file);
});
}).bind(this));
} else {
DOMApplicationRegistry.denyInstall(aData);
}
},
writeDefaultPrefs: function webapps_writeDefaultPrefs(aFile, aPrefs) {
if (aPrefs.length > 0) {
let data = JSON.stringify(aPrefs);
var ostream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
ostream.init(aFile, -1, -1, 0);
let istream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
istream.setData(data, data.length);
NetUtil.asyncCopy(istream, ostream, function(aResult) {
if (!Components.isSuccessCode(aResult)) {
console.log("Error writing default prefs: " + aResult);
}
});
}
},
openURL: function openURL(aURI, aOrigin) {
sendMessageToJava({
gecko: {