1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim: sw=2 ts=2 sts=2 et */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const AUTH_TYPE = {
SCHEME_HTML: 0,
SCHEME_BASIC: 1,
SCHEME_DIGEST: 2,
};
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { MigrationUtils } from "resource:///modules/MigrationUtils.sys.mjs";
import { MigratorBase } from "resource:///modules/MigratorBase.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ChromeMigrationUtils: "resource:///modules/ChromeMigrationUtils.sys.mjs",
PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
Qihoo360seMigrationUtils: "resource:///modules/360seMigrationUtils.sys.mjs",
});
XPCOMUtils.defineLazyModuleGetters(lazy, {
NetUtil: "resource://gre/modules/NetUtil.jsm",
});
/**
* Converts an array of chrome bookmark objects into one our own places code
* understands.
*
* @param {object[]} items Chrome Bookmark items to be inserted on this parent
* @param {Function} errorAccumulator function that gets called with any errors
* thrown so we don't drop them on the floor.
* @returns {object[]}
*/
function convertBookmarks(items, errorAccumulator) {
let itemsToInsert = [];
for (let item of items) {
try {
if (item.type == "url") {
if (item.url.trim().startsWith("chrome:")) {
// Skip invalid internal URIs. Creating an actual URI always reports
// messages to the console because Gecko has its own concept of how
// chrome:// URIs should be formed, so we avoid doing that.
continue;
}
if (item.url.trim().startsWith("edge:")) {
// Don't import internal Microsoft Edge URIs as they won't resolve within Firefox.
continue;
}
itemsToInsert.push({ url: item.url, title: item.name });
} else if (item.type == "folder") {
let folderItem = {
type: lazy.PlacesUtils.bookmarks.TYPE_FOLDER,
title: item.name,
};
folderItem.children = convertBookmarks(item.children, errorAccumulator);
itemsToInsert.push(folderItem);
}
} catch (ex) {
Cu.reportError(ex);
errorAccumulator(ex);
}
}
return itemsToInsert;
}
/**
* Chrome profile migrator. This can also be used as a parent class for
* migrators for browsers that are variants of Chrome.
*/
export class ChromeProfileMigrator extends MigratorBase {
static get key() {
return "chrome";
}
get _chromeUserDataPathSuffix() {
return "Chrome";
}
_keychainServiceName = "Chrome Safe Storage";
_keychainAccountName = "Chrome";
async _getChromeUserDataPathIfExists() {
if (this._chromeUserDataPath) {
return this._chromeUserDataPath;
}
let path = lazy.ChromeMigrationUtils.getDataPath(
this._chromeUserDataPathSuffix
);
let exists = await IOUtils.exists(path);
if (exists) {
this._chromeUserDataPath = path;
} else {
this._chromeUserDataPath = null;
}
return this._chromeUserDataPath;
}
async getResources(aProfile) {
let chromeUserDataPath = await this._getChromeUserDataPathIfExists();
if (chromeUserDataPath) {
let profileFolder = chromeUserDataPath;
if (aProfile) {
profileFolder = PathUtils.join(chromeUserDataPath, aProfile.id);
}
if (await IOUtils.exists(profileFolder)) {
let possibleResourcePromises = [
GetBookmarksResource(profileFolder, this.constructor.key),
GetHistoryResource(profileFolder),
GetCookiesResource(profileFolder),
];
if (lazy.ChromeMigrationUtils.supportsLoginsForPlatform) {
possibleResourcePromises.push(
this._GetPasswordsResource(profileFolder)
);
}
let possibleResources = await Promise.all(possibleResourcePromises);
return possibleResources.filter(r => r != null);
}
}
return [];
}
async getLastUsedDate() {
let sourceProfiles = await this.getSourceProfiles();
let chromeUserDataPath = await this._getChromeUserDataPathIfExists();
if (!chromeUserDataPath) {
return new Date(0);
}
let datePromises = sourceProfiles.map(async profile => {
let basePath = PathUtils.join(chromeUserDataPath, profile.id);
let fileDatePromises = ["Bookmarks", "History", "Cookies"].map(
async leafName => {
let path = PathUtils.join(basePath, leafName);
let info = await IOUtils.stat(path).catch(() => null);
return info ? info.lastModificationDate : 0;
}
);
let dates = await Promise.all(fileDatePromises);
return Math.max(...dates);
});
let datesOuter = await Promise.all(datePromises);
datesOuter.push(0);
return new Date(Math.max(...datesOuter));
}
async getSourceProfiles() {
if ("__sourceProfiles" in this) {
return this.__sourceProfiles;
}
let chromeUserDataPath = await this._getChromeUserDataPathIfExists();
if (!chromeUserDataPath) {
return [];
}
let localState;
let profiles = [];
try {
localState = await lazy.ChromeMigrationUtils.getLocalState(
this._chromeUserDataPathSuffix
);
let info_cache = localState.profile.info_cache;
for (let profileFolderName in info_cache) {
profiles.push({
id: profileFolderName,
name: info_cache[profileFolderName].name || profileFolderName,
});
}
} catch (e) {
// Avoid reporting NotFoundErrors from trying to get local state.
if (localState || e.name != "NotFoundError") {
Cu.reportError("Error detecting Chrome profiles: " + e);
}
// If we weren't able to detect any profiles above, fallback to the Default profile.
let defaultProfilePath = PathUtils.join(chromeUserDataPath, "Default");
if (await IOUtils.exists(defaultProfilePath)) {
profiles = [
{
id: "Default",
name: "Default",
},
];
}
}
let profileResources = await Promise.all(
profiles.map(async profile => ({
profile,
resources: await this.getResources(profile),
}))
);
// Only list profiles from which any data can be imported
this.__sourceProfiles = profileResources
.filter(({ resources }) => {
return resources && !!resources.length;
}, this)
.map(({ profile }) => profile);
return this.__sourceProfiles;
}
async _GetPasswordsResource(aProfileFolder) {
let loginPath = PathUtils.join(aProfileFolder, "Login Data");
if (!(await IOUtils.exists(loginPath))) {
return null;
}
let {
_chromeUserDataPathSuffix,
_keychainServiceName,
_keychainAccountName,
_keychainMockPassphrase = null,
} = this;
return {
type: MigrationUtils.resourceTypes.PASSWORDS,
async migrate(aCallback) {
let rows = await MigrationUtils.getRowsFromDBWithoutLocks(
loginPath,
"Chrome passwords",
`SELECT origin_url, action_url, username_element, username_value,
password_element, password_value, signon_realm, scheme, date_created,
times_used FROM logins WHERE blacklisted_by_user = 0`
).catch(ex => {
Cu.reportError(ex);
aCallback(false);
});
// If the promise was rejected we will have already called aCallback,
// so we can just return here.
if (!rows) {
return;
}
// If there are no relevant rows, return before initializing crypto and
// thus prompting for Keychain access on macOS.
if (!rows.length) {
aCallback(true);
return;
}
let crypto;
try {
if (AppConstants.platform == "win") {
let { ChromeWindowsLoginCrypto } = ChromeUtils.importESModule(
"resource:///modules/ChromeWindowsLoginCrypto.sys.mjs"
);
crypto = new ChromeWindowsLoginCrypto(_chromeUserDataPathSuffix);
} else if (AppConstants.platform == "macosx") {
let { ChromeMacOSLoginCrypto } = ChromeUtils.importESModule(
"resource:///modules/ChromeMacOSLoginCrypto.sys.mjs"
);
crypto = new ChromeMacOSLoginCrypto(
_keychainServiceName,
_keychainAccountName,
_keychainMockPassphrase
);
} else {
aCallback(false);
return;
}
} catch (ex) {
// Handle the user canceling Keychain access or other OSCrypto errors.
Cu.reportError(ex);
aCallback(false);
return;
}
let logins = [];
let fallbackCreationDate = new Date();
for (let row of rows) {
try {
let origin_url = lazy.NetUtil.newURI(
row.getResultByName("origin_url")
);
// Ignore entries for non-http(s)/ftp URLs because we likely can't
// use them anyway.
const kValidSchemes = new Set(["https", "http", "ftp"]);
if (!kValidSchemes.has(origin_url.scheme)) {
continue;
}
let loginInfo = {
username: row.getResultByName("username_value"),
password: await crypto.decryptData(
row.getResultByName("password_value"),
null
),
origin: origin_url.prePath,
formActionOrigin: null,
httpRealm: null,
usernameElement: row.getResultByName("username_element"),
passwordElement: row.getResultByName("password_element"),
timeCreated: lazy.ChromeMigrationUtils.chromeTimeToDate(
row.getResultByName("date_created") + 0,
fallbackCreationDate
).getTime(),
timesUsed: row.getResultByName("times_used") + 0,
};
switch (row.getResultByName("scheme")) {
case AUTH_TYPE.SCHEME_HTML:
let action_url = row.getResultByName("action_url");
if (!action_url) {
// If there is no action_url, store the wildcard "" value.
// See the `formActionOrigin` IDL comments.
loginInfo.formActionOrigin = "";
break;
}
let action_uri = lazy.NetUtil.newURI(action_url);
if (!kValidSchemes.has(action_uri.scheme)) {
continue; // This continues the outer for loop.
}
loginInfo.formActionOrigin = action_uri.prePath;
break;
case AUTH_TYPE.SCHEME_BASIC:
case AUTH_TYPE.SCHEME_DIGEST:
// signon_realm format is URIrealm, so we need remove URI
loginInfo.httpRealm = row
.getResultByName("signon_realm")
.substring(loginInfo.origin.length + 1);
break;
default:
throw new Error(
"Login data scheme type not supported: " +
row.getResultByName("scheme")
);
}
logins.push(loginInfo);
} catch (e) {
Cu.reportError(e);
}
}
try {
if (logins.length) {
await MigrationUtils.insertLoginsWrapper(logins);
}
} catch (e) {
Cu.reportError(e);
}
if (crypto.finalize) {
crypto.finalize();
}
aCallback(true);
},
};
}
}
async function GetBookmarksResource(aProfileFolder, aBrowserKey) {
let bookmarksPath = PathUtils.join(aProfileFolder, "Bookmarks");
if (aBrowserKey === "chromium-360se") {
let localState = {};
try {
localState = await lazy.ChromeMigrationUtils.getLocalState("360 SE");
} catch (ex) {
Cu.reportError(ex);
}
let alternativeBookmarks = await lazy.Qihoo360seMigrationUtils.getAlternativeBookmarks(
{ bookmarksPath, localState }
);
if (alternativeBookmarks.resource) {
return alternativeBookmarks.resource;
}
bookmarksPath = alternativeBookmarks.path;
}
if (!(await IOUtils.exists(bookmarksPath))) {
return null;
}
return {
type: MigrationUtils.resourceTypes.BOOKMARKS,
migrate(aCallback) {
return (async function() {
let gotErrors = false;
let errorGatherer = function() {
gotErrors = true;
};
// Parse Chrome bookmark file that is JSON format
let bookmarkJSON = await IOUtils.readJSON(bookmarksPath);
let roots = bookmarkJSON.roots;
// Importing bookmark bar items
if (roots.bookmark_bar.children && roots.bookmark_bar.children.length) {
// Toolbar
let parentGuid = lazy.PlacesUtils.bookmarks.toolbarGuid;
let bookmarks = convertBookmarks(
roots.bookmark_bar.children,
errorGatherer
);
await MigrationUtils.insertManyBookmarksWrapper(
bookmarks,
parentGuid
);
}
// Importing Other Bookmarks items
if (roots.other.children && roots.other.children.length) {
// Other Bookmarks
let parentGuid = lazy.PlacesUtils.bookmarks.unfiledGuid;
let bookmarks = convertBookmarks(roots.other.children, errorGatherer);
await MigrationUtils.insertManyBookmarksWrapper(
bookmarks,
parentGuid
);
}
if (gotErrors) {
throw new Error("The migration included errors.");
}
})().then(
() => aCallback(true),
() => aCallback(false)
);
},
};
}
async function GetHistoryResource(aProfileFolder) {
let historyPath = PathUtils.join(aProfileFolder, "History");
if (!(await IOUtils.exists(historyPath))) {
return null;
}
return {
type: MigrationUtils.resourceTypes.HISTORY,
migrate(aCallback) {
(async function() {
const MAX_AGE_IN_DAYS = Services.prefs.getIntPref(
"browser.migrate.chrome.history.maxAgeInDays"
);
const LIMIT = Services.prefs.getIntPref(
"browser.migrate.chrome.history.limit"
);
let query =
"SELECT url, title, last_visit_time, typed_count FROM urls WHERE hidden = 0";
if (MAX_AGE_IN_DAYS) {
let maxAge = lazy.ChromeMigrationUtils.dateToChromeTime(
Date.now() - MAX_AGE_IN_DAYS * 24 * 60 * 60 * 1000
);
query += " AND last_visit_time > " + maxAge;
}
if (LIMIT) {
query += " ORDER BY last_visit_time DESC LIMIT " + LIMIT;
}
let rows = await MigrationUtils.getRowsFromDBWithoutLocks(
historyPath,
"Chrome history",
query
);
let pageInfos = [];
let fallbackVisitDate = new Date();
for (let row of rows) {
try {
// if having typed_count, we changes transition type to typed.
let transition = lazy.PlacesUtils.history.TRANSITIONS.LINK;
if (row.getResultByName("typed_count") > 0) {
transition = lazy.PlacesUtils.history.TRANSITIONS.TYPED;
}
pageInfos.push({
title: row.getResultByName("title"),
url: new URL(row.getResultByName("url")),
visits: [
{
transition,
date: lazy.ChromeMigrationUtils.chromeTimeToDate(
row.getResultByName("last_visit_time"),
fallbackVisitDate
),
},
],
});
} catch (e) {
Cu.reportError(e);
}
}
if (pageInfos.length) {
await MigrationUtils.insertVisitsWrapper(pageInfos);
}
})().then(
() => {
aCallback(true);
},
ex => {
Cu.reportError(ex);
aCallback(false);
}
);
},
};
}
async function GetCookiesResource(aProfileFolder) {
let cookiesPath = PathUtils.join(aProfileFolder, "Cookies");
if (!(await IOUtils.exists(cookiesPath))) {
return null;
}
return {
type: MigrationUtils.resourceTypes.COOKIES,
async migrate(aCallback) {
// Get columns names and set is_sceure, is_httponly fields accordingly.
let columns = await MigrationUtils.getRowsFromDBWithoutLocks(
cookiesPath,
"Chrome cookies",
`PRAGMA table_info(cookies)`
).catch(ex => {
Cu.reportError(ex);
aCallback(false);
});
// If the promise was rejected we will have already called aCallback,
// so we can just return here.
if (!columns) {
return;
}
columns = columns.map(c => c.getResultByName("name"));
let isHttponly = columns.includes("is_httponly")
? "is_httponly"
: "httponly";
let isSecure = columns.includes("is_secure") ? "is_secure" : "secure";
let source_scheme = columns.includes("source_scheme")
? "source_scheme"
: `"${Ci.nsICookie.SCHEME_UNSET}" as source_scheme`;
// We don't support decrypting cookies yet so only import plaintext ones.
let rows = await MigrationUtils.getRowsFromDBWithoutLocks(
cookiesPath,
"Chrome cookies",
`SELECT host_key, name, value, path, expires_utc, ${isSecure}, ${isHttponly}, encrypted_value, ${source_scheme}
FROM cookies
WHERE length(encrypted_value) = 0`
).catch(ex => {
Cu.reportError(ex);
aCallback(false);
});
// If the promise was rejected we will have already called aCallback,
// so we can just return here.
if (!rows) {
return;
}
let fallbackExpiryDate = 0;
for (let row of rows) {
let host_key = row.getResultByName("host_key");
if (host_key.match(/^\./)) {
// 1st character of host_key may be ".", so we have to remove it
host_key = host_key.substr(1);
}
let schemeType = Ci.nsICookie.SCHEME_UNSET;
switch (row.getResultByName("source_scheme")) {
case 1:
schemeType = Ci.nsICookie.SCHEME_HTTP;
break;
case 2:
schemeType = Ci.nsICookie.SCHEME_HTTPS;
break;
}
try {
let expiresUtc =
lazy.ChromeMigrationUtils.chromeTimeToDate(
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"),
row.getResultByName("name"),
row.getResultByName("value"),
row.getResultByName(isSecure),
row.getResultByName(isHttponly),
false,
parseInt(expiresUtc),
{},
Ci.nsICookie.SAMESITE_NONE,
schemeType
);
} catch (e) {
Cu.reportError(e);
}
}
aCallback(true);
},
};
}
/**
* Chromium migrator
*/
export class ChromiumProfileMigrator extends ChromeProfileMigrator {
static get key() {
return "chromium";
}
_chromeUserDataPathSuffix = "Chromium";
_keychainServiceName = "Chromium Safe Storage";
_keychainAccountName = "Chromium";
}
/**
* Chrome Canary
* Not available on Linux
*/
export class CanaryProfileMigrator extends ChromeProfileMigrator {
static get key() {
return "canary";
}
get _chromeUserDataPathSuffix() {
return "Canary";
}
get _keychainServiceName() {
return "Chromium Safe Storage";
}
get _keychainAccountName() {
return "Chromium";
}
}
/**
* Chrome Dev - Linux only (not available in Mac and Windows)
*/
export class ChromeDevMigrator extends ChromeProfileMigrator {
static get key() {
return "chrome-dev";
}
_chromeUserDataPathSuffix = "Chrome Dev";
_keychainServiceName = "Chromium Safe Storage";
_keychainAccountName = "Chromium";
}
/**
* Chrome Beta migrator
*/
export class ChromeBetaMigrator extends ChromeProfileMigrator {
static get key() {
return "chrome-beta";
}
_chromeUserDataPathSuffix = "Chrome Beta";
_keychainServiceName = "Chromium Safe Storage";
_keychainAccountName = "Chromium";
}
/**
* Brave migrator
*/
export class BraveProfileMigrator extends ChromeProfileMigrator {
static get key() {
return "brave";
}
_chromeUserDataPathSuffix = "Brave";
_keychainServiceName = "Brave Browser Safe Storage";
_keychainAccountName = "Brave Browser";
}
/**
* Edge (Chromium-based) migrator
*/
export class ChromiumEdgeMigrator extends ChromeProfileMigrator {
static get key() {
return "chromium-edge";
}
_chromeUserDataPathSuffix = "Edge";
_keychainServiceName = "Microsoft Edge Safe Storage";
_keychainAccountName = "Microsoft Edge";
}
/**
* Edge Beta (Chromium-based) migrator
*/
export class ChromiumEdgeBetaMigrator extends ChromeProfileMigrator {
static get key() {
return "chromium-edge-beta";
}
_chromeUserDataPathSuffix = "Edge Beta";
_keychainServiceName = "Microsoft Edge Safe Storage";
_keychainAccountName = "Microsoft Edge";
}
/**
* Chromium 360 migrator
*/
export class Chromium360seMigrator extends ChromeProfileMigrator {
static get key() {
return "chromium-360se";
}
_chromeUserDataPathSuffix = "360 SE";
_keychainServiceName = "Microsoft Edge Safe Storage";
_keychainAccountName = "Microsoft Edge";
}
/**
* Opera migrator
*/
export class OperaProfileMigrator extends ChromeProfileMigrator {
static get key() {
return "opera";
}
_chromeUserDataPathSuffix = "Opera";
_keychainServiceName = "Opera Safe Storage";
_keychainAccountName = "Opera";
getSourceProfiles() {
return null;
}
}
/**
* Opera GX migrator
*/
export class OperaGXProfileMigrator extends ChromeProfileMigrator {
static get key() {
return "opera-gx";
}
_chromeUserDataPathSuffix = "Opera GX";
_keychainServiceName = "Opera Safe Storage";
_keychainAccountName = "Opera";
getSourceProfiles() {
return null;
}
}
/**
* Vivaldi migrator
*/
export class VivaldiProfileMigrator extends ChromeProfileMigrator {
static get key() {
return "vivaldi";
}
_chromeUserDataPathSuffix = "Vivaldi";
_keychainServiceName = "Vivaldi Safe Storage";
_keychainAccountName = "Vivaldi";
}
|