|
1 /** |
|
2 * Asynchronously adds visits to a page, invoking a callback function when done. |
|
3 * |
|
4 * @param aPlaceInfo |
|
5 * Can be an nsIURI, in such a case a single LINK visit will be added. |
|
6 * Otherwise can be an object describing the visit to add, or an array |
|
7 * of these objects: |
|
8 * { uri: nsIURI of the page, |
|
9 * transition: one of the TRANSITION_* from nsINavHistoryService, |
|
10 * [optional] title: title of the page, |
|
11 * [optional] visitDate: visit date in microseconds from the epoch |
|
12 * [optional] referrer: nsIURI of the referrer for this visit |
|
13 * } |
|
14 * @param [optional] aCallback |
|
15 * Function to be invoked on completion. |
|
16 */ |
|
17 function addVisits(aPlaceInfo, aCallback) { |
|
18 let places = []; |
|
19 if (aPlaceInfo instanceof Ci.nsIURI) { |
|
20 places.push({ uri: aPlaceInfo }); |
|
21 } |
|
22 else if (Array.isArray(aPlaceInfo)) { |
|
23 places = places.concat(aPlaceInfo); |
|
24 } else { |
|
25 places.push(aPlaceInfo) |
|
26 } |
|
27 |
|
28 // Create mozIVisitInfo for each entry. |
|
29 let now = Date.now(); |
|
30 for (let i = 0; i < places.length; i++) { |
|
31 if (!places[i].title) { |
|
32 places[i].title = "test visit for " + places[i].uri.spec; |
|
33 } |
|
34 places[i].visits = [{ |
|
35 transitionType: places[i].transition === undefined ? PlacesUtils.history.TRANSITION_LINK |
|
36 : places[i].transition, |
|
37 visitDate: places[i].visitDate || (now++) * 1000, |
|
38 referrerURI: places[i].referrer |
|
39 }]; |
|
40 } |
|
41 |
|
42 PlacesUtils.asyncHistory.updatePlaces( |
|
43 places, |
|
44 { |
|
45 handleError: function AAV_handleError() { |
|
46 throw("Unexpected error in adding visit."); |
|
47 }, |
|
48 handleResult: function () {}, |
|
49 handleCompletion: function UP_handleCompletion() { |
|
50 if (aCallback) |
|
51 aCallback(); |
|
52 } |
|
53 } |
|
54 ); |
|
55 } |
|
56 |
|
57 /** |
|
58 * Clears history invoking callback when done. |
|
59 */ |
|
60 function waitForClearHistory(aCallback) { |
|
61 const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished"; |
|
62 Services.obs.addObserver(function observer(aSubject, aTopic, aData) { |
|
63 Services.obs.removeObserver(observer, TOPIC_EXPIRATION_FINISHED); |
|
64 aCallback(); |
|
65 }, TOPIC_EXPIRATION_FINISHED, false); |
|
66 Cc["@mozilla.org/browser/nav-history-service;1"] |
|
67 .getService(Ci.nsINavHistoryService) |
|
68 .QueryInterface(Ci.nsIBrowserHistory).removeAllPages(); |
|
69 } |