Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
6 Components.utils.import("resource://gre/modules/Services.jsm");
7 Components.utils.import("resource://gre/modules/FormHistory.jsm");
9 const Ci = Components.interfaces;
10 const Cc = Components.classes;
11 const Cu = Components.utils;
13 const CURRENT_SCHEMA = 4;
14 const PR_HOURS = 60 * 60 * 1000000;
16 do_get_profile();
18 var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
19 getService(Ci.nsIProperties);
21 // Send the profile-after-change notification to the form history component to ensure
22 // that it has been initialized.
23 var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"].
24 getService(Ci.nsIObserver);
25 formHistoryStartup.observe(null, "profile-after-change", null);
27 function getDBVersion(dbfile) {
28 var ss = Cc["@mozilla.org/storage/service;1"].
29 getService(Ci.mozIStorageService);
30 var dbConnection = ss.openDatabase(dbfile);
31 var version = dbConnection.schemaVersion;
32 dbConnection.close();
34 return version;
35 }
37 const isGUID = /[A-Za-z0-9\+\/]{16}/;
39 // Find form history entries.
40 function searchEntries(terms, params, iter) {
41 let results = [];
42 FormHistory.search(terms, params, { handleResult: function (result) results.push(result),
43 handleError: function (error) {
44 do_throw("Error occurred searching form history: " + error);
45 },
46 handleCompletion: function (reason) { if (!reason) iter.send(results); }
47 });
48 }
50 // Count the number of entries with the given name and value, and call then(number)
51 // when done. If name or value is null, then the value of that field does not matter.
52 function countEntries(name, value, then) {
53 var obj = {};
54 if (name !== null)
55 obj.fieldname = name;
56 if (value !== null)
57 obj.value = value;
59 let count = 0;
60 FormHistory.count(obj, { handleResult: function (result) count = result,
61 handleError: function (error) {
62 do_throw("Error occurred searching form history: " + error);
63 },
64 handleCompletion: function (reason) { if (!reason) then(count); }
65 });
66 }
68 // Perform a single form history update and call then() when done.
69 function updateEntry(op, name, value, then) {
70 var obj = { op: op };
71 if (name !== null)
72 obj.fieldname = name;
73 if (value !== null)
74 obj.value = value;
75 updateFormHistory(obj, then);
76 }
78 // Add a single form history entry with the current time and call then() when done.
79 function addEntry(name, value, then) {
80 let now = Date.now() * 1000;
81 updateFormHistory({ op: "add", fieldname: name, value: value, timesUsed: 1,
82 firstUsed: now, lastUsed: now }, then);
83 }
85 // Wrapper around FormHistory.update which handles errors. Calls then() when done.
86 function updateFormHistory(changes, then) {
87 FormHistory.update(changes, { handleError: function (error) {
88 do_throw("Error occurred updating form history: " + error);
89 },
90 handleCompletion: function (reason) { if (!reason) then(); },
91 });
92 }
94 /**
95 * Logs info to the console in the standard way (includes the filename).
96 *
97 * @param aMessage
98 * The message to log to the console.
99 */
100 function do_log_info(aMessage) {
101 print("TEST-INFO | " + _TEST_FILE + " | " + aMessage);
102 }