Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 sts=2 et */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | /* |
michael@0 | 10 | * Migrates from a Firefox profile in a lossy manner in order to clean up a |
michael@0 | 11 | * user's profile. Data is only migrated where the benefits outweigh the |
michael@0 | 12 | * potential problems caused by importing undesired/invalid configurations |
michael@0 | 13 | * from the source profile. |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 17 | Components.utils.import("resource:///modules/MigrationUtils.jsm"); |
michael@0 | 18 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 19 | XPCOMUtils.defineLazyModuleGetter(this, "PlacesBackups", |
michael@0 | 20 | "resource://gre/modules/PlacesBackups.jsm"); |
michael@0 | 21 | XPCOMUtils.defineLazyModuleGetter(this, "SessionMigration", |
michael@0 | 22 | "resource:///modules/sessionstore/SessionMigration.jsm"); |
michael@0 | 23 | |
michael@0 | 24 | function FirefoxProfileMigrator() { } |
michael@0 | 25 | |
michael@0 | 26 | FirefoxProfileMigrator.prototype = Object.create(MigratorPrototype); |
michael@0 | 27 | |
michael@0 | 28 | FirefoxProfileMigrator.prototype._getFileObject = function(dir, fileName) { |
michael@0 | 29 | let file = dir.clone(); |
michael@0 | 30 | file.append(fileName); |
michael@0 | 31 | |
michael@0 | 32 | // File resources are monolithic. We don't make partial copies since |
michael@0 | 33 | // they are not expected to work alone. Return null to avoid trying to |
michael@0 | 34 | // copy non-existing files. |
michael@0 | 35 | return file.exists() ? file : null; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | FirefoxProfileMigrator.prototype.getResources = function() { |
michael@0 | 39 | // Only allow migrating from the default (selected) profile since this will |
michael@0 | 40 | // be the only one returned by the toolkit profile service after bug 214675. |
michael@0 | 41 | let sourceProfile = |
michael@0 | 42 | Components.classes["@mozilla.org/toolkit/profile-service;1"] |
michael@0 | 43 | .getService(Components.interfaces.nsIToolkitProfileService) |
michael@0 | 44 | .selectedProfile; |
michael@0 | 45 | if (!sourceProfile) |
michael@0 | 46 | return null; |
michael@0 | 47 | |
michael@0 | 48 | let sourceProfileDir = sourceProfile.rootDir; |
michael@0 | 49 | if (!sourceProfileDir || !sourceProfileDir.exists() || |
michael@0 | 50 | !sourceProfileDir.isReadable()) |
michael@0 | 51 | return null; |
michael@0 | 52 | |
michael@0 | 53 | // Being a startup-only migrator, we can rely on |
michael@0 | 54 | // MigrationUtils.profileStartup being set. |
michael@0 | 55 | let currentProfileDir = MigrationUtils.profileStartup.directory; |
michael@0 | 56 | |
michael@0 | 57 | // Surely data cannot be imported from the current profile. |
michael@0 | 58 | if (sourceProfileDir.equals(currentProfileDir)) |
michael@0 | 59 | return null; |
michael@0 | 60 | |
michael@0 | 61 | let getFileResource = function(aMigrationType, aFileNames) { |
michael@0 | 62 | let files = []; |
michael@0 | 63 | for (let fileName of aFileNames) { |
michael@0 | 64 | let file = this._getFileObject(sourceProfileDir, fileName); |
michael@0 | 65 | if (!file) |
michael@0 | 66 | return null; |
michael@0 | 67 | files.push(file); |
michael@0 | 68 | } |
michael@0 | 69 | return { |
michael@0 | 70 | type: aMigrationType, |
michael@0 | 71 | migrate: function(aCallback) { |
michael@0 | 72 | for (let file of files) { |
michael@0 | 73 | file.copyTo(currentProfileDir, ""); |
michael@0 | 74 | } |
michael@0 | 75 | aCallback(true); |
michael@0 | 76 | } |
michael@0 | 77 | }; |
michael@0 | 78 | }.bind(this); |
michael@0 | 79 | |
michael@0 | 80 | let types = MigrationUtils.resourceTypes; |
michael@0 | 81 | let places = getFileResource(types.HISTORY, ["places.sqlite"]); |
michael@0 | 82 | let cookies = getFileResource(types.COOKIES, ["cookies.sqlite"]); |
michael@0 | 83 | let passwords = getFileResource(types.PASSWORDS, |
michael@0 | 84 | ["signons.sqlite", "key3.db"]); |
michael@0 | 85 | let formData = getFileResource(types.FORMDATA, ["formhistory.sqlite"]); |
michael@0 | 86 | let bookmarksBackups = getFileResource(types.OTHERDATA, |
michael@0 | 87 | [PlacesBackups.profileRelativeFolderPath]); |
michael@0 | 88 | let dictionary = getFileResource(types.OTHERDATA, ["persdict.dat"]); |
michael@0 | 89 | |
michael@0 | 90 | let sessionFile = this._getFileObject(sourceProfileDir, "sessionstore.js"); |
michael@0 | 91 | let session; |
michael@0 | 92 | if (sessionFile) { |
michael@0 | 93 | session = { |
michael@0 | 94 | type: types.SESSION, |
michael@0 | 95 | migrate: function(aCallback) { |
michael@0 | 96 | let newSessionFile = currentProfileDir.clone(); |
michael@0 | 97 | newSessionFile.append("sessionstore.js"); |
michael@0 | 98 | let migrationPromise = SessionMigration.migrate(sessionFile.path, newSessionFile.path); |
michael@0 | 99 | migrationPromise.then(function() { |
michael@0 | 100 | let buildID = Services.appinfo.platformBuildID; |
michael@0 | 101 | let mstone = Services.appinfo.platformVersion; |
michael@0 | 102 | // Force the browser to one-off resume the session that we give it: |
michael@0 | 103 | Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true); |
michael@0 | 104 | // Reset the homepage_override prefs so that the browser doesn't override our |
michael@0 | 105 | // session with the "what's new" page: |
michael@0 | 106 | Services.prefs.setCharPref("browser.startup.homepage_override.mstone", mstone); |
michael@0 | 107 | Services.prefs.setCharPref("browser.startup.homepage_override.buildID", buildID); |
michael@0 | 108 | // It's too early in startup for the pref service to have a profile directory, |
michael@0 | 109 | // so we have to manually tell it where to save the prefs file. |
michael@0 | 110 | let newPrefsFile = currentProfileDir.clone(); |
michael@0 | 111 | newPrefsFile.append("prefs.js"); |
michael@0 | 112 | Services.prefs.savePrefFile(newPrefsFile); |
michael@0 | 113 | aCallback(true); |
michael@0 | 114 | }, function() { |
michael@0 | 115 | aCallback(false); |
michael@0 | 116 | }); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | return [r for each (r in [places, cookies, passwords, formData, |
michael@0 | 122 | dictionary, bookmarksBackups, session]) if (r)]; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | Object.defineProperty(FirefoxProfileMigrator.prototype, "startupOnlyMigrator", { |
michael@0 | 126 | get: function() true |
michael@0 | 127 | }); |
michael@0 | 128 | |
michael@0 | 129 | |
michael@0 | 130 | FirefoxProfileMigrator.prototype.classDescription = "Firefox Profile Migrator"; |
michael@0 | 131 | FirefoxProfileMigrator.prototype.contractID = "@mozilla.org/profile/migrator;1?app=browser&type=firefox"; |
michael@0 | 132 | FirefoxProfileMigrator.prototype.classID = Components.ID("{91185366-ba97-4438-acba-48deaca63386}"); |
michael@0 | 133 | |
michael@0 | 134 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FirefoxProfileMigrator]); |