fix: dispose JCEF JSQuery handler correctly
The JCEF browser wasn’t registered as a Disposable when held by the service. Service was disposed, but we had leak, because this caused JSQuery handlers to run on a disposed browser (browsers are disposed automatically by default according to docs), throwing exceptions when opening another project. This fix registers both the browser and the handler properly. I tested it manually, and opening multiple projects no longer causes errors. Note: I think it would be useful to have an e2e test at some stage that opens a project, closes it, and then opens another one. Such a test should catch this issue early.
This commit is contained in:
parent
9b373b61e1
commit
e6cf3fd756
2 changed files with 22 additions and 6 deletions
|
|
@ -1,28 +1,29 @@
|
|||
package com.github.continuedev.continueintellijextension.browser
|
||||
|
||||
import com.github.continuedev.continueintellijextension.constants.MessageTypes
|
||||
import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings
|
||||
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
|
||||
import com.github.continuedev.continueintellijextension.utils.uuid
|
||||
import com.google.gson.Gson
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.ui.jcef.*
|
||||
import org.cef.CefApp
|
||||
import org.cef.browser.CefBrowser
|
||||
import org.cef.handler.CefLoadHandlerAdapter
|
||||
import javax.swing.JComponent
|
||||
|
||||
class ContinueBrowser(private val project: Project) {
|
||||
class ContinueBrowser(private val project: Project): Disposable {
|
||||
|
||||
private val log = Logger.getInstance(ContinueBrowser::class.java.simpleName)
|
||||
private val browser: JBCefBrowser = JBCefBrowser.createBuilder().setOffScreenRendering(true).build()
|
||||
private val myJSQueryOpenInBrowser = JBCefJSQuery.create(browser as JBCefBrowserBase)
|
||||
|
||||
init {
|
||||
CefApp.getInstance().registerSchemeHandlerFactory("http", "continue", CustomSchemeHandlerFactory())
|
||||
browser.jbCefClient.setProperty(JBCefClient.Properties.JS_QUERY_POOL_SIZE, 200)
|
||||
val myJSQueryOpenInBrowser = JBCefJSQuery.create(browser as JBCefBrowserBase)
|
||||
myJSQueryOpenInBrowser.addHandler { msg: String? ->
|
||||
val json = Gson().fromJson(msg, BrowserMessage::class.java)
|
||||
val messageType = json.messageType
|
||||
|
|
@ -100,6 +101,11 @@ class ContinueBrowser(private val project: Project) {
|
|||
browser.cefBrowser.executeJavaScript(script, getGuiUrl(), 0)
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
Disposer.dispose(myJSQueryOpenInBrowser)
|
||||
Disposer.dispose(browser)
|
||||
}
|
||||
|
||||
// todo: remove and use types.Message
|
||||
private data class BrowserMessage(
|
||||
val messageType: String,
|
||||
|
|
|
|||
|
|
@ -1,22 +1,32 @@
|
|||
package com.github.continuedev.continueintellijextension.browser
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.components.Service
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.ui.jcef.JBCefApp
|
||||
|
||||
@Service(Service.Level.PROJECT)
|
||||
class ContinueBrowserService(project: Project) {
|
||||
class ContinueBrowserService(project: Project): Disposable {
|
||||
|
||||
private val browser: ContinueBrowser? =
|
||||
if (JBCefApp.isSupported())
|
||||
ContinueBrowser(project)
|
||||
else null
|
||||
|
||||
override fun dispose() {
|
||||
if (browser != null)
|
||||
Disposer.dispose(browser)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun Project.getBrowser() =
|
||||
service<ContinueBrowserService>().browser
|
||||
fun Project.getBrowser(): ContinueBrowser? {
|
||||
if (isDisposed)
|
||||
return null
|
||||
return service<ContinueBrowserService>().browser
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue