Bug 1300993 - Add ProfileAutoCompleteResult. draft
authorSean Lee <selee@mozilla.com>
Fri, 30 Dec 2016 12:06:06 +0800
changeset 455531 5f3ccb94c26092e88e1e1ba2278b283a4072cfee
parent 455530 7d9b8952083da7c1ae866d98134dacfa0e0bdfff
child 541024 1bf1db55e340a7402a2c9b4c0688dec22e791dbe
push id40274
push userbmo:selee@mozilla.com
push dateWed, 04 Jan 2017 02:44:40 +0000
bugs1300993
milestone53.0a1
Bug 1300993 - Add ProfileAutoCompleteResult. MozReview-Commit-ID: 63LHFgC66X8
browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
browser/extensions/formautofill/content/FormAutofillContent.js
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
@@ -0,0 +1,177 @@
+/* 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/. */
+
+this.EXPORTED_SYMBOLS = [ "ProfileAutoCompleteResult" ];
+
+const Ci = Components.interfaces;
+const Cr = Components.results;
+
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+this.ProfileAutoCompleteResult =
+ function ProfileAutoCompleteResult(searchString,
+                                 searchResult,
+                                 defaultIndex,
+                                 errorDescription,
+                                 values,
+                                 labels,
+                                 comments,
+                                 prevResult) {
+  this.searchString = searchString;
+  this._searchResult = searchResult;
+  this._defaultIndex = defaultIndex;
+  this._errorDescription = errorDescription;
+  this._values = values;
+  this._labels = labels;
+  this._comments = comments;
+  this._formHistResult = prevResult;
+
+  if (prevResult) {
+    this.entries = prevResult.wrappedJSObject.entries;
+  } else {
+    this.entries = [];
+  }
+}
+
+ProfileAutoCompleteResult.prototype = {
+
+  // The user's query string
+  searchString: "",
+
+  // The result code of this result object, see |get searchResult| for possible values.
+  _searchResult: 0,
+
+  // The default item that should be entered if none is selected
+  _defaultIndex: 0,
+
+  // The reason the search failed
+  _errorDescription: "",
+
+  /**
+   * A reference to the form history nsIAutocompleteResult that we're wrapping.
+   * We use this to forward removeEntryAt calls as needed.
+   */
+  _formHistResult: null,
+
+  entries: null,
+
+  get wrappedJSObject() {
+    return this;
+  },
+
+  /**
+   * @return the result code of this result object, either:
+   *         RESULT_IGNORED   (invalid searchString)
+   *         RESULT_FAILURE   (failure)
+   *         RESULT_NOMATCH   (no matches found)
+   *         RESULT_SUCCESS   (matches found)
+   */
+  get searchResult() {
+    return this._searchResult;
+  },
+
+  /**
+   * @return the default item that should be entered if none is selected
+   */
+  get defaultIndex() {
+    return this._defaultIndex;
+  },
+
+  /**
+   * @return the reason the search failed
+   */
+  get errorDescription() {
+    return this._errorDescription;
+  },
+
+  /**
+   * @return the number of results
+   */
+  get matchCount() {
+    return this._values.length;
+  },
+
+  _checkIndexBounds : function(index) {
+    if (index < 0 || index >= this._values.length) {
+      throw Components.Exception("Index out of range.", Cr.NS_ERROR_ILLEGAL_VALUE);
+    }
+  },
+
+  /**
+   * Retrieves a result
+   * @param  index    the index of the result requested
+   * @return          the result at the specified index
+   */
+  getValueAt: function(index) {
+    this._checkIndexBounds(index);
+    return this._values[index];
+  },
+
+  getLabelAt: function(index) {
+    this._checkIndexBounds(index);
+    return (this._labels[index] || this._values[index]) + " [FormFillLabel]";
+  },
+
+  /**
+   * Retrieves a comment (metadata instance)
+   * @param  index    the index of the comment requested
+   * @return          the comment at the specified index
+   */
+  getCommentAt: function(index) {
+    this._checkIndexBounds(index);
+    return this._comments[index];
+  },
+
+  /**
+   * Retrieves a style hint specific to a particular index.
+   * @param  index    the index of the style hint requested
+   * @return          the style hint at the specified index
+   */
+  getStyleAt: function(index) {
+    this._checkIndexBounds(index);
+
+    return "formFill";
+  },
+
+  /**
+   * Retrieves an image url.
+   * @param  index    the index of the image url requested
+   * @return          the image url at the specified index
+   */
+  getImageAt: function(index) {
+    this._checkIndexBounds(index);
+    return "";
+  },
+
+  /**
+   * Retrieves a result
+   * @param  index    the index of the result requested
+   * @return          the result at the specified index
+   */
+  getFinalCompleteValueAt: function(index) {
+    return this.getValueAt(index);
+  },
+
+  /**
+   * Removes a result from the resultset
+   * @param  index    the index of the result to remove
+   */
+  removeValueAt: function(index, removeFromDatabase) {
+    this._checkIndexBounds(index);
+    // Forward the removeValueAt call to the underlying result if we have one
+    // Note: this assumes that the form history results were added to the top
+    // of our arrays.
+    if (removeFromDatabase && this._formHistResult &&
+        index < this._formHistResult.matchCount) {
+      // Delete the history result from the DB
+      this._formHistResult.removeValueAt(index, true);
+    }
+    this._values.splice(index, 1);
+    this._labels.splice(index, 1);
+    this._comments.splice(index, 1);
+  },
+
+  // nsISupports
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult])
+};
--- a/browser/extensions/formautofill/content/FormAutofillContent.js
+++ b/browser/extensions/formautofill/content/FormAutofillContent.js
@@ -6,18 +6,19 @@
  * Form Autofill frame script.
  */
 
 "use strict";
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr, manager: Cm} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "ProfileAutoCompleteResult",
+                                  "resource://formautofill/ProfileAutoCompleteResult.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "FormLikeFactory",
                                   "resource://gre/modules/FormLikeFactory.jsm");
 
 const formFillController = Cc["@mozilla.org/satchel/form-fill-controller;1"]
                              .getService(Ci.nsIFormFillController);
 
 const AUTOFILL_FIELDS_THRESHOLD = 3;
 
@@ -225,17 +226,17 @@ AutofillProfileAutoCompleteSearch.protot
    * @param {Object} previousResult a previous result to use for faster searchinig
    * @param {Object} listener the listener to notify when the search is complete
    */
   startSearch(searchString, searchParam, previousResult, listener) {
     // TODO: These mock data should be replaced by form autofill API
     let labels = ["Mary", "John"];
     let values = ["Mary S.", "John S."];
     let comments = ["123 Sesame Street.", "331 E. Evelyn Avenue"];
-    let result = new FormAutoCompleteResult(searchString,
+    let result = new ProfileAutoCompleteResult(searchString,
                                             Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
                                             0, "", values, labels,
                                             comments);
 
     listener.onSearchResult(this, result);
   },
 
   /**