Commit [286d8b] Maximize Restore History

Backing out f085bbca2ee9 (bug 609043) since it triggered perma-orange in Android b-c tests

Daniel Holbert Daniel Holbert 2011-08-16

changed mobile
changed mobile/chrome
changed mobile/chrome/content
changed mobile/chrome/content/browser-ui.js
changed mobile/chrome/content/common-ui.js
changed mobile/chrome/content/content.js
changed mobile/chrome/jar.mn
changed mobile/components
changed mobile/components/Makefile.in
changed mobile/components/MobileComponents.manifest
changed mobile/installer
changed mobile/installer/package-manifest.in
changed mobile/modules
changed mobile/modules/Makefile.in
copied mobile/chrome/content/injected.js -> mobile/components/WebappsSupport.idl
copied mobile/modules/openWebapps.jsm -> mobile/components/WebappsSupport.js
mobile
Directory.
mobile/chrome
Directory.
mobile/chrome/content/browser-ui.js Diff Switch to side-by-side view
Loading...
mobile/chrome/content/common-ui.js Diff Switch to side-by-side view
Loading...
mobile/chrome/content/content.js Diff Switch to side-by-side view
Loading...
mobile/chrome/jar.mn Diff Switch to side-by-side view
Loading...
mobile/components
Directory.
mobile/components/Makefile.in Diff Switch to side-by-side view
Loading...
mobile/components/MobileComponents.manifest Diff Switch to side-by-side view
Loading...
mobile/installer
Directory.
mobile/installer/package-manifest.in Diff Switch to side-by-side view
Loading...
mobile/modules
Directory.
mobile/modules/Makefile.in Diff Switch to side-by-side view
Loading...
mobile/chrome/content/injected.js to mobile/components/WebappsSupport.idl
--- a/mobile/chrome/content/injected.js
+++ b/mobile/components/WebappsSupport.idl
@@ -1,4 +1,5 @@
-/* ***** BEGIN LICENSE BLOCK *****
+/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
  * The contents of this file are subject to the Mozilla Public License Version
@@ -11,10 +12,10 @@
  * for the specific language governing rights and limitations under the
  * License.
  *
- * The Original Code is Mozilla Mobile Browser.
+ * The Original Code is Mozilla Webapp code.
  *
  * The Initial Developer of the Original Code is Mozilla Foundation.
- * Portions created by the Initial Developer are Copyright (C) 2011
+ * Portions created by the Initial Developer are Copyright (C) 2010
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
@@ -34,33 +35,26 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
-/* Expose API under window.navigator.apps */
-if (window && window.navigator) {
-  window.navigator.mozApps = {
-    install: function(aParam) {
-      return OpenWebapps_install(window.location, aParam.url, aParam.install_data, aParam.onsuccess, aParam.onerror);
-    },
-    
-    amInstalled: function(aSuccessCallback, aErrorCallback) {
-      return OpenWebapps_amInstalled(window.location, aSuccessCallback, aErrorCallback);
-    },
-    
-    getInstalledBy: function(aSuccessCallback, aErrorCallback) {
-      return OpenWebapps_getInstalledBy(window.location, aSuccessCallback, aErrorCallback);
-    }
-  }
+#include "nsISupports.idl"
+
+[scriptable, uuid(adb91273-0cf1-4bbe-a37b-22e660192e2a)]
+interface nsIWebappsSupport : nsISupports
+{
+  /**
+   * This method installs a web app.
+   *
+   * @param title     the user-friendly name of the application.
+   * @param uri       the uri of the web app.
+   * @param iconData  a base64 encoded representation of the application's icon.
+   */
+  void installApplication(in wstring title, in wstring uri, in wstring iconUri, in wstring iconData);
   
-  window.navigator.mozApps.mgmt = {
-    launch: function(aOrigin, aSuccessCallback, aErrorCallback) {
-      return OpenWebappsMgmt_launch(aOrigin, aSuccessCallback, aErrorCallback);
-    },
-    
-    list: function(aSuccessCallback, aErrorCallback) {
-      return OpenWebappsMgmt_list(aSuccessCallback, aErrorCallback);
-    },
-    
-    uninstall: function(aOrigin, aSuccessCallback, aErrorCallback) {
-      return OpenWebappsMgmt_uninstall(aOrigin, aSuccessCallback, aErrorCallback);
-    }
-  }
-}
+  /**
+   * Checks is a web app is already installed
+   *
+   * @param uri the uri of the web app
+   * @return true if the web app is installed, false if it's not installed
+   */
+  boolean isApplicationInstalled(in wstring uri);
+};
+
mobile/modules/openWebapps.jsm to mobile/components/WebappsSupport.js
--- a/mobile/modules/openWebapps.jsm
+++ b/mobile/components/WebappsSupport.js
@@ -39,171 +39,87 @@
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
-let EXPORTED_SYMBOLS = ["OpenWebapps"];
-
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
-XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
-  Cu.import("resource://gre/modules/NetUtil.jsm");
-  return NetUtil;
-});
+const DB_VERSION = 1;
 
