-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90cc3d4
commit 1e184d4
Showing
10 changed files
with
199 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/kotlin/ai/codemaker/jetbrains/window/FileResourceProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 CodeMaker AI Inc. All rights reserved. | ||
*/ | ||
|
||
package ai.codemaker.jetbrains.window | ||
|
||
import com.intellij.openapi.util.io.toNioPath | ||
import org.cef.browser.CefBrowser | ||
import org.cef.browser.CefFrame | ||
import org.cef.callback.CefCallback | ||
import org.cef.handler.* | ||
import org.cef.misc.BoolRef | ||
import org.cef.network.CefRequest | ||
import java.net.URL | ||
|
||
private typealias CefResourceProvider = () -> CefResourceHandler? | ||
|
||
class FileResourceProvider() : CefRequestHandlerAdapter() { | ||
|
||
private val protocol = "file" | ||
|
||
private val resources = HashMap<String, CefResourceProvider>() | ||
|
||
private val REJECTING_RESOURCE_HANDLER: CefResourceHandler = object : CefResourceHandlerAdapter() { | ||
override fun processRequest(request: CefRequest, callback: CefCallback): Boolean { | ||
callback.cancel() | ||
return false | ||
} | ||
} | ||
|
||
private val RESOURCE_REQUEST_HANDLER = object : CefResourceRequestHandlerAdapter() { | ||
override fun getResourceHandler( | ||
browser: CefBrowser?, | ||
frame: CefFrame?, | ||
request: CefRequest | ||
): CefResourceHandler? { | ||
val url = URL(request.url) | ||
if (protocol != url.protocol) { | ||
return REJECTING_RESOURCE_HANDLER | ||
} | ||
|
||
return resolveHandler(url) | ||
} | ||
} | ||
|
||
fun addResource(path: String, resourceProvider: CefResourceProvider) { | ||
resources[path] = resourceProvider | ||
} | ||
|
||
override fun getResourceRequestHandler( | ||
browser: CefBrowser?, | ||
frame: CefFrame?, | ||
request: CefRequest?, | ||
isNavigation: Boolean, | ||
isDownload: Boolean, | ||
requestInitiator: String?, | ||
disableDefaultHandling: BoolRef? | ||
): CefResourceRequestHandler { | ||
return RESOURCE_REQUEST_HANDLER | ||
} | ||
|
||
private fun resolveHandler(url: URL): CefResourceHandler? { | ||
var path = url.path.toNioPath() | ||
while (path != null) { | ||
val handler = resources[path.toString()] | ||
if (handler != null) { | ||
return handler() | ||
} | ||
path = path.parent | ||
} | ||
return null | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/ai/codemaker/jetbrains/window/StreamResourceHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2023 CodeMaker AI Inc. All rights reserved. | ||
*/ | ||
|
||
package ai.codemaker.jetbrains.window | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.util.Disposer | ||
import com.intellij.openapi.util.io.toNioPath | ||
import io.ktor.util.* | ||
import org.cef.callback.CefCallback | ||
import org.cef.handler.CefResourceHandler | ||
import org.cef.misc.IntRef | ||
import org.cef.misc.StringRef | ||
import org.cef.network.CefRequest | ||
import org.cef.network.CefResponse | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.net.URL | ||
import java.nio.file.Path | ||
|
||
class StreamResourceHandler(private val resourcePath: String, parent: Disposable) : CefResourceHandler, Disposable { | ||
|
||
private var input: InputStream? = null | ||
private var mimeType = "text/html" | ||
|
||
init { | ||
Disposer.register(parent, this) | ||
} | ||
|
||
override fun processRequest(request: CefRequest?, callback: CefCallback?): Boolean { | ||
val url = URL(request!!.url) | ||
val path = url.path | ||
|
||
input = this.javaClass.classLoader.getResourceAsStream(Path.of(resourcePath, path).toString()) | ||
if (input == null) { | ||
return false | ||
} | ||
|
||
when (path.toNioPath().extension) { | ||
"html" -> mimeType = "text/html"; | ||
"svg" -> mimeType = "image/svg+xml"; | ||
} | ||
|
||
callback!!.Continue() | ||
return true | ||
} | ||
|
||
override fun getResponseHeaders(response: CefResponse?, responseLength: IntRef?, redirectUrl: StringRef?) { | ||
responseLength!!.set(input!!.available()) | ||
response!!.mimeType = mimeType | ||
response!!.status = 200 | ||
} | ||
|
||
override fun readResponse( | ||
dataOut: ByteArray?, | ||
bytesToRead: Int, | ||
bytesRead: IntRef?, | ||
callback: CefCallback? | ||
): Boolean { | ||
try { | ||
bytesRead!!.set(input!!.read(dataOut!!, 0, bytesToRead)) | ||
if (bytesRead!!.get() != -1) { | ||
return true | ||
} | ||
} catch (e: IOException) { | ||
callback!!.cancel() | ||
} | ||
bytesRead!!.set(0) | ||
Disposer.dispose(this) | ||
return false | ||
} | ||
|
||
override fun cancel() { | ||
Disposer.dispose(this) | ||
} | ||
|
||
override fun dispose() { | ||
try { | ||
input?.close() | ||
} catch (e: IOException) { | ||
// ignores | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.