browser/components/migration/content/migration.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 const Cu = Components.utils;
michael@0 8
michael@0 9 const kIMig = Ci.nsIBrowserProfileMigrator;
michael@0 10 const kIPStartup = Ci.nsIProfileStartup;
michael@0 11
michael@0 12 Cu.import("resource:///modules/MigrationUtils.jsm");
michael@0 13
michael@0 14 var MigrationWizard = {
michael@0 15 _source: "", // Source Profile Migrator ContractID suffix
michael@0 16 _itemsFlags: kIMig.ALL, // Selected Import Data Sources (16-bit bitfield)
michael@0 17 _selectedProfile: null, // Selected Profile name to import from
michael@0 18 _wiz: null,
michael@0 19 _migrator: null,
michael@0 20 _autoMigrate: null,
michael@0 21
michael@0 22 init: function ()
michael@0 23 {
michael@0 24 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 25 .getService(Components.interfaces.nsIObserverService);
michael@0 26 os.addObserver(this, "Migration:Started", false);
michael@0 27 os.addObserver(this, "Migration:ItemBeforeMigrate", false);
michael@0 28 os.addObserver(this, "Migration:ItemAfterMigrate", false);
michael@0 29 os.addObserver(this, "Migration:ItemError", false);
michael@0 30 os.addObserver(this, "Migration:Ended", false);
michael@0 31
michael@0 32 this._wiz = document.documentElement;
michael@0 33
michael@0 34 if ("arguments" in window && window.arguments.length > 1) {
michael@0 35 this._source = window.arguments[0];
michael@0 36 this._migrator = window.arguments[1] instanceof kIMig ?
michael@0 37 window.arguments[1] : null;
michael@0 38 this._autoMigrate = window.arguments[2].QueryInterface(kIPStartup);
michael@0 39 this._skipImportSourcePage = window.arguments[3];
michael@0 40
michael@0 41 if (this._autoMigrate) {
michael@0 42 // Show the "nothing" option in the automigrate case to provide an
michael@0 43 // easily identifiable way to avoid migration and create a new profile.
michael@0 44 var nothing = document.getElementById("nothing");
michael@0 45 nothing.hidden = false;
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 this.onImportSourcePageShow();
michael@0 50 },
michael@0 51
michael@0 52 uninit: function ()
michael@0 53 {
michael@0 54 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 55 .getService(Components.interfaces.nsIObserverService);
michael@0 56 os.removeObserver(this, "Migration:Started");
michael@0 57 os.removeObserver(this, "Migration:ItemBeforeMigrate");
michael@0 58 os.removeObserver(this, "Migration:ItemAfterMigrate");
michael@0 59 os.removeObserver(this, "Migration:ItemError");
michael@0 60 os.removeObserver(this, "Migration:Ended");
michael@0 61 MigrationUtils.finishMigration();
michael@0 62 },
michael@0 63
michael@0 64 // 1 - Import Source
michael@0 65 onImportSourcePageShow: function ()
michael@0 66 {
michael@0 67 this._wiz.canRewind = false;
michael@0 68
michael@0 69 var selectedMigrator = null;
michael@0 70
michael@0 71 // Figure out what source apps are are available to import from:
michael@0 72 var group = document.getElementById("importSourceGroup");
michael@0 73 for (var i = 0; i < group.childNodes.length; ++i) {
michael@0 74 var migratorKey = group.childNodes[i].id;
michael@0 75 if (migratorKey != "nothing") {
michael@0 76 var migrator = MigrationUtils.getMigrator(migratorKey);
michael@0 77 if (migrator) {
michael@0 78 // Save this as the first selectable item, if we don't already have
michael@0 79 // one, or if it is the migrator that was passed to us.
michael@0 80 if (!selectedMigrator || this._source == migratorKey)
michael@0 81 selectedMigrator = group.childNodes[i];
michael@0 82 } else {
michael@0 83 // Hide this option
michael@0 84 group.childNodes[i].hidden = true;
michael@0 85 }
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 if (selectedMigrator)
michael@0 90 group.selectedItem = selectedMigrator;
michael@0 91 else {
michael@0 92 // We didn't find a migrator, notify the user
michael@0 93 document.getElementById("noSources").hidden = false;
michael@0 94
michael@0 95 this._wiz.canAdvance = false;
michael@0 96
michael@0 97 document.getElementById("importBookmarks").hidden = true;
michael@0 98 document.getElementById("importAll").hidden = true;
michael@0 99 }
michael@0 100
michael@0 101 // Advance to the next page if the caller told us to.
michael@0 102 if (this._migrator && this._skipImportSourcePage) {
michael@0 103 this._wiz.advance();
michael@0 104 this._wiz.canRewind = false;
michael@0 105 }
michael@0 106 },
michael@0 107
michael@0 108 onImportSourcePageAdvanced: function ()
michael@0 109 {
michael@0 110 var newSource = document.getElementById("importSourceGroup").selectedItem.id;
michael@0 111
michael@0 112 if (newSource == "nothing") {
michael@0 113 document.documentElement.cancel();
michael@0 114 return false;
michael@0 115 }
michael@0 116
michael@0 117 if (!this._migrator || (newSource != this._source)) {
michael@0 118 // Create the migrator for the selected source.
michael@0 119 this._migrator = MigrationUtils.getMigrator(newSource);
michael@0 120
michael@0 121 this._itemsFlags = kIMig.ALL;
michael@0 122 this._selectedProfile = null;
michael@0 123 }
michael@0 124 this._source = newSource;
michael@0 125
michael@0 126 // check for more than one source profile
michael@0 127 var sourceProfiles = this._migrator.sourceProfiles;
michael@0 128 if (sourceProfiles && sourceProfiles.length > 1) {
michael@0 129 this._wiz.currentPage.next = "selectProfile";
michael@0 130 }
michael@0 131 else {
michael@0 132 if (this._autoMigrate)
michael@0 133 this._wiz.currentPage.next = "homePageImport";
michael@0 134 else
michael@0 135 this._wiz.currentPage.next = "importItems";
michael@0 136
michael@0 137 if (sourceProfiles && sourceProfiles.length == 1)
michael@0 138 this._selectedProfile = sourceProfiles[0];
michael@0 139 else
michael@0 140 this._selectedProfile = "";
michael@0 141 }
michael@0 142 },
michael@0 143
michael@0 144 // 2 - [Profile Selection]
michael@0 145 onSelectProfilePageShow: function ()
michael@0 146 {
michael@0 147 // Disabling this for now, since we ask about import sources in automigration
michael@0 148 // too and don't want to disable the back button
michael@0 149 // if (this._autoMigrate)
michael@0 150 // document.documentElement.getButton("back").disabled = true;
michael@0 151
michael@0 152 var profiles = document.getElementById("profiles");
michael@0 153 while (profiles.hasChildNodes())
michael@0 154 profiles.removeChild(profiles.firstChild);
michael@0 155
michael@0 156 // Note that this block is still reached even if the user chose 'From File'
michael@0 157 // and we canceled the dialog. When that happens, _migrator will be null.
michael@0 158 if (this._migrator) {
michael@0 159 var sourceProfiles = this._migrator.sourceProfiles;
michael@0 160 for (var i = 0; i < sourceProfiles.length; ++i) {
michael@0 161 var item = document.createElement("radio");
michael@0 162 item.id = sourceProfiles[i];
michael@0 163 item.setAttribute("label", sourceProfiles[i]);
michael@0 164 profiles.appendChild(item);
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168 profiles.selectedItem = this._selectedProfile ? document.getElementById(this._selectedProfile) : profiles.firstChild;
michael@0 169 },
michael@0 170
michael@0 171 onSelectProfilePageRewound: function ()
michael@0 172 {
michael@0 173 var profiles = document.getElementById("profiles");
michael@0 174 this._selectedProfile = profiles.selectedItem.id;
michael@0 175 },
michael@0 176
michael@0 177 onSelectProfilePageAdvanced: function ()
michael@0 178 {
michael@0 179 var profiles = document.getElementById("profiles");
michael@0 180 this._selectedProfile = profiles.selectedItem.id;
michael@0 181
michael@0 182 // If we're automigrating or just doing bookmarks don't show the item selection page
michael@0 183 if (this._autoMigrate)
michael@0 184 this._wiz.currentPage.next = "homePageImport";
michael@0 185 },
michael@0 186
michael@0 187 // 3 - ImportItems
michael@0 188 onImportItemsPageShow: function ()
michael@0 189 {
michael@0 190 var dataSources = document.getElementById("dataSources");
michael@0 191 while (dataSources.hasChildNodes())
michael@0 192 dataSources.removeChild(dataSources.firstChild);
michael@0 193
michael@0 194 var items = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
michael@0 195 for (var i = 0; i < 16; ++i) {
michael@0 196 var itemID = (items >> i) & 0x1 ? Math.pow(2, i) : 0;
michael@0 197 if (itemID > 0) {
michael@0 198 var checkbox = document.createElement("checkbox");
michael@0 199 checkbox.id = itemID;
michael@0 200 checkbox.setAttribute("label",
michael@0 201 MigrationUtils.getLocalizedString(itemID + "_" + this._source));
michael@0 202 dataSources.appendChild(checkbox);
michael@0 203 if (!this._itemsFlags || this._itemsFlags & itemID)
michael@0 204 checkbox.checked = true;
michael@0 205 }
michael@0 206 }
michael@0 207 },
michael@0 208
michael@0 209 onImportItemsPageRewound: function ()
michael@0 210 {
michael@0 211 this._wiz.canAdvance = true;
michael@0 212 this.onImportItemsPageAdvanced();
michael@0 213 },
michael@0 214
michael@0 215 onImportItemsPageAdvanced: function ()
michael@0 216 {
michael@0 217 var dataSources = document.getElementById("dataSources");
michael@0 218 this._itemsFlags = 0;
michael@0 219 for (var i = 0; i < dataSources.childNodes.length; ++i) {
michael@0 220 var checkbox = dataSources.childNodes[i];
michael@0 221 if (checkbox.localName == "checkbox" && checkbox.checked)
michael@0 222 this._itemsFlags |= parseInt(checkbox.id);
michael@0 223 }
michael@0 224 },
michael@0 225
michael@0 226 onImportItemCommand: function (aEvent)
michael@0 227 {
michael@0 228 var items = document.getElementById("dataSources");
michael@0 229 var checkboxes = items.getElementsByTagName("checkbox");
michael@0 230
michael@0 231 var oneChecked = false;
michael@0 232 for (var i = 0; i < checkboxes.length; ++i) {
michael@0 233 if (checkboxes[i].checked) {
michael@0 234 oneChecked = true;
michael@0 235 break;
michael@0 236 }
michael@0 237 }
michael@0 238
michael@0 239 this._wiz.canAdvance = oneChecked;
michael@0 240 },
michael@0 241
michael@0 242 // 4 - Home Page Selection
michael@0 243 onHomePageMigrationPageShow: function ()
michael@0 244 {
michael@0 245 // only want this on the first run
michael@0 246 if (!this._autoMigrate) {
michael@0 247 this._wiz.advance();
michael@0 248 return;
michael@0 249 }
michael@0 250
michael@0 251 var brandBundle = document.getElementById("brandBundle");
michael@0 252 // These strings don't exist when not using official branding. If that's
michael@0 253 // the case, just skip this page.
michael@0 254 try {
michael@0 255 var pageTitle = brandBundle.getString("homePageMigrationPageTitle");
michael@0 256 var pageDesc = brandBundle.getString("homePageMigrationDescription");
michael@0 257 var mainStr = brandBundle.getString("homePageSingleStartMain");
michael@0 258 }
michael@0 259 catch (e) {
michael@0 260 this._wiz.advance();
michael@0 261 return;
michael@0 262 }
michael@0 263
michael@0 264 document.getElementById("homePageImport").setAttribute("label", pageTitle);
michael@0 265 document.getElementById("homePageImportDesc").setAttribute("value", pageDesc);
michael@0 266
michael@0 267 this._wiz._adjustWizardHeader();
michael@0 268
michael@0 269 var singleStart = document.getElementById("homePageSingleStart");
michael@0 270 singleStart.setAttribute("label", mainStr);
michael@0 271 singleStart.setAttribute("value", "DEFAULT");
michael@0 272
michael@0 273 var source = null;
michael@0 274 switch (this._source) {
michael@0 275 case "ie":
michael@0 276 source = "sourceNameIE";
michael@0 277 break;
michael@0 278 case "safari":
michael@0 279 source = "sourceNameSafari";
michael@0 280 break;
michael@0 281 case "chrome":
michael@0 282 source = "sourceNameChrome";
michael@0 283 break;
michael@0 284 case "firefox":
michael@0 285 source = "sourceNameFirefox";
michael@0 286 break;
michael@0 287 }
michael@0 288
michael@0 289 // semi-wallpaper for crash when multiple profiles exist, since we haven't initialized mSourceProfile in places
michael@0 290 this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
michael@0 291
michael@0 292 var oldHomePageURL = this._migrator.sourceHomePageURL;
michael@0 293
michael@0 294 if (oldHomePageURL && source) {
michael@0 295 var appName = MigrationUtils.getLocalizedString(source);
michael@0 296 var oldHomePageLabel =
michael@0 297 brandBundle.getFormattedString("homePageImport", [appName]);
michael@0 298 var oldHomePage = document.getElementById("oldHomePage");
michael@0 299 oldHomePage.setAttribute("label", oldHomePageLabel);
michael@0 300 oldHomePage.setAttribute("value", oldHomePageURL);
michael@0 301 oldHomePage.removeAttribute("hidden");
michael@0 302 }
michael@0 303 else {
michael@0 304 // if we don't have at least two options, just advance
michael@0 305 this._wiz.advance();
michael@0 306 }
michael@0 307 },
michael@0 308
michael@0 309 onHomePageMigrationPageAdvanced: function ()
michael@0 310 {
michael@0 311 // we might not have a selectedItem if we're in fallback mode
michael@0 312 try {
michael@0 313 var radioGroup = document.getElementById("homePageRadiogroup");
michael@0 314
michael@0 315 this._newHomePage = radioGroup.selectedItem.value;
michael@0 316 } catch(ex) {}
michael@0 317 },
michael@0 318
michael@0 319 // 5 - Migrating
michael@0 320 onMigratingPageShow: function ()
michael@0 321 {
michael@0 322 this._wiz.getButton("cancel").disabled = true;
michael@0 323 this._wiz.canRewind = false;
michael@0 324 this._wiz.canAdvance = false;
michael@0 325
michael@0 326 // When automigrating, show all of the data that can be received from this source.
michael@0 327 if (this._autoMigrate)
michael@0 328 this._itemsFlags = this._migrator.getMigrateData(this._selectedProfile, this._autoMigrate);
michael@0 329
michael@0 330 this._listItems("migratingItems");
michael@0 331 setTimeout(this.onMigratingMigrate, 0, this);
michael@0 332 },
michael@0 333
michael@0 334 onMigratingMigrate: function (aOuter)
michael@0 335 {
michael@0 336 aOuter._migrator.migrate(aOuter._itemsFlags, aOuter._autoMigrate, aOuter._selectedProfile);
michael@0 337 },
michael@0 338
michael@0 339 _listItems: function (aID)
michael@0 340 {
michael@0 341 var items = document.getElementById(aID);
michael@0 342 while (items.hasChildNodes())
michael@0 343 items.removeChild(items.firstChild);
michael@0 344
michael@0 345 var brandBundle = document.getElementById("brandBundle");
michael@0 346 var itemID;
michael@0 347 for (var i = 0; i < 16; ++i) {
michael@0 348 var itemID = (this._itemsFlags >> i) & 0x1 ? Math.pow(2, i) : 0;
michael@0 349 if (itemID > 0) {
michael@0 350 var label = document.createElement("label");
michael@0 351 label.id = itemID + "_migrated";
michael@0 352 try {
michael@0 353 label.setAttribute("value",
michael@0 354 MigrationUtils.getLocalizedString(itemID + "_" + this._source));
michael@0 355 items.appendChild(label);
michael@0 356 }
michael@0 357 catch (e) {
michael@0 358 // if the block above throws, we've enumerated all the import data types we
michael@0 359 // currently support and are now just wasting time, break.
michael@0 360 break;
michael@0 361 }
michael@0 362 }
michael@0 363 }
michael@0 364 },
michael@0 365
michael@0 366 observe: function (aSubject, aTopic, aData)
michael@0 367 {
michael@0 368 switch (aTopic) {
michael@0 369 case "Migration:Started":
michael@0 370 break;
michael@0 371 case "Migration:ItemBeforeMigrate":
michael@0 372 var label = document.getElementById(aData + "_migrated");
michael@0 373 if (label)
michael@0 374 label.setAttribute("style", "font-weight: bold");
michael@0 375 break;
michael@0 376 case "Migration:ItemAfterMigrate":
michael@0 377 var label = document.getElementById(aData + "_migrated");
michael@0 378 if (label)
michael@0 379 label.removeAttribute("style");
michael@0 380 break;
michael@0 381 case "Migration:Ended":
michael@0 382 if (this._autoMigrate) {
michael@0 383 if (this._newHomePage) {
michael@0 384 try {
michael@0 385 // set homepage properly
michael@0 386 var prefSvc = Components.classes["@mozilla.org/preferences-service;1"]
michael@0 387 .getService(Components.interfaces.nsIPrefService);
michael@0 388 var prefBranch = prefSvc.getBranch(null);
michael@0 389
michael@0 390 if (this._newHomePage == "DEFAULT") {
michael@0 391 prefBranch.clearUserPref("browser.startup.homepage");
michael@0 392 }
michael@0 393 else {
michael@0 394 var str = Components.classes["@mozilla.org/supports-string;1"]
michael@0 395 .createInstance(Components.interfaces.nsISupportsString);
michael@0 396 str.data = this._newHomePage;
michael@0 397 prefBranch.setComplexValue("browser.startup.homepage",
michael@0 398 Components.interfaces.nsISupportsString,
michael@0 399 str);
michael@0 400 }
michael@0 401
michael@0 402 var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
michael@0 403 .getService(Components.interfaces.nsIProperties);
michael@0 404 var prefFile = dirSvc.get("ProfDS", Components.interfaces.nsIFile);
michael@0 405 prefFile.append("prefs.js");
michael@0 406 prefSvc.savePrefFile(prefFile);
michael@0 407 } catch(ex) {
michael@0 408 dump(ex);
michael@0 409 }
michael@0 410 }
michael@0 411
michael@0 412 // We're done now.
michael@0 413 this._wiz.canAdvance = true;
michael@0 414 this._wiz.advance();
michael@0 415
michael@0 416 setTimeout(close, 5000);
michael@0 417 }
michael@0 418 else {
michael@0 419 this._wiz.canAdvance = true;
michael@0 420 var nextButton = this._wiz.getButton("next");
michael@0 421 nextButton.click();
michael@0 422 }
michael@0 423 break;
michael@0 424 case "Migration:ItemError":
michael@0 425 var type = "undefined";
michael@0 426 switch (parseInt(aData)) {
michael@0 427 case Ci.nsIBrowserProfileMigrator.SETTINGS:
michael@0 428 type = "settings";
michael@0 429 break;
michael@0 430 case Ci.nsIBrowserProfileMigrator.COOKIES:
michael@0 431 type = "cookies";
michael@0 432 break;
michael@0 433 case Ci.nsIBrowserProfileMigrator.HISTORY:
michael@0 434 type = "history";
michael@0 435 break;
michael@0 436 case Ci.nsIBrowserProfileMigrator.FORMDATA:
michael@0 437 type = "form data";
michael@0 438 break;
michael@0 439 case Ci.nsIBrowserProfileMigrator.PASSWORDS:
michael@0 440 type = "passwords";
michael@0 441 break;
michael@0 442 case Ci.nsIBrowserProfileMigrator.BOOKMARKS:
michael@0 443 type = "bookmarks";
michael@0 444 break;
michael@0 445 case Ci.nsIBrowserProfileMigrator.OTHERDATA:
michael@0 446 type = "misc. data";
michael@0 447 break;
michael@0 448 }
michael@0 449 Cc["@mozilla.org/consoleservice;1"]
michael@0 450 .getService(Ci.nsIConsoleService)
michael@0 451 .logStringMessage("some " + type + " did not successfully migrate.");
michael@0 452 break;
michael@0 453 }
michael@0 454 },
michael@0 455
michael@0 456 onDonePageShow: function ()
michael@0 457 {
michael@0 458 this._wiz.getButton("cancel").disabled = true;
michael@0 459 this._wiz.canRewind = false;
michael@0 460 this._listItems("doneItems");
michael@0 461 }
michael@0 462 };

mercurial