From e6d919425972632a8d2394ad4d7efde6c7886536 Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Tue, 13 Jan 2015 11:23:16 +0100 Subject: [PATCH] Bug 1113175 - Implement async History.clear() r=mak --- toolkit/components/places/History.jsm | 56 +++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/toolkit/components/places/History.jsm b/toolkit/components/places/History.jsm index 0d223c7758c..78ad760c043 100644 --- a/toolkit/components/places/History.jsm +++ b/toolkit/components/places/History.jsm @@ -148,7 +148,7 @@ function ensureModuleIsOpen() { * @param args * array of arguments to pass to the notification. */ -function notify(observers, notification, args) { +function notify(observers, notification, args = []) { for (let observer of observers) { try { observer[notification](...args); @@ -253,7 +253,7 @@ this.History = Object.freeze({ * A callback invoked for each page found. * * @return (Promise) - * A promise resoled once the operation is complete. + * A promise resolved once the operation is complete. * @resolve (bool) * `true` if at least one page was removed, `false` otherwise. * @throws (TypeError) @@ -328,7 +328,7 @@ this.History = Object.freeze({ * The full URI of the page or the GUID of the page. * * @return (Promise) - * A promise resoled once the operation is complete. + * A promise resolved once the operation is complete. * @resolve (bool) * `true` if the page has been visited, `false` otherwise. * @throws (Error) @@ -339,6 +339,28 @@ this.History = Object.freeze({ throw new Error("Method not implemented"); }, + /** + * Clear all history. + * + * @return (Promise) + * A promise resolved once the operation is complete. + */ + clear() { + ensureModuleIsOpen(); + + return Task.spawn(function* () { + let promise = clear(); + operationsBarrier.client.addBlocker("History.clear", promise); + + try { + return (yield promise); + } finally { + // Cleanup the barrier. + operationsBarrier.client.removeBlocker(promise); + } + }); + }, + /** * Possible values for the `transition` property of `VisitInfo` * objects. @@ -453,6 +475,34 @@ let invalidateFrecencies = Task.async(function*(db, idList) { ); }); +// Inner implementation of History.clear(). +let clear = Task.async(function* () { + let db = yield DBConnPromised; + + // Remove all history. + yield db.execute("DELETE FROM moz_historyvisits"); + + // Clear the registered embed visits. + PlacesUtils.history.clearEmbedVisits(); + + // Expiration will take care of orphans. + let observers = PlacesUtils.history.getObservers(); + notify(observers, "onClearHistory"); + + // Invalidate frecencies for the remaining places. This must happen + // after the notification to ensure it runs enqueued to expiration. + yield db.execute( + `UPDATE moz_places SET frecency = + (CASE + WHEN url BETWEEN 'place:' AND 'place;' + THEN 0 + ELSE -1 + END) + WHERE frecency > 0`); + + // Notify frecency change observers. + notify(observers, "onManyFrecenciesChanged"); +}); // Inner implementation of History.remove. let remove = Task.async(function*({guids, urls}, onResult = null) {