From 2b449941ae65d588e9f8e9a7f3bf9171213bff95 Mon Sep 17 00:00:00 2001 From: Gijs Kruitbosch Date: Mon, 15 Jul 2019 21:52:56 +0000 Subject: [PATCH] Bug 1557960 - cope with 0 values in Chrome's date/time columns when importing, r=mak Differential Revision: https://phabricator.services.mozilla.com/D37810 --HG-- extra : moz-landing-system : lando --- .../migration/ChromeMigrationUtils.jsm | 11 ++++++++++- .../migration/ChromeProfileMigrator.jsm | 16 +++++++++++++--- .../migration/tests/unit/test_Chrome_history.js | 3 ++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/browser/components/migration/ChromeMigrationUtils.jsm b/browser/components/migration/ChromeMigrationUtils.jsm index f5b4309c7e4d..ae71cc385c13 100644 --- a/browser/components/migration/ChromeMigrationUtils.jsm +++ b/browser/components/migration/ChromeMigrationUtils.jsm @@ -321,11 +321,20 @@ var ChromeMigrationUtils = { * * @param aTime * Chrome time + * @param aFallbackValue + * a date or timestamp (valid argument for the Date constructor) + * that will be used if the chrometime value passed is invalid. * @return converted Date object * @note Google Chrome uses FILETIME / 10 as time. * FILETIME is based on same structure of Windows. */ - chromeTimeToDate(aTime) { + chromeTimeToDate(aTime, aFallbackValue) { + // The date value may be 0 in some cases. Because of the subtraction below, + // that'd generate a date before the unix epoch, which can upset consumers + // due to the unix timestamp then being negative. Catch this case: + if (!aTime) { + return new Date(aFallbackValue); + } return new Date((aTime * S100NS_PER_MS - S100NS_FROM1601TO1970) / 10000); }, diff --git a/browser/components/migration/ChromeProfileMigrator.jsm b/browser/components/migration/ChromeProfileMigrator.jsm index c365527f2653..10d03d033fe3 100644 --- a/browser/components/migration/ChromeProfileMigrator.jsm +++ b/browser/components/migration/ChromeProfileMigrator.jsm @@ -302,6 +302,7 @@ async function GetHistoryResource(aProfileFolder) { query ); let pageInfos = []; + let fallbackVisitDate = new Date(); for (let row of rows) { try { // if having typed_count, we changes transition type to typed. @@ -317,7 +318,8 @@ async function GetHistoryResource(aProfileFolder) { { transition, date: ChromeMigrationUtils.chromeTimeToDate( - row.getResultByName("last_visit_time") + row.getResultByName("last_visit_time"), + fallbackVisitDate ), }, ], @@ -390,6 +392,7 @@ async function GetCookiesResource(aProfileFolder) { return; } + let fallbackExpiryDate = 0; for (let row of rows) { let host_key = row.getResultByName("host_key"); if (host_key.match(/^\./)) { @@ -400,8 +403,13 @@ async function GetCookiesResource(aProfileFolder) { try { let expiresUtc = ChromeMigrationUtils.chromeTimeToDate( - row.getResultByName("expires_utc") + row.getResultByName("expires_utc"), + fallbackExpiryDate ) / 1000; + // No point adding cookies that don't have a valid expiry. + if (!expiresUtc) { + continue; + } Services.cookies.add( host_key, row.getResultByName("path"), @@ -450,6 +458,7 @@ async function GetWindowsPasswordsResource(aProfileFolder) { } let crypto = new OSCrypto(); let logins = []; + let fallbackCreationDate = new Date(); for (let row of rows) { try { let origin_url = NetUtil.newURI(row.getResultByName("origin_url")); @@ -471,7 +480,8 @@ async function GetWindowsPasswordsResource(aProfileFolder) { usernameElement: row.getResultByName("username_element"), passwordElement: row.getResultByName("password_element"), timeCreated: ChromeMigrationUtils.chromeTimeToDate( - row.getResultByName("date_created") + 0 + row.getResultByName("date_created") + 0, + fallbackCreationDate ).getTime(), timesUsed: row.getResultByName("times_used") + 0, }; diff --git a/browser/components/migration/tests/unit/test_Chrome_history.js b/browser/components/migration/tests/unit/test_Chrome_history.js index 28f04d36b13a..9b1836d80ff3 100644 --- a/browser/components/migration/tests/unit/test_Chrome_history.js +++ b/browser/components/migration/tests/unit/test_Chrome_history.js @@ -107,7 +107,8 @@ function assertEntryMatches(entry, urlInfo, dateWasInFuture = false) { Assert.equal( entry.visits[index].date.getTime(), ChromeMigrationUtils.chromeTimeToDate( - urlInfo.visits[index].visit_time + urlInfo.visits[index].visit_time, + new Date() ).getTime(), "Should have the correct date" );