diff --git a/app/src/main/java/com/itsaky/androidide/app/CredentialProtectedApplicationLoader.kt b/app/src/main/java/com/itsaky/androidide/app/CredentialProtectedApplicationLoader.kt index 82f0d511ce..bafbd3165b 100644 --- a/app/src/main/java/com/itsaky/androidide/app/CredentialProtectedApplicationLoader.kt +++ b/app/src/main/java/com/itsaky/androidide/app/CredentialProtectedApplicationLoader.kt @@ -1,9 +1,11 @@ package com.itsaky.androidide.app +import android.content.Context import android.content.Intent import android.os.Handler import android.os.Looper import android.os.UserManager +import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AppCompatDelegate import androidx.core.os.LocaleListCompat import androidx.work.WorkManager @@ -21,6 +23,7 @@ import com.itsaky.androidide.managers.ToolsManager import com.itsaky.androidide.plugins.PluginLogger import com.itsaky.androidide.plugins.base.PluginFragmentHelper import com.itsaky.androidide.plugins.manager.core.PluginManager +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSource import com.itsaky.androidide.plugins.manager.services.IdeBuildServiceImpl import com.itsaky.androidide.plugins.manager.services.IdeLogServiceImpl import com.itsaky.androidide.plugins.manager.services.IdeTerminalServiceImpl @@ -45,6 +48,8 @@ import kotlinx.coroutines.withContext import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode +import org.koin.core.component.KoinComponent +import org.koin.core.component.get import org.slf4j.LoggerFactory import java.io.File import java.util.concurrent.atomic.AtomicBoolean @@ -59,7 +64,9 @@ import kotlin.system.exitProcess * * @author Akash Yadav */ -internal object CredentialProtectedApplicationLoader : ApplicationLoader { +internal object CredentialProtectedApplicationLoader : + ApplicationLoader, + KoinComponent { private val logger = LoggerFactory.getLogger(CredentialProtectedApplicationLoader::class.java) private val _isLoaded = AtomicBoolean(false) @@ -327,8 +334,13 @@ internal object CredentialProtectedApplicationLoader : ApplicationLoader { ideLogcatReader = null } + /** [context] and [endpointSource] are parameters only so a test can start the plugin system without the app. */ + @VisibleForTesting @OptIn(DelicateCoroutinesApi::class) - private fun initializePluginSystem() { + internal fun initializePluginSystem( + context: Context = application, + endpointSource: () -> DebuggerEndpointSource = { get() }, + ) { try { logger.info("Initializing plugin system...") @@ -368,7 +380,7 @@ internal object CredentialProtectedApplicationLoader : ApplicationLoader { pluginManager = PluginManager.getInstance( - context = application, + context = context, eventBus = EventBus.getDefault(), logger = pluginLogger, ) @@ -376,7 +388,7 @@ internal object CredentialProtectedApplicationLoader : ApplicationLoader { pluginManager?.setLanguageContributionsListener(PluginLanguageSupport::refresh) // Set up plugin service providers - setupPluginServices() + setupPluginServices(endpointSource) setupPluginInflationErrorHandler() // Load plugins asynchronously @@ -398,17 +410,35 @@ internal object CredentialProtectedApplicationLoader : ApplicationLoader { /** * Sets up the plugin service providers to integrate with AndroidIDE's actual systems. */ - private fun setupPluginServices() { + private fun setupPluginServices(endpointSource: () -> DebuggerEndpointSource) { pluginManager?.let { manager -> manager.setActivityProvider { application.foregroundActivity } setupBuildServiceProviders() setupProjectManipulationProviders() IdeLogServiceImpl.getInstance().setLogReader(LogsProvider::read) + installDebuggerEndpointSource(manager, endpointSource) IdeTerminalServiceImpl.setSessionLauncher(PluginTerminalLauncher { application.foregroundActivity }) logger.info("Plugin services configured successfully") } } + /** + * Installs where every plugin's DebuggerEndpointService reads the SQLite debugger endpoints. The + * SQLite debugger is optional, so a failure here, such as a binding that cannot be built or a + * different source already installed, is reported and the plugin system starts anyway: plugins load, + * and getEndpoints() throws IllegalStateException saying the endpoints are unavailable. + */ + private fun installDebuggerEndpointSource( + manager: PluginManager, + endpointSource: () -> DebuggerEndpointSource, + ) { + runCatching { manager.installDebuggerEndpointSource(endpointSource()) } + .onFailure { e -> + Sentry.captureException(e) + logger.error("Could not set up the SQLite debugger endpoints for plugins; plugins load without them", e) + } + } + @OptIn(DelicateCoroutinesApi::class) private fun setupBuildServiceProviders() { val buildServiceImpl = IdeBuildServiceImpl.getInstance() diff --git a/app/src/main/java/com/itsaky/androidide/di/AppModule.kt b/app/src/main/java/com/itsaky/androidide/di/AppModule.kt index 194847f4eb..201cafd598 100644 --- a/app/src/main/java/com/itsaky/androidide/di/AppModule.kt +++ b/app/src/main/java/com/itsaky/androidide/di/AppModule.kt @@ -7,6 +7,7 @@ import com.itsaky.androidide.deeplink.PendingDeepLinkOpen import com.itsaky.androidide.editor.language.outline.OutlineProvider import com.itsaky.androidide.editor.language.outline.TreeSitterOutlineProvider import com.itsaky.androidide.git.core.GitCredentialsManager +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSource import com.itsaky.androidide.repositories.DebuggerEndpointRepository import com.itsaky.androidide.repositories.RecentProjectRepository import com.itsaky.androidide.repositories.RecentProjectRepositoryImpl @@ -19,11 +20,13 @@ import com.itsaky.androidide.viewmodel.OutlineViewModel import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.runBlocking import org.koin.android.ext.koin.androidApplication import org.koin.android.ext.koin.androidContext import org.koin.core.module.dsl.viewModel import org.koin.core.qualifier.named import org.koin.dsl.module +import java.io.File /** Qualifier for the process-lifetime [CoroutineScope]; see the binding for why it is named. */ const val APPLICATION_SCOPE = "applicationScope" @@ -65,13 +68,21 @@ val coreModule = single { val context = androidContext() + val packageManager = context.packageManager DebuggerEndpointRepository( get(), get(named(APPLICATION_SCOPE)), + isInstalled = { DebuggerEndpointRepository.isPackageInstalled(packageManager, it) }, announce = { DebuggerEndpointAnnounce.send(context, it) }, ) } + // What DebuggerEndpointService reads. Plugins call it off the main thread, so blocking on Room is fine. + single { + val repository = get() + DebuggerEndpointSource { location -> runBlocking { repository.reportedEndpoints(File(location)) } } + } + single { RecentProjectRepositoryImpl(get()) } diff --git a/app/src/main/java/com/itsaky/androidide/repositories/DebuggerEndpointRepository.kt b/app/src/main/java/com/itsaky/androidide/repositories/DebuggerEndpointRepository.kt index 2b01028382..5f5d638e98 100644 --- a/app/src/main/java/com/itsaky/androidide/repositories/DebuggerEndpointRepository.kt +++ b/app/src/main/java/com/itsaky/androidide/repositories/DebuggerEndpointRepository.kt @@ -1,5 +1,6 @@ package com.itsaky.androidide.repositories +import android.content.pm.PackageManager import com.itsaky.androidide.roomData.recentproject.DebuggerEndpointDao import com.itsaky.androidide.sqlitedebugger.DebuggerEndpointValidation import com.itsaky.androidide.utils.canonicalProjectLocation @@ -12,17 +13,22 @@ import kotlinx.coroutines.withContext import org.slf4j.LoggerFactory import java.io.File import java.util.concurrent.ConcurrentHashMap +import com.itsaky.androidide.plugins.services.DebuggerEndpoint as PluginDebuggerEndpoint /** * Which app each project builds, and the SQLite debugger endpoint the app last reported (ADFA-7137). - * Nothing here throws except [CancellationException]: any other failure is logged and reported as - * false, because none of the callers can do anything about it, and they run on the application scope, - * which has no CoroutineExceptionHandler (an escaping exception would crash the process). + * The writes throw nothing but [CancellationException]: any other failure is logged and reported as + * false, because none of their callers can do anything about it, and they run on the application + * scope, which has no CoroutineExceptionHandler (an escaping exception would crash the process). + * [reportedEndpoints], the read behind the plugin API, is the exception: it throws on a read failure, + * so call it where a failure is handled, not on the application scope. */ class DebuggerEndpointRepository( private val dao: DebuggerEndpointDao, private val scope: CoroutineScope, private val clock: () -> Long = System::currentTimeMillis, + /** Required, with no default: one that answered true would serve an uninstalled app's port and token as live. */ + private val isInstalled: (String) -> Boolean, /** * Asks the app to report its endpoint again, after each pairing; see DebuggerEndpointAnnounce. Required, with no * default: one that did nothing would leave an app that was already running unknown until its process restarts. @@ -152,7 +158,39 @@ class DebuggerEndpointRepository( false } - private companion object { + /** + * The endpoints the apps of [projectDir] have reported, for the plugin API (ADFA-7139). Looked + * up by the canonical path, as the pairing stores it; apps that have not reported yet are left + * out, and so are apps that are no longer installed: their stored port and token would otherwise + * come back as live. An endpoint can still be stale when the app's process has died. Unlike the + * writes, a read failure is thrown: the plugin API reports it rather than answering "no apps". + */ + suspend fun reportedEndpoints(projectDir: File): List = + dao.getByProjectLocation(projectDir.canonicalProjectLocation()).mapNotNull { row -> + val token = row.token ?: return@mapNotNull null + val port = row.port ?: return@mapNotNull null + // Last: a binder call per app, which an app that has not reported need not cost. + if (!isInstalled(row.applicationId)) return@mapNotNull null + PluginDebuggerEndpoint(row.applicationId, port, token, row.updatedAt ?: 0L) + } + + companion object { + /** + * Whether [applicationId] is installed. Every package is visible because Code on the Go targets SDK 28; at a + * target of 30 or later, package visibility filtering would make every endpoint look uninstalled unless the + * manifest declares a matching ``. + */ + fun isPackageInstalled( + packageManager: PackageManager, + applicationId: String, + ): Boolean = + try { + packageManager.getPackageInfo(applicationId, 0) + true + } catch (e: PackageManager.NameNotFoundException) { + false + } + private val log = LoggerFactory.getLogger(DebuggerEndpointRepository::class.java) } } diff --git a/app/src/test/java/com/itsaky/androidide/app/PluginSystemStartTest.kt b/app/src/test/java/com/itsaky/androidide/app/PluginSystemStartTest.kt new file mode 100644 index 0000000000..a93f9cea68 --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/app/PluginSystemStartTest.kt @@ -0,0 +1,176 @@ +package com.itsaky.androidide.app + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.os.Bundle +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.plugins.IPlugin +import com.itsaky.androidide.plugins.PluginContext +import com.itsaky.androidide.plugins.PluginLogger +import com.itsaky.androidide.plugins.base.PluginFragmentHelper +import com.itsaky.androidide.plugins.manager.core.LoadedPlugin +import com.itsaky.androidide.plugins.manager.core.PluginManager +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSource +import com.itsaky.androidide.plugins.manager.services.IdeTerminalServiceImpl +import com.itsaky.androidide.plugins.services.DebuggerEndpointService +import org.junit.After +import org.junit.Assert.assertThrows +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.io.File +import java.util.concurrent.atomic.AtomicReference +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream +import kotlin.concurrent.thread + +/** + * CredentialProtectedApplicationLoader's plugin start when the SQLite debugger endpoint source cannot + * be installed: the steps after it still run and plugins still load. BaseApplication backs the + * preferences the plugin manager reads; IDEApplication would start the whole app. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], application = BaseApplication::class) +class PluginSystemStartTest { + private val app get() = RuntimeEnvironment.getApplication() + + @Before + fun setUp() { + reset() + probePlugin(File(app.filesDir, "plugins/probe.cgp")) + } + + @After + fun tearDown() = reset() + + @Test + fun `a source that cannot be built leaves the later setup steps and plugin loading in place`() { + CredentialProtectedApplicationLoader.initializePluginSystem(app) { error("database unavailable") } + + assertLaterStepsRan() + val service = checkNotNull(awaitProbe().context.services.get(DebuggerEndpointService::class.java)) + val thrown = offMainThread { assertThrows(IllegalStateException::class.java) { service.getEndpoints() } } + assertThat(thrown).hasMessageThat().startsWith("The SQLite debugger endpoints are unavailable") + } + + @Test + fun `a different source already installed leaves the later setup steps and plugin loading in place`() { + PluginManager.getInstance(app, Any(), QuietLogger).installDebuggerEndpointSource { emptyList() } + + CredentialProtectedApplicationLoader.initializePluginSystem(app) { DebuggerEndpointSource { emptyList() } } + + assertLaterStepsRan() + awaitProbe() + } + + /** The steps that come after the endpoint source: the terminal launcher and the inflation error handler. */ + private fun assertLaterStepsRan() { + assertWithMessage("terminal session launcher").that(sessionLauncherField().get(null)).isNotNull() + assertWithMessage("plugin inflation error handler").that(PluginFragmentHelper.onPluginInflationError).isNotNull() + } + + /** Waits for the plugin loading that initializePluginSystem starts on a background coroutine. */ + private fun awaitProbe(): LoadedPlugin { + val deadline = System.currentTimeMillis() + 20_000 + while (System.currentTimeMillis() < deadline) { + PluginManager.getInstance()?.getLoadedPlugin("probe")?.let { return it } + Thread.sleep(50) + } + val error = PluginManager.getInstance()?.getLoadError("probe") + throw AssertionError("plugin probe was not loaded within 20 s (load error: $error)") + } + + /** getEndpoints refuses the main thread, where Robolectric runs the test. */ + private fun offMainThread(block: () -> T): T { + val result = AtomicReference>() + thread { result.set(runCatching(block)) }.join() + return result.get().getOrThrow() + } + + /** Forgets what a start leaves in process-wide state, so each test starts from none. */ + private fun reset() { + PluginManager::class.java + .getDeclaredField("INSTANCE") + .apply { isAccessible = true } + .set(null, null) + CredentialProtectedApplicationLoader::class.java + .getDeclaredField("pluginManager") + .apply { isAccessible = true } + .set(null, null) + sessionLauncherField().set(null, null) + PluginFragmentHelper.onPluginInflationError = null + } + + private fun sessionLauncherField() = + IdeTerminalServiceImpl::class.java + .getDeclaredField("sessionLauncher") + .apply { isAccessible = true } + + /** Writes [file] as a plugin with the debugger.endpoints permission: Robolectric can't read a manifest from an APK. */ + private fun probePlugin(file: File) { + file.parentFile?.mkdirs() + ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } + val info = + PackageInfo().apply { + packageName = "org.example.probe" + applicationInfo = + ApplicationInfo().apply { + metaData = + Bundle().apply { + putString("plugin.id", "probe") + putString("plugin.main_class", ProbePlugin::class.java.name) + putString("plugin.permissions", "debugger.endpoints") + } + } + } + shadowOf(app.packageManager).setPackageArchiveInfo(file.absolutePath, info) + } + + /** A plugin that loads and activates. Its class loader finds it through its parent, the test's. */ + class ProbePlugin : IPlugin { + override fun initialize(context: PluginContext) = true + + override fun activate() = true + + override fun deactivate() = true + + override fun dispose() = Unit + } + + private object QuietLogger : PluginLogger { + override val pluginId = "test" + + override fun debug(message: String) = Unit + + override fun debug( + message: String, + error: Throwable, + ) = Unit + + override fun info(message: String) = Unit + + override fun info( + message: String, + error: Throwable, + ) = Unit + + override fun warn(message: String) = Unit + + override fun warn( + message: String, + error: Throwable, + ) = Unit + + override fun error(message: String) = Unit + + override fun error( + message: String, + error: Throwable, + ) = Unit + } +} diff --git a/app/src/test/java/com/itsaky/androidide/di/DebuggerEndpointBindingsTest.kt b/app/src/test/java/com/itsaky/androidide/di/DebuggerEndpointBindingsTest.kt new file mode 100644 index 0000000000..4e8bc0406c --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/di/DebuggerEndpointBindingsTest.kt @@ -0,0 +1,91 @@ +package com.itsaky.androidide.di + +import android.app.Application +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSource +import com.itsaky.androidide.repositories.DebuggerEndpointRepository +import com.itsaky.androidide.roomData.recentproject.RecentProject +import com.itsaky.androidide.roomData.recentproject.RecentProjectRoomDatabase +import com.itsaky.androidide.sqlitedebugger.DebuggerEndpointValidation +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.koin.android.ext.koin.androidContext +import org.koin.core.Koin +import org.koin.dsl.koinApplication +import org.koin.dsl.module +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import java.io.File +import java.nio.file.Files + +/** The SQLite debugger bindings of [coreModule], resolved as the app resolves them, over an in-memory database. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33], application = Application::class) +class DebuggerEndpointBindingsTest { + private val app: Application get() = ApplicationProvider.getApplicationContext() + private lateinit var room: RecentProjectRoomDatabase + private lateinit var koin: Koin + private lateinit var project: File + + @Before + fun setUp() { + room = + Room + .inMemoryDatabaseBuilder(app, RecentProjectRoomDatabase::class.java) + .allowMainThreadQueries() + .build() + koin = + koinApplication { + androidContext(app) + allowOverride(true) + modules(coreModule, module { single { room } }) + }.koin + project = Files.createTempDirectory("proj").toFile() + runBlocking { + room.recentProjectDao().insert(RecentProject(name = "p", createdAt = "1", location = project.canonicalPath)) + } + } + + @After + fun tearDown() { + koin.close() + room.close() + project.deleteRecursively() + } + + private suspend fun report( + applicationId: String, + token: String, + ) { + val repository = koin.get() + assertThat(repository.recordInstalled(applicationId, project.canonicalPath)).isTrue() + assertThat(repository.storeEndpoint(applicationId, DebuggerEndpointValidation.Endpoint(token, 5555))).isTrue() + } + + @Test + fun `the source plugins read returns what the bound repository stored`() = + runBlocking { + report(app.packageName, "tok-en_1") + + val endpoints = koin.get().endpointsFor(project.path) + + assertThat(endpoints.map { it.applicationId }).containsExactly(app.packageName) + assertThat(endpoints.single().token).isEqualTo("tok-en_1") + } + + @Test + fun `the bound repository asks the package manager, so an app that is not installed is left out`() = + runBlocking { + report(app.packageName, "tok-en_1") + report("com.example.not.installed", "tok-en_2") + + val endpoints = koin.get().reportedEndpoints(project) + + assertThat(endpoints.map { it.applicationId }).containsExactly(app.packageName) + } +} diff --git a/app/src/test/java/com/itsaky/androidide/repositories/DebuggerEndpointRepositoryTest.kt b/app/src/test/java/com/itsaky/androidide/repositories/DebuggerEndpointRepositoryTest.kt index d9a6c74849..34104196f1 100644 --- a/app/src/test/java/com/itsaky/androidide/repositories/DebuggerEndpointRepositoryTest.kt +++ b/app/src/test/java/com/itsaky/androidide/repositories/DebuggerEndpointRepositoryTest.kt @@ -41,6 +41,7 @@ class DebuggerEndpointRepositoryTest { room.debuggerEndpointDao(), CoroutineScope(Dispatchers.IO), clock = { 1234L }, + isInstalled = { true }, announce = { announced += it }, ) } @@ -132,6 +133,7 @@ class DebuggerEndpointRepositoryTest { room.debuggerEndpointDao(), CoroutineScope(Dispatchers.IO), clock = { now.get() }, + isInstalled = { true }, announce = { announced += it }, ) @@ -374,7 +376,12 @@ class DebuggerEndpointRepositoryTest { { android.database.SQLException("generic") }, )) { val failing = - DebuggerEndpointRepository(ThrowingDao(error), CoroutineScope(Dispatchers.IO), announce = { announced += it }) + DebuggerEndpointRepository( + ThrowingDao(error), + CoroutineScope(Dispatchers.IO), + isInstalled = { true }, + announce = { announced += it }, + ) assertThat(failing.recordInstalled("com.example", "/p/a")).isFalse() assertThat(failing.recordLaunched("com.example", "/p/a")).isFalse() @@ -389,6 +396,7 @@ class DebuggerEndpointRepositoryTest { DebuggerEndpointRepository( ThrowingDao { kotlinx.coroutines.CancellationException("cancelled") }, CoroutineScope(Dispatchers.IO), + isInstalled = { true }, announce = {}, ) @@ -396,4 +404,138 @@ class DebuggerEndpointRepositoryTest { assertThat(result.exceptionOrNull()).isInstanceOf(kotlinx.coroutines.CancellationException::class.java) } + + @Test + fun `reported endpoints are found through a non-canonical path and unreported apps are left out`() = + runBlocking { + val dir = + java.nio.file.Files + .createTempDirectory("proj") + .toFile() + try { + addProject(dir.canonicalPath) + assertThat(repository.recordInstalled("com.example.reported", dir.canonicalPath)).isTrue() + assertThat(repository.recordInstalled("com.example.silent", dir.canonicalPath)).isTrue() + assertThat(repository.storeEndpoint("com.example.reported", endpoint)).isTrue() + + val endpoints = repository.reportedEndpoints(File(dir, "../" + dir.name)) + + assertThat(endpoints.map { it.applicationId }).containsExactly("com.example.reported") + val e = endpoints.single() + assertThat(e.port).isEqualTo(5555) + assertThat(e.token).isEqualTo("tok-en_1") + assertThat(e.updatedAtMillis).isEqualTo(1234L) + } finally { + dir.delete() + } + } + + @Test + fun `an app that has not reported is dropped without asking whether it is installed, a binder call`() = + runBlocking { + val dir = + java.nio.file.Files + .createTempDirectory("proj") + .toFile() + try { + addProject(dir.canonicalPath) + val asked = CopyOnWriteArrayList() + val counting = + DebuggerEndpointRepository( + room.debuggerEndpointDao(), + CoroutineScope(Dispatchers.IO), + clock = { 1234L }, + isInstalled = { + asked += it + true + }, + announce = {}, + ) + assertThat(counting.recordInstalled("com.example.reported", dir.canonicalPath)).isTrue() + assertThat(counting.recordInstalled("com.example.silent", dir.canonicalPath)).isTrue() + assertThat(counting.storeEndpoint("com.example.reported", endpoint)).isTrue() + + assertThat(counting.reportedEndpoints(dir).map { it.applicationId }).containsExactly("com.example.reported") + assertThat(asked).containsExactly("com.example.reported") + } finally { + dir.delete() + } + } + + @Test + fun `an app that is no longer installed is left out and installed ones are returned`() = + runBlocking { + val dir = + java.nio.file.Files + .createTempDirectory("proj") + .toFile() + try { + addProject(dir.canonicalPath) + val installed = setOf("com.example.alive") + val pruning = + DebuggerEndpointRepository( + room.debuggerEndpointDao(), + CoroutineScope(Dispatchers.IO), + clock = { 1234L }, + isInstalled = { it in installed }, + announce = {}, + ) + for (id in listOf("com.example.alive", "com.example.gone")) { + assertThat(pruning.recordInstalled(id, dir.canonicalPath)).isTrue() + assertThat(pruning.storeEndpoint(id, endpoint)).isTrue() + } + + val endpoints = pruning.reportedEndpoints(dir) + + assertThat(endpoints.map { it.applicationId }).containsExactly("com.example.alive") + } finally { + dir.delete() + } + } + + @Test + fun `each project gets only its own apps, also through a symlinked path, and an app moved away leaves the old one`() = + runBlocking { + val a = + java.nio.file.Files + .createTempDirectory("projA") + .toFile() + val b = + java.nio.file.Files + .createTempDirectory("projB") + .toFile() + val link = File(a.parentFile, a.name + "-link") + java.nio.file.Files + .createSymbolicLink(link.toPath(), a.toPath()) + try { + addProject(a.canonicalPath) + addProject(b.canonicalPath) + assertThat(repository.recordInstalled("com.example.a", a.canonicalPath)).isTrue() + assertThat(repository.recordInstalled("com.example.b", b.canonicalPath)).isTrue() + assertThat(repository.storeEndpoint("com.example.a", DebuggerEndpointValidation.Endpoint("tok-a", 1111))).isTrue() + assertThat(repository.storeEndpoint("com.example.b", DebuggerEndpointValidation.Endpoint("tok-b", 2222))).isTrue() + + assertThat(repository.reportedEndpoints(a).map { it.applicationId }).containsExactly("com.example.a") + assertThat(repository.reportedEndpoints(b).map { it.applicationId }).containsExactly("com.example.b") + assertThat(repository.reportedEndpoints(link).map { it.token }).containsExactly("tok-a") + + // The app is now built by project b: a no longer lists it, and its token was cleared. + assertThat(repository.recordInstalled("com.example.a", b.canonicalPath)).isTrue() + assertThat(repository.reportedEndpoints(a)).isEmpty() + assertThat(repository.reportedEndpoints(b).map { it.applicationId }).containsExactly("com.example.b") + } finally { + link.delete() + a.delete() + b.delete() + } + } + + @Test + fun `the package manager check tells installed from missing apps`() { + val app = ApplicationProvider.getApplicationContext() + val packageManager = app.packageManager + + assertThat(DebuggerEndpointRepository.isPackageInstalled(packageManager, app.packageName)).isTrue() + assertThat(DebuggerEndpointRepository.isPackageInstalled(packageManager, "com.example.not.installed")).isFalse() + } } diff --git a/app/src/test/java/com/itsaky/androidide/services/InstallationResultReceiverTest.kt b/app/src/test/java/com/itsaky/androidide/services/InstallationResultReceiverTest.kt index 1513a5e606..e95064d785 100644 --- a/app/src/test/java/com/itsaky/androidide/services/InstallationResultReceiverTest.kt +++ b/app/src/test/java/com/itsaky/androidide/services/InstallationResultReceiverTest.kt @@ -80,7 +80,7 @@ class InstallationResultReceiverTest { dao: DebuggerEndpointDao = room.debuggerEndpointDao(), scope: CoroutineScope = CoroutineScope(Dispatchers.IO), ): DebuggerEndpointRepository { - val repository = DebuggerEndpointRepository(dao, scope, announce = {}) + val repository = DebuggerEndpointRepository(dao, scope, isInstalled = { true }, announce = {}) if (GlobalContext.getOrNull() != null) stopKoin() startKoin { modules(module { single { repository } }) } return repository diff --git a/app/src/test/java/com/itsaky/androidide/sqlitedebugger/DebuggerEndpointReceiverTest.kt b/app/src/test/java/com/itsaky/androidide/sqlitedebugger/DebuggerEndpointReceiverTest.kt index 32a5abd3fd..d85c1ac724 100644 --- a/app/src/test/java/com/itsaky/androidide/sqlitedebugger/DebuggerEndpointReceiverTest.kt +++ b/app/src/test/java/com/itsaky/androidide/sqlitedebugger/DebuggerEndpointReceiverTest.kt @@ -42,7 +42,8 @@ class DebuggerEndpointReceiverTest { .allowMainThreadQueries() .build() val scope = CoroutineScope(Dispatchers.IO) - val repository = DebuggerEndpointRepository(room.debuggerEndpointDao(), scope, clock = { 99L }, announce = {}) + val repository = + DebuggerEndpointRepository(room.debuggerEndpointDao(), scope, clock = { 99L }, isInstalled = { true }, announce = {}) if (GlobalContext.getOrNull() != null) stopKoin() startKoin { modules( diff --git a/docs/PLUGIN_API_CHANGELOG.md b/docs/PLUGIN_API_CHANGELOG.md index 1986000ee7..92a949f21b 100644 --- a/docs/PLUGIN_API_CHANGELOG.md +++ b/docs/PLUGIN_API_CHANGELOG.md @@ -35,6 +35,30 @@ need a source change, a recompile, or both · `tooling` = API-stability milestone. **[verified]** = read from the checked-in ABI dump. **[reconstructed]** = diffed from `plugin-api/src` history (predates the dump; symbol-accurate). +### 26.48 — unreleased +- **added — Find the SQLite debugger of the open project's apps** _(ADFA-7139)_ **[verified]** + `DebuggerEndpointService.getEndpoints()` returns an unmodifiable `List`, one per app of the open + project that has reported to the IDE: `applicationId`, `port` (on `127.0.0.1`), `token` (the bearer + token the server requires) and `updatedAtMillis`. The list is empty, never null, when no project is + open in the editor (the home screen, even right after a project was closed) or no app has reported. Port and token change every time the app starts, so call again after a + refused connection or a 401/403. An app that was uninstalled is left out, but one whose process died stays + listed until it reports again (`updatedAtMillis` is when it last reported). The call reads the IDE's database: use a background thread. It needs + the new `debugger.endpoints` permission (`PluginPermission.DEBUGGER_ENDPOINTS`) and throws + `SecurityException` without it. The token is the server's only credential, so a plugin holding the + permission can query the databases of the open project's apps (read-only in this version; a later + version may allow writes). The call throws `IllegalStateException` on the main thread, when the database + cannot be read, or when the IDE could not set up access to the endpoints at startup (neither failure is + reported as an empty list). Once the plugin is disabled + or unloaded, a call throws `java.util.concurrent.CancellationException` until it is enabled again, as + `IdeTerminalService` calls do. The method declares `InterruptedException`, thrown with the thread's + interrupt status set again when the caller is interrupted during the read. Plugins run in Code on the + Go's process, so the permission declares intent and cannot stop reflection, as with `IdeLogService` + below, and does not stop a plugin from taking another plugin's service from + `PluginManager.getInstance().getLoadedPlugin(id).context.services` or `PluginFragmentHelper.getServiceRegistry` + (tracked in ADFA-7194). Floor `plugin.min_ide_version` + at `26.48`: an IDE of 26.47 or older rejects the unknown `debugger.endpoints` permission when it + validates the manifest, so it refuses to install a plugin that declares it. + ### 26.43 — unreleased - **breaking — JavaScript highlighting and the IDE's grammar names are the IDE's** _(ADFA-7084)_ The IDE now highlights `js`, `mjs`, `cjs` and `jsx` itself. A `LanguageDefinition` can still diff --git a/docs/PLUGIN_AUTHORING.md b/docs/PLUGIN_AUTHORING.md index c51558dd79..338760f0bd 100644 --- a/docs/PLUGIN_AUTHORING.md +++ b/docs/PLUGIN_AUTHORING.md @@ -343,6 +343,11 @@ Defined by `PluginPermission` in `plugin-api/src/main/kotlin/com/itsaky/androidi | `project.structure` | Modify project structure | | `native.code` | Execute native machine code | | `ide.environment.write` | Write to IDE-managed directories (Android SDK, NDK, cache) | +| `debugger.endpoints` | Read the port and token of the open project's SQLite debugger servers, and so query the apps' databases (read-only for now) | + +Plugins run in Code on the Go's process. A permission declares what a plugin means to do, and the +services check it on every call, but it cannot stop a plugin that reaches into the host by +reflection. ## Contributing a language diff --git a/docs/plugin-api.md b/docs/plugin-api.md index 3eb7d85b76..40e729aebf 100644 --- a/docs/plugin-api.md +++ b/docs/plugin-api.md @@ -11,7 +11,7 @@ The surface a plugin binds to is broader than one module. All of the following a - **The `:plugin-api` module** — package `com.itsaky.androidide.plugins.*`: - Core: `IPlugin` (lifecycle), `PluginContext`, `PluginLogger`, `ServiceRegistry`, `ResourceManager`. - Extension interfaces plugins **implement**: `UIExtension`, `EditorExtension`, `EditorTabExtension`, `DocumentationExtension`, `BuildActionExtension`, `SnippetExtension`, `ProjectExtension`, `FileOpenExtension`, `SettingsExtension`, `LanguageExtension`. - - IDE service interfaces plugins **call** (via `ServiceRegistry.get(X::class.java)`): `IdeProjectService`, `IdeEditorService`, `IdeFileService`, `IdeEnvironmentService`, `IdeArchiveService`, `IdeBuildService`, `IdeLogService`, `IdeUIService`, `IdeEditorTabService`, `IdeTooltipService`, `IdeThemeService`, `IdeFeatureFlagService` (the overall experiments switch only - see [EXPERIMENTS.md](../EXPERIMENTS.md#plugins)), `IdeCommandService`, `IdeTerminalService`, `IdeTemplateService`, `IdeSnippetService`, `IdeSidebarService`. + - IDE service interfaces plugins **call** (via `ServiceRegistry.get(X::class.java)`): `IdeProjectService`, `IdeEditorService`, `IdeFileService`, `IdeEnvironmentService`, `IdeArchiveService`, `IdeBuildService`, `IdeLogService`, `IdeUIService`, `IdeEditorTabService`, `IdeTooltipService`, `IdeThemeService`, `IdeFeatureFlagService` (the overall experiments switch only - see [EXPERIMENTS.md](../EXPERIMENTS.md#plugins)), `IdeCommandService`, `IdeTerminalService`, `IdeTemplateService`, `IdeSnippetService`, `IdeSidebarService`, `DebuggerEndpointService` (with `DebuggerEndpoint`, the value type it returns: one app's SQLite debugger port and token). - Cross-plugin service interfaces, where **one plugin implements what another calls** (via `SharedServices`): `LlmInferenceService` — implemented by ai-core, called by every AI plugin — together with the types nested in it that a *backend* plugin implements (`LlmBackend`, `HistoryCapableBackend`, `ToolCallingBackend`, `CancellableBackend`, `ConfigurableBackend`, `EmbeddingBackend`) and the value types either side constructs (`ChatMessage`, `LlmConfig`, `LlmResponse`, `SystemPromptRequest`, `ToolDefinition`, `ToolCallRequest`). Also `ToolSourceRegistry` — implemented by ai-core, called by any plugin contributing tools to the agent — with `ToolSource` and `ToolSpec`, which a *contributing* plugin implements, `ToolInvocation`, which ai-core constructs and passes to `ToolSource.invoke`, and `ToolOutcome`, which the source returns. - Utility classes plugins **instantiate**: `KeystoreSecretStore` (AES/GCM over the Android Keystore, alias supplied by the caller); the AI prompt config engine in `ai.prompt` (`PromptTemplateEngine`, `PromptConfigLoader`, `PromptConfigStore`, `PromptConfigDocument`, `PromptConfigObject`, `AssetPromptConfigSource`), generic over a plugin's own config type through `PromptConfigParser` and `PromptConfigProvider`, which a plugin implements; and the settings-pane helpers in `ai.ui` (`SecretRevealController` with `RevealToggle`, `applyPaneStyling` with `PaneStyle`, `ButtonColors` and `FieldColors`). Host-side implementations rather than interfaces, so plugins share one copy in the process instead of compiling their own. - Data classes plugins **construct** (e.g. `MenuItem`, `TabItem`, `EditorTabItem`, `NavigationItem`, `ToolbarAction`, `FabAction`, `PluginBuildAction`, `SnippetContribution`, `PluginTooltipEntry`, `PluginSettingsEntry`, `LanguageDefinition`, `TreeSitterGrammar`, `LanguageServerDefinition`). @@ -22,7 +22,7 @@ The surface a plugin binds to is broader than one module. All of the following a - **Wire/format contracts outside the module:** - Manifest `` keys — `plugin.id`, `plugin.name`, `plugin.version`, `plugin.description`, `plugin.author`, `plugin.main_class`, `plugin.min_ide_version`, `plugin.max_ide_version`, `plugin.permissions`, `plugin.sidebar_items`, `plugin.icon_day`, `plugin.icon_night`, `plugin.vcs_revision`, `plugin.build_timestamp`. Matched **by string** — a rename silently breaks every plugin. - Plugin language assets: `lib//libtree-sitter-.so` exporting `tree_sitter_`, the query file names under `TreeSitterGrammar.queriesAssetPath`, and the standard capture names mapped by each colour scheme's `generic.json`. - - Permission **key strings** (`filesystem.read`, `filesystem.write`, `network.access`, `system.commands`, `ide.settings`, `project.structure`, `native.code`, `ide.environment.write`) — also matched by string. + - Permission **key strings** (`filesystem.read`, `filesystem.write`, `network.access`, `system.commands`, `ide.settings`, `project.structure`, `native.code`, `ide.environment.write`, `debugger.endpoints`) — also matched by string. - The path allowlist and per-plugin data directories enforced by `IdeFileService` / `IdeArchiveService`. - File/format contracts: the `.cgp` package format (including `assets/cgp-build.properties`, the provenance record the builder writes into every artifact — see [PLUGIN_AUTHORING.md](PLUGIN_AUTHORING.md#provenance)), the `.cgt` template format, the `plugin_documentation.db` schema, `.codeonthego/scripts.json`, the TextMate snippet syntax, and the `http://localhost:6174/` help-server URL scheme with the `plugin//` namespace. diff --git a/plugin-api/api/plugin-api.api b/plugin-api/api/plugin-api.api index df35e7d108..85403778ad 100644 --- a/plugin-api/api/plugin-api.api +++ b/plugin-api/api/plugin-api.api @@ -107,6 +107,7 @@ public final class com/itsaky/androidide/plugins/PluginMetadata : android/os/Par } public final class com/itsaky/androidide/plugins/PluginPermission : java/lang/Enum { + public static final field DEBUGGER_ENDPOINTS Lcom/itsaky/androidide/plugins/PluginPermission; public static final field FILESYSTEM_READ Lcom/itsaky/androidide/plugins/PluginPermission; public static final field FILESYSTEM_WRITE Lcom/itsaky/androidide/plugins/PluginPermission; public static final field IDE_ENVIRONMENT_WRITE Lcom/itsaky/androidide/plugins/PluginPermission; @@ -1651,6 +1652,22 @@ public final class com/itsaky/androidide/plugins/services/CursorPosition { public fun toString ()Ljava/lang/String; } +public final class com/itsaky/androidide/plugins/services/DebuggerEndpoint { + public fun (Ljava/lang/String;ILjava/lang/String;J)V + public fun equals (Ljava/lang/Object;)Z + public fun getApplicationId ()Ljava/lang/String; + public fun getPort ()I + public fun getToken ()Ljava/lang/String; + public fun getUpdatedAtMillis ()J + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public abstract interface class com/itsaky/androidide/plugins/services/DebuggerEndpointService { + public static final field CONTRACT_VERSION I + public abstract fun getEndpoints ()Ljava/util/List; +} + public abstract interface class com/itsaky/androidide/plugins/services/EditorContentChangeListener { public abstract fun onContentChanged (Ljava/lang/String;IILjava/lang/String;)V } diff --git a/plugin-api/src/main/java/com/itsaky/androidide/plugins/services/DebuggerEndpoint.java b/plugin-api/src/main/java/com/itsaky/androidide/plugins/services/DebuggerEndpoint.java new file mode 100644 index 0000000000..943fd5a236 --- /dev/null +++ b/plugin-api/src/main/java/com/itsaky/androidide/plugins/services/DebuggerEndpoint.java @@ -0,0 +1,101 @@ +package com.itsaky.androidide.plugins.services; + +import androidx.annotation.NonNull; +import java.util.Objects; + +/** + * Where one app's SQLite debugger server is listening, as the app last reported it to the IDE. Immutable. + * + *

+ * The port and the token are chosen by the app on every start, so a value read here goes stale when the app dies or is relaunched; a client that is refused (or answered 401/403) asks {@link DebuggerEndpointService#getEndpoints} again instead of retrying the same values. The token is a credential for the debugger server: do not log it or persist it. {@link #toString} leaves it out. + * + *

+ * An endpoint whose app has been uninstalled is not returned, but one whose process has died is: nothing removes it until the app reports again. {@link #getUpdatedAtMillis} is when the app last reported, not a sign that it is still running. + */ +public final class DebuggerEndpoint { + + private final String applicationId; + private final int port; + private final String token; + private final long updatedAtMillis; + + /** + * Creates an endpoint. + * + * @param applicationId + * the app's package name (must not be null) + * @param port + * the TCP port the debugger server listens on, on the loopback interface + * @param token + * the bearer token the server requires (must not be null) + * @param updatedAtMillis + * when the app reported these values, in milliseconds since the Unix epoch + */ + public DebuggerEndpoint(@NonNull String applicationId, int port, @NonNull String token, long updatedAtMillis) { + this.applicationId = Objects.requireNonNull(applicationId, "applicationId"); + this.port = port; + this.token = Objects.requireNonNull(token, "token"); + this.updatedAtMillis = updatedAtMillis; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof DebuggerEndpoint)) { + return false; + } + final DebuggerEndpoint that = (DebuggerEndpoint) other; + return port == that.port && updatedAtMillis == that.updatedAtMillis && applicationId.equals(that.applicationId) && token.equals(that.token); + } + + /** + * Gets the app's package name, which identifies the app within the project. + * + * @return the application ID (never null) + */ + @NonNull + public String getApplicationId() { + return applicationId; + } + + /** + * Gets the port the debugger server listens on, on {@code 127.0.0.1}. + * + * @return the port + */ + public int getPort() { + return port; + } + + /** + * Gets the bearer token the server requires on every request. + * + * @return the token (never null) + */ + @NonNull + public String getToken() { + return token; + } + + /** + * Gets when the app reported {@link #getPort} and {@link #getToken}. + * + * @return milliseconds since the Unix epoch + */ + public long getUpdatedAtMillis() { + return updatedAtMillis; + } + + @Override + public int hashCode() { + return Objects.hash(applicationId, port, token, updatedAtMillis); + } + + @NonNull + @Override + public String toString() { + return "DebuggerEndpoint{applicationId=" + applicationId + ", port=" + port + ", updatedAtMillis=" + updatedAtMillis + "}"; + } +} diff --git a/plugin-api/src/main/java/com/itsaky/androidide/plugins/services/DebuggerEndpointService.java b/plugin-api/src/main/java/com/itsaky/androidide/plugins/services/DebuggerEndpointService.java new file mode 100644 index 0000000000..69b8ab4e20 --- /dev/null +++ b/plugin-api/src/main/java/com/itsaky/androidide/plugins/services/DebuggerEndpointService.java @@ -0,0 +1,43 @@ +package com.itsaky.androidide.plugins.services; + +import androidx.annotation.NonNull; +import java.util.List; + +/** + * Tells a plugin where the SQLite debugger servers of the open project's apps are listening, so a plugin such as an MCP client can connect without the user typing a port or token. + * + *

+ * What a plugin gets: the token is the only credential a SQLite debugger server checks, so a plugin with this service's permission can query the databases of the open project's apps through it. The server opens them read-only in this version; a later version may allow writes. Declare the permission only if the plugin needs that access. + * + *

+ * The host registers this service for every plugin; a call needs the {@link com.itsaky.androidide.plugins.PluginPermission#DEBUGGER_ENDPOINTS} permission and throws {@link SecurityException} without it, like the other permission-gated services. Plugins run in Code on the Go's process, so the permission declares what a plugin means to do: it cannot stop a plugin that reaches into the host by reflection, nor one that takes another plugin's service from the public {@code PluginManager.getInstance().getLoadedPlugin(id).context.services} or {@code PluginFragmentHelper.getServiceRegistry} (tracked in ADFA-7194). + * + *

+ * Calls are synchronous, as in {@link IdeLogService} and {@link IdeProjectService}, rather than returning a {@link java.util.concurrent.CompletableFuture} as the services that start long work do: the answer is a small read of the IDE's own database, not an operation with a lifecycle. That read is disk I/O, so call from a background thread: on the main thread the call throws {@link IllegalStateException}. This is stricter than those two services on purpose: they answer on any thread, but a database read on the main thread can stall the UI. + */ +public interface DebuggerEndpointService { + + /** + * Contract revision, bumped whenever a member is added here. It marks the revision and does not negotiate one; see {@link ToolSourceRegistry#CONTRACT_VERSION}. + */ + int CONTRACT_VERSION = 1; + + /** + * Gets the SQLite debugger endpoints of the project open in the editor, one per app that has reported. Read fresh on every call, so call it again after a connection fails: a relaunched app has a new port and token. + * + *

+ * An app the IDE installed or launched but that has not yet reported is not listed, and neither is an app that has since been uninstalled. An endpoint can still be stale: when the app's process has died, its last reported port and token stay here until the app reports again, so treat {@link DebuggerEndpoint#getUpdatedAtMillis} (when it last reported) as a hint, and when a connection is refused, call this again and reconnect. + * + * @return the endpoints, in a list the caller cannot modify (never null; empty when no project is open in the editor, as on the home screen, or no app has reported) + * @throws SecurityException + * if the plugin lacks the {@code debugger.endpoints} permission + * @throws IllegalStateException + * if called on the main thread, if the IDE's database could not be read, or if the IDE could not set up access to the endpoints when it started (its log says why). A failed read is not the same as no apps: keep what you had and try again later + * @throws java.util.concurrent.CancellationException + * if the plugin has been disabled or unloaded, until it is enabled again; also when a call already reading sees it disabled, or when the read itself is cancelled + * @throws InterruptedException + * if the calling thread is interrupted while it waits for the read. The thread's interrupt status is set again before this is thrown, so code further up still sees it + */ + @NonNull + List getEndpoints() throws InterruptedException; +} diff --git a/plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/IPlugin.kt b/plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/IPlugin.kt index c54581cc1f..246836d1c8 100644 --- a/plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/IPlugin.kt +++ b/plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/IPlugin.kt @@ -54,6 +54,12 @@ enum class PluginPermission( PROJECT_STRUCTURE("project.structure", "Modify project structure"), NATIVE_CODE("native.code", "Execute native machine code"), IDE_ENVIRONMENT_WRITE("ide.environment.write", "Write to IDE-managed directories such as the Android SDK, NDK, and cache"), + DEBUGGER_ENDPOINTS( + "debugger.endpoints", + "Read the port and token of the open project's SQLite debugger servers. The token lets the plugin query the " + + "databases of the project's apps: read-only for now, and later versions may allow writes. Plugins run in " + + "Code on the Go's process, so this declares intent and cannot stop reflection", + ), } data class PluginInfo( diff --git a/plugin-api/src/test/java/com/itsaky/androidide/plugins/services/DebuggerEndpointTest.java b/plugin-api/src/test/java/com/itsaky/androidide/plugins/services/DebuggerEndpointTest.java new file mode 100644 index 0000000000..e0073e71c8 --- /dev/null +++ b/plugin-api/src/test/java/com/itsaky/androidide/plugins/services/DebuggerEndpointTest.java @@ -0,0 +1,68 @@ +package com.itsaky.androidide.plugins.services; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; + +import com.itsaky.androidide.plugins.PluginPermission; +import org.junit.Test; + +public class DebuggerEndpointTest { + + @Test + public void aJavaCallerCanCatchTheInterrupt() { + final DebuggerEndpointService interrupted = () -> { + throw new InterruptedException("stop"); + }; + try { + interrupted.getEndpoints(); + fail(); + } catch (InterruptedException expected) { + assertEquals("stop", expected.getMessage()); + } + } + + @Test + public void equalityCoversEveryField() { + final DebuggerEndpoint base = new DebuggerEndpoint("a", 1, "t", 2L); + assertEquals(base, new DebuggerEndpoint("a", 1, "t", 2L)); + assertEquals(base.hashCode(), new DebuggerEndpoint("a", 1, "t", 2L).hashCode()); + assertNotEquals(base, new DebuggerEndpoint("b", 1, "t", 2L)); + assertNotEquals(base, new DebuggerEndpoint("a", 9, "t", 2L)); + assertNotEquals(base, new DebuggerEndpoint("a", 1, "u", 2L)); + assertNotEquals(base, new DebuggerEndpoint("a", 1, "t", 3L)); + } + + @Test + public void exposesWhatItWasBuiltWith() { + final DebuggerEndpoint endpoint = new DebuggerEndpoint("com.example.app", 43210, "secret", 1234L); + assertEquals("com.example.app", endpoint.getApplicationId()); + assertEquals(43210, endpoint.getPort()); + assertEquals("secret", endpoint.getToken()); + assertEquals(1234L, endpoint.getUpdatedAtMillis()); + } + + @Test + public void nullApplicationIdOrTokenIsRejected() { + try { + new DebuggerEndpoint(null, 1, "t", 0L); + fail(); + } catch (NullPointerException expected) {} + try { + new DebuggerEndpoint("a", 1, null, 0L); + fail(); + } catch (NullPointerException expected) {} + } + + @Test + public void permissionKeyIsStable() { + assertEquals("debugger.endpoints", PluginPermission.DEBUGGER_ENDPOINTS.getKey()); + assertEquals(1, DebuggerEndpointService.CONTRACT_VERSION); + } + + @Test + public void toStringDoesNotLeakTheToken() { + assertFalse(new DebuggerEndpoint("a", 1, "hunter2", 2L).toString().contains("hunter2")); + } +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 4f1dd0f8b7..673e1278d2 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -43,6 +43,8 @@ import com.itsaky.androidide.plugins.manager.loaders.toPluginMetadata import com.itsaky.androidide.plugins.manager.project.PluginProjectManager import com.itsaky.androidide.plugins.manager.security.PluginSecurityManager import com.itsaky.androidide.plugins.manager.services.CogoProjectProvider +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointServiceImpl +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSource import com.itsaky.androidide.plugins.manager.services.IdeArchiveServiceImpl import com.itsaky.androidide.plugins.manager.services.IdeCommandServiceImpl import com.itsaky.androidide.plugins.manager.services.IdeEditorServiceImpl @@ -63,6 +65,7 @@ import com.itsaky.androidide.plugins.manager.services.IdeUIServiceImpl import com.itsaky.androidide.plugins.manager.services.PluginBuildService import com.itsaky.androidide.plugins.manager.snippets.PluginSnippetManager import com.itsaky.androidide.plugins.manager.ui.PluginEditorTabManager +import com.itsaky.androidide.plugins.services.DebuggerEndpointService import com.itsaky.androidide.plugins.services.IdeArchiveService import com.itsaky.androidide.plugins.services.IdeBuildService import com.itsaky.androidide.plugins.services.IdeCommandService @@ -81,6 +84,7 @@ import com.itsaky.androidide.plugins.services.IdeTerminalService import com.itsaky.androidide.plugins.services.IdeThemeService import com.itsaky.androidide.plugins.services.IdeTooltipService import com.itsaky.androidide.plugins.services.IdeUIService +import com.itsaky.androidide.projects.IProjectManager import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -95,6 +99,7 @@ import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION import java.io.File import java.nio.file.Files import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicReference class PluginManager private constructor( private val context: Context, @@ -395,6 +400,41 @@ class PluginManager private constructor( // IDE service providers private val projectProvider = CogoProjectProvider() + /** + * The root of the project open in the editor, or null on the home screen. Not [projectProvider]: + * `GeneralPreferences.lastOpenedProject` still names a project after it is closed, and so does + * [IProjectManager.projectDirPath]. The editor activity registers [editorProvider] while it shows + * a project and clears it when it ends, so that is what says a project is open. + */ + private fun openProjectRoot(): File? { + if (editorProvider == null) return null + return IProjectManager + .getInstance() + .projectDirPath + .takeIf { it.isNotEmpty() } + ?.let(::File) + } + + /** Where [DebuggerEndpointService] reads. Private, so no member of this class hands it out. This does not seal every path: see [DebuggerEndpointServiceImpl]. */ + private val debuggerEndpointSource = AtomicReference() + + /** + * Installs where every plugin's [DebuggerEndpointService] reads the SQLite debugger endpoints apps + * reported. The app installs it before plugins load; when it cannot, plugins load anyway and their + * getEndpoints() throws IllegalStateException. Installing the same source again does nothing: + * the app's start runs again when a later step of it failed (CredentialProtectedApplicationLoader.load + * retries after the user unlocks). Plugins share this class loader, so a different source, which can + * only be a plugin's, fails rather than replacing or blanking the source for every plugin. + * + * @throws IllegalStateException if a different source is already installed + */ + fun installDebuggerEndpointSource(source: DebuggerEndpointSource) { + // Never cleared, so once compareAndSet fails, get() is the installed source. + check(debuggerEndpointSource.compareAndSet(null, source) || debuggerEndpointSource.get() === source) { + "A different SQLite debugger endpoint source is already installed" + } + } + init { if (!pluginsDir.exists()) { pluginsDir.mkdirs() @@ -554,6 +594,10 @@ class PluginManager private constructor( */ fun loadPlugin(file: File): Result { var reservedSlotsPluginId: String? = null + // The new plugin's context until loadedPlugins holds the plugin. Every way out before that, a + // failure path or a throw, leaves a plugin whose initialize() may have started threads, so the + // finally below closes its endpoint service. Once loaded, disabling or unloading closes it. + var notLoadedContext: PluginContext? = null return try { logger.debug("Loading plugin from: ${file.absolutePath}") @@ -664,6 +708,7 @@ class PluginManager private constructor( } val pluginContext = createPluginContextWithResources(manifest.id, manifest.name, classLoader, permissions, ctx) + notLoadedContext = pluginContext logger.debug("Initializing plugin: ${manifest.id}") val initResult = @@ -703,6 +748,7 @@ class PluginManager private constructor( nativeLibraryDir = nativeLibPath?.let(::File), ) loadedPlugins[manifest.id] = loadedPlugin + notLoadedContext = null if (!isEnabled) { logger.info("Successfully loaded plugin (disabled): ${manifest.name} (${manifest.id})") @@ -718,6 +764,8 @@ class PluginManager private constructor( logger.error("Failed to load plugin from ${file.name}: ${e.javaClass.simpleName}: ${e.message}", e) val prefix = if (e is RuntimeException) "" else "[${e.javaClass.simpleName}] " Result.failure(RuntimeException("Error loading plugin: $prefix${e.message}", e)) + } finally { + notLoadedContext?.debuggerEndpointService()?.close() } } @@ -746,7 +794,7 @@ class PluginManager private constructor( notifyLanguageContributionsChanged(plugin) }.onFailure { e -> logger.error("Failed to activate plugin: ${manifest.id}", e) - loadedPlugin.isEnabled = false + markDisabled(loadedPlugin) savePluginState(manifest.id, false) } } @@ -787,6 +835,9 @@ class PluginManager private constructor( val loadedPlugin = loadedPlugins.remove(pluginId) ?: return false try { + // First: a later step can throw and end the unload. + loadedPlugin.context.debuggerEndpointService()?.close() + // Remove documentation if plugin implements DocumentationExtension if (loadedPlugin.plugin is DocumentationExtension) { CoroutineScope(Dispatchers.IO).launch { @@ -1113,8 +1164,9 @@ class PluginManager private constructor( return true } - // The services outlive a disable, which closed the terminal service. + // The services outlive a disable, which closed the terminal and SQLite debugger endpoint services. (loadedPlugin.context.services.get(IdeTerminalService::class.java) as? IdeTerminalServiceImpl)?.reopen() + loadedPlugin.context.debuggerEndpointService()?.reopen() loadedPlugin.isEnabled = true activateLoadedPlugin(loadedPlugin) return if (loadedPlugin.isEnabled) { @@ -1139,7 +1191,7 @@ class PluginManager private constructor( return try { cleanupPluginContributions(loadedPlugin) loadedPlugin.plugin.deactivate() - loadedPlugin.isEnabled = false + markDisabled(loadedPlugin) savePluginState(pluginId, false) lifecycleDispatcher.notifyDeactivated(pluginId) notifyLanguageContributionsChanged(loadedPlugin.plugin) @@ -1162,7 +1214,7 @@ class PluginManager private constructor( runCatching { loadedPlugin.plugin.deactivate() }.onFailure { e -> logger.error("Plugin deactivate threw during force-disable: $pluginId", e) } - loadedPlugin.isEnabled = false + markDisabled(loadedPlugin) savePluginState(pluginId, false) lifecycleDispatcher.notifyDeactivated(pluginId) notifyLanguageContributionsChanged(loadedPlugin.plugin) @@ -1193,6 +1245,21 @@ class PluginManager private constructor( }.onFailure { logger.error("Failed to cleanup templates for: $pluginId", it) } } + /** + * Marks [loadedPlugin] disabled and closes its SQLite debugger endpoint service in one step, so the + * service is closed exactly when the plugin stops being enabled. A disable that fails before this + * point, as when deactivate() throws, leaves the plugin enabled and its service open: enablePlugin + * returns early for an enabled plugin, so it could never reopen the service. + */ + private fun markDisabled(loadedPlugin: LoadedPlugin) { + loadedPlugin.context.debuggerEndpointService()?.close() + loadedPlugin.isEnabled = false + } + + /** The plugin's [DebuggerEndpointServiceImpl]. Its services outlive a disable, so it is closed instead. */ + private fun PluginContext.debuggerEndpointService(): DebuggerEndpointServiceImpl? = + services.get(DebuggerEndpointService::class.java) as? DebuggerEndpointServiceImpl + sealed class CrashResult { abstract val pluginId: String abstract val pluginName: String @@ -1560,6 +1627,23 @@ class PluginManager private constructor( IdeFeatureFlagServiceImpl() } + registerServiceWithErrorHandling( + pluginServiceRegistry, + DebuggerEndpointService::class.java, + pluginId, + "debugger_endpoint", + ) { + DebuggerEndpointServiceImpl( + pluginId = pluginId, + permissions = permissions, + projectRootProvider = ::openProjectRoot, + sourceProvider = debuggerEndpointSource::get, + // The only close a plugin loaded as disabled needs: it stays closed through its own + // initialize() and after, until enablePlugin reopens it. + startOpen = getPluginState(pluginId), + ) + } + registerServiceWithErrorHandling( pluginServiceRegistry, IdeTemplateService::class.java, @@ -1668,212 +1752,6 @@ class PluginManager private constructor( ) } - private fun createPluginContext( - pluginId: String, - classLoader: ClassLoader, - permissions: Set, - ): PluginContext { - val pluginServiceRegistry = ServiceRegistryImpl() - - logger.debug("Creating IDE services for plugin: $pluginId") - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeProjectService::class.java, - pluginId, - "project", - ) { - IdeProjectServiceImpl( - pluginId = pluginId, - permissions = permissions, - projectProvider = projectProvider, - requiredPermissions = projectServicePermissions, - pathValidator = - pathValidator?.let { validator -> - object : IdeProjectServiceImpl.PathValidator { - override fun isPathAllowed(path: File): Boolean = validator.isPathAllowed(path) - - override fun getAllowedPaths(): List = validator.getAllowedPaths() - } - }, - activityProvider = delegatingActivityProvider, - ) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeUIService::class.java, - pluginId, - "UI", - ) { - IdeUIServiceImpl(delegatingActivityProvider) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeBuildService::class.java, - pluginId, - "build", - ) { - PluginBuildService(pluginId, permissions) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeLogService::class.java, - pluginId, - "log", - ) { - IdeLogServiceImpl.getInstance() - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeProjectManipulationService::class.java, - pluginId, - "project_manipulation", - ) { - IdeProjectManipulationServiceImpl.getInstance() - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeTooltipService::class.java, - pluginId, - "tooltip", - ) { - IdeTooltipServiceImpl(context, pluginId, delegatingActivityProvider) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeEditorTabService::class.java, - pluginId, - "editor_tab", - ) { - IdeEditorTabServiceImpl(delegatingActivityProvider) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeFileService::class.java, - pluginId, - "file", - ) { - IdeFileServiceImpl( - pluginId = pluginId, - permissions = permissions, - pathValidator = - pathValidator?.let { validator -> - object : IdeFileServiceImpl.PathValidator { - override fun isPathAllowed(path: File): Boolean = validator.isPathAllowed(path) - - override fun getAllowedPaths(): List = validator.getAllowedPaths() - } - }, - ) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeEnvironmentService::class.java, - pluginId, - "environment", - ) { - IdeEnvironmentServiceImpl(pluginId) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeArchiveService::class.java, - pluginId, - "archive", - ) { - IdeArchiveServiceImpl( - pluginId = pluginId, - permissions = permissions, - pathValidator = - pathValidator?.let { validator -> - object : IdeFileServiceImpl.PathValidator { - override fun isPathAllowed(path: File): Boolean = validator.isPathAllowed(path) - - override fun getAllowedPaths(): List = validator.getAllowedPaths() - } - }, - ) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeSidebarService::class.java, - pluginId, - "sidebar", - ) { - IdeSidebarServiceImpl(pluginId) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeThemeService::class.java, - pluginId, - "theme", - ) { - IdeThemeServiceImpl(context) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeFeatureFlagService::class.java, - pluginId, - "feature_flag", - ) { - IdeFeatureFlagServiceImpl() - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeTemplateService::class.java, - pluginId, - "template", - ) { - IdeTemplateServiceImpl( - pluginId = pluginId, - permissions = permissions, - onTemplatesChanged = { templateReloadListener?.invoke() }, - loadedPluginIds = { loadedPlugins.keys }, - ) - } - - registerServiceWithErrorHandling( - pluginServiceRegistry, - IdeSnippetService::class.java, - pluginId, - "snippet", - ) { - IdeSnippetServiceImpl().apply { - setRefreshCallback { pid -> - snippetRefreshListener?.invoke(pid) - } - } - } - - return PluginContextImpl( - androidContext = context, - services = pluginServiceRegistry, - eventBus = eventBus, - logger = PluginLoggerImpl(pluginId, logger), - resources = ResourceManagerImpl(pluginId, pluginsDir, classLoader), - pluginId = pluginId, - sharedServices = serviceRegistry, - lifecycleDispatcher = lifecycleDispatcher, - pluginInfoProvider = { id -> - loadedPlugins[id]?.let { - PluginInfo(it.toPluginMetadata(), it.isEnabled, isLoaded = true) - } - }, - ) - } - /** * Clean up ALL plugin files and cache directories */ diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/DebuggerEndpointServiceImpl.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/DebuggerEndpointServiceImpl.kt new file mode 100644 index 0000000000..1175f2007a --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/DebuggerEndpointServiceImpl.kt @@ -0,0 +1,109 @@ +package com.itsaky.androidide.plugins.manager.services + +import android.os.Looper +import com.itsaky.androidide.plugins.PluginPermission +import com.itsaky.androidide.plugins.services.DebuggerEndpoint +import com.itsaky.androidide.plugins.services.DebuggerEndpointService +import org.slf4j.LoggerFactory +import java.io.File +import java.util.Collections +import java.util.EnumSet +import java.util.concurrent.CancellationException + +/** + * Where the host keeps what apps reported: the app module's `debugger_endpoint_table`. The app + * installs one with [com.itsaky.androidide.plugins.manager.core.PluginManager.installDebuggerEndpointSource]; + * this module cannot reach the table itself. + */ +fun interface DebuggerEndpointSource { + /** Returns the endpoints stored for the project at [projectLocation], as it appears in `recent_project_table.location`: its canonical path. */ + fun endpointsFor(projectLocation: String): List +} + +/** + * The [DebuggerEndpointService] the plugin manager registers for each plugin, with that plugin's permissions. + * + * Plugins run in Code on the Go's process, and their class loader's parent is the host's, so the + * permission check declares intent: it cannot stop a plugin that reaches host objects by reflection. + * It does not stop a plugin without the permission from reaching another plugin's service either: + * two public entry points hand it out, `PluginManager.getInstance().getLoadedPlugin(id).context.services` + * and `PluginFragmentHelper.getServiceRegistry`. Both are tracked in ADFA-7194. What this class does + * keep: the source is held privately by the plugin manager, which installs it once, and this service + * keeps its own unmodifiable copy of the permissions, so changing the set it was built from grants nothing. + */ +internal class DebuggerEndpointServiceImpl( + private val pluginId: String, + permissions: Set, + private val projectRootProvider: () -> File?, + /** Null when the app could not install one; [getEndpoints] then fails rather than report no apps. */ + private val sourceProvider: () -> DebuggerEndpointSource?, + private val isMainThread: () -> Boolean = { Looper.myLooper() == Looper.getMainLooper() }, + /** False for a plugin saved as disabled: it stays closed through its own initialize() until [reopen]. */ + startOpen: Boolean = true, +) : DebuggerEndpointService { + private val permissions: Set = + Collections.unmodifiableSet(EnumSet.noneOf(PluginPermission::class.java).apply { addAll(permissions) }) + + // Set by close, cleared by reopen. A disabled plugin keeps its services, and any thread it left running. + @Volatile + private var closed = !startOpen + + @Throws(InterruptedException::class) + override fun getEndpoints(): List { + if (PluginPermission.DEBUGGER_ENDPOINTS !in permissions) { + throw SecurityException( + "Plugin $pluginId does not have required permissions: ${PluginPermission.DEBUGGER_ENDPOINTS.name}", + ) + } + requireOpen() + + check(!isMainThread()) { "DebuggerEndpointService.getEndpoints() reads a database; call it off the main thread" } + + val source = + sourceProvider() + ?: throw IllegalStateException( + "The SQLite debugger endpoints are unavailable: Code on the Go could not set up access to them when it started", + ) + val root = projectRootProvider() ?: return emptyList() + // The canonical path, as recent_project_table.location holds it, falling back to the absolute + // path as the app's pairing does, so a project opened through a symlink still finds its apps. + val location = runCatching { root.canonicalPath }.getOrElse { root.absolutePath } + val endpoints = + try { + source.endpointsFor(location) + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + throw e + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + log.warn("getEndpoints failed for plugin {}", pluginId, e) + throw IllegalStateException("Could not read the SQLite debugger endpoints: ${e.message}", e) + } + // Again after the read: a plugin disabled while it ran gets no tokens either. + requireOpen() + // The contract promises an unmodifiable list, whatever list the source built. + return Collections.unmodifiableList(ArrayList(endpoints)) + } + + /** + * Refuses every later call, and one still reading, with [CancellationException] until [reopen], + * as [IdeTerminalServiceImpl.cancelAll] does. Called when the plugin is disabled or unloaded. + */ + fun close() { + closed = true + } + + /** Lets calls through again after [close]. Called when the plugin is enabled. */ + fun reopen() { + closed = false + } + + private fun requireOpen() { + if (closed) throw CancellationException("Plugin $pluginId is disabled or unloaded") + } + + private companion object { + private val log = LoggerFactory.getLogger(DebuggerEndpointServiceImpl::class.java) + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerDebuggerEndpointTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerDebuggerEndpointTest.kt new file mode 100644 index 0000000000..effcc5796a --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerDebuggerEndpointTest.kt @@ -0,0 +1,480 @@ +package com.itsaky.androidide.plugins.manager.core + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.os.Bundle +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.app.BaseApplication +import com.itsaky.androidide.plugins.IPlugin +import com.itsaky.androidide.plugins.PluginContext +import com.itsaky.androidide.plugins.PluginLogger +import com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSource +import com.itsaky.androidide.plugins.manager.services.IdeEditorServiceImpl +import com.itsaky.androidide.plugins.services.DebuggerEndpoint +import com.itsaky.androidide.plugins.services.DebuggerEndpointService +import com.itsaky.androidide.preferences.internal.GeneralPreferences +import com.itsaky.androidide.projects.ProjectManagerImpl +import com.itsaky.androidide.utils.Environment +import org.junit.After +import org.junit.Assert.assertThrows +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.io.File +import java.nio.file.Files +import java.util.concurrent.CancellationException +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.atomic.AtomicReference +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream +import kotlin.concurrent.thread + +/** + * The [DebuggerEndpointService] a plugin gets when [PluginManager] loads it, through the registration + * the app runs (createPluginContextWithResources). BaseApplication backs GeneralPreferences, which + * remembers the last opened project. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], application = BaseApplication::class) +class PluginManagerDebuggerEndpointTest { + private lateinit var project: File + private val endpoint = DebuggerEndpoint("com.example.demo", 4321, "tok", 99L) + private val asked = CopyOnWriteArrayList() + + /** The app's source, which setUp installs. */ + private val source = + DebuggerEndpointSource { location -> + asked += location + if (location == project.canonicalPath) listOf(endpoint) else emptyList() + } + + private val templatesDir = Environment.TEMPLATES_DIR + + @Before + fun setUp() { + project = Files.createTempDirectory("proj").toFile() + // Disabling and unloading clean up the plugin's templates there. + Environment.TEMPLATES_DIR = File(RuntimeEnvironment.getApplication().filesDir, "templates").apply { mkdirs() } + pluginManager().installDebuggerEndpointSource(source) + } + + @After + fun tearDown() { + QuietLogger.failOn = null + forgetPluginManager() + Environment.TEMPLATES_DIR = templatesDir + ProjectManagerImpl.getInstance().projectPath = "" + GeneralPreferences.lastOpenedProject = GeneralPreferences.NO_OPENED_PROJECT + project.deleteRecursively() + } + + @Test + fun `the endpoints of the project open in the editor are returned`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + + assertThat(offMainThread { service.getEndpoints() }).containsExactly(endpoint) + } + + @Test + fun `a closed project's endpoints are not returned, though it is still the last opened project`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + pluginManager().setEditorProvider(null) + + assertThat(offMainThread { service.getEndpoints() }).isEmpty() + assertThat(asked).isEmpty() + } + + @Test + fun `each loaded plugin gets a service bound to its own permissions and the project now open`() { + val reader = load("reader", "debugger.endpoints") + val other = load("other", "filesystem.read,network.access") + val elsewhere = Files.createTempDirectory("other").toFile() + try { + openInEditor(project) + + assertThat(offMainThread { reader.getEndpoints() }).containsExactly(endpoint) + assertThrows(SecurityException::class.java) { offMainThread { other.getEndpoints() } } + + openInEditor(elsewhere) + + assertThat(offMainThread { reader.getEndpoints() }).isEmpty() + assertThat(asked).containsExactly(project.canonicalPath, elsewhere.canonicalPath).inOrder() + } finally { + elsewhere.deleteRecursively() + } + } + + @Test + fun `a disabled plugin's service refuses until the plugin is enabled again`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + + assertThat(pluginManager().disablePlugin("reader")).isTrue() + + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + + assertThat(pluginManager().enablePlugin("reader")).isTrue() + + assertThat(offMainThread { service.getEndpoints() }).containsExactly(endpoint) + } + + @Test + fun `a disable whose deactivate throws leaves the plugin enabled and its service open`() { + val service = load("reader", "debugger.endpoints", FailingDeactivatePlugin::class.java) + openInEditor(project) + + assertThat(pluginManager().disablePlugin("reader")).isFalse() + + assertThat(pluginManager().getLoadedPlugin("reader")).isNotNull() + assertThat(offMainThread { service.getEndpoints() }).containsExactly(endpoint) + } + + @Test + fun `a plugin force-disabled after crashing gets nothing, even when its deactivate throws`() { + val service = load("reader", "debugger.endpoints", FailingDeactivatePlugin::class.java) + openInEditor(project) + + pluginManager().forceDisablePlugin("reader") + + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + assertThat(asked).isEmpty() + } + + @Test + fun `a plugin disabled after crashing gets nothing`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + + pluginManager().forceDisablePlugin("reader") + + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + assertThat(asked).isEmpty() + } + + @Test + fun `an unloaded plugin's service refuses`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + + assertThat(pluginManager().unloadPlugin("reader")).isTrue() + + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + assertThat(asked).isEmpty() + } + + @Test + fun `a plugin loaded while disabled gets a service that refuses`() { + load("reader", "debugger.endpoints") + assertThat(pluginManager().disablePlugin("reader")).isTrue() + assertThat(pluginManager().unloadPlugin("reader")).isTrue() + openInEditor(project) + + val reloaded = load("reader", "debugger.endpoints") + + assertThat(pluginManager().getLoadedPlugin("reader")).isNull() + assertThrows(CancellationException::class.java) { offMainThread { reloaded.getEndpoints() } } + } + + @Test + fun `a plugin saved as disabled cannot read endpoints from its own initialize`() { + load("reader", "debugger.endpoints") + assertThat(pluginManager().disablePlugin("reader")).isTrue() + assertThat(pluginManager().unloadPlugin("reader")).isTrue() + openInEditor(project) + ReadingInInitializePlugin.outcome = null + + load("reader", "debugger.endpoints", ReadingInInitializePlugin::class.java) + + assertThat(ReadingInInitializePlugin.outcome!!.exceptionOrNull()).isInstanceOf(CancellationException::class.java) + assertThat(asked).isEmpty() + } + + @Test + fun `an enabled plugin can read endpoints from its own initialize`() { + openInEditor(project) + ReadingInInitializePlugin.outcome = null + + load("reader", "debugger.endpoints", ReadingInInitializePlugin::class.java) + + assertThat(ReadingInInitializePlugin.outcome!!.getOrThrow()).containsExactly(endpoint) + } + + @Test + fun `a plugin whose initialize returns false or throws is left with a service that refuses`() { + openInEditor(project) + for (mode in listOf(FailingInitializePlugin.Mode.RETURN_FALSE, FailingInitializePlugin.Mode.THROW)) { + FailingInitializePlugin.mode = mode + FailingInitializePlugin.service = null + val file = + fakePlugin( + File(RuntimeEnvironment.getApplication().filesDir, "plugins/failing.cgp"), + "failing", + "debugger.endpoints", + FailingInitializePlugin::class.java, + ) + + assertThat(pluginManager().loadPlugin(file).isFailure).isTrue() + + val service = checkNotNull(FailingInitializePlugin.service) + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + } + assertThat(asked).isEmpty() + } + + @Test + fun `a plugin that fails to load after its initialize succeeded is left with a service that refuses`() { + openInEditor(project) + ServiceKeepingPlugin.service = null + // Logged by the step right after initialize(), which registers the plugin's resource context. + QuietLogger.failOn = "Registered resource context for plugin: keeper" + val file = + fakePlugin( + File(RuntimeEnvironment.getApplication().filesDir, "plugins/keeper.cgp"), + "keeper", + "debugger.endpoints", + ServiceKeepingPlugin::class.java, + ) + + assertThat(pluginManager().loadPlugin(file).isFailure).isTrue() + + val service = checkNotNull(ServiceKeepingPlugin.service) + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + assertThat(asked).isEmpty() + } + + @Test + fun `a plugin whose activation fails is left disabled and its service refuses`() { + openInEditor(project) + + val service = load("reader", "debugger.endpoints", FailingActivationPlugin::class.java) + + assertThat(pluginManager().getLoadedPlugin("reader")).isNull() + assertThrows(CancellationException::class.java) { offMainThread { service.getEndpoints() } } + assertThat(asked).isEmpty() + } + + @Test + fun `a second install, as a plugin would try, fails and leaves the app's source in place`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + + assertThrows(IllegalStateException::class.java) { pluginManager().installDebuggerEndpointSource { emptyList() } } + + assertThat(offMainThread { service.getEndpoints() }).containsExactly(endpoint) + } + + @Test + fun `installing the same source again, as a retried app start does, is allowed and a different one still fails`() { + val service = load("reader", "debugger.endpoints") + openInEditor(project) + + pluginManager().installDebuggerEndpointSource(source) + assertThrows(IllegalStateException::class.java) { pluginManager().installDebuggerEndpointSource { emptyList() } } + + assertThat(offMainThread { service.getEndpoints() }).containsExactly(endpoint) + } + + @Test + fun `no public member hands out the source, and the public holder plugins could read is gone`() { + val types = PluginManager::class.java.methods.map { it.returnType } + PluginManager::class.java.fields.map { it.type } + + assertThat(types.filter { DebuggerEndpointSource::class.java.isAssignableFrom(it) }).isEmpty() + assertThrows(ClassNotFoundException::class.java) { + Class.forName("com.itsaky.androidide.plugins.manager.services.DebuggerEndpointSources") + } + } + + /** What opening [dir] leaves behind: the project path and the last opened project, and an editor showing it. */ + private fun openInEditor(dir: File) { + ProjectManagerImpl.getInstance().projectPath = dir.absolutePath + GeneralPreferences.lastOpenedProject = dir.absolutePath + pluginManager().setEditorProvider(OpenEditor) + } + + private fun load( + id: String, + permissions: String, + mainClass: Class = ContextKeepingPlugin::class.java, + ): DebuggerEndpointService { + val file = fakePlugin(File(RuntimeEnvironment.getApplication().filesDir, "plugins/$id.cgp"), id, permissions, mainClass) + val result = pluginManager().loadPlugin(file) + assertWithMessage("plugin $id did not load: ${result.exceptionOrNull()}").that(result.isSuccess).isTrue() + val plugin = pluginManager().getPlugin(id) as ContextKeepingPlugin + return checkNotNull(plugin.context.services.get(DebuggerEndpointService::class.java)) { "no DebuggerEndpointService for $id" } + } + + /** Runs [block] on a worker thread: on the main thread, where Robolectric runs the test, getEndpoints throws. */ + private fun offMainThread(block: () -> T): T { + val result = AtomicReference>() + thread { result.set(runCatching(block)) }.join() + return result.get().getOrThrow() + } + + private fun pluginManager() = PluginManager.getInstance(RuntimeEnvironment.getApplication(), Any(), QuietLogger) + + /** Forgets the singleton plugin manager: each test has an application of its own. */ + private fun forgetPluginManager() { + PluginManager::class.java + .getDeclaredField("INSTANCE") + .apply { isAccessible = true } + .set(null, null) + } + + /** Writes [file] as a plugin whose manifest states [id] and [permissions]: Robolectric can't read one from an APK. */ + private fun fakePlugin( + file: File, + id: String, + permissions: String, + mainClass: Class = ContextKeepingPlugin::class.java, + ): File { + file.parentFile?.mkdirs() + ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } + val info = + PackageInfo().apply { + packageName = "org.example.$id" + applicationInfo = + ApplicationInfo().apply { + metaData = + Bundle().apply { + putString("plugin.id", id) + putString("plugin.main_class", mainClass.name) + putString("plugin.permissions", permissions) + } + } + } + shadowOf(RuntimeEnvironment.getApplication().packageManager).setPackageArchiveInfo(file.absolutePath, info) + return file + } + + /** A plugin that keeps its context. Its class loader finds it through its parent, the test's. */ + open class ContextKeepingPlugin : IPlugin { + lateinit var context: PluginContext + + override fun initialize(context: PluginContext): Boolean { + this.context = context + return true + } + + override fun activate() = true + + override fun deactivate() = true + + override fun dispose() = Unit + } + + /** Reads endpoints from initialize(), on a thread of its own as a real plugin would. */ + class ReadingInInitializePlugin : ContextKeepingPlugin() { + override fun initialize(context: PluginContext): Boolean { + super.initialize(context) + val service = checkNotNull(context.services.get(DebuggerEndpointService::class.java)) + val result = AtomicReference>>() + thread { result.set(runCatching { service.getEndpoints() }) }.join() + outcome = result.get() + return true + } + + companion object { + @Volatile + var outcome: Result>? = null + } + } + + /** Keeps its service, then fails initialize() as [mode] says. */ + class FailingInitializePlugin : ContextKeepingPlugin() { + enum class Mode { RETURN_FALSE, THROW } + + override fun initialize(context: PluginContext): Boolean { + service = context.services.get(DebuggerEndpointService::class.java) + if (mode == Mode.THROW) error("initialize failed") + return false + } + + companion object { + @Volatile + var mode = Mode.RETURN_FALSE + + @Volatile + var service: DebuggerEndpointService? = null + } + } + + /** Keeps its service where the test can reach it when the load fails, then succeeds in initialize(). */ + class ServiceKeepingPlugin : ContextKeepingPlugin() { + override fun initialize(context: PluginContext): Boolean { + service = context.services.get(DebuggerEndpointService::class.java) + return super.initialize(context) + } + + companion object { + @Volatile + var service: DebuggerEndpointService? = null + } + } + + /** initialize() succeeds, activate() throws. */ + class FailingActivationPlugin : ContextKeepingPlugin() { + override fun activate(): Boolean = error("activate failed") + } + + /** Loads and activates, then deactivate() throws. */ + class FailingDeactivatePlugin : ContextKeepingPlugin() { + override fun deactivate(): Boolean = error("deactivate failed") + } + + /** The editor activity's provider, as registered while it shows a project. */ + private object OpenEditor : IdeEditorServiceImpl.EditorProvider { + override fun getCurrentFile(): File? = null + + override fun getOpenFiles(): List = emptyList() + + override fun isFileOpen(file: File): Boolean = false + + override fun getCurrentSelection(): String? = null + } + + /** Logs nothing. A debug message starting with [failOn] throws, standing in for a load step that fails. */ + private object QuietLogger : PluginLogger { + override val pluginId = "test" + + @Volatile + var failOn: String? = null + + override fun debug(message: String) { + // Not error(): in here that is this logger's own error(String). + if (failOn?.let(message::startsWith) == true) throw IllegalStateException("simulated failure at: $message") + } + + override fun debug( + message: String, + error: Throwable, + ) = Unit + + override fun info(message: String) = Unit + + override fun info( + message: String, + error: Throwable, + ) = Unit + + override fun warn(message: String) = Unit + + override fun warn( + message: String, + error: Throwable, + ) = Unit + + override fun error(message: String) = Unit + + override fun error( + message: String, + error: Throwable, + ) = Unit + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/services/DebuggerEndpointServiceImplTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/services/DebuggerEndpointServiceImplTest.kt new file mode 100644 index 0000000000..a384315eff --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/services/DebuggerEndpointServiceImplTest.kt @@ -0,0 +1,179 @@ +package com.itsaky.androidide.plugins.manager.services + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.PluginPermission +import com.itsaky.androidide.plugins.services.DebuggerEndpoint +import org.junit.Assert.assertThrows +import org.junit.Test +import java.io.File +import java.nio.file.Files + +class DebuggerEndpointServiceImplTest { + private val project = File("/data/projects/demo") + private val endpoint = DebuggerEndpoint("com.example.demo", 4321, "tok", 99L) + private val seen = mutableListOf() + private val source = + DebuggerEndpointSource { location -> + seen += location + if (location == project.canonicalPath) listOf(endpoint) else emptyList() + } + + private fun service( + permissions: Set = setOf(PluginPermission.DEBUGGER_ENDPOINTS), + root: File? = project, + source: DebuggerEndpointSource = this.source, + mainThread: Boolean = false, + ) = DebuggerEndpointServiceImpl("p", permissions, { root }, { source }, { mainThread }) + + @Test + fun `returns the endpoints stored for the open project`() { + assertThat(service().getEndpoints()).containsExactly(endpoint) + assertThat(seen).containsExactly(project.canonicalPath) + } + + @Test + fun `asks the source by the canonical path, as recent_project_table stores it, for a root opened through a symlink`() { + val dir = Files.createTempDirectory("proj").toFile() + val links = Files.createTempDirectory("links").toFile() + val link = File(links, "proj") + try { + Files.createSymbolicLink(link.toPath(), dir.toPath()) + val stored = + DebuggerEndpointSource { location -> + seen += location + if (location == dir.canonicalPath) listOf(endpoint) else emptyList() + } + + assertThat(service(root = link, source = stored).getEndpoints()).containsExactly(endpoint) + assertThat(seen).containsExactly(dir.canonicalPath) + } finally { + // The link first, by itself: walking it would reach into the directory it points at. + link.delete() + links.delete() + dir.delete() + } + } + + @Test + fun `is empty and never asks the source when no project is open`() { + assertThat(service(root = null).getEndpoints()).isEmpty() + assertThat(seen).isEmpty() + } + + @Test + fun `the list returned cannot be modified, whatever list the source built`() { + val endpoints = service(source = { arrayListOf(endpoint) }).getEndpoints() + + assertThrows(UnsupportedOperationException::class.java) { (endpoints as MutableList).add(endpoint) } + } + + @Test + fun `is empty when the project has no endpoint`() { + assertThat(service(root = File("/data/projects/other")).getEndpoints()).isEmpty() + } + + @Test + fun `refuses a plugin without the permission`() { + val denied = service(permissions = setOf(PluginPermission.FILESYSTEM_READ, PluginPermission.NETWORK_ACCESS)) + + assertThrows(SecurityException::class.java) { denied.getEndpoints() } + assertThat(seen).isEmpty() + } + + @Test + fun `a failing source is reported, not mistaken for no apps`() { + val cause = IllegalStateException("db closed") + val failing = service(source = { throw cause }) + + val thrown = assertThrows(IllegalStateException::class.java) { failing.getEndpoints() } + assertThat(thrown).hasCauseThat().isSameInstanceAs(cause) + } + + @Test + fun `an interrupt from the source is rethrown unchanged and keeps the flag`() { + val interrupted = service(source = { throw InterruptedException("stop") }) + + try { + val thrown = assertThrows(InterruptedException::class.java) { interrupted.getEndpoints() } + assertThat(thrown).hasMessageThat().isEqualTo("stop") + assertThat(Thread.currentThread().isInterrupted).isTrue() + } finally { + Thread.interrupted() + } + } + + @Test + fun `a cancellation from the source is rethrown unchanged`() { + val cancelled = service(source = { throw java.util.concurrent.CancellationException("cancelled") }) + + val thrown = assertThrows(java.util.concurrent.CancellationException::class.java) { cancelled.getEndpoints() } + assertThat(thrown).hasMessageThat().isEqualTo("cancelled") + } + + @Test + fun `refuses to read on the main thread`() { + val onMain = service(mainThread = true) + + assertThrows(IllegalStateException::class.java) { onMain.getEndpoints() } + assertThat(seen).isEmpty() + } + + @Test + fun `a closed service refuses until reopened`() { + val service = service() + service.close() + + assertThrows(java.util.concurrent.CancellationException::class.java) { service.getEndpoints() } + assertThat(seen).isEmpty() + + service.reopen() + + assertThat(service.getEndpoints()).containsExactly(endpoint) + } + + @Test + fun `a plugin disabled during a read gets no tokens from it`() { + lateinit var disabledMidRead: DebuggerEndpointServiceImpl + disabledMidRead = + service(source = { + disabledMidRead.close() + listOf(endpoint) + }) + + assertThrows(java.util.concurrent.CancellationException::class.java) { disabledMidRead.getEndpoints() } + } + + @Test + fun `without a source, as when the app could not set one up, a call fails clearly instead of reporting no apps`() { + for (root in listOf(project, null)) { + val service = DebuggerEndpointServiceImpl("p", setOf(PluginPermission.DEBUGGER_ENDPOINTS), { root }, { null }, { false }) + + val thrown = assertThrows(IllegalStateException::class.java) { service.getEndpoints() } + assertThat(thrown).hasMessageThat().startsWith("The SQLite debugger endpoints are unavailable") + } + } + + @Test + fun `adding the permission to the set the service was built from grants nothing`() { + val granted = linkedSetOf(PluginPermission.FILESYSTEM_READ, PluginPermission.NETWORK_ACCESS) + val denied = service(permissions = granted) + + granted += PluginPermission.DEBUGGER_ENDPOINTS + + assertThrows(SecurityException::class.java) { denied.getEndpoints() } + assertThat(seen).isEmpty() + } + + @Test + fun `the service's own permission set cannot be added to, as a plugin would by reflection`() { + val denied = service(permissions = linkedSetOf(PluginPermission.FILESYSTEM_READ, PluginPermission.NETWORK_ACCESS)) + val field = DebuggerEndpointServiceImpl::class.java.getDeclaredField("permissions").apply { isAccessible = true } + + @Suppress("UNCHECKED_CAST") + val held = field.get(denied) as MutableSet + assertThrows(UnsupportedOperationException::class.java) { held.add(PluginPermission.DEBUGGER_ENDPOINTS) } + + assertThrows(SecurityException::class.java) { denied.getEndpoints() } + assertThat(seen).isEmpty() + } +} diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index 516f79f558..5c8e21d2ee 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -24,6 +24,7 @@ MCP client --HTTP, 127.0.0.1--> McpHttp -> McpProtocol -> DatabaseTools -> Debug - `DebuggerStartup` is a `ContentProvider` with the highest `initOrder`, declared in the AAR's manifest. It runs before the app's own providers and `Application.onCreate`, so the extension is registered before the app opens a database. It installs the extension synchronously in `onCreate` (so an app that opens its database in `Application.onCreate` is still seen) and starts the server on a background thread. Everything after the debuggable check is wrapped in `catch (Throwable)`: a missing requery library (`UnsatisfiedLinkError`) means the debugger is disabled and logged, never a crash of the host app. - `EndpointBroadcast` runs once the server listens, on every process start (the port changes). It sends an explicit broadcast, action `com.itsaky.androidide.SQLITE_DEBUGGER_ENDPOINT`, to `com.itsaky.androidide` with `FLAG_INCLUDE_STOPPED_PACKAGES`, carrying the token, the port and a `PendingIntent` that identifies the app. The `PendingIntent` is immutable (on API 23 and later; the flag does not exist before) and wraps an intent that is never sent: Code on the Go reads its `creatorPackage`, which the framework sets and another app cannot forge, as the app's package name. The library manifest declares `` for Code on the Go. A failure to send is logged and never thrown. Before the first send it registers a receiver at run time for `org.appdevforall.sqlitedebugger.ANNOUNCE` (exported, as Code on the Go is another app; `RECEIVER_EXPORTED` on API 33 and later), and sends the same token and port again each time it receives one. Code on the Go sends that request when it pairs the app, because it drops a report from an app it has not paired: an app that was already running then, installed some other way or started before its project was opened, would otherwise stay unknown until its process restarted. The receiver is not in the manifest, so a request never starts an app that is not running, and is not authenticated (ADFA-7131 D8): the reply goes only to Code on the Go. - On the Code on the Go side, `DebuggerEndpointReceiver` (exported) validates the token and port and resolves the sender, then `DebuggerEndpointRepository` stores them in `debugger_endpoint_table` of the Recent Projects database, keyed by application ID. A row exists only for an app Code on the Go installed or launched (`LaunchAppAction` and `ProjectHandlerActivity` record the pairing); a broadcast from any other app stores nothing. When `ProjectHandlerActivity` installs a build, it notes the PackageInstaller session ID with the project and the APK's application ID before the session is committed. `InstallationResultReceiver` takes the result app-wide, so a result that arrives while the editor is stopped still counts: on success it moves the app to that session's project (Quick Build's installs are never noted, so they never pair); on failure it drops that session's note, as `ApkInstaller`'s `onCommitFailed` does when staging or committing the session throws, since no result follows. Only the same session's result uses a note, however long the install prompt stayed open, so two projects with the same application ID, such as two made from one template, each pair their own installs. The post-install launch does not wait for the write; the announce request above brings the app's report once the row exists. An install Code on the Go did not start from a project (an APK opened from the file tree), or one whose result arrives after Code on the Go's process restarted (notes are kept in memory), is not paired at install; `LaunchAppAction` pairs it at the next launch. After each pairing, `DebuggerEndpointAnnounce` sends the app the announce request above. The row is deleted with its project (foreign key, ON DELETE CASCADE). +- Plugins get the stored endpoints through `DebuggerEndpointService` (plugin API), which needs the `debugger.endpoints` permission. `getEndpoints()` returns one entry per app of the project open in the editor that has reported (application ID, port, token, last update), and none on the home screen, read fresh on every call and not on the main thread, so a plugin asks again after a refused connection. Apps that are no longer installed are left out; an app whose process died is still listed until it reports again. The AI-Agent-MCP plugin (addons repository) is the first client. ### The event record @@ -52,5 +53,4 @@ The layout of `sqlitedbg_event_record` lives in `sqlitedbg.h`. The JNI layer han ## Not done yet - Gradle plugin wiring and the build-time rewrite that adds the library to debug builds (ADFA-7138). -- The AI-Agent-MCP side that connects to the server (ADFA-7139). - A device run of the current code. The device results on ADFA-7135 (statements captured on both ABIs, the overhead) predate the review fixes.