[tbb-commits] [Git][tpo/applications/fenix][tor-browser-102.2.1-12.0-1] fixup! Modify Add-on support

Pier Angelo Vendrame (@pierov) git at gitlab.torproject.org
Fri Aug 26 08:04:43 UTC 2022



Pier Angelo Vendrame pushed to branch tor-browser-102.2.1-12.0-1 at The Tor Project / Applications / fenix


Commits:
b3677ba2 by hackademix at 2022-08-26T10:03:30+02:00
fixup! Modify Add-on support

- - - - -


4 changed files:

- .gitignore
- app/src/main/java/org/mozilla/fenix/components/TorBrowserFeatures.kt
- app/src/main/java/org/mozilla/fenix/utils/Settings.kt
- app/src/main/res/values/preference_keys.xml


Changes:

=====================================
.gitignore
=====================================
@@ -104,3 +104,7 @@ test_artifacts/
 
 # Web extensions: manifest.json files are generated
 manifest.json
+
+# Other files modified at build time
+.experimenter.json
+


=====================================
app/src/main/java/org/mozilla/fenix/components/TorBrowserFeatures.kt
=====================================
@@ -6,15 +6,19 @@
 
 package org.mozilla.fenix.components
 
+import android.os.StrictMode
 import android.content.Context
 import mozilla.components.concept.engine.webextension.WebExtension
 import mozilla.components.concept.engine.webextension.WebExtensionRuntime
 import mozilla.components.support.base.log.logger.Logger
+import org.mozilla.fenix.ext.components
 import org.mozilla.fenix.ext.settings
+import org.mozilla.fenix.tor.TorEvents
 import java.io.IOException
 
 object TorBrowserFeatures {
     private val logger = Logger("torbrowser-features")
+    private const val NOSCRIPT_ID = "{73a6fe31-595d-460b-a920-fcc0f8843232}"
 
     private fun installNoScript(
         context: Context,
@@ -25,11 +29,15 @@ object TorBrowserFeatures {
         /**
          * Copy the xpi from assets to cacheDir, we do not care if the file is later deleted.
          */
-        val addonPath =
-            context.cacheDir.resolve("{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi")
+        val xpiName = "$NOSCRIPT_ID.xpi"
+        val addonPath = context.cacheDir.resolve(xpiName)
+        val policy = StrictMode.getThreadPolicy()
         try {
-            context.assets.open("extensions/{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi")
+            context.assets.open("extensions/$xpiName")
                 .use { inStream ->
+                    // we don't want penaltyDeath() on disk write
+                    StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX)
+
                     addonPath.outputStream().use { outStream ->
                         inStream.copyTo(outStream)
                     }
@@ -37,13 +45,15 @@ object TorBrowserFeatures {
         } catch (throwable: IOException) {
             onError(throwable)
             return
+        } finally {
+            StrictMode.setThreadPolicy(policy)
         }
 
         /**
          * Install with a file:// URI pointing to the temp location where the addon was copied to.
          */
         runtime.installWebExtension(
-            id = "{73a6fe31-595d-460b-a920-fcc0f8843232}",
+            id = NOSCRIPT_ID,
             url = addonPath.toURI().toString(),
             onSuccess = { extension ->
                 runtime.setAllowedInPrivateBrowsing(
@@ -89,8 +99,7 @@ object TorBrowserFeatures {
 
         /**
          *  Install NoScript as a user WebExtension if we have not already done so.
-         *  AMO signature is checked, and AMO automatic updates will work. The extension should
-         *  behave as if the user had installed it manually.
+         *  AMO signature is checked, but automatic updates still need to be enabled.
          */
         if (!context.settings().noscriptInstalled) {
             installNoScript(
@@ -105,5 +114,40 @@ object TorBrowserFeatures {
                 }
             )
         }
+
+        /**
+         *  If we have not done it yet, enable automatic updates for NoScript and force a
+         *  one-time immediate update check, in order to upgrade old profiles and ensure we've got
+         *  the latest stable AMO version available on first startup.
+         *  We will do it as soon as the Tor is connected, to prevent early addonUpdater activation
+         *  causing automatic update checks failures (components.addonUpdater being a lazy prop).
+         *  The extension, from then on, should behave as if the user had installed it manually.
+         */
+        if (context.settings().noscriptUpdated == 0) {
+            context.components.torController.registerTorListener(object : TorEvents {
+                override fun onTorConnected() {
+                    context.components.torController.unregisterTorListener(this)
+                    // Enable automatic updates
+                    context.components.addonUpdater.registerForFutureUpdates(NOSCRIPT_ID)
+                    // Force an immediate update check
+                    context.components.addonUpdater.update(NOSCRIPT_ID)
+                    context.settings().noscriptUpdated = 1
+                }
+
+                @SuppressWarnings("EmptyFunctionBlock")
+                override fun onTorConnecting() {
+                }
+
+                @SuppressWarnings("EmptyFunctionBlock")
+                override fun onTorStopped() {
+                }
+
+                @SuppressWarnings("EmptyFunctionBlock")
+                override fun onTorStatusUpdate(entry: String?, status: String?) {
+                }
+            })
+        }
     }
+
+
 }


=====================================
app/src/main/java/org/mozilla/fenix/utils/Settings.kt
=====================================
@@ -1379,4 +1379,9 @@ class Settings(private val appContext: Context) : PreferencesHolder {
         appContext.getPreferenceKey(R.string.pref_key_noscript_installed),
         default = false
     )
+
+    var noscriptUpdated by intPreference(
+        appContext.getPreferenceKey(R.string.pref_key_noscript_updated),
+        default = 0
+    )
 }


=====================================
app/src/main/res/values/preference_keys.xml
=====================================
@@ -297,6 +297,7 @@
     <string name="pref_key_show_unified_search" translatable="false">pref_key_show_unified_search</string>
 
     <string name="pref_key_noscript_installed" translatable="false">pref_key_noscript_installed</string>
+    <string name="pref_key_noscript_updated" translatable="false">pref_key_noscript_updated</string>
 
     <!-- Security Level Settings -->
     <string name="pref_key_tor_security_level_settings" translatable="false">pref_key_tor_security_level_settings</string>



View it on GitLab: https://gitlab.torproject.org/tpo/applications/fenix/-/commit/b3677ba2ea06a58233079466ca868369f6665da1

-- 
View it on GitLab: https://gitlab.torproject.org/tpo/applications/fenix/-/commit/b3677ba2ea06a58233079466ca868369f6665da1
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/20220826/c390706f/attachment-0001.htm>


More information about the tbb-commits mailing list