Bug 741608 - Create homescreen shortcut for installed app r=wesj
authorMark Finkle <mfinkle@mozilla.com>
Tue, 22 May 2012 01:00:35 -0400
changeset 96832 80f607fa91ebc7402c0de519ca14f18f39bfb68a
parent 96831 d1be5a84f4029385cdccc293d1aed080121e094d
child 96833 3de4dae2dd8f706fe1bc683b6268da8c3def8970
push id1439
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 20:19:22 +0000
treeherdermozilla-aurora@ea74834dccd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs741608
milestone15.0a1
Bug 741608 - Create homescreen shortcut for installed app r=wesj
mobile/android/chrome/content/aboutApps.js
mobile/android/chrome/content/browser.js
--- a/mobile/android/chrome/content/aboutApps.js
+++ b/mobile/android/chrome/content/aboutApps.js
@@ -39,17 +39,17 @@ function onLoad(aEvent) {
   navigator.mozApps.mgmt.oninstall = onInstall;
   navigator.mozApps.mgmt.onuninstall = onUninstall;
   updateList();
 
   let contextmenus = gChromeWin.NativeWindow.contextmenus;
   AppsUI.shortcut = contextmenus.add(gStrings.GetStringFromName("appsContext.shortcut"), contextmenus.SelectorContext("div[mozApp]"),
     function(aTarget) {
       let manifest = aTarget.manifest;
-      createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.iconURLForSize("64"), "webapp");
+      gChromeWin.WebappsUI.createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.iconURLForSize("64"), "webapp");
     });
   AppsUI.uninstall = contextmenus.add(gStrings.GetStringFromName("appsContext.uninstall"), contextmenus.SelectorContext("div[mozApp]"),
     function(aTarget) {
       aTarget.app.uninstall();
     });
 }
 
 function onUnload(aEvent) {
@@ -116,42 +116,8 @@ function onUninstall(aEvent) {
   let node = document.getElementById("app-" + aEvent.application.origin);
   if (node) {
     let parent = node.parentNode;
     parent.removeChild(node);
     if (!parent.firstChild)
       document.getElementById("noapps").className = "";
   }
 }
-
-function createShortcut(aTitle, aURL, aIconURL, aType) {
-  // The images are 64px, but Android will resize as needed.
-  // Bigger is better than too small.
-  const kIconSize = 64;
-
-  let canvas = document.createElement("canvas");
-
-  function _createShortcut() {
-    let icon = canvas.toDataURL("image/png", "");
-    canvas = null;
-    try {
-      let shell = Cc["@mozilla.org/browser/shell-service;1"].createInstance(Ci.nsIShellService);
-      shell.createShortcut(aTitle, aURL, icon, aType);
-    } catch(e) {
-      Cu.reportError(e);
-    }
-  }
-
-  canvas.width = canvas.height = kIconSize;
-  let ctx = canvas.getContext("2d");
-
-  let favicon = new Image();
-  favicon.onload = function() {
-    ctx.drawImage(favicon, 0, 0, kIconSize, kIconSize);
-    _createShortcut();
-  }
-
-  favicon.onerror = function() {
-    Cu.reportError("CreateShortcut: favicon image load error");
-  }
-
-  favicon.src = aIconURL;
-}
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -4973,59 +4973,105 @@ var ActivityObserver = {
     let tab = BrowserApp.selectedTab;
     if (tab && tab.getActive() != isForeground) {
       tab.setActive(isForeground);
     }
   }
 };
 
 var WebappsUI = {
-  init: function() {
+  init: function init() {
     Cu.import("resource://gre/modules/Webapps.jsm");
 
     Services.obs.addObserver(this, "webapps-ask-install", false);
     Services.obs.addObserver(this, "webapps-launch", false);
+    Services.obs.addObserver(this, "webapps-sync-install", false);
   },
   
-  uninit: function() {
+  uninit: function unint() {
     Services.obs.removeObserver(this, "webapps-ask-install");
     Services.obs.removeObserver(this, "webapps-launch");
+    Services.obs.removeObserver(this, "webapps-sync-install");
   },
   
-  observe: function(aSubject, aTopic, aData) {
+  observe: function observe(aSubject, aTopic, aData) {
     let data = JSON.parse(aData);
     switch (aTopic) {
       case "webapps-ask-install":
         this.doInstall(data);
         break;
       case "webapps-launch":
         DOMApplicationRegistry.getManifestFor(data.origin, (function(aManifest) {
 	   if (!aManifest)
 	     return;
           let manifest = new DOMApplicationManifest(aManifest, data.origin);
           this.openURL(manifest.fullLaunchPath(), data.origin);
         }).bind(this));
         break;
+      case "webapps-sync-install":
+        // Wait until we know the app install worked, then make a homescreen shortcut
+        DOMApplicationRegistry.getManifestFor(data.origin, (function(aManifest) {
+	   if (!aManifest)
+	     return;
+          let manifest = new DOMApplicationManifest(aManifest, data.origin);
+          this.createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.iconURLForSize("64"), "webapp");
+        }).bind(this));
+        break;
     }
   },
   
-  doInstall: function(aData) {
+  doInstall: function doInstall(aData) {
     let manifest = new DOMApplicationManifest(aData.app.manifest, aData.app.origin);
     let name = manifest.name ? manifest.name : manifest.fullLaunchPath();
     if (Services.prompt.confirm(null, Strings.browser.GetStringFromName("webapps.installTitle"), name))
       DOMApplicationRegistry.confirmInstall(aData);
+    else
+      DOMApplicationRegistry.denyInstall(aData);
   },
   
-  openURL: function(aURI, aOrigin) {
+  openURL: function openURL(aURI, aOrigin) {
     let uri = Services.io.newURI(aURI, null, null);
     if (!uri)
       return;
 
     let bwin = window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow;
     bwin.openURI(uri, null, Ci.nsIBrowserDOMWindow.OPEN_SWITCHTAB, Ci.nsIBrowserDOMWindow.OPEN_NEW);
+  },
+
+  createShortcut: function createShortcut(aTitle, aURL, aIconURL, aType) {
+    // The images are 64px, but Android will resize as needed.
+    // Bigger is better than too small.
+    const kIconSize = 64;
+
+    let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
+
+    function _createShortcut() {
+      let icon = canvas.toDataURL("image/png", "");
+      canvas = null;
+      try {
+        let shell = Cc["@mozilla.org/browser/shell-service;1"].createInstance(Ci.nsIShellService);
+        shell.createShortcut(aTitle, aURL, icon, aType);
+      } catch(e) {
+        Cu.reportError(e);
+      }
+    }
+
+    canvas.width = canvas.height = kIconSize;
+    let ctx = canvas.getContext("2d");
+
+    let favicon = new Image();
+    favicon.onload = function() {
+      ctx.drawImage(favicon, 0, 0, kIconSize, kIconSize);
+      _createShortcut();
+    }
+    favicon.onerror = function() {
+      Cu.reportError("CreateShortcut: favicon image load error");
+    }
+  
+    favicon.src = aIconURL;
   }
 }
 
 var RemoteDebugger = {
   init: function rd_init() {
     Services.prefs.addObserver("remote-debugger.", this, false);
 
     if (this._isEnabled())