Bug 1429108 - Switch FormHistory::search to promises. r=sgalich,mconley

Differential Revision: https://phabricator.services.mozilla.com/D160922
This commit is contained in:
Marco Bonardo 2022-11-03 09:35:38 +00:00
Родитель dd5c1b2f20
Коммит 3fe82d13db
8 изменённых файлов: 60 добавлений и 192 удалений

Просмотреть файл

@ -870,19 +870,7 @@ async function setupHistory() {
async function setupFormHistory() {
function searchEntries(terms, params) {
return new Promise((resolve, reject) => {
let results = [];
FormHistory.search(terms, params, {
handleResult: result => results.push(result),
handleError(error) {
reject(error);
throw new Error("Error occurred searching form history: " + error);
},
handleCompletion(reason) {
resolve(results);
},
});
});
return FormHistory.search(terms, params);
}
function update(changes) {

Просмотреть файл

@ -28,15 +28,8 @@ function countEntries(fieldname, message, expected) {
}
async function setupFormHistory() {
function searchEntries(terms, params) {
return new Promise((resolve, reject) => {
let callback = {
handleResult: resolve,
handleError: reject,
};
FormHistory.search(terms, params, callback);
});
async function searchFirstEntry(terms, params) {
return (await FormHistory.search(terms, params))[0];
}
function update(changes) {
@ -71,15 +64,15 @@ async function setupFormHistory() {
// Age the entries to the proper vintage.
let timestamp = PlacesUtils.toPRTime(REFERENCE_DATE);
let result = await searchEntries(["guid"], { fieldname: "reference" });
let result = await searchFirstEntry(["guid"], { fieldname: "reference" });
await update({ op: "update", firstUsed: timestamp, guid: result.guid });
timestamp = PlacesUtils.toPRTime(REFERENCE_DATE - 10000);
result = await searchEntries(["guid"], { fieldname: "10secondsAgo" });
result = await searchFirstEntry(["guid"], { fieldname: "10secondsAgo" });
await update({ op: "update", firstUsed: timestamp, guid: result.guid });
timestamp = PlacesUtils.toPRTime(REFERENCE_DATE - 10000 * 60);
result = await searchEntries(["guid"], { fieldname: "10minutesAgo" });
result = await searchFirstEntry(["guid"], { fieldname: "10minutesAgo" });
await update({ op: "update", firstUsed: timestamp, guid: result.guid });
// Sanity check.

Просмотреть файл

@ -322,17 +322,8 @@ class TestFirefoxRefresh(MarionetteTestCase):
"""
let resolve = arguments[arguments.length - 1];
let results = [];
global.FormHistory.search(["value"], {fieldname: arguments[0]}, {
handleError(error) {
results = error;
},
handleResult(result) {
results.push(result);
},
handleCompletion() {
resolve(results);
},
});
global.FormHistory.search(["value"], {fieldname: arguments[0]})
.then(resolve);
""",
script_args=(self._formHistoryFieldName,),
)

Просмотреть файл

@ -47,19 +47,8 @@ var FormWrapper = {
_getEntryCols: ["fieldname", "value"],
_guidCols: ["guid"],
async _search(terms, searchData) {
return new Promise(resolve => {
let results = [];
let callbacks = {
handleResult(result) {
results.push(result);
},
handleCompletion(reason) {
resolve(results);
},
};
lazy.FormHistory.search(terms, searchData, callbacks);
});
_search(terms, searchData) {
return lazy.FormHistory.search(terms, searchData);
},
async _update(changes) {

Просмотреть файл

@ -89,33 +89,15 @@ var FormDB = {
* or an object containing the row's guid, lastUsed, and firstUsed
* values>
*/
getDataForValue(fieldname, value) {
return new Promise((resolve, reject) => {
let result = null;
let handlers = {
handleResult(oneResult) {
if (result != null) {
reject("more than 1 result for this query");
return;
}
result = oneResult;
},
handleError(error) {
Logger.logError(
"Error occurred updating form history: " + Log.exceptionStr(error)
);
reject(error);
},
handleCompletion(reason) {
resolve(result);
},
};
FormHistory.search(
["guid", "lastUsed", "firstUsed"],
{ fieldname, value },
handlers
);
async getDataForValue(fieldname, value) {
let results = await FormHistory.search(["guid", "lastUsed", "firstUsed"], {
fieldname,
value,
});
if (results.length > 1) {
throw new Error("more than 1 result for this query");
}
return results;
},
/**

Просмотреть файл

@ -1043,7 +1043,7 @@ FormHistory = {
return defaultHandlers;
},
search(aSelectTerms, aSearchData, aRowFuncOrHandlers) {
async search(aSelectTerms, aSearchData, aRowFunc) {
// if no terms selected, select everything
if (!aSelectTerms) {
// Source is not a valid column in moz_formhistory.
@ -1058,45 +1058,21 @@ FormHistory = {
query += " WHERE " + queryTerms;
}
let handlers;
if (typeof aRowFuncOrHandlers == "function") {
handlers = this._prepareHandlers();
handlers.handleResult = aRowFuncOrHandlers;
} else if (typeof aRowFuncOrHandlers == "object") {
handlers = this._prepareHandlers(aRowFuncOrHandlers);
}
let allResults = [];
return new Promise((resolve, reject) => {
this.db.then(async conn => {
try {
await conn.executeCached(query, params, row => {
let result = {};
for (let field of aSelectTerms) {
result[field] = row.getResultByName(field);
}
if (handlers) {
handlers.handleResult(result);
} else {
allResults.push(result);
}
});
if (handlers) {
handlers.handleCompletion(0);
}
resolve(allResults);
} catch (e) {
if (handlers) {
handlers.handleError(e);
handlers.handleCompletion(1);
}
reject(e);
}
});
let conn = await this.db;
await conn.executeCached(query, params, row => {
let result = {};
for (let field of aSelectTerms) {
result[field] = row.getResultByName(field);
}
if (aRowFunc) {
aRowFunc(result);
}
allResults.push(result);
});
return allResults;
},
count(aSearchData, aHandlers) {
@ -1128,12 +1104,6 @@ FormHistory = {
},
async update(aChanges, aHandlers) {
// 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) {
// The identifier is only valid if one of either the guid
// or the (fieldname/value) are set (so an X-OR)
@ -1206,69 +1176,38 @@ FormHistory = {
);
}
numSearches++;
let changeToUpdate = change;
FormHistory.search(
["guid"],
{
try {
let results = await FormHistory.search(["guid"], {
fieldname: change.fieldname,
value: change.value,
},
{
foundResult: false,
handleResult(aResult) {
if (this.foundResult) {
log(
"Database contains multiple entries with the same fieldname/value pair."
);
handlers.handleError({
message:
"Database contains multiple entries with the same fieldname/value pair.",
result: 19, // Constraint violation
});
searchFailed = true;
return;
}
this.foundResult = true;
changeToUpdate.guid = aResult.guid;
},
handleError(aError) {
handlers.handleError(aError);
},
async handleCompletion(aReason) {
completedSearches++;
if (completedSearches == numSearches) {
if (!aReason && !searchFailed) {
try {
await updateFormHistoryWrite(aChanges);
handlers.handleCompletion(0);
} catch (e) {
handlers.handleError(e);
handlers.handleCompletion(1);
}
} else {
handlers.handleCompletion(1);
}
}
},
});
if (results.length > 1) {
log(
"Database contains multiple entries with the same fieldname/value pair."
);
handlers.handleError({
message:
"Database contains multiple entries with the same fieldname/value pair.",
result: 19, // Constraint violation
});
handlers.handleCompletion(1);
return;
}
);
}
if (numSearches == 0) {
// We don't have to wait for any statements to return.
try {
await updateFormHistoryWrite(aChanges);
handlers.handleCompletion(0);
change.guid = results[0]?.guid;
} catch (e) {
handlers.handleError(e);
handlers.handleCompletion(1);
return;
}
}
try {
await updateFormHistoryWrite(aChanges);
handlers.handleCompletion(0);
} catch (e) {
handlers.handleError(e);
handlers.handleCompletion(1);
}
},
getAutoCompleteResults(searchString, params, aHandlers) {

Просмотреть файл

@ -130,13 +130,7 @@ var FormHistoryTestUtils = {
* @resolves {Array} Array of found form history entries.
*/
search(fieldname, filters = {}) {
return new Promise((resolve, reject) => {
lazy.FormHistory.search(
null,
Object.assign({ fieldname }, filters),
this.makeListener(resolve, reject)
);
});
return lazy.FormHistory.search(null, Object.assign({ fieldname }, filters));
},
/**

Просмотреть файл

@ -63,18 +63,10 @@ const isGUID = /[A-Za-z0-9\+\/]{16}/;
// Find form history entries.
function searchEntries(terms, params, iter) {
let results = [];
FormHistory.search(terms, params, {
handleResult: result => results.push(result),
handleError(error) {
do_throw("Error occurred searching form history: " + error);
},
handleCompletion(reason) {
if (!reason) {
iter.next(results);
}
},
});
FormHistory.search(terms, params).then(
results => iter.next(results),
error => do_throw("Error occurred searching form history: " + error)
);
}
// Count the number of entries with the given name and value, and call then(number)