Bug 1265845 - Implement browser.history.onVisited, r=aswan

MozReview-Commit-ID: 6jXEUA1Cll9

--HG--
extra : transplant_source : %FC%8Frg%7C2%25%A6%A0%FB%27%AB%DE%04Q%3E%83%BE%A6%EE
This commit is contained in:
Bob Silverberg 2016-06-08 12:42:13 -04:00
parent e71c0859af
commit 53b6985242
3 changed files with 113 additions and 2 deletions

View File

@ -98,7 +98,19 @@ function getObserver() {
onDeleteURI: function(uri, guid, reason) {
this.emit("visitRemoved", {allHistory: false, urls: [uri.spec]});
},
onVisit: function() {},
onVisit: function(uri, visitId, time, sessionId, referringId, transitionType, guid, hidden, visitCount, typed) {
PlacesUtils.promisePlaceInfo(guid).then(placeInfo => {
let data = {
id: guid,
url: uri.spec,
title: placeInfo.title,
lastVisitTime: time / 1000, // time from Places is microseconds,
visitCount,
typedCount: typed,
};
this.emit("visited", data);
});
},
onBeginUpdateBatch: function() {},
onEndUpdateBatch: function() {},
onTitleChanged: function() {},
@ -147,9 +159,11 @@ extensions.registerSchemaAPI("history", (extension, context) => {
return Promise.reject({message: error.message});
}
},
deleteAll: function() {
return History.clear();
},
deleteRange: function(filter) {
let newFilter = {
beginDate: normalizeTime(filter.startTime),
@ -158,11 +172,13 @@ extensions.registerSchemaAPI("history", (extension, context) => {
// History.removeVisitsByFilter returns a boolean, but our API should return nothing
return History.removeVisitsByFilter(newFilter).then(() => undefined);
},
deleteUrl: function(details) {
let url = details.url;
// History.remove returns a boolean, but our API should return nothing
return History.remove(url).then(() => undefined);
},
search: function(query) {
let beginTime = (query.startTime == null) ?
PlacesUtils.toPRTime(Date.now() - 24 * 60 * 60 * 1000) :
@ -186,6 +202,7 @@ extensions.registerSchemaAPI("history", (extension, context) => {
let results = convertNavHistoryContainerResultNode(queryResult, convertNodeToHistoryItem);
return Promise.resolve(results);
},
getVisits: function(details) {
let url = details.url;
if (!url) {
@ -203,6 +220,17 @@ extensions.registerSchemaAPI("history", (extension, context) => {
return Promise.resolve(results);
},
onVisited: new SingletonEventManager(context, "history.onVisited", fire => {
let listener = (event, data) => {
context.runSafe(fire, data);
};
getObserver().on("visited", listener);
return () => {
getObserver().off("visited", listener);
};
}).api(),
onVisitRemoved: new SingletonEventManager(context, "history.onVisitRemoved", fire => {
let listener = (event, data) => {
context.runSafe(fire, data);

View File

@ -292,7 +292,6 @@
"events": [
{
"name": "onVisited",
"unsupported": true,
"type": "function",
"description": "Fired when a URL is visited, providing the HistoryItem data for that URL. This event fires before the page has loaded.",
"parameters": [

View File

@ -399,3 +399,87 @@ add_task(function* test_get_visits() {
yield extension.awaitFinish("get-visits");
yield extension.unload();
});
add_task(function* test_on_visited() {
const SINGLE_VISIT_URL = "http://example.com/1/";
const DOUBLE_VISIT_URL = "http://example.com/2/";
let visitDate = new Date(1999, 9, 9, 9, 9).getTime();
// pages/visits to add via History.insertMany
const PAGE_INFOS = [
{
url: SINGLE_VISIT_URL,
title: `visit to ${SINGLE_VISIT_URL}`,
visits: [
{date: new Date(visitDate)},
],
},
{
url: DOUBLE_VISIT_URL,
title: `visit to ${DOUBLE_VISIT_URL}`,
visits: [
{date: new Date(visitDate += 1000)},
{date: new Date(visitDate += 1000)},
],
},
];
function background() {
let onVisitedData = [];
browser.history.onVisited.addListener(data => {
if (data.url.includes("moz-extension")) {
return;
}
onVisitedData.push(data);
if (onVisitedData.length == 3) {
browser.test.sendMessage("on-visited-data", onVisitedData);
}
});
browser.test.sendMessage("ready");
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["history"],
},
background: `(${background})()`,
});
yield PlacesTestUtils.clearHistory();
yield extension.startup();
yield extension.awaitMessage("ready");
yield PlacesUtils.history.insertMany(PAGE_INFOS);
let onVisitedData = yield extension.awaitMessage("on-visited-data");
function checkOnVisitedData(index, expected) {
let onVisited = onVisitedData[index];
ok(PlacesUtils.isValidGuid(onVisited.id), "onVisited received a valid id");
is(onVisited.url, expected.url, "onVisited received the expected url");
is(onVisited.title, expected.title, "onVisited received the expected title");
is(onVisited.lastVisitTime, expected.time, "onVisited received the expected time");
is(onVisited.visitCount, expected.visitCount, "onVisited received the expected visitCount");
}
let expected = {
url: PAGE_INFOS[0].url,
title: PAGE_INFOS[0].title,
time: PAGE_INFOS[0].visits[0].date.getTime(),
visitCount: 1,
};
checkOnVisitedData(0, expected);
expected.url = PAGE_INFOS[1].url;
expected.title = PAGE_INFOS[1].title;
expected.time = PAGE_INFOS[1].visits[0].date.getTime();
checkOnVisitedData(1, expected);
expected.time = PAGE_INFOS[1].visits[1].date.getTime();
expected.visitCount = 2;
checkOnVisitedData(2, expected);
yield extension.unload();
});