-let OpenWebapps = {
-  appsDir: null,
-  appsFile: null,
-  webapps: { },
+function WebappsSupport() {
+  this.init();
+}
 
+WebappsSupport.prototype = {
+  db: null,
+  
   init: function() {
     let file =  Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
-    file.append("webapps");
-    if (!file.exists() || !file.isDirectory()) {
-      file.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
+    file.append("webapps.sqlite");
+    this.db = Services.storage.openDatabase(file);
+    let version = this.db.schemaVersion;
+    
+    if (version == 0) {
+      this.db.executeSimpleSQL("CREATE TABLE webapps (title TEXT, uri TEXT PRIMARY KEY, icon TEXT)");
+      this.db.schemaVersion = DB_VERSION;
     }
-    this.appsDir = file;
-    this.appsFile = file.clone();
-    this.appsFile.append("webapps.json");
-    if (!this.appsFile.exists())
-      return;
-    
-    try {
-      let channel = NetUtil.newChannel(this.appsFile);
-      channel.contentType = "application/json";
-      let self = this;
-      NetUtil.asyncFetch(channel, function(aStream, aResult) {
-        if (!Components.isSuccessCode(aResult)) {
-          Cu.reportError("OpenWebappsSupport: Could not read from webapps.json file");
-          return;
-        }
 
-        // Read webapps json file into a string
-        let data = null;
-        try {
-          data = JSON.parse(NetUtil.readInputStreamToString(aStream, aStream.available()) || "");
-          self.webapps = data;
-          aStream.close();
-        } catch (ex) {
-          Cu.reportError("OpenWebsappsStore: Could not parse JSON: " + ex);
-        }
-      });
-    } catch (ex) {
-      Cu.reportError("OpenWebappsSupport: Could not read from webapps.json file: " + ex);
-    }
+    XPCOMUtils.defineLazyGetter(this, "_installQuery", function() {
+      return this.db.createAsyncStatement("INSERT INTO webapps (title, uri, icon) VALUES(:title, :uri, :icon)");
+    });
+
+    XPCOMUtils.defineLazyGetter(this, "_findQuery", function() {
+      return this.db.createStatement("SELECT uri FROM webapps where uri = :uri");
+    });
+
+    Services.obs.addObserver(this, "quit-application-granted", false);
   },
  
-  _writeFile: function ss_writeFile(aFile, aData) {
-    // Initialize the file output stream.
-    let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
-    ostream.init(aFile, 0x02 | 0x08 | 0x20, 0600, ostream.DEFER_OPEN);
-
-    // Obtain a converter to convert our data to a UTF-8 encoded input stream.
-    let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
-    converter.charset = "UTF-8";
-
-    // Asynchronously copy the data to the file.
-    let istream = converter.convertToInputStream(aData);
-    NetUtil.asyncCopy(istream, ostream, function(rc) {
-      // nothing to do
-    });
-  },
-  
-  install: function(aApplication) {
-    // Don't install twice an application
-    if (this.amInstalled(aApplication.appURI))
-      return;
-
-    let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
-    let id = uuidGenerator.generateUUID().toString();
-    let dir = this.appsDir.clone();
-    dir.append(id);
-    dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
-    
-    let manFile = dir.clone();
-    manFile.append("manifest.json");
-    this._writeFile(manFile, JSON.stringify(aApplication.manifest));
-    
-    this.webapps[id] = {
-      title: aApplication.title,
-      storeURI: aApplication.storeURI,
-      appURI: aApplication.appURI,
-      iconData: aApplication.iconData,
-      installData: aApplication.installData,
-      installTime: (new Date()).getTime(),
-      manifest: aApplication.manifest
-    };
-    this._writeFile(this.appsFile, JSON.stringify(this.webapps));
+  // entry point
+  installApplication: function(aTitle, aURI, aIconURI, aIconData) {
+    let stmt = this._installQuery;
+    stmt.params.title = aTitle;
+    stmt.params.uri = aURI;
+    stmt.params.icon = aIconData;
+    stmt.executeAsync();
   },
  
-  amInstalled: function(aURI) {
-    for (let id in this.webapps) {
-      let app = this.webapps[id];
-      if (app.appURI == aURI) {
-        return { origin: app.appURI,
-                 install_origin: app.storeURI,
-                 install_data: app.installData,
-                 install_time: app.installTime,
-                 manifest: app.manifest };
+  isApplicationInstalled: function(aURI) {
+    let stmt = this._findQuery;
+    let found = false;
+    try {
+      stmt.params.uri = aURI;
+      found = stmt.executeStep();
+    } finally {
+      stmt.reset();
+    }
+    return found;
+  },
+
+  // nsIObserver
+  observe: function(aSubject, aTopic, aData) {
+    Services.obs.removeObserver(this, "quit-application-granted");
+
+    // Finalize the statements that we have used
+    let stmts = [
+      "_installQuery",
+      "_findQuery"
+    ];
+    for (let i = 0; i < stmts.length; i++) {
+      // We do not want to create any query we haven't already created, so
+      // see if it is a getter first.
+      if (Object.getOwnPropertyDescriptor(this, stmts[i]).value !== undefined) {
+        this[stmts[i]].finalize();
       }
     }
-    return null;
+
+    this.db.asyncClose();
   },
 
-  getInstalledBy: function(aStoreURI) {
-    let res = [];
-    for (let id in this.webapps) {
-      let app = this.webapps[id];
-      if (app.storeURI == aStoreURI)
-        res.push({ origin: app.appURI,
-                   install_origin: app.storeURI,
-                   install_data: app.installData,
-                   install_time: app.installTime,
-                   manifest: app.manifest });
-    }
-    return res;
-  },
-  
-  mgmtList: function() {
-    let res = {};
-    for (let id in this.webapps) {
-      let app = this.webapps[id];
-      res[app.appURI] = { origin: app.appURI,
-                          install_origin: app.storeURI,
-                          install_data: app.installData,
-                          install_time: app.installTime,
-                          manifest: app.manifest };
-    }
-    return res;
-  },
-  
-  mgmtLaunch: function(aOrigin) {
-    for (let id in this.webapps) {
-      let app = this.webapps[id];
-      if (app.appURI == aOrigin) {
-        let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
-        browserWin.browserDOMWindow.openURI(Services.io.newURI(aOrigin, null, null), null, browserWin.OPEN_APPTAB, Ci.nsIBrowserDOMWindow.OPEN_NEW);
-        return true;
-      }
-    }
-    return false;
-  },
-  
-  mgmtUninstall: function(aOrigin) {
-    for (let id in this.webapps) {
-      let app = this.webapps[id];
-      if (app.appURI == aOrigin) {
-        delete this.webapps[id];
-        this._writeFile(this.appsFile, JSON.stringify(this.webapps));
-        let dir = this.appsFile.clone();
-        dir.append(id);
-        try {
-          dir.remove(true);
-        } catch (e) {
-        }
-        return true;
-      }
-    }
-    return false;
-  }
+  // QI
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebappsSupport]),
+
+  // XPCOMUtils factory
+  classID: Components.ID("{cb1107c1-1e15-4f11-99c8-27b9ec221a2a}")
 };
 
-OpenWebapps.init();
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsSupport]);
+