[tbb-commits] [Git][tpo/applications/tor-browser][tor-browser-115.8.0esr-13.5-1] fixup! Bug 40926: Implemented the New Identity feature

ma1 (@ma1) git at gitlab.torproject.org
Tue Mar 5 09:42:32 UTC 2024



ma1 pushed to branch tor-browser-115.8.0esr-13.5-1 at The Tor Project / Applications / Tor Browser


Commits:
4e4718b4 by hackademix at 2024-03-05T10:26:15+01:00
fixup! Bug 40926: Implemented the New Identity feature

Bug 42236: Let users decide whether to load their home page on new identity.

- - - - -


2 changed files:

- browser/components/newidentity/content/newidentity.js
- browser/locales/en-US/chrome/browser/newIdentity.properties


Changes:

=====================================
browser/components/newidentity/content/newidentity.js
=====================================
@@ -10,37 +10,28 @@ XPCOMUtils.defineLazyGetter(this, "NewIdentityStrings", () => {
   );
   const brandShortName = brandBundle.GetStringFromName("brandShortName");
 
-  let strings = {
-    new_identity: "New Identity",
-    new_identity_sentence_case: "New identity",
-    new_identity_prompt_title: "Reset your identity?",
-    new_identity_prompt: `${brandShortName} will close all windows and tabs. All website sessions will be lost. \nRestart ${brandShortName} now to reset your identity?`,
-    new_identity_restart: `Restart ${brandShortName}`,
-    new_identity_ask_again: "Never ask me again",
-    new_identity_menu_accesskey: "I",
-  };
-  let bundle = null;
+  const fallbackBundle = Services.strings.createBundle(
+    "resource:///chrome/en-US/locale/browser/newIdentity.properties"
+  );
+  const strings = {};
+  const brandedStrings = ["new_identity_prompt", "new_identity_restart"];
+  for (let { key } of fallbackBundle.getSimpleEnumeration()) {
+    strings[key] = fallbackBundle.GetStringFromName(key);
+  }
   try {
-    bundle = Services.strings.createBundle(
+    const bundle = Services.strings.createBundle(
       "chrome://browser/locale/newIdentity.properties"
     );
-  } catch (e) {
-    console.warn("Could not load the New Identity strings");
-  }
-  if (bundle) {
     for (const key of Object.keys(strings)) {
       try {
         strings[key] = bundle.GetStringFromName(key);
       } catch (e) {}
     }
-    strings.new_identity_prompt = strings.new_identity_prompt.replaceAll(
-      "%S",
-      brandShortName
-    );
-    strings.new_identity_restart = strings.new_identity_restart.replaceAll(
-      "%S",
-      brandShortName
-    );
+  } catch (e) {
+    console.warn("Could not load localized New Identity strings");
+  }
+  for (let key of brandedStrings) {
+    strings[key] = strings[key].replaceAll("%S", brandShortName);
   }
   return strings;
 });
@@ -437,10 +428,76 @@ XPCOMUtils.defineLazyGetter(this, "NewIdentityButton", () => {
       logger.info("Opening a new window");
       return new Promise(resolve => {
         // Open a new window forcing the about:privatebrowsing page (tor-browser#41765)
-        const win = OpenBrowserWindow({private: "no-home"});
+        // unless user explicitly overrides this policy (tor-browser #42236)
+        const homePref = "browser.startup.homepage";
+        const trustedHomePref = "browser.startup.homepage.new_identity";
+        const homeURL = Services.prefs.getStringPref(homePref, "");
+        const isTrustedHome =
+          homeURL === "about:tor" ||
+          homeURL.startsWith("chrome://") || // about:blank and other built-ins
+          homeURL === Services.prefs.getStringPref(trustedHomePref, "");
+        const isCustomHome =
+          Services.prefs.getIntPref("browser.startup.page") === 1;
+        const win = OpenBrowserWindow({
+          private: isCustomHome && isTrustedHome ? "private" : "no-home",
+        });
         // This mechanism to know when the new window is ready is used by
         // OpenBrowserWindow itself (see its definition in browser.js).
-        win.addEventListener("MozAfterPaint", () => resolve(), { once: true });
+        win.addEventListener(
+          "MozAfterPaint",
+          () => {
+            resolve();
+            if (isTrustedHome || !isCustomHome) {
+              return;
+            }
+            const tbl = win.TabsProgressListener;
+            const { onLocationChange } = tbl;
+            tbl.onLocationChange = (...args) => {
+              tbl.onLocationChange = onLocationChange;
+              tbl.onLocationChange(...args);
+              let displayAddress;
+              try {
+                const url = new URL(homeURL);
+                displayAddress = url.hostname;
+                if (!displayAddress) {
+                  // no host, use full address and truncate if too long
+                  const MAX_LEN = 32;
+                  displayAddress = url.href;
+                  if (displayAddress.length > MAX_LEN) {
+                    displayAddress = `${displayAddress.substring(0, MAX_LEN)}…`;
+                  }
+                }
+              } catch (e) {
+                // malformed URL, bail out
+                return;
+              }
+              const label =
+                NewIdentityStrings.new_identity_home_notification.replace(
+                  "%S",
+                  displayAddress
+                );
+              const callback = () => {
+                Services.prefs.setStringPref(trustedHomePref, homeURL);
+                win.BrowserHome();
+              };
+              const notificationBox = win.gBrowser.getNotificationBox();
+              notificationBox.appendNotification(
+                "new-identity-safe-home",
+                {
+                  label,
+                  priority: notificationBox.PRIORITY_INFO_MEDIUM,
+                },
+                [
+                  {
+                    label: NewIdentityStrings.new_identity_home_load_button,
+                    callback,
+                  },
+                ]
+              );
+            };
+          },
+          { once: true }
+        );
       });
     }
 


=====================================
browser/locales/en-US/chrome/browser/newIdentity.properties
=====================================
@@ -8,3 +8,6 @@ new_identity_restart = Restart %S
 new_identity_ask_again = Never ask me again
 # Shown in the File menu (use Alt to show File, if you do not see)
 new_identity_menu_accesskey = I
+new_identity_home_notification = Tor Browser blocked your homepage (%S) from loading because it might recognize your previous session.
+# %S is replaced with the custom homepage URL's domain if applicable, or some short-hand of it otherwise
+new_identity_home_load_button  = Load it anyway



View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/commit/4e4718b4a7c5add604e7d458ce2bc5d34af1e892

-- 
View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/commit/4e4718b4a7c5add604e7d458ce2bc5d34af1e892
You're receiving this email because of your account on gitlab.torproject.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.torproject.org/pipermail/tbb-commits/attachments/20240305/e47c9120/attachment-0001.htm>


More information about the tbb-commits mailing list