Bug 851217 - Create webapp specific branding. r=Mossop
authorMarco Castelluccio <mar.castelluccio@studenti.unina.it>
Wed, 09 Oct 2013 09:16:18 -0400
changeset 164027 d8109ff79880146b99fa165468154560ae2e1483
parent 164026 e4c2830550418025aad2f51134982a83b8648c3b
child 164028 18d00b699ad6741c91879db63709ee0930dc391a
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMossop
bugs851217
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 851217 - Create webapp specific branding. r=Mossop
webapprt/ContentPermission.js
webapprt/Startup.jsm
webapprt/WebappRT.jsm
webapprt/locales/jar.mn
--- a/webapprt/ContentPermission.js
+++ b/webapprt/ContentPermission.js
@@ -57,17 +57,17 @@ ContentPermission.prototype = {
     } else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
                (result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
                 UNKNOWN_FAIL.indexOf(request.type) >= 0)) {
       request.cancel();
       return;
     }
 
     // Display a prompt at the top level
-    let {name} = WebappRT.config.app.manifest;
+    let {name} = WebappRT.localeManifest;
     let requestingWindow = request.window.top;
     let chromeWin = this._getChromeWindow(requestingWindow);
     let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
 
     // Construct a prompt with share/don't and remember checkbox
     let remember = {value: false};
     let choice = Services.prompt.confirmEx(
       chromeWin,
--- a/webapprt/Startup.jsm
+++ b/webapprt/Startup.jsm
@@ -15,37 +15,72 @@ const Cu = Components.utils;
 Cu.import("resource://gre/modules/Services.jsm");
 // Initialize DOMApplicationRegistry by importing Webapps.jsm.
 Cu.import("resource://gre/modules/Webapps.jsm");
 Cu.import("resource://gre/modules/AppsUtils.jsm");
 Cu.import("resource://gre/modules/PermissionsInstaller.jsm");
 Cu.import('resource://gre/modules/Payment.jsm');
 Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/Promise.jsm");
+Cu.import("resource://gre/modules/osfile.jsm");
 
 // Initialize window-independent handling of webapps- notifications.
 Cu.import("resource://webapprt/modules/WebappsHandler.jsm");
 Cu.import("resource://webapprt/modules/WebappRT.jsm");
 
+const PROFILE_DIR = OS.Constants.Path.profileDir;
+
 function isFirstRunOrUpdate() {
   let savedBuildID = null;
   try {
     savedBuildID = Services.prefs.getCharPref("webapprt.buildID");
   } catch (e) {}
 
   let ourBuildID = Services.appinfo.platformBuildID;
 
   if (ourBuildID != savedBuildID) {
     Services.prefs.setCharPref("webapprt.buildID", ourBuildID);
     return true;
   }
 
   return false;
 }
 
+function writeFile(aPath, aData) {
+  return Task.spawn(function() {
+    let data = TextEncoder().encode(aData);
+    yield OS.File.writeAtomic(aPath, data, { tmpPath: aPath + ".tmp" });
+  });
+}
+
+function createBrandingFiles() {
+  return Task.spawn(function() {
+    let manifest = WebappRT.localeManifest;
+    let name = WebappRT.localeManifest.name;
+    let developer = " ";
+    if (WebappRT.localeManifest.developer) {
+      developer = WebappRT.localeManifest.developer.name;
+    }
+
+    let brandDTDContent = '<!ENTITY brandShortName "' + name + '">\n\
+  <!ENTITY brandFullName "' + name + '">\n\
+  <!ENTITY vendorShortName "' + developer + '">\n\
+  <!ENTITY trademarkInfo.part1 " ">';
+
+    yield writeFile(OS.Path.join(PROFILE_DIR, "brand.dtd"), brandDTDContent);
+
+    let brandPropertiesContent = 'brandShortName=' + name + '\n\
+  brandFullName=' + name + '\n\
+  vendorShortName=' + developer;
+
+    yield writeFile(OS.Path.join(PROFILE_DIR, "brand.properties"),
+                    brandPropertiesContent);
+  });
+}
+
 // Observes all the events needed to actually launch an application.
 // It waits for XUL window and webapps registry loading.
 this.startup = function(window) {
   return Task.spawn(function () {
     // Observe registry loading.
     let deferredRegistry = Promise.defer();
     function observeRegistryLoading() {
       Services.obs.removeObserver(observeRegistryLoading, "webapps-registry-start");
@@ -74,19 +109,31 @@ this.startup = function(window) {
     if (manifestURL) {
       appID = DOMApplicationRegistry.getAppLocalIdByManifestURL(manifestURL);
 
       // On firstrun, set permissions to their default values.
       // When the webapp runtime is updated, update the permissions.
       // TODO: Update the permissions when the application is updated.
       if (isFirstRunOrUpdate(Services.prefs)) {
         PermissionsInstaller.installPermissions(WebappRT.config.app, true);
+        yield createBrandingFiles();
       }
     }
 
+    // Branding substitution
+    let aliasFile = Components.classes["@mozilla.org/file/local;1"]
+                              .createInstance(Ci.nsIFile);
+    aliasFile.initWithPath(PROFILE_DIR);
+
+    let aliasURI = Services.io.newFileURI(aliasFile);
+
+    Services.io.getProtocolHandler("resource")
+               .QueryInterface(Ci.nsIResProtocolHandler)
+               .setSubstitution("webappbranding", aliasURI);
+
     // Wait for XUL window loading
     yield deferredWindowLoad.promise;
 
     // Get the <browser> element in the webapp.xul window.
     let appBrowser = window.document.getElementById("content");
 
     // Set the principal to the correct appID and launch the application.
     appBrowser.docShell.setIsApp(appID);
--- a/webapprt/WebappRT.jsm
+++ b/webapprt/WebappRT.jsm
@@ -41,13 +41,17 @@ this.WebappRT = {
   // will have a reference to its global object, so our reference to it
   // will leak that object (per bug 780674).  The setter enables us to clone
   // the new value so we don't actually retain a reference to it.
   set config(newVal) {
     this._config = JSON.parse(JSON.stringify(newVal));
   },
 
   get launchURI() {
-    let manifest = new ManifestHelper(this.config.app.manifest,
-                                      this.config.app.origin);
+    let manifest = this.localeManifest;
     return manifest.fullLaunchPath();
-  }
+  },
+
+  get localeManifest() {
+    return new ManifestHelper(this.config.app.manifest,
+                              this.config.app.origin);
+  },
 };
--- a/webapprt/locales/jar.mn
+++ b/webapprt/locales/jar.mn
@@ -2,8 +2,10 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # 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/.
 
 @AB_CD@.jar:
 % locale webapprt @AB_CD@ %locale/webapprt/
     locale/webapprt/webapp.dtd                     (%webapprt/webapp.dtd)
     locale/webapprt/webapp.properties              (%webapprt/webapp.properties)
+
+% locale branding @AB_CD@ resource://webappbranding/