michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/FormHistory.jsm"); michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cu = Components.utils; michael@0: michael@0: const CURRENT_SCHEMA = 4; michael@0: const PR_HOURS = 60 * 60 * 1000000; michael@0: michael@0: do_get_profile(); michael@0: michael@0: var dirSvc = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties); michael@0: michael@0: // Send the profile-after-change notification to the form history component to ensure michael@0: // that it has been initialized. michael@0: var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"]. michael@0: getService(Ci.nsIObserver); michael@0: formHistoryStartup.observe(null, "profile-after-change", null); michael@0: michael@0: function getDBVersion(dbfile) { michael@0: var ss = Cc["@mozilla.org/storage/service;1"]. michael@0: getService(Ci.mozIStorageService); michael@0: var dbConnection = ss.openDatabase(dbfile); michael@0: var version = dbConnection.schemaVersion; michael@0: dbConnection.close(); michael@0: michael@0: return version; michael@0: } michael@0: michael@0: const isGUID = /[A-Za-z0-9\+\/]{16}/; michael@0: michael@0: // Find form history entries. michael@0: function searchEntries(terms, params, iter) { michael@0: let results = []; michael@0: FormHistory.search(terms, params, { handleResult: function (result) results.push(result), michael@0: handleError: function (error) { michael@0: do_throw("Error occurred searching form history: " + error); michael@0: }, michael@0: handleCompletion: function (reason) { if (!reason) iter.send(results); } michael@0: }); michael@0: } michael@0: michael@0: // Count the number of entries with the given name and value, and call then(number) michael@0: // when done. If name or value is null, then the value of that field does not matter. michael@0: function countEntries(name, value, then) { michael@0: var obj = {}; michael@0: if (name !== null) michael@0: obj.fieldname = name; michael@0: if (value !== null) michael@0: obj.value = value; michael@0: michael@0: let count = 0; michael@0: FormHistory.count(obj, { handleResult: function (result) count = result, michael@0: handleError: function (error) { michael@0: do_throw("Error occurred searching form history: " + error); michael@0: }, michael@0: handleCompletion: function (reason) { if (!reason) then(count); } michael@0: }); michael@0: } michael@0: michael@0: // Perform a single form history update and call then() when done. michael@0: function updateEntry(op, name, value, then) { michael@0: var obj = { op: op }; michael@0: if (name !== null) michael@0: obj.fieldname = name; michael@0: if (value !== null) michael@0: obj.value = value; michael@0: updateFormHistory(obj, then); michael@0: } michael@0: michael@0: // Add a single form history entry with the current time and call then() when done. michael@0: function addEntry(name, value, then) { michael@0: let now = Date.now() * 1000; michael@0: updateFormHistory({ op: "add", fieldname: name, value: value, timesUsed: 1, michael@0: firstUsed: now, lastUsed: now }, then); michael@0: } michael@0: michael@0: // Wrapper around FormHistory.update which handles errors. Calls then() when done. michael@0: function updateFormHistory(changes, then) { michael@0: FormHistory.update(changes, { handleError: function (error) { michael@0: do_throw("Error occurred updating form history: " + error); michael@0: }, michael@0: handleCompletion: function (reason) { if (!reason) then(); }, michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Logs info to the console in the standard way (includes the filename). michael@0: * michael@0: * @param aMessage michael@0: * The message to log to the console. michael@0: */ michael@0: function do_log_info(aMessage) { michael@0: print("TEST-INFO | " + _TEST_FILE + " | " + aMessage); michael@0: }