Skip to content
Snippets Groups Projects
Commit 1f564fb6 authored by ma1's avatar ma1 Committed by Richard Pospesel
Browse files

fixup! Modify Add-on support

parent 3125a002
No related merge requests found
......@@ -103,3 +103,7 @@ test_artifacts/
# Web extensions: manifest.json files are generated
manifest.json
# Other files modified at build time
.experimenter.json
......@@ -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?) {
}
})
}
}
}
......@@ -1370,4 +1370,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
)
}
......@@ -291,6 +291,7 @@
<string name="pref_key_pocket_homescreen_recommendations" translatable="false">pref_key_pocket_homescreen_recommendations</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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment