diff --git a/mobile/android/chrome/content/WebAppRT.js b/mobile/android/chrome/content/WebAppRT.js index d81c1c6259c..c6a96e3929a 100644 --- a/mobile/android/chrome/content/WebAppRT.js +++ b/mobile/android/chrome/content/WebAppRT.js @@ -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,33 +30,60 @@ 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) { - switch (typeof aPref.value) { - case "string": - Services.prefs.setCharPref(aPref.name, aPref.value); - break; - case "boolean": - Services.prefs.setBoolPref(aPref.name, aPref.value); - break; - case "number": - Services.prefs.setIntPref(aPref.name, aPref.value); - break; - } - }); + this.getDefaultPrefs().forEach(this.addPref); // update the blocklist url to use a different app id - var blocklist = Services.prefs.getCharPref("extensions.blocklist.url"); + 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); + break; + case "boolean": + Services.prefs.setBoolPref(aPref.name, aPref.value); + break; + case "number": + Services.prefs.setIntPref(aPref.name, aPref.value); + break; + } + }, + handleEvent: function(event) { let target = event.target; diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js index 19947abfd76..2fdd20d37ce 100644 --- a/mobile/android/chrome/content/browser.js +++ b/mobile/android/chrome/content/browser.js @@ -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"], @@ -5916,7 +5921,9 @@ var WebappsUI = { Services.obs.removeObserver(this, "webapps-sync-install"); 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: {