Skip to content

Commit

Permalink
Store global DevSessions
Browse files Browse the repository at this point in the history
  • Loading branch information
JingMatrix committed Sep 8, 2023
1 parent 01807c4 commit 08f4126
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
18 changes: 4 additions & 14 deletions app/src/main/java/org/matrix/chromext/Chrome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.File
import java.lang.ref.WeakReference
import java.util.concurrent.Executors
import org.json.JSONObject
import org.matrix.chromext.devtools.DevSessions
import org.matrix.chromext.devtools.DevToolClient
import org.matrix.chromext.devtools.getInspectPages
import org.matrix.chromext.devtools.hitDevTools
Expand All @@ -31,7 +32,6 @@ object Chrome {
var isVivaldi = false

val IO = Executors.newCachedThreadPool()
val cspBypassed = mutableSetOf<DevToolClient>()

fun init(ctx: Context, packageName: String? = null) {
val initialized = mContext != null
Expand Down Expand Up @@ -116,25 +116,15 @@ object Chrome {

private fun evaluateJavascriptDevTools(codes: List<String>, tabId: String, bypassCSP: Boolean) {
wakeUpDevTools()
val lastClient = cspBypassed.find { it.tabId == tabId }
var client = lastClient ?: DevToolClient(tabId)
var client = DevSessions.get(tabId) ?: DevToolClient(tabId)
if (client.isClosed()) {
cspBypassed.remove(client)
hitDevTools().close()
client = DevToolClient(tabId)
}
codes.forEach { client.evaluateJavascript(it) }
// Bypass CSP is only effective after reloading
client.bypassCSP(bypassCSP)

if ((lastClient == client) != bypassCSP) {
client.command(null, "Page.enable", JSONObject())
client.command(null, "Page.setBypassCSP", JSONObject().put("enabled", bypassCSP))
if (bypassCSP) {
cspBypassed.add(client)
} else {
cspBypassed.remove(client)
}
}
if (!bypassCSP) client.close()
}

Expand All @@ -145,7 +135,7 @@ object Chrome {
bypassCSP: Boolean = false,
) {
if (forceDevTools) {
this.IO.submit {
IO.submit {
val tabId =
if (WebViewHook.isInit) {
val url = getUrl(currentTab)
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/org/matrix/chromext/Listener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.net.HttpURLConnection
import java.net.URL
import org.json.JSONArray
import org.json.JSONObject
import org.matrix.chromext.devtools.DevSessions
import org.matrix.chromext.devtools.DevToolClient
import org.matrix.chromext.devtools.getInspectPages
import org.matrix.chromext.devtools.hitDevTools
Expand All @@ -31,7 +32,6 @@ import org.matrix.chromext.utils.parseOrigin
object Listener {

val xmlhttpRequests = mutableMapOf<Double, XMLHttpRequest>()
val devSessions = mutableSetOf<DevToolClient>()
val allowedActions =
mapOf(
"front-end" to listOf("inspect_pages", "userscript", "extension"),
Expand Down Expand Up @@ -275,7 +275,7 @@ object Listener {
"websocket" -> {
val detail = JSONObject(payload)
val targetTabId = detail.getString("targetTabId")
var target = devSessions.find { it.tabId == targetTabId }
var target = DevSessions.get(targetTabId)
if (detail.has("message")) {
val message = JSONObject(detail.getString("message"))
target?.command(
Expand All @@ -285,17 +285,15 @@ object Listener {
if (Chrome.checkTab(currentTab)) {
Chrome.evaluateJavascript(listOf("ChromeXt.post('websocket', ${res})"), currentTab)
} else {
Log.d("Tab closed")
target?.close()
}
}
Chrome.IO.submit {
target?.close()
devSessions.remove(target)
hitDevTools().close()
target = DevToolClient(targetTabId)
if (!target!!.isClosed()) {
devSessions.add(target!!)
DevSessions.add(target!!)
response(JSONObject(mapOf("open" to true)))
target!!.listen { response(JSONObject(mapOf("message" to it))) }
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/org/matrix/chromext/devtools/Inspect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ fun hitDevTools(): LocalSocket {
client.outputStream.write("GET /json HTTP/1.1\r\n\r\n".toByteArray())
return client
}

object DevSessions {
private val clients = mutableSetOf<DevToolClient>()
fun get(tabId: String): DevToolClient? {
var cached = clients.find { it.tabId == tabId }
if (cached?.isClosed() == true) {
clients.remove(cached)
cached = null
}
return cached
}
fun add(client: DevToolClient?) {
if (client == null) return
val cached = clients.find { it.tabId == client.tabId }
if (cached != null) clients.remove(cached)
clients.add(client)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.matrix.chromext.utils.Log
class DevToolClient(tabId: String) : LocalSocket() {

val tabId = tabId
private var cspBypassed = false
private var id = 1
private var mClosed = false

Expand Down Expand Up @@ -65,6 +66,14 @@ class DevToolClient(tabId: String) : LocalSocket() {
WebSocketFrame(msg.toString()).write(outputStream)
}

fun bypassCSP(bypass: Boolean) {
if (cspBypassed == bypass) return
command(null, "Page.enable", JSONObject())
command(null, "Page.setBypassCSP", JSONObject().put("enabled", bypass))
cspBypassed = bypass
if (bypass) DevSessions.add(this)
}

fun evaluateJavascript(script: String) {
command(this.id, "Runtime.evaluate", JSONObject(mapOf("expression" to script)))
this.id += 1
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/matrix/chromext/proxy/UserScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ object UserScriptProxy {
val kMaxURLChars = 2097152

private fun loadUrl(url: String, tab: Any? = Chrome.getTab()) {
runCatching { loadUrl.invoke(tab, newLoadUrlParams(url)) }.onFailure { Log.ex(it) }
if (!Chrome.checkTab(tab)) return
loadUrl.invoke(tab, newLoadUrlParams(url))
}

fun newLoadUrlParams(url: String): Any {
Expand Down

0 comments on commit 08f4126

Please sign in to comment.