michael@0: /** michael@0: * Asynchronously adds visits to a page, invoking a callback function when done. michael@0: * michael@0: * @param aPlaceInfo michael@0: * Can be an nsIURI, in such a case a single LINK visit will be added. michael@0: * Otherwise can be an object describing the visit to add, or an array michael@0: * of these objects: michael@0: * { uri: nsIURI of the page, michael@0: * transition: one of the TRANSITION_* from nsINavHistoryService, michael@0: * [optional] title: title of the page, michael@0: * [optional] visitDate: visit date in microseconds from the epoch michael@0: * [optional] referrer: nsIURI of the referrer for this visit michael@0: * } michael@0: * @param [optional] aCallback michael@0: * Function to be invoked on completion. michael@0: */ michael@0: function addVisits(aPlaceInfo, aCallback) { michael@0: let places = []; michael@0: if (aPlaceInfo instanceof Ci.nsIURI) { michael@0: places.push({ uri: aPlaceInfo }); michael@0: } michael@0: else if (Array.isArray(aPlaceInfo)) { michael@0: places = places.concat(aPlaceInfo); michael@0: } else { michael@0: places.push(aPlaceInfo) michael@0: } michael@0: michael@0: // Create mozIVisitInfo for each entry. michael@0: let now = Date.now(); michael@0: for (let i = 0; i < places.length; i++) { michael@0: if (!places[i].title) { michael@0: places[i].title = "test visit for " + places[i].uri.spec; michael@0: } michael@0: places[i].visits = [{ michael@0: transitionType: places[i].transition === undefined ? PlacesUtils.history.TRANSITION_LINK michael@0: : places[i].transition, michael@0: visitDate: places[i].visitDate || (now++) * 1000, michael@0: referrerURI: places[i].referrer michael@0: }]; michael@0: } michael@0: michael@0: PlacesUtils.asyncHistory.updatePlaces( michael@0: places, michael@0: { michael@0: handleError: function AAV_handleError() { michael@0: throw("Unexpected error in adding visit."); michael@0: }, michael@0: handleResult: function () {}, michael@0: handleCompletion: function UP_handleCompletion() { michael@0: if (aCallback) michael@0: aCallback(); michael@0: } michael@0: } michael@0: ); michael@0: } michael@0: michael@0: /** michael@0: * Clears history invoking callback when done. michael@0: */ michael@0: function waitForClearHistory(aCallback) { michael@0: const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished"; michael@0: Services.obs.addObserver(function observer(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(observer, TOPIC_EXPIRATION_FINISHED); michael@0: aCallback(); michael@0: }, TOPIC_EXPIRATION_FINISHED, false); michael@0: Cc["@mozilla.org/browser/nav-history-service;1"] michael@0: .getService(Ci.nsINavHistoryService) michael@0: .QueryInterface(Ci.nsIBrowserHistory).removeAllPages(); michael@0: }