Bug 1282770 - Convert uses of PU.asyncHistory.updatePlaces in browser/components/migration to PU.history.insertMany. r=mak

Patch originally by Ganesh, updated by Standard8

MozReview-Commit-ID: AihTLo5OyK1

--HG--
extra : rebase_source : f5a886326dd7f2fb2dba3309199ef277affe6b77
This commit is contained in:
Ganesh Chaitanya Kale 2018-03-20 12:36:31 +00:00
Родитель 2aff048970
Коммит e44c888947
7 изменённых файлов: 134 добавлений и 150 удалений

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

@ -610,6 +610,8 @@ const AutoMigrate = {
},
async _removeSomeVisits(visits) {
// It is necessary to recreate URL and date objects because the data
// can be serialized through JSON that destroys those objects.
for (let urlVisits of visits) {
let urlObj;
try {
@ -619,8 +621,8 @@ const AutoMigrate = {
}
let visitData = {
url: urlObj,
beginDate: PlacesUtils.toDate(urlVisits.first),
endDate: PlacesUtils.toDate(urlVisits.last),
beginDate: new Date(urlVisits.first),
endDate: new Date(urlVisits.last),
limit: urlVisits.visitCount,
};
try {

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

@ -273,22 +273,20 @@ async function GetHistoryResource(aProfileFolder) {
let rows =
await MigrationUtils.getRowsFromDBWithoutLocks(historyPath, "Chrome history", query);
let places = [];
let pageInfos = [];
for (let row of rows) {
try {
// if having typed_count, we changes transition type to typed.
let transType = PlacesUtils.history.TRANSITION_LINK;
let transition = PlacesUtils.history.TRANSITIONS.LINK;
if (row.getResultByName("typed_count") > 0)
transType = PlacesUtils.history.TRANSITION_TYPED;
transition = PlacesUtils.history.TRANSITIONS.TYPED;
places.push({
uri: NetUtil.newURI(row.getResultByName("url")),
pageInfos.push({
title: row.getResultByName("title"),
url: new URL(row.getResultByName("url")),
visits: [{
transitionType: transType,
visitDate: chromeTimeToDate(
row.getResultByName(
"last_visit_time")) * 1000,
transition,
date: chromeTimeToDate(row.getResultByName("last_visit_time")),
}],
});
} catch (e) {
@ -296,20 +294,8 @@ async function GetHistoryResource(aProfileFolder) {
}
}
if (places.length > 0) {
await new Promise((resolve, reject) => {
MigrationUtils.insertVisitsWrapper(places, {
ignoreErrors: true,
ignoreResults: true,
handleCompletion(updatedCount) {
if (updatedCount > 0) {
resolve();
} else {
reject(new Error("Couldn't add visits"));
}
},
});
});
if (pageInfos.length > 0) {
await MigrationUtils.insertVisitsWrapper(pageInfos);
}
})().then(() => { aCallback(true); },
ex => {

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

@ -109,12 +109,12 @@ EdgeTypedURLMigrator.prototype = {
migrate(aCallback) {
let typedURLs = this._typedURLs;
let places = [];
let pageInfos = [];
for (let [urlString, time] of typedURLs) {
let uri;
let url;
try {
uri = Services.io.newURI(urlString);
if (!["http", "https", "ftp"].includes(uri.scheme)) {
url = new URL(urlString);
if (!["http", "https", "ftp"].includes(url.scheme)) {
continue;
}
} catch (ex) {
@ -122,29 +122,23 @@ EdgeTypedURLMigrator.prototype = {
continue;
}
// Note that the time will be in microseconds (PRTime),
// and Date.now() returns milliseconds. Places expects PRTime,
// so we multiply the Date.now return value to make up the difference.
let visitDate = time || (Date.now() * 1000);
places.push({
uri,
visits: [{ transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED,
visitDate}],
pageInfos.push({
url,
visits: [{
transition: PlacesUtils.history.TRANSITIONS.TYPED,
date: time ? PlacesUtils.toDate(time) : new Date(),
}],
});
}
if (places.length == 0) {
if (pageInfos.length == 0) {
aCallback(typedURLs.size == 0);
return;
}
MigrationUtils.insertVisitsWrapper(places, {
ignoreErrors: true,
ignoreResults: true,
handleCompletion(updatedCount) {
aCallback(updatedCount > 0);
},
});
MigrationUtils.insertVisitsWrapper(pageInfos).then(
() => aCallback(true),
() => aCallback(false));
},
};

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

@ -38,17 +38,17 @@ History.prototype = {
},
migrate: function H_migrate(aCallback) {
let places = [];
let pageInfos = [];
let typedURLs = MSMigrationUtils.getTypedURLs("Software\\Microsoft\\Internet Explorer");
let historyEnumerator = Cc["@mozilla.org/profile/migrator/iehistoryenumerator;1"].
createInstance(Ci.nsISimpleEnumerator);
while (historyEnumerator.hasMoreElements()) {
let entry = historyEnumerator.getNext().QueryInterface(Ci.nsIPropertyBag2);
let uri = entry.get("uri").QueryInterface(Ci.nsIURI);
let url = entry.get("uri").QueryInterface(Ci.nsIURI);
// MSIE stores some types of URLs in its history that we don't handle,
// like HTMLHelp and others. Since we don't properly map handling for
// all of them we just avoid importing them.
if (!["http", "https", "ftp", "file"].includes(uri.scheme)) {
if (!["http", "https", "ftp", "file"].includes(url.scheme)) {
continue;
}
@ -59,37 +59,31 @@ History.prototype = {
}
// The typed urls are already fixed-up, so we can use them for comparison.
let transitionType = typedURLs.has(uri.spec) ?
Ci.nsINavHistoryService.TRANSITION_TYPED :
Ci.nsINavHistoryService.TRANSITION_LINK;
let transition = typedURLs.has(url.spec) ?
PlacesUtils.history.TRANSITIONS.LINK :
PlacesUtils.history.TRANSITIONS.TYPED;
// use the current date if we have no visits for this entry.
// Note that the entry will have a time in microseconds (PRTime),
// and Date.now() returns milliseconds. Places expects PRTime,
// so we multiply the Date.now return value to make up the difference.
let lastVisitTime = entry.get("time") || (Date.now() * 1000);
let time = entry.get("time");
places.push(
{ uri,
title,
visits: [{ transitionType,
visitDate: lastVisitTime }],
}
);
pageInfos.push({
url,
title,
visits: [{
transition,
date: time ? PlacesUtils.toDate(entry.get("time")) : new Date(),
}],
});
}
// Check whether there is any history to import.
if (places.length == 0) {
if (pageInfos.length == 0) {
aCallback(true);
return;
}
MigrationUtils.insertVisitsWrapper(places, {
ignoreErrors: true,
ignoreResults: true,
handleCompletion(updatedCount) {
aCallback(updatedCount > 0);
},
});
MigrationUtils.insertVisitsWrapper(pageInfos).then(
() => aCallback(true),
() => aCallback(false));
},
};

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

@ -1042,12 +1042,12 @@ var MigrationUtils = Object.freeze({
}, ex => Cu.reportError(ex));
},
insertVisitsWrapper(places, options) {
this._importQuantities.history += places.length;
insertVisitsWrapper(pageInfos) {
this._importQuantities.history += pageInfos.length;
if (gKeepUndoData) {
this._updateHistoryUndo(places);
this._updateHistoryUndo(pageInfos);
}
return PlacesUtils.asyncHistory.updatePlaces(places, options, true);
return PlacesUtils.history.insertMany(pageInfos);
},
async insertLoginsWrapper(logins) {
@ -1101,20 +1101,26 @@ var MigrationUtils = Object.freeze({
return this._postProcessUndoData(undoData);
},
_updateHistoryUndo(places) {
_updateHistoryUndo(pageInfos) {
let visits = gUndoData.get("visits");
let visitMap = new Map(visits.map(v => [v.url, v]));
for (let place of places) {
let visitCount = place.visits.length;
for (let pageInfo of pageInfos) {
let visitCount = pageInfo.visits.length;
let first, last;
if (visitCount > 1) {
let visitDates = place.visits.map(v => v.visitDate);
first = Math.min.apply(Math, visitDates);
last = Math.max.apply(Math, visitDates);
let dates = pageInfo.visits.map(v => v.date);
first = Math.min.apply(Math, dates);
last = Math.max.apply(Math, dates);
} else {
first = last = place.visits[0].visitDate;
first = last = pageInfo.visits[0].date;
}
let url = place.uri.spec;
let url = pageInfo.url;
if (url instanceof Ci.nsIURI) {
url = pageInfo.url.spec;
} else if (typeof url != "string") {
pageInfo.url.href;
}
try {
new URL(url);
} catch (ex) {

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

@ -17,8 +17,6 @@ ChromeUtils.defineModuleGetter(this, "PropertyListUtils",
"resource://gre/modules/PropertyListUtils.jsm");
ChromeUtils.defineModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
ChromeUtils.defineModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
ChromeUtils.defineModuleGetter(this, "FormHistory",
"resource://gre/modules/FormHistory.jsm");
@ -193,9 +191,9 @@ History.prototype = {
// reference date of NSDate.
let date = new Date("1 January 2001, GMT");
date.setMilliseconds(asDouble * 1000);
return date * 1000;
return date;
}
return 0;
return new Date();
},
migrate: function H_migrate(aCallback) {
@ -206,38 +204,42 @@ History.prototype = {
if (!aDict.has("WebHistoryDates"))
throw new Error("Unexpected history-property list format");
// Safari's History file contains only top-level urls. It does not
// distinguish between typed urls and linked urls.
let transType = PlacesUtils.history.TRANSITION_LINK;
let places = [];
let pageInfos = [];
let entries = aDict.get("WebHistoryDates");
let failedOnce = false;
for (let entry of entries) {
if (entry.has("lastVisitedDate")) {
let visitDate = this._parseCocoaDate(entry.get("lastVisitedDate"));
let date = this._parseCocoaDate(entry.get("lastVisitedDate"));
try {
places.push({ uri: NetUtil.newURI(entry.get("")),
title: entry.get("title"),
visits: [{ transitionType: transType,
visitDate }] });
pageInfos.push({
url: new URL(entry.get("")),
title: entry.get("title"),
visits: [{
// Safari's History file contains only top-level urls. It does not
// distinguish between typed urls and linked urls.
transition: PlacesUtils.history.TRANSITIONS.LINK,
date,
}],
});
} catch (ex) {
// Safari's History file may contain malformed URIs which
// will be ignored.
Cu.reportError(ex);
failedOnce = true;
}
}
}
if (places.length > 0) {
MigrationUtils.insertVisitsWrapper(places, {
ignoreErrors: true,
ignoreResults: true,
handleCompletion(updatedCount) {
aCallback(updatedCount > 0);
},
});
} else {
aCallback(false);
if (pageInfos.length == 0) {
// If we failed at least once, then we didn't succeed in importing,
// otherwise we didn't actually have anything to import, so we'll
// report it as a success.
aCallback(!failedOnce);
return;
}
MigrationUtils.insertVisitsWrapper(pageInfos).then(
() => aCallback(true),
() => aCallback(false));
} catch (ex) {
Cu.reportError(ex);
aCallback(false);

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

@ -5,7 +5,7 @@ ChromeUtils.import("resource:///modules/AutoMigrate.jsm", this);
let gShimmedMigratorKeyPicker = null;
let gShimmedMigrator = null;
const kUsecPerMin = 60 * 1000000;
const kMsecPerMin = 60 * 1000;
// This is really a proxy on MigrationUtils, but if we specify that directly,
// we get in trouble because the object itself is frozen, and Proxies can't
@ -261,7 +261,7 @@ add_task(async function checkUndoRemoval() {
Assert.equal(bookmark.title, "Some example bookmark", "Should have correct bookmark before undo.");
// Insert 2 history visits
let now_uSec = Date.now() * 1000;
let now = new Date();
let visitedURI = Services.io.newURI("http://www.example.com/");
let frecencyUpdatePromise = new Promise(resolve => {
let observer = {
@ -273,15 +273,15 @@ add_task(async function checkUndoRemoval() {
PlacesUtils.history.addObserver(observer);
});
await MigrationUtils.insertVisitsWrapper([{
uri: visitedURI,
url: visitedURI,
visits: [
{
transitionType: PlacesUtils.history.TRANSITION_LINK,
visitDate: now_uSec,
transition: PlacesUtils.history.TRANSITION_LINK,
date: now,
},
{
transitionType: PlacesUtils.history.TRANSITION_LINK,
visitDate: now_uSec - 100 * kUsecPerMin,
transition: PlacesUtils.history.TRANSITION_LINK,
date: new Date(now - 100 * kMsecPerMin),
},
],
}]);
@ -496,34 +496,34 @@ add_task(async function testLoginsRemovalByUndo() {
add_task(async function checkUndoVisitsState() {
MigrationUtils.initializeUndoData();
await MigrationUtils.insertVisitsWrapper([{
uri: NetUtil.newURI("http://www.example.com/"),
url: NetUtil.newURI("http://www.example.com/"),
title: "Example",
visits: [{
visitDate: new Date("2015-07-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-07-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}, {
visitDate: new Date("2015-09-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-09-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}, {
visitDate: new Date("2015-08-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-08-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}, {
uri: NetUtil.newURI("http://www.example.org/"),
url: Services.io.newURI("http://www.example.org/"),
title: "Example",
visits: [{
visitDate: new Date("2016-04-03").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2016-04-03"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}, {
visitDate: new Date("2015-08-03").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-08-03"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}, {
uri: NetUtil.newURI("http://www.example.com/"),
url: "http://www.example.com/",
title: "Example",
visits: [{
visitDate: new Date("2015-10-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-10-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}]);
let undoVisitData = (await MigrationUtils.stopAndRetrieveUndoData()).get("visits");
@ -532,14 +532,14 @@ add_task(async function checkUndoVisitsState() {
Assert.deepEqual(undoVisitData.find(v => v.url == "http://www.example.com/"), {
url: "http://www.example.com/",
visitCount: 4,
first: new Date("2015-07-10").getTime() * 1000,
last: new Date("2015-10-10").getTime() * 1000,
first: new Date("2015-07-10").getTime(),
last: new Date("2015-10-10").getTime(),
});
Assert.deepEqual(undoVisitData.find(v => v.url == "http://www.example.org/"), {
url: "http://www.example.org/",
visitCount: 2,
first: new Date("2015-08-03").getTime() * 1000,
last: new Date("2016-04-03").getTime() * 1000,
first: new Date("2015-08-03").getTime(),
last: new Date("2016-04-03").getTime(),
});
await PlacesUtils.history.clear();
@ -548,41 +548,41 @@ add_task(async function checkUndoVisitsState() {
add_task(async function checkUndoVisitsState() {
MigrationUtils.initializeUndoData();
await MigrationUtils.insertVisitsWrapper([{
uri: NetUtil.newURI("http://www.example.com/"),
url: NetUtil.newURI("http://www.example.com/"),
title: "Example",
visits: [{
visitDate: new Date("2015-07-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-07-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}, {
visitDate: new Date("2015-09-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-09-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}, {
visitDate: new Date("2015-08-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-08-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}, {
uri: NetUtil.newURI("http://www.example.org/"),
url: NetUtil.newURI("http://www.example.org/"),
title: "Example",
visits: [{
visitDate: new Date("2016-04-03").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2016-04-03"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}, {
visitDate: new Date("2015-08-03").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-08-03"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}, {
uri: NetUtil.newURI("http://www.example.com/"),
url: NetUtil.newURI("http://www.example.com/"),
title: "Example",
visits: [{
visitDate: new Date("2015-10-10").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-10-10"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}, {
uri: NetUtil.newURI("http://www.mozilla.org/"),
url: NetUtil.newURI("http://www.mozilla.org/"),
title: "Example",
visits: [{
visitDate: new Date("2015-01-01").getTime() * 1000,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK,
date: new Date("2015-01-01"),
transition: Ci.nsINavHistoryService.TRANSITION_LINK,
}],
}]);