Bug 942288 - Gecko data storage for home panels. r=wesj
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Fri, 24 Jan 2014 15:11:17 -0800
changeset 165165 7dcb7216e8808dfa4175ed51d406c632e7fc3431
parent 165164 a1f08c3b158aac2ecd89d258949be62934994998
child 165166 53c80ea7f4c92b9e402cfe73413ab86329e03da2
push id26081
push userttaubert@mozilla.com
push dateSun, 26 Jan 2014 03:07:52 +0000
treeherdermozilla-central@3f1dd2a8e972 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs942288
milestone29.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 942288 - Gecko data storage for home panels. r=wesj
mobile/android/modules/HomeProvider.jsm
mobile/android/modules/moz.build
new file mode 100644
--- /dev/null
+++ b/mobile/android/modules/HomeProvider.jsm
@@ -0,0 +1,114 @@
+// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
+/* 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/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = [ "HomeProvider" ];
+
+const { utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/osfile.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
+Cu.import("resource://gre/modules/Sqlite.jsm");
+Cu.import("resource://gre/modules/Task.jsm");
+
+const SCHEMA_VERSION = 1;
+
+const DB_PATH = OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");
+
+/**
+ * All SQL statements should be defined here.
+ */
+const SQL = {
+  createItemsTable:
+    "CREATE TABLE items (" +
+      "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
+      "dataset_id TEXT NOT NULL, " +
+      "url TEXT," +
+      "primary_text TEXT," +
+      "secondary_text TEXT" +
+    ")",
+
+  insertItem:
+    "INSERT INTO items (dataset_id, url) " +
+      "VALUES (:dataset_id, :url)",
+
+  deleteFromDataset:
+    "DELETE FROM items WHERE dataset_id = :dataset_id"
+}
+
+this.HomeProvider = Object.freeze({
+  /**
+   * Returns a storage associated with a given dataset identifer.
+   *
+   * @param datasetId
+   *        (string) Unique identifier for the dataset.
+   *
+   * @return HomeStorage
+   */
+  getStorage: function(datasetId) {
+    return new HomeStorage(datasetId);
+  }
+});
+
+this.HomeStorage = function(datasetId) {
+  this.datasetId = datasetId;
+};
+
+HomeStorage.prototype = {
+  /**
+   * Saves data rows to the DB.
+   *
+   * @param data
+   *        (array) JSON array of row items
+   *
+   * @return Promise
+   * @resolves When the operation has completed.
+   */
+  save: function(data) {
+    return Task.spawn(function save_task() {
+      let db = yield Sqlite.openConnection({ path: DB_PATH });
+
+      try {
+        // XXX: Factor this out to some migration path.
+        if (!(yield db.tableExists("items"))) {
+          yield db.execute(SQL.createItemsTable);
+        }
+
+        // Insert data into DB.
+        for (let item of data) {
+          // XXX: Directly pass item as params? More validation for item? Batch insert?
+          let params = {
+            dataset_id: this.datasetId,
+            url: item.url
+          };
+          yield db.executeCached(SQL.insertItem, params);
+        }
+      } finally {
+        yield db.close();
+      }
+    }.bind(this));
+  },
+
+  /**
+   * Deletes all rows associated with this storage.
+   *
+   * @return Promise
+   * @resolves When the operation has completed.
+   */
+  deleteAll: function() {
+    return Task.spawn(function delete_all_task() {
+      let db = yield Sqlite.openConnection({ path: DB_PATH });
+
+      try {
+        // XXX: Check to make sure table exists.
+        let params = { dataset_id: this.datasetId };
+        yield db.executeCached(SQL.deleteFromDataset, params);
+      } finally {
+        yield db.close();
+      }
+    }.bind(this));
+  }
+};
--- a/mobile/android/modules/moz.build
+++ b/mobile/android/modules/moz.build
@@ -3,16 +3,17 @@
 # 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/.
 
 EXTRA_JS_MODULES += [
     'ContactService.jsm',
     'HelperApps.jsm',
     'Home.jsm',
+    'HomeProvider.jsm',
     'JNI.jsm',
     'LightweightThemeConsumer.jsm',
     'Notifications.jsm',
     'OrderedBroadcast.jsm',
     'Prompt.jsm',
     'Sanitizer.jsm',
     'SharedPreferences.jsm',
     'SimpleServiceDiscovery.jsm',