Bug 663571 - OS agnostic webapps backend [r=mbrubeck, r=mfinkle]
authorFabrice Desré <fabrice@mozilla.com>
Fri, 10 Jun 2011 17:08:00 -0400
changeset 72488 b1707b7835e217ed355ec0fb987f166e45ffc8a5
parent 72487 3e5c94427f4a215f7e2c065bced41ccdcbe83980
child 72489 22d8bb992dc2130493cdd3966992e77d85250c06
push id451
push usermfinkle@mozilla.com
push dateFri, 08 Jul 2011 02:34:56 +0000
treeherdermozilla-inbound@22d8bb992dc2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmbrubeck, mfinkle
bugs663571
milestone8.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 663571 - OS agnostic webapps backend [r=mbrubeck, r=mfinkle]
mobile/components/Makefile.in
mobile/components/MobileComponents.manifest
mobile/components/WebappsSupport.js
--- a/mobile/components/Makefile.in
+++ b/mobile/components/Makefile.in
@@ -70,16 +70,17 @@ EXTRA_COMPONENTS = \
         PromptService.js \
         ContentDispatchChooser.js \
         AutoCompleteCache.js \
         AddonUpdateService.js \
         FormAutoComplete.js \
         LoginManager.js \
         LoginManagerPrompter.js \
         BlocklistPrompt.js \
+        WebappsSupport.js \
         $(NULL)
 
 ifdef MOZ_SAFE_BROWSING
 EXTRA_COMPONENTS += SafeBrowsing.js
 endif
 
 ifdef MOZ_UPDATER
 EXTRA_COMPONENTS += UpdatePrompt.js
--- a/mobile/components/MobileComponents.manifest
+++ b/mobile/components/MobileComponents.manifest
@@ -115,8 +115,12 @@ contract @mozilla.org/safebrowsing/appli
 category app-startup SafeBrowsing service,@mozilla.org/safebrowsing/application;1
 #endif
 
 #ifdef MOZ_UPDATER
 # UpdatePrompt.js
 component {88b3eb21-d072-4e3b-886d-f89d8c49fe59} UpdatePrompt.js
 contract @mozilla.org/updates/update-prompt;1 {88b3eb21-d072-4e3b-886d-f89d8c49fe59}
 #endif
+
+# webapps
+component {cb1107c1-1e15-4f11-99c8-27b9ec221a2a} WebappsSupport.js
+contract @mozilla.org/webapps/support;1 {cb1107c1-1e15-4f11-99c8-27b9ec221a2a}
new file mode 100644
--- /dev/null
+++ b/mobile/components/WebappsSupport.js
@@ -0,0 +1,125 @@
+/* ***** 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
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Mobile Browser.
+ *
+ * The Initial Developer of the Original Code is Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Fabrice Desré <fabrice@mozilla.com>
+ *   Mark Finkle <mfinkle@mozilla.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+const Cu = Components.utils; 
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+const DB_VERSION = 1;
+
+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.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;
+    }
+
+    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);
+  },
+ 
+  // 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();
+  },
+ 
+  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();
+      }
+    }
+
+    this.db.asyncClose();
+  },
+
+  // QI
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebappsSupport]),
+
+  // XPCOMUtils factory
+  classID: Components.ID("{cb1107c1-1e15-4f11-99c8-27b9ec221a2a}")
+};
+
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsSupport]);
+