Bug 1335349 - add survey link option to 'undo' for automigration, r?dolske
MozReview-Commit-ID: BGsxAdsTu7X
--- a/browser/components/migration/AutoMigrate.jsm
+++ b/browser/components/migration/AutoMigrate.jsm
@@ -11,16 +11,19 @@ const { classes: Cc, interfaces: Ci, res
const kAutoMigrateEnabledPref = "browser.migrate.automigrate.enabled";
const kUndoUIEnabledPref = "browser.migrate.automigrate.ui.enabled";
const kAutoMigrateBrowserPref = "browser.migrate.automigrate.browser";
const kAutoMigrateLastUndoPromptDateMsPref = "browser.migrate.automigrate.lastUndoPromptDateMs";
const kAutoMigrateDaysToOfferUndoPref = "browser.migrate.automigrate.daysToOfferUndo";
+const kAutoMigrateUndoSurveyPref = "browser.migrate.automigrate.undo-survey";
+const kAutoMigrateUndoSurveyLocalePref = "browser.migrate.automigrate.undo-survey-locales";
+
const kNotificationId = "automigration-undo";
Cu.import("resource:///modules/MigrationUtils.jsm");
Cu.import("resource://gre/modules/Preferences.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
@@ -311,16 +314,17 @@ const AutoMigrate = {
this._purgeUndoState(this.UNDO_REMOVED_REASON_OFFER_REJECTED);
this._removeNotificationBars();
},
},
{
label: MigrationUtils.getLocalizedString("automigration.undo.dontkeep.label"),
accessKey: MigrationUtils.getLocalizedString("automigration.undo.dontkeep.accesskey"),
callback: () => {
+ this._maybeOpenUndoSurveyTab(win);
this.undo();
},
},
];
notificationBox.appendNotification(
message, kNotificationId, null, notificationBox.PRIORITY_INFO_HIGH, buttons
);
let remainingDays = Preferences.get(kAutoMigrateDaysToOfferUndoPref, 0);
@@ -495,14 +499,57 @@ const AutoMigrate = {
visitData.url = visitData.url.href;
} catch (ex) {}
Cu.reportError("Failed to remove a visit: " + JSON.stringify(visitData));
Cu.reportError(ex);
}
}
}),
+ /**
+ * Maybe open a new tab with a survey. The tab will only be opened if all of
+ * the following are true:
+ * - the 'browser.migrate.automigrate.undo-survey' pref is not empty.
+ * It should contain the URL of the survey to open.
+ * - the 'browser.migrate.automigrate.undo-survey-locales' pref, a
+ * comma-separated list of language codes, contains the language code
+ * that is currently in use for the 'global' chrome pacakge (ie the
+ * locale in which the user is currently using Firefox).
+ * The URL will be passed through nsIURLFormatter to allow including
+ * build ids etc. The special additional formatting variable
+ * "%IMPORTEDBROWSER" is also replaced with the name of the browser
+ * from which we imported data.
+ *
+ * @param {Window} chromeWindow A reference to the window in which to open a link.
+ */
+ _maybeOpenUndoSurveyTab(chromeWindow) {
+ let canDoSurveyInLocale = false;
+ try {
+ let surveyLocales = Preferences.get(kAutoMigrateUndoSurveyLocalePref, "");
+ surveyLocales = surveyLocales.split(",").map(str => str.trim());
+ // Strip out any empty elements, so an empty pref doesn't
+ // lead to a an array with 1 empty string in it.
+ surveyLocales = new Set(surveyLocales.filter(str => !!str));
+ let chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"]
+ .getService(Ci.nsIXULChromeRegistry);
+ canDoSurveyInLocale =
+ surveyLocales.has(chromeRegistry.getSelectedLocale("global"));
+ } catch (ex) {
+ /* ignore exceptions and just don't do the survey. */
+ }
+
+ let migrationBrowser = this.getBrowserUsedForMigration();
+ let rawURL = Preferences.get(kAutoMigrateUndoSurveyPref, "");
+ if (!canDoSurveyInLocale || !migrationBrowser || !rawURL) {
+ return;
+ }
+
+ let url = Services.urlFormatter.formatURL(rawURL);
+ url = url.replace("%IMPORTEDBROWSER%", encodeURIComponent(migrationBrowser));
+ chromeWindow.openUILinkIn(url, "tab");
+ },
+
QueryInterface: XPCOMUtils.generateQI(
[Ci.nsIObserver, Ci.nsINavBookmarkObserver, Ci.nsISupportsWeakReference]
),
};
AutoMigrate.init();
--- a/browser/components/migration/tests/browser/browser_undo_notification.js
+++ b/browser/components/migration/tests/browser/browser_undo_notification.js
@@ -33,26 +33,35 @@ add_task(function* autoMigrationUndoNoti
let notification = getNotification(browser);
let notificationBox = notification.parentNode;
notification.querySelector("button.notification-button-default").click();
ok(!undoCalled, "Undo should not be called when clicking the default button");
is(notification, notificationBox._closedNotification, "Notification should be closing");
yield BrowserTestUtils.removeTab(tab);
undoCalled = false;
- Services.prefs.setCharPref("browser.migrate.automigrate.browser", "someunknownbrowser");
+ Services.prefs.setCharPref("browser.migrate.automigrate.browser", "chrome");
tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, url, false);
browser = tab.linkedBrowser;
if (!getNotification(browser)) {
info(`Notification for ${url} not immediately present, waiting for it.`);
yield BrowserTestUtils.waitForNotificationBar(gBrowser, browser, kExpectedNotificationId);
}
ok(true, `Got notification for ${url}`);
notification = getNotification(browser);
notificationBox = notification.parentNode;
+ // Set up the survey:
+ yield SpecialPowers.pushPrefEnv({set: [
+ ["browser.migrate.automigrate.undo-survey", "https://example.com/?browser=%IMPORTEDBROWSER%"],
+ ["browser.migrate.automigrate.undo-survey-locales", "en-US"],
+ ]});
+ let tabOpenedPromise = BrowserTestUtils.waitForNewTab(gBrowser, "https://example.com/?browser=Google%20Chrome");
notification.querySelector("button:not(.notification-button-default)").click();
ok(undoCalled, "Undo should be called when clicking the non-default (Don't Keep) button");
is(notification, notificationBox._closedNotification, "Notification should be closing");
+ let surveyTab = yield tabOpenedPromise;
+ ok(surveyTab, "Should have opened a tab with a survey");
+ yield BrowserTestUtils.removeTab(surveyTab);
yield BrowserTestUtils.removeTab(tab);
}
});