2013-04-20 02:21:30 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FormHistory
|
|
|
|
*
|
|
|
|
* Used to store values that have been entered into forms which may later
|
|
|
|
* be used to automatically fill in the values when the form is visited again.
|
|
|
|
*
|
|
|
|
* search(terms, queryData, callback)
|
|
|
|
* Look up values that have been previously stored.
|
|
|
|
* terms - array of terms to return data for
|
|
|
|
* queryData - object that contains the query terms
|
|
|
|
* The query object contains properties for each search criteria to match, where the value
|
|
|
|
* of the property specifies the value that term must have. For example,
|
|
|
|
* { term1: value1, term2: value2 }
|
|
|
|
* callback - callback that is called when results are available or an error occurs.
|
|
|
|
* The callback is passed a result array containing each found entry. Each element in
|
|
|
|
* the array is an object containing a property for each search term specified by 'terms'.
|
2013-09-03 21:24:25 +04:00
|
|
|
* count(queryData, callback)
|
2013-04-20 02:21:30 +04:00
|
|
|
* Find the number of stored entries that match the given criteria.
|
|
|
|
* queryData - array of objects that indicate the query. See the search method for details.
|
|
|
|
* callback - callback that is called when results are available or an error occurs.
|
|
|
|
* The callback is passed the number of found entries.
|
|
|
|
* update(changes, callback)
|
|
|
|
* Write data to form history storage.
|
|
|
|
* changes - an array of changes to be made. If only one change is to be made, it
|
|
|
|
* may be passed as an object rather than a one-element array.
|
|
|
|
* Each change object is of the form:
|
|
|
|
* { op: operation, term1: value1, term2: value2, ... }
|
|
|
|
* Valid operations are:
|
|
|
|
* add - add a new entry
|
|
|
|
* update - update an existing entry
|
|
|
|
* remove - remove an entry
|
|
|
|
* bump - update the last accessed time on an entry
|
|
|
|
* The terms specified allow matching of one or more specific entries. If no terms
|
|
|
|
* are specified then all entries are matched. This means that { op: "remove" } is
|
|
|
|
* used to remove all entries and clear the form history.
|
|
|
|
* callback - callback that is called when results have been stored.
|
|
|
|
* getAutoCompeteResults(searchString, params, callback)
|
|
|
|
* Retrieve an array of form history values suitable for display in an autocomplete list.
|
|
|
|
* Returns an mozIStoragePendingStatement that can be used to cancel the operation if
|
|
|
|
* needed.
|
|
|
|
* searchString - the string to search for, typically the entered value of a textbox
|
|
|
|
* params - zero or more filter arguments:
|
|
|
|
* fieldname - form field name
|
|
|
|
* agedWeight
|
|
|
|
* bucketSize
|
|
|
|
* expiryDate
|
|
|
|
* maxTimeGroundings
|
|
|
|
* timeGroupingSize
|
|
|
|
* prefixWeight
|
|
|
|
* boundaryWeight
|
|
|
|
* callback - callback that is called with the array of results. Each result in the array
|
|
|
|
* is an object with four arguments:
|
|
|
|
* text, textLowerCase, frecency, totalScore
|
|
|
|
* schemaVersion
|
|
|
|
* This property holds the version of the database schema
|
|
|
|
*
|
|
|
|
* Terms:
|
|
|
|
* guid - entry identifier. For 'add', a guid will be generated.
|
|
|
|
* fieldname - form field name
|
|
|
|
* value - form value
|
|
|
|
* timesUsed - the number of times the entry has been accessed
|
|
|
|
* firstUsed - the time the the entry was first created
|
|
|
|
* lastUsed - the time the entry was last accessed
|
|
|
|
* firstUsedStart - search for entries created after or at this time
|
|
|
|
* firstUsedEnd - search for entries created before or at this time
|
|
|
|
* lastUsedStart - search for entries last accessed after or at this time
|
|
|
|
* lastUsedEnd - search for entries last accessed before or at this time
|
2013-06-18 03:58:54 +04:00
|
|
|
* newGuid - a special case valid only for 'update' and allows the guid for
|
|
|
|
* an existing record to be updated. The 'guid' term is the only
|
|
|
|
* other term which can be used (ie, you can not also specify a
|
|
|
|
* fieldname, value etc) and indicates the guid of the existing
|
|
|
|
* record that should be updated.
|
2013-04-20 02:21:30 +04:00
|
|
|
*
|
|
|
|
* In all of the above methods, the callback argument should be an object with
|
|
|
|
* handleResult(result), handleFailure(error) and handleCompletion(reason) functions.
|
|
|
|
* For search and getAutoCompeteResults, result is an object containing the desired
|
|
|
|
* properties. For count, result is the integer count. For, update, handleResult is
|
|
|
|
* not called. For handleCompletion, reason is either 0 if successful or 1 if
|
|
|
|
* an error occurred.
|
|
|
|
*/
|
|
|
|
|
2013-05-14 16:29:31 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["FormHistory"];
|
2013-04-20 02:21:30 +04:00
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
2013-04-20 02:21:30 +04:00
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/AppConstants.jsm");
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "uuidService",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator");
|
|
|
|
|
|
|
|
const DB_SCHEMA_VERSION = 4;
|
|
|
|
const DAY_IN_MS = 86400000; // 1 day in milliseconds
|
2013-12-11 12:05:31 +04:00
|
|
|
const MAX_SEARCH_TOKENS = 10;
|
2013-04-20 02:21:30 +04:00
|
|
|
const NOOP = function noop() {};
|
|
|
|
|
2015-12-03 21:01:41 +03:00
|
|
|
var supportsDeletedTable = AppConstants.platform == "android";
|
2013-04-20 02:21:30 +04:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var Prefs = {
|
2013-04-20 02:21:30 +04:00
|
|
|
initialized: false,
|
|
|
|
|
|
|
|
get debug() { this.ensureInitialized(); return this._debug; },
|
|
|
|
get enabled() { this.ensureInitialized(); return this._enabled; },
|
|
|
|
get expireDays() { this.ensureInitialized(); return this._expireDays; },
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
ensureInitialized() {
|
2017-05-17 01:56:28 +03:00
|
|
|
if (this.initialized) {
|
2013-04-20 02:21:30 +04:00
|
|
|
return;
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
|
|
|
this._debug = Services.prefs.getBoolPref("browser.formfill.debug");
|
|
|
|
this._enabled = Services.prefs.getBoolPref("browser.formfill.enable");
|
|
|
|
this._expireDays = Services.prefs.getIntPref("browser.formfill.expire_days");
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
function log(aMessage) {
|
|
|
|
if (Prefs.debug) {
|
|
|
|
Services.console.logStringMessage("FormHistory: " + aMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendNotification(aType, aData) {
|
|
|
|
if (typeof aData == "string") {
|
2017-06-14 02:34:54 +03:00
|
|
|
let strWrapper = Cc["@mozilla.org/supports-string;1"]
|
|
|
|
.createInstance(Ci.nsISupportsString);
|
2013-04-20 02:21:30 +04:00
|
|
|
strWrapper.data = aData;
|
|
|
|
aData = strWrapper;
|
2016-12-31 05:47:25 +03:00
|
|
|
} else if (typeof aData == "number") {
|
2017-06-14 02:34:54 +03:00
|
|
|
let intWrapper = Cc["@mozilla.org/supports-PRInt64;1"]
|
|
|
|
.createInstance(Ci.nsISupportsPRInt64);
|
2013-04-20 02:21:30 +04:00
|
|
|
intWrapper.data = aData;
|
|
|
|
aData = intWrapper;
|
2016-12-31 05:47:25 +03:00
|
|
|
} else if (aData) {
|
2013-04-20 02:21:30 +04:00
|
|
|
throw Components.Exception("Invalid type " + (typeof aType) + " passed to sendNotification",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.notifyObservers(aData, "satchel-storage-changed", aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current database schema
|
|
|
|
*/
|
|
|
|
|
|
|
|
const dbSchema = {
|
2017-03-21 21:29:43 +03:00
|
|
|
tables: {
|
|
|
|
moz_formhistory: {
|
|
|
|
"id": "INTEGER PRIMARY KEY",
|
|
|
|
"fieldname": "TEXT NOT NULL",
|
|
|
|
"value": "TEXT NOT NULL",
|
|
|
|
"timesUsed": "INTEGER",
|
|
|
|
"firstUsed": "INTEGER",
|
|
|
|
"lastUsed": "INTEGER",
|
|
|
|
"guid": "TEXT",
|
2013-04-20 02:21:30 +04:00
|
|
|
},
|
|
|
|
moz_deleted_formhistory: {
|
2017-06-13 01:45:00 +03:00
|
|
|
"id": "INTEGER PRIMARY KEY",
|
|
|
|
"timeDeleted": "INTEGER",
|
2017-06-15 03:46:42 +03:00
|
|
|
"guid": "TEXT",
|
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
},
|
2017-03-21 21:29:43 +03:00
|
|
|
indices: {
|
|
|
|
moz_formhistory_index: {
|
|
|
|
table: "moz_formhistory",
|
2017-06-15 03:46:42 +03:00
|
|
|
columns: ["fieldname"],
|
2013-04-20 02:21:30 +04:00
|
|
|
},
|
2017-03-21 21:29:43 +03:00
|
|
|
moz_formhistory_lastused_index: {
|
|
|
|
table: "moz_formhistory",
|
2017-06-15 03:46:42 +03:00
|
|
|
columns: ["lastUsed"],
|
2013-04-20 02:21:30 +04:00
|
|
|
},
|
2017-03-21 21:29:43 +03:00
|
|
|
moz_formhistory_guid_index: {
|
|
|
|
table: "moz_formhistory",
|
2017-06-15 03:46:42 +03:00
|
|
|
columns: ["guid"],
|
2013-04-20 02:21:30 +04:00
|
|
|
},
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validating and processing API querying data
|
|
|
|
*/
|
|
|
|
|
|
|
|
const validFields = [
|
|
|
|
"fieldname",
|
|
|
|
"value",
|
|
|
|
"timesUsed",
|
|
|
|
"firstUsed",
|
|
|
|
"lastUsed",
|
|
|
|
"guid",
|
|
|
|
];
|
|
|
|
|
|
|
|
const searchFilters = [
|
|
|
|
"firstUsedStart",
|
|
|
|
"firstUsedEnd",
|
|
|
|
"lastUsedStart",
|
|
|
|
"lastUsedEnd",
|
|
|
|
];
|
|
|
|
|
|
|
|
function validateOpData(aData, aDataType) {
|
2013-06-18 03:58:54 +04:00
|
|
|
let thisValidFields = validFields;
|
|
|
|
// A special case to update the GUID - in this case there can be a 'newGuid'
|
|
|
|
// field and of the normally valid fields, only 'guid' is accepted.
|
|
|
|
if (aDataType == "Update" && "newGuid" in aData) {
|
|
|
|
thisValidFields = ["guid", "newGuid"];
|
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
for (let field in aData) {
|
2017-05-17 04:23:56 +03:00
|
|
|
if (field != "op" && !thisValidFields.includes(field)) {
|
2013-04-20 02:21:30 +04:00
|
|
|
throw Components.Exception(
|
|
|
|
aDataType + " query contains an unrecognized field: " + field,
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateSearchData(aData, aDataType) {
|
|
|
|
for (let field in aData) {
|
2017-05-17 04:23:56 +03:00
|
|
|
if (field != "op" && !validFields.includes(field) && !searchFilters.includes(field)) {
|
2013-04-20 02:21:30 +04:00
|
|
|
throw Components.Exception(
|
|
|
|
aDataType + " query contains an unrecognized field: " + field,
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 18:48:17 +03:00
|
|
|
function makeQueryPredicates(aQueryData, delimiter = " AND ") {
|
2017-05-17 04:23:56 +03:00
|
|
|
return Object.keys(aQueryData).map(field => {
|
2017-05-17 01:56:28 +03:00
|
|
|
switch (field) {
|
|
|
|
case "firstUsedStart": {
|
|
|
|
return "firstUsed >= :" + field;
|
|
|
|
}
|
|
|
|
case "firstUsedEnd": {
|
|
|
|
return "firstUsed <= :" + field;
|
|
|
|
}
|
|
|
|
case "lastUsedStart": {
|
|
|
|
return "lastUsed >= :" + field;
|
|
|
|
}
|
|
|
|
case "lastUsedEnd": {
|
|
|
|
return "lastUsed <= :" + field;
|
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
2017-05-17 01:56:28 +03:00
|
|
|
|
2013-04-20 02:21:30 +04:00
|
|
|
return field + " = :" + field;
|
|
|
|
}).join(delimiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage statement creation and parameter binding
|
|
|
|
*/
|
|
|
|
|
|
|
|
function makeCountStatement(aSearchData) {
|
|
|
|
let query = "SELECT COUNT(*) AS numEntries FROM moz_formhistory";
|
|
|
|
let queryTerms = makeQueryPredicates(aSearchData);
|
|
|
|
if (queryTerms) {
|
|
|
|
query += " WHERE " + queryTerms;
|
|
|
|
}
|
|
|
|
return dbCreateAsyncStatement(query, aSearchData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSearchStatement(aSearchData, aSelectTerms) {
|
|
|
|
let query = "SELECT " + aSelectTerms.join(", ") + " FROM moz_formhistory";
|
|
|
|
let queryTerms = makeQueryPredicates(aSearchData);
|
|
|
|
if (queryTerms) {
|
|
|
|
query += " WHERE " + queryTerms;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbCreateAsyncStatement(query, aSearchData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeAddStatement(aNewData, aNow, aBindingArrays) {
|
2017-06-14 02:48:31 +03:00
|
|
|
let query = "INSERT INTO moz_formhistory " +
|
|
|
|
"(fieldname, value, timesUsed, firstUsed, lastUsed, guid) " +
|
2013-04-20 02:21:30 +04:00
|
|
|
"VALUES (:fieldname, :value, :timesUsed, :firstUsed, :lastUsed, :guid)";
|
|
|
|
|
|
|
|
aNewData.timesUsed = aNewData.timesUsed || 1;
|
|
|
|
aNewData.firstUsed = aNewData.firstUsed || aNow;
|
|
|
|
aNewData.lastUsed = aNewData.lastUsed || aNow;
|
|
|
|
return dbCreateAsyncStatement(query, aNewData, aBindingArrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeBumpStatement(aGuid, aNow, aBindingArrays) {
|
2017-06-14 02:48:31 +03:00
|
|
|
let query = "UPDATE moz_formhistory " +
|
|
|
|
"SET timesUsed = timesUsed + 1, lastUsed = :lastUsed WHERE guid = :guid";
|
2013-04-20 02:21:30 +04:00
|
|
|
let queryParams = {
|
2017-03-21 21:29:43 +03:00
|
|
|
lastUsed: aNow,
|
|
|
|
guid: aGuid,
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
return dbCreateAsyncStatement(query, queryParams, aBindingArrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeRemoveStatement(aSearchData, aBindingArrays) {
|
|
|
|
let query = "DELETE FROM moz_formhistory";
|
|
|
|
let queryTerms = makeQueryPredicates(aSearchData);
|
|
|
|
|
|
|
|
if (queryTerms) {
|
|
|
|
log("removeEntries");
|
|
|
|
query += " WHERE " + queryTerms;
|
|
|
|
} else {
|
|
|
|
log("removeAllEntries");
|
|
|
|
// Not specifying any fields means we should remove all entries. We
|
|
|
|
// won't need to modify the query in this case.
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbCreateAsyncStatement(query, aSearchData, aBindingArrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeUpdateStatement(aGuid, aNewData, aBindingArrays) {
|
|
|
|
let query = "UPDATE moz_formhistory SET ";
|
2017-01-17 18:48:17 +03:00
|
|
|
let queryTerms = makeQueryPredicates(aNewData, ", ");
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
if (!queryTerms) {
|
|
|
|
throw Components.Exception("Update query must define fields to modify.",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:58:54 +04:00
|
|
|
query += queryTerms + " WHERE guid = :existing_guid";
|
2017-06-15 03:50:48 +03:00
|
|
|
aNewData.existing_guid = aGuid;
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
return dbCreateAsyncStatement(query, aNewData, aBindingArrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeMoveToDeletedStatement(aGuid, aNow, aData, aBindingArrays) {
|
|
|
|
if (supportsDeletedTable) {
|
|
|
|
let query = "INSERT INTO moz_deleted_formhistory (guid, timeDeleted)";
|
|
|
|
let queryTerms = makeQueryPredicates(aData);
|
|
|
|
|
|
|
|
if (aGuid) {
|
|
|
|
query += " VALUES (:guid, :timeDeleted)";
|
|
|
|
} else {
|
|
|
|
// TODO: Add these items to the deleted items table once we've sorted
|
|
|
|
// out the issues from bug 756701
|
2017-06-10 01:56:08 +03:00
|
|
|
if (!queryTerms) {
|
2016-02-04 00:12:18 +03:00
|
|
|
return undefined;
|
2017-06-10 01:56:08 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
query += " SELECT guid, :timeDeleted FROM moz_formhistory WHERE " + queryTerms;
|
|
|
|
}
|
|
|
|
|
|
|
|
aData.timeDeleted = aNow;
|
|
|
|
|
|
|
|
return dbCreateAsyncStatement(query, aData, aBindingArrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateGUID() {
|
|
|
|
// string like: "{f60d9eac-9421-4abc-8491-8e8322b063d4}"
|
|
|
|
let uuid = uuidService.generateUUID().toString();
|
|
|
|
let raw = ""; // A string with the low bytes set to random values
|
|
|
|
let bytes = 0;
|
2016-11-11 01:48:04 +03:00
|
|
|
for (let i = 1; bytes < 12 ; i += 2) {
|
2013-04-20 02:21:30 +04:00
|
|
|
// Skip dashes
|
2017-05-17 01:56:28 +03:00
|
|
|
if (uuid[i] == "-") {
|
2013-04-20 02:21:30 +04:00
|
|
|
i++;
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
let hexVal = parseInt(uuid[i] + uuid[i + 1], 16);
|
|
|
|
raw += String.fromCharCode(hexVal);
|
|
|
|
bytes++;
|
|
|
|
}
|
|
|
|
return btoa(raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database creation and access
|
|
|
|
*/
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var _dbConnection = null;
|
2013-04-20 02:21:30 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "dbConnection", function() {
|
|
|
|
let dbFile;
|
|
|
|
|
|
|
|
try {
|
|
|
|
dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile).clone();
|
|
|
|
dbFile.append("formhistory.sqlite");
|
|
|
|
log("Opening database at " + dbFile.path);
|
|
|
|
|
|
|
|
_dbConnection = Services.storage.openUnsharedDatabase(dbFile);
|
|
|
|
dbInit();
|
2015-12-03 21:01:41 +03:00
|
|
|
} catch (e) {
|
2017-06-10 01:56:08 +03:00
|
|
|
if (e.result != Cr.NS_ERROR_FILE_CORRUPTED) {
|
2015-12-03 21:01:41 +03:00
|
|
|
throw e;
|
2017-06-10 01:56:08 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
dbCleanup(dbFile);
|
|
|
|
_dbConnection = Services.storage.openUnsharedDatabase(dbFile);
|
|
|
|
dbInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _dbConnection;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var dbStmts = new Map();
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* dbCreateAsyncStatement
|
|
|
|
*
|
|
|
|
* Creates a statement, wraps it, and then does parameter replacement
|
|
|
|
*/
|
|
|
|
function dbCreateAsyncStatement(aQuery, aParams, aBindingArrays) {
|
2017-05-17 01:56:28 +03:00
|
|
|
if (!aQuery) {
|
2013-04-20 02:21:30 +04:00
|
|
|
return null;
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
let stmt = dbStmts.get(aQuery);
|
|
|
|
if (!stmt) {
|
|
|
|
log("Creating new statement for query: " + aQuery);
|
|
|
|
stmt = dbConnection.createAsyncStatement(aQuery);
|
|
|
|
dbStmts.set(aQuery, stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBindingArrays) {
|
|
|
|
let bindingArray = aBindingArrays.get(stmt);
|
|
|
|
if (!bindingArray) {
|
|
|
|
// first time using a particular statement in update
|
|
|
|
bindingArray = stmt.newBindingParamsArray();
|
|
|
|
aBindingArrays.set(stmt, bindingArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aParams) {
|
|
|
|
let bindingParams = bindingArray.newBindingParams();
|
|
|
|
for (let field in aParams) {
|
|
|
|
bindingParams.bindByName(field, aParams[field]);
|
|
|
|
}
|
|
|
|
bindingArray.addParams(bindingParams);
|
|
|
|
}
|
2016-08-12 18:50:35 +03:00
|
|
|
} else if (aParams) {
|
|
|
|
for (let field in aParams) {
|
|
|
|
stmt.params[field] = aParams[field];
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to initialize the database. This creates the file if it doesn't
|
|
|
|
* exist, performs any migrations, etc.
|
|
|
|
*/
|
|
|
|
function dbInit() {
|
|
|
|
log("Initializing Database");
|
|
|
|
|
|
|
|
if (!_dbConnection.tableExists("moz_formhistory")) {
|
|
|
|
dbCreate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When FormHistory is released, we will no longer support the various schema versions prior to
|
|
|
|
// this release that nsIFormHistory2 once did.
|
|
|
|
let version = _dbConnection.schemaVersion;
|
|
|
|
if (version < 3) {
|
|
|
|
throw Components.Exception("DB version is unsupported.",
|
|
|
|
Cr.NS_ERROR_FILE_CORRUPTED);
|
|
|
|
} else if (version != DB_SCHEMA_VERSION) {
|
|
|
|
dbMigrate(version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 00:34:12 +03:00
|
|
|
var Migrators = {
|
|
|
|
/*
|
|
|
|
* Updates the DB schema to v3 (bug 506402).
|
|
|
|
* Adds deleted form history table.
|
|
|
|
*/
|
|
|
|
dbMigrateToVersion4() {
|
|
|
|
if (!_dbConnection.tableExists("moz_deleted_formhistory")) {
|
2017-06-15 03:50:48 +03:00
|
|
|
let table = dbSchema.tables.moz_deleted_formhistory;
|
2017-06-14 00:34:12 +03:00
|
|
|
let tSQL = Object.keys(table).map(col => [col, table[col]].join(" ")).join(", ");
|
|
|
|
_dbConnection.createTable("moz_deleted_formhistory", tSQL);
|
|
|
|
}
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2017-06-14 00:34:12 +03:00
|
|
|
};
|
|
|
|
|
2013-04-20 02:21:30 +04:00
|
|
|
function dbCreate() {
|
|
|
|
log("Creating DB -- tables");
|
|
|
|
for (let name in dbSchema.tables) {
|
|
|
|
let table = dbSchema.tables[name];
|
2015-12-03 21:01:41 +03:00
|
|
|
let tSQL = Object.keys(table).map(col => [col, table[col]].join(" ")).join(", ");
|
2013-04-20 02:21:30 +04:00
|
|
|
log("Creating table " + name + " with " + tSQL);
|
|
|
|
_dbConnection.createTable(name, tSQL);
|
|
|
|
}
|
|
|
|
|
|
|
|
log("Creating DB -- indices");
|
|
|
|
for (let name in dbSchema.indices) {
|
|
|
|
let index = dbSchema.indices[name];
|
|
|
|
let statement = "CREATE INDEX IF NOT EXISTS " + name + " ON " + index.table +
|
|
|
|
"(" + index.columns.join(", ") + ")";
|
|
|
|
_dbConnection.executeSimpleSQL(statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dbConnection.schemaVersion = DB_SCHEMA_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbMigrate(oldVersion) {
|
|
|
|
log("Attempting to migrate from version " + oldVersion);
|
|
|
|
|
|
|
|
if (oldVersion > DB_SCHEMA_VERSION) {
|
|
|
|
log("Downgrading to version " + DB_SCHEMA_VERSION);
|
|
|
|
// User's DB is newer. Sanity check that our expected columns are
|
|
|
|
// present, and if so mark the lower version and merrily continue
|
|
|
|
// on. If the columns are borked, something is wrong so blow away
|
|
|
|
// the DB and start from scratch. [Future incompatible upgrades
|
|
|
|
// should switch to a different table or file.]
|
|
|
|
|
|
|
|
if (!dbAreExpectedColumnsPresent()) {
|
|
|
|
throw Components.Exception("DB is missing expected columns",
|
|
|
|
Cr.NS_ERROR_FILE_CORRUPTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the stored version to the current version. If the user
|
|
|
|
// runs the newer code again, it will see the lower version number
|
|
|
|
// and re-upgrade (to fixup any entries the old code added).
|
|
|
|
_dbConnection.schemaVersion = DB_SCHEMA_VERSION;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that migration is currently performed synchronously.
|
|
|
|
_dbConnection.beginTransaction();
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let v = oldVersion + 1; v <= DB_SCHEMA_VERSION; v++) {
|
|
|
|
this.log("Upgrading to version " + v + "...");
|
|
|
|
Migrators["dbMigrateToVersion" + v]();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-12-31 05:47:25 +03:00
|
|
|
this.log("Migration failed: " + e);
|
2013-04-20 02:21:30 +04:00
|
|
|
this.dbConnection.rollbackTransaction();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dbConnection.schemaVersion = DB_SCHEMA_VERSION;
|
|
|
|
_dbConnection.commitTransaction();
|
|
|
|
|
|
|
|
log("DB migration completed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanity check to ensure that the columns this version of the code expects
|
|
|
|
* are present in the DB we're using.
|
2017-06-13 20:48:52 +03:00
|
|
|
* @returns {boolean} whether expected columns are present
|
2013-04-20 02:21:30 +04:00
|
|
|
*/
|
|
|
|
function dbAreExpectedColumnsPresent() {
|
|
|
|
for (let name in dbSchema.tables) {
|
|
|
|
let table = dbSchema.tables[name];
|
|
|
|
let query = "SELECT " +
|
2015-12-03 21:01:41 +03:00
|
|
|
Object.keys(table).join(", ") +
|
2013-04-20 02:21:30 +04:00
|
|
|
" FROM " + name;
|
|
|
|
try {
|
|
|
|
let stmt = _dbConnection.createStatement(query);
|
|
|
|
// (no need to execute statement, if it compiled we're good)
|
|
|
|
stmt.finalize();
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log("verified that expected columns are present in DB.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when database creation fails. Finalizes database statements,
|
|
|
|
* closes the database connection, deletes the database file.
|
2017-06-13 20:48:52 +03:00
|
|
|
* @param {Object} dbFile database file to close
|
2013-04-20 02:21:30 +04:00
|
|
|
*/
|
|
|
|
function dbCleanup(dbFile) {
|
|
|
|
log("Cleaning up DB file - close & remove & backup");
|
|
|
|
|
|
|
|
// Create backup file
|
|
|
|
let backupFile = dbFile.leafName + ".corrupt";
|
|
|
|
Services.storage.backupDatabaseFile(dbFile, backupFile);
|
|
|
|
|
|
|
|
dbClose(false);
|
|
|
|
dbFile.remove(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dbClose(aShutdown) {
|
|
|
|
log("dbClose(" + aShutdown + ")");
|
|
|
|
|
|
|
|
if (aShutdown) {
|
|
|
|
sendNotification("formhistory-shutdown", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection may never have been created if say open failed but we still
|
|
|
|
// end up calling dbClose as part of the rest of dbCleanup.
|
|
|
|
if (!_dbConnection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log("dbClose finalize statements");
|
|
|
|
for (let stmt of dbStmts.values()) {
|
|
|
|
stmt.finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
dbStmts = new Map();
|
|
|
|
|
|
|
|
let closed = false;
|
2015-09-24 14:32:23 +03:00
|
|
|
_dbConnection.asyncClose(() => closed = true);
|
2013-04-20 02:21:30 +04:00
|
|
|
|
|
|
|
if (!aShutdown) {
|
2017-06-21 19:59:28 +03:00
|
|
|
Services.tm.spinEventLoopUntil(() => closed);
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs and executes database statements from a pre-processed list of
|
|
|
|
* inputted changes.
|
2017-06-13 20:48:52 +03:00
|
|
|
*
|
|
|
|
* @param {Array.<Object>} aChanges changes to form history
|
|
|
|
* @param {Object} aCallbacks
|
2013-04-20 02:21:30 +04:00
|
|
|
*/
|
|
|
|
function updateFormHistoryWrite(aChanges, aCallbacks) {
|
|
|
|
log("updateFormHistoryWrite " + aChanges.length);
|
|
|
|
|
|
|
|
// pass 'now' down so that every entry in the batch has the same timestamp
|
|
|
|
let now = Date.now() * 1000;
|
|
|
|
|
|
|
|
// for each change, we either create and append a new storage statement to
|
|
|
|
// stmts or bind a new set of parameters to an existing storage statement.
|
|
|
|
// stmts and bindingArrays are updated when makeXXXStatement eventually
|
|
|
|
// calls dbCreateAsyncStatement.
|
|
|
|
let stmts = [];
|
|
|
|
let notifications = [];
|
|
|
|
let bindingArrays = new Map();
|
|
|
|
|
2015-01-08 21:58:45 +03:00
|
|
|
for (let change of aChanges) {
|
2013-04-20 02:21:30 +04:00
|
|
|
let operation = change.op;
|
|
|
|
delete change.op;
|
|
|
|
let stmt;
|
|
|
|
switch (operation) {
|
|
|
|
case "remove":
|
|
|
|
log("Remove from form history " + change);
|
|
|
|
let delStmt = makeMoveToDeletedStatement(change.guid, now, change, bindingArrays);
|
2017-05-17 04:23:56 +03:00
|
|
|
if (delStmt && !stmts.includes(delStmt)) {
|
2013-04-20 02:21:30 +04:00
|
|
|
stmts.push(delStmt);
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
|
|
|
if ("timeDeleted" in change) {
|
2013-04-20 02:21:30 +04:00
|
|
|
delete change.timeDeleted;
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
stmt = makeRemoveStatement(change, bindingArrays);
|
2017-06-15 03:35:30 +03:00
|
|
|
notifications.push(["formhistory-remove", change.guid]);
|
2013-04-20 02:21:30 +04:00
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
log("Update form history " + change);
|
|
|
|
let guid = change.guid;
|
|
|
|
delete change.guid;
|
2013-06-18 03:58:54 +04:00
|
|
|
// a special case for updating the GUID - the new value can be
|
|
|
|
// specified in newGuid.
|
|
|
|
if (change.newGuid) {
|
2017-06-13 19:51:07 +03:00
|
|
|
change.guid = change.newGuid;
|
2013-06-18 03:58:54 +04:00
|
|
|
delete change.newGuid;
|
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
stmt = makeUpdateStatement(guid, change, bindingArrays);
|
2017-06-15 03:35:30 +03:00
|
|
|
notifications.push(["formhistory-update", guid]);
|
2013-04-20 02:21:30 +04:00
|
|
|
break;
|
|
|
|
case "bump":
|
|
|
|
log("Bump form history " + change);
|
|
|
|
if (change.guid) {
|
|
|
|
stmt = makeBumpStatement(change.guid, now, bindingArrays);
|
2017-06-15 03:35:30 +03:00
|
|
|
notifications.push(["formhistory-update", change.guid]);
|
2013-04-20 02:21:30 +04:00
|
|
|
} else {
|
|
|
|
change.guid = generateGUID();
|
|
|
|
stmt = makeAddStatement(change, now, bindingArrays);
|
2017-06-15 03:35:30 +03:00
|
|
|
notifications.push(["formhistory-add", change.guid]);
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "add":
|
|
|
|
log("Add to form history " + change);
|
|
|
|
change.guid = generateGUID();
|
|
|
|
stmt = makeAddStatement(change, now, bindingArrays);
|
2017-06-15 03:35:30 +03:00
|
|
|
notifications.push(["formhistory-add", change.guid]);
|
2013-04-20 02:21:30 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// We should've already guaranteed that change.op is one of the above
|
|
|
|
throw Components.Exception("Invalid operation " + operation,
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// As identical statements are reused, only add statements if they aren't already present.
|
2017-05-17 04:23:56 +03:00
|
|
|
if (stmt && !stmts.includes(stmt)) {
|
2013-04-20 02:21:30 +04:00
|
|
|
stmts.push(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let stmt of stmts) {
|
|
|
|
stmt.bindParameters(bindingArrays.get(stmt));
|
|
|
|
}
|
|
|
|
|
|
|
|
let handlers = {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleCompletion(aReason) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
|
|
|
for (let [notification, param] of notifications) {
|
|
|
|
// We're either sending a GUID or nothing at all.
|
|
|
|
sendNotification(notification, param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallbacks && aCallbacks.handleCompletion) {
|
2017-06-14 02:48:31 +03:00
|
|
|
aCallbacks.handleCompletion(
|
|
|
|
aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED ?
|
|
|
|
0 :
|
|
|
|
1
|
|
|
|
);
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError(aError);
|
|
|
|
}
|
|
|
|
},
|
2017-06-15 03:46:42 +03:00
|
|
|
handleResult: NOOP,
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
dbConnection.executeAsync(stmts, stmts.length, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions that expire entries in form history and shrinks database
|
|
|
|
* afterwards as necessary initiated by expireOldEntries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes entries from database.
|
2017-06-13 20:48:52 +03:00
|
|
|
*
|
|
|
|
* @param {number} aExpireTime expiration timestamp
|
|
|
|
* @param {number} aBeginningCount numer of entries at first
|
2013-04-20 02:21:30 +04:00
|
|
|
*/
|
|
|
|
function expireOldEntriesDeletion(aExpireTime, aBeginningCount) {
|
|
|
|
log("expireOldEntriesDeletion(" + aExpireTime + "," + aBeginningCount + ")");
|
|
|
|
|
2017-06-13 01:45:00 +03:00
|
|
|
FormHistory.update([{
|
|
|
|
op: "remove",
|
|
|
|
lastUsedEnd: aExpireTime,
|
|
|
|
}], {
|
|
|
|
handleCompletion() {
|
|
|
|
expireOldEntriesVacuum(aExpireTime, aBeginningCount);
|
|
|
|
},
|
|
|
|
handleError(aError) {
|
|
|
|
log("expireOldEntriesDeletionFailure");
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counts number of entries removed and shrinks database as necessary.
|
2017-06-13 20:48:52 +03:00
|
|
|
*
|
|
|
|
* @param {number} aExpireTime expiration timestamp
|
|
|
|
* @param {number} aBeginningCount number of entries at first
|
2013-04-20 02:21:30 +04:00
|
|
|
*/
|
|
|
|
function expireOldEntriesVacuum(aExpireTime, aBeginningCount) {
|
|
|
|
FormHistory.count({}, {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(aEndingCount) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aBeginningCount - aEndingCount > 500) {
|
|
|
|
log("expireOldEntriesVacuum");
|
|
|
|
|
|
|
|
let stmt = dbCreateAsyncStatement("VACUUM");
|
|
|
|
stmt.executeAsync({
|
2017-03-21 21:29:43 +03:00
|
|
|
handleResult: NOOP,
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
log("expireVacuumError");
|
|
|
|
},
|
2017-06-15 03:46:42 +03:00
|
|
|
handleCompletion: NOOP,
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendNotification("formhistory-expireoldentries", aExpireTime);
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
log("expireEndCountFailure");
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-14 16:29:31 +04:00
|
|
|
this.FormHistory = {
|
2015-09-24 14:32:23 +03:00
|
|
|
get enabled() {
|
|
|
|
return Prefs.enabled;
|
|
|
|
},
|
2014-02-26 10:12:16 +04:00
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
search(aSelectTerms, aSearchData, aCallbacks) {
|
2013-04-20 02:21:30 +04:00
|
|
|
// if no terms selected, select everything
|
2017-05-17 01:56:28 +03:00
|
|
|
if (!aSelectTerms) {
|
|
|
|
aSelectTerms = validFields;
|
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
validateSearchData(aSearchData, "Search");
|
|
|
|
|
|
|
|
let stmt = makeSearchStatement(aSearchData, aSelectTerms);
|
|
|
|
|
|
|
|
let handlers = {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(aResultSet) {
|
2013-04-20 02:21:30 +04:00
|
|
|
for (let row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow()) {
|
|
|
|
let result = {};
|
2015-01-08 21:58:45 +03:00
|
|
|
for (let field of aSelectTerms) {
|
2013-04-20 02:21:30 +04:00
|
|
|
result[field] = row.getResultByName(field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallbacks && aCallbacks.handleResult) {
|
|
|
|
aCallbacks.handleResult(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError(aError);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
handleCompletion(aReason) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleCompletion) {
|
2017-06-14 02:48:31 +03:00
|
|
|
aCallbacks.handleCompletion(
|
|
|
|
aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED ?
|
|
|
|
0 :
|
|
|
|
1
|
|
|
|
);
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
stmt.executeAsync(handlers);
|
|
|
|
},
|
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
count(aSearchData, aCallbacks) {
|
2013-04-20 02:21:30 +04:00
|
|
|
validateSearchData(aSearchData, "Count");
|
|
|
|
let stmt = makeCountStatement(aSearchData);
|
|
|
|
let handlers = {
|
2017-05-17 04:23:56 +03:00
|
|
|
handleResult(aResultSet) {
|
2013-04-20 02:21:30 +04:00
|
|
|
let row = aResultSet.getNextRow();
|
|
|
|
let count = row.getResultByName("numEntries");
|
|
|
|
if (aCallbacks && aCallbacks.handleResult) {
|
|
|
|
aCallbacks.handleResult(count);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError(aError);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
handleCompletion(aReason) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleCompletion) {
|
2017-06-14 02:48:31 +03:00
|
|
|
aCallbacks.handleCompletion(
|
|
|
|
aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED ?
|
|
|
|
0 :
|
|
|
|
1
|
|
|
|
);
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
stmt.executeAsync(handlers);
|
|
|
|
},
|
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
update(aChanges, aCallbacks) {
|
2013-04-20 02:21:30 +04:00
|
|
|
// Used to keep track of how many searches have been started. When that number
|
|
|
|
// are finished, updateFormHistoryWrite can be called.
|
|
|
|
let numSearches = 0;
|
|
|
|
let completedSearches = 0;
|
|
|
|
let searchFailed = false;
|
|
|
|
|
|
|
|
function validIdentifier(change) {
|
2017-06-14 02:48:31 +03:00
|
|
|
// The identifier is only valid if one of either the guid
|
|
|
|
// or the (fieldname/value) are set (so an X-OR)
|
2013-04-20 02:21:30 +04:00
|
|
|
return Boolean(change.guid) != Boolean(change.fieldname && change.value);
|
|
|
|
}
|
|
|
|
|
2017-05-17 01:56:28 +03:00
|
|
|
if (!("length" in aChanges)) {
|
2013-04-20 02:21:30 +04:00
|
|
|
aChanges = [aChanges];
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
|
2016-02-22 20:17:43 +03:00
|
|
|
let isRemoveOperation = aChanges.every(change => change && change.op && change.op == "remove");
|
|
|
|
if (!Prefs.enabled && !isRemoveOperation) {
|
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError({
|
|
|
|
message: "Form history is disabled, only remove operations are allowed",
|
2017-06-15 03:46:42 +03:00
|
|
|
result: Ci.mozIStorageError.MISUSE,
|
2016-02-22 20:17:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (aCallbacks && aCallbacks.handleCompletion) {
|
|
|
|
aCallbacks.handleCompletion(1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:58:45 +03:00
|
|
|
for (let change of aChanges) {
|
2013-04-20 02:21:30 +04:00
|
|
|
switch (change.op) {
|
|
|
|
case "remove":
|
|
|
|
validateSearchData(change, "Remove");
|
|
|
|
continue;
|
|
|
|
case "update":
|
|
|
|
if (validIdentifier(change)) {
|
|
|
|
validateOpData(change, "Update");
|
|
|
|
if (change.guid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Components.Exception(
|
|
|
|
"update op='update' does not correctly reference a entry.",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "bump":
|
|
|
|
if (validIdentifier(change)) {
|
|
|
|
validateOpData(change, "Bump");
|
|
|
|
if (change.guid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Components.Exception(
|
|
|
|
"update op='bump' does not correctly reference a entry.",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "add":
|
|
|
|
if (change.guid) {
|
|
|
|
throw Components.Exception(
|
|
|
|
"op='add' cannot contain field 'guid'. Either use op='update' " +
|
|
|
|
"explicitly or make 'guid' undefined.",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
} else if (change.fieldname && change.value) {
|
|
|
|
validateOpData(change, "Add");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Components.Exception(
|
|
|
|
"update does not recognize op='" + change.op + "'",
|
|
|
|
Cr.NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
numSearches++;
|
2013-09-03 21:24:25 +04:00
|
|
|
let changeToUpdate = change;
|
2013-04-20 02:21:30 +04:00
|
|
|
FormHistory.search(
|
2017-06-15 03:35:30 +03:00
|
|
|
["guid"],
|
2013-04-20 02:21:30 +04:00
|
|
|
{
|
2017-03-21 21:29:43 +03:00
|
|
|
fieldname: change.fieldname,
|
2017-06-15 03:46:42 +03:00
|
|
|
value: change.value,
|
2013-04-20 02:21:30 +04:00
|
|
|
}, {
|
2017-03-21 21:29:43 +03:00
|
|
|
foundResult: false,
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(aResult) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (this.foundResult) {
|
|
|
|
log("Database contains multiple entries with the same fieldname/value pair.");
|
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError({
|
2017-03-21 21:29:43 +03:00
|
|
|
message:
|
2013-04-20 02:21:30 +04:00
|
|
|
"Database contains multiple entries with the same fieldname/value pair.",
|
2017-06-15 03:46:42 +03:00
|
|
|
result: 19, // Constraint violation
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
searchFailed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.foundResult = true;
|
2017-06-15 03:50:48 +03:00
|
|
|
changeToUpdate.guid = aResult.guid;
|
2013-04-20 02:21:30 +04:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError(aError);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleCompletion(aReason) {
|
2013-04-20 02:21:30 +04:00
|
|
|
completedSearches++;
|
|
|
|
if (completedSearches == numSearches) {
|
|
|
|
if (!aReason && !searchFailed) {
|
|
|
|
updateFormHistoryWrite(aChanges, aCallbacks);
|
2016-12-31 05:47:25 +03:00
|
|
|
} else if (aCallbacks && aCallbacks.handleCompletion) {
|
2013-04-20 02:21:30 +04:00
|
|
|
aCallbacks.handleCompletion(1);
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numSearches == 0) {
|
|
|
|
// We don't have to wait for any statements to return.
|
|
|
|
updateFormHistoryWrite(aChanges, aCallbacks);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
getAutoCompleteResults(searchString, params, aCallbacks) {
|
2013-04-20 02:21:30 +04:00
|
|
|
// only do substring matching when the search string contains more than one character
|
|
|
|
let searchTokens;
|
2017-06-13 19:51:07 +03:00
|
|
|
let where = "";
|
2013-04-20 02:21:30 +04:00
|
|
|
let boundaryCalc = "";
|
|
|
|
if (searchString.length > 1) {
|
2017-06-13 01:45:00 +03:00
|
|
|
searchTokens = searchString.split(/\s+/);
|
|
|
|
|
|
|
|
// build up the word boundary and prefix match bonus calculation
|
|
|
|
boundaryCalc = "MAX(1, :prefixWeight * (value LIKE :valuePrefix ESCAPE '/') + (";
|
|
|
|
// for each word, calculate word boundary weights for the SELECT clause and
|
|
|
|
// add word to the WHERE clause of the query
|
|
|
|
let tokenCalc = [];
|
|
|
|
let searchTokenCount = Math.min(searchTokens.length, MAX_SEARCH_TOKENS);
|
|
|
|
for (let i = 0; i < searchTokenCount; i++) {
|
|
|
|
tokenCalc.push("(value LIKE :tokenBegin" + i + " ESCAPE '/') + " +
|
2013-04-20 02:21:30 +04:00
|
|
|
"(value LIKE :tokenBoundary" + i + " ESCAPE '/')");
|
2017-06-13 01:45:00 +03:00
|
|
|
where += "AND (value LIKE :tokenContains" + i + " ESCAPE '/') ";
|
|
|
|
}
|
|
|
|
// add more weight if we have a traditional prefix match and
|
|
|
|
// multiply boundary bonuses by boundary weight
|
|
|
|
boundaryCalc += tokenCalc.join(" + ") + ") * :boundaryWeight)";
|
2013-04-20 02:21:30 +04:00
|
|
|
} else if (searchString.length == 1) {
|
2017-06-13 01:45:00 +03:00
|
|
|
where = "AND (value LIKE :valuePrefix ESCAPE '/') ";
|
|
|
|
boundaryCalc = "1";
|
|
|
|
delete params.prefixWeight;
|
|
|
|
delete params.boundaryWeight;
|
2013-04-20 02:21:30 +04:00
|
|
|
} else {
|
2017-06-13 01:45:00 +03:00
|
|
|
where = "";
|
|
|
|
boundaryCalc = "1";
|
|
|
|
delete params.prefixWeight;
|
|
|
|
delete params.boundaryWeight;
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
params.now = Date.now() * 1000; // convert from ms to microseconds
|
|
|
|
|
|
|
|
/* Three factors in the frecency calculation for an entry (in order of use in calculation):
|
|
|
|
* 1) average number of times used - items used more are ranked higher
|
|
|
|
* 2) how recently it was last used - items used recently are ranked higher
|
|
|
|
* 3) additional weight for aged entries surviving expiry - these entries are relevant
|
|
|
|
* since they have been used multiple times over a large time span so rank them higher
|
|
|
|
* The score is then divided by the bucket size and we round the result so that entries
|
|
|
|
* with a very similar frecency are bucketed together with an alphabetical sort. This is
|
|
|
|
* to reduce the amount of moving around by entries while typing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let query = "/* do not warn (bug 496471): can't use an index */ " +
|
|
|
|
"SELECT value, " +
|
|
|
|
"ROUND( " +
|
|
|
|
"timesUsed / MAX(1.0, (lastUsed - firstUsed) / :timeGroupingSize) * " +
|
2016-11-11 01:48:04 +03:00
|
|
|
"MAX(1.0, :maxTimeGroupings - (:now - lastUsed) / :timeGroupingSize) * " +
|
2013-04-20 02:21:30 +04:00
|
|
|
"MAX(1.0, :agedWeight * (firstUsed < :expiryDate)) / " +
|
2016-11-11 01:48:04 +03:00
|
|
|
":bucketSize " +
|
2013-04-20 02:21:30 +04:00
|
|
|
", 3) AS frecency, " +
|
|
|
|
boundaryCalc + " AS boundaryBonuses " +
|
|
|
|
"FROM moz_formhistory " +
|
|
|
|
"WHERE fieldname=:fieldname " + where +
|
|
|
|
"ORDER BY ROUND(frecency * boundaryBonuses) DESC, UPPER(value) ASC";
|
|
|
|
|
|
|
|
let stmt = dbCreateAsyncStatement(query, params);
|
|
|
|
|
|
|
|
// Chicken and egg problem: Need the statement to escape the params we
|
|
|
|
// pass to the function that gives us the statement. So, fix it up now.
|
2017-05-17 01:56:28 +03:00
|
|
|
if (searchString.length >= 1) {
|
2013-04-20 02:21:30 +04:00
|
|
|
stmt.params.valuePrefix = stmt.escapeStringForLIKE(searchString, "/") + "%";
|
2017-05-17 01:56:28 +03:00
|
|
|
}
|
2013-04-20 02:21:30 +04:00
|
|
|
if (searchString.length > 1) {
|
2013-12-11 11:34:42 +04:00
|
|
|
let searchTokenCount = Math.min(searchTokens.length, MAX_SEARCH_TOKENS);
|
|
|
|
for (let i = 0; i < searchTokenCount; i++) {
|
2013-04-20 02:21:30 +04:00
|
|
|
let escapedToken = stmt.escapeStringForLIKE(searchTokens[i], "/");
|
|
|
|
stmt.params["tokenBegin" + i] = escapedToken + "%";
|
|
|
|
stmt.params["tokenBoundary" + i] = "% " + escapedToken + "%";
|
|
|
|
stmt.params["tokenContains" + i] = "%" + escapedToken + "%";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no additional params need to be substituted into the query when the
|
|
|
|
// length is zero or one
|
|
|
|
}
|
|
|
|
|
|
|
|
let pending = stmt.executeAsync({
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(aResultSet) {
|
2013-04-20 02:21:30 +04:00
|
|
|
for (let row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow()) {
|
|
|
|
let value = row.getResultByName("value");
|
|
|
|
let frecency = row.getResultByName("frecency");
|
|
|
|
let entry = {
|
2017-03-21 21:29:43 +03:00
|
|
|
text: value,
|
|
|
|
textLowerCase: value.toLowerCase(),
|
2016-12-30 02:34:54 +03:00
|
|
|
frecency,
|
2017-06-15 03:46:42 +03:00
|
|
|
totalScore: Math.round(frecency * row.getResultByName("boundaryBonuses")),
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
if (aCallbacks && aCallbacks.handleResult) {
|
|
|
|
aCallbacks.handleResult(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleError) {
|
|
|
|
aCallbacks.handleError(aError);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleCompletion(aReason) {
|
2013-04-20 02:21:30 +04:00
|
|
|
if (aCallbacks && aCallbacks.handleCompletion) {
|
2017-06-14 02:48:31 +03:00
|
|
|
aCallbacks.handleCompletion(
|
|
|
|
aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED ?
|
|
|
|
0 :
|
|
|
|
1
|
|
|
|
);
|
2013-04-20 02:21:30 +04:00
|
|
|
}
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
return pending;
|
|
|
|
},
|
|
|
|
|
|
|
|
get schemaVersion() {
|
|
|
|
return dbConnection.schemaVersion;
|
|
|
|
},
|
|
|
|
|
|
|
|
// This is used only so that the test can verify deleted table support.
|
|
|
|
get _supportsDeletedTable() {
|
|
|
|
return supportsDeletedTable;
|
|
|
|
},
|
|
|
|
set _supportsDeletedTable(val) {
|
|
|
|
supportsDeletedTable = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
// The remaining methods are called by FormHistoryStartup.js
|
2017-05-17 04:23:56 +03:00
|
|
|
updatePrefs() {
|
2013-04-20 02:21:30 +04:00
|
|
|
Prefs.initialized = false;
|
|
|
|
},
|
|
|
|
|
2017-05-17 04:23:56 +03:00
|
|
|
expireOldEntries() {
|
2013-04-20 02:21:30 +04:00
|
|
|
log("expireOldEntries");
|
|
|
|
|
|
|
|
// Determine how many days of history we're supposed to keep.
|
|
|
|
// Calculate expireTime in microseconds
|
|
|
|
let expireTime = (Date.now() - Prefs.expireDays * DAY_IN_MS) * 1000;
|
|
|
|
|
|
|
|
sendNotification("formhistory-beforeexpireoldentries", expireTime);
|
|
|
|
|
|
|
|
FormHistory.count({}, {
|
2016-12-30 02:34:54 +03:00
|
|
|
handleResult(aBeginningCount) {
|
2013-04-20 02:21:30 +04:00
|
|
|
expireOldEntriesDeletion(expireTime, aBeginningCount);
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
handleError(aError) {
|
2013-04-20 02:21:30 +04:00
|
|
|
log("expireStartCountFailure");
|
2017-06-15 03:46:42 +03:00
|
|
|
},
|
2013-04-20 02:21:30 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-06-15 03:46:42 +03:00
|
|
|
shutdown() { dbClose(true); },
|
2013-04-20 02:21:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Prevent add-ons from redefining this API
|
|
|
|
Object.freeze(FormHistory);
|