pl.wendigo.chrome.Inspector.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chrome-reactive-kotlin Show documentation
Show all versions of chrome-reactive-kotlin Show documentation
Chrome Reactive - low level, remote chrome debugger protocol client (DevTools)
package pl.wendigo.chrome
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.Single
import okhttp3.OkHttpClient
import okhttp3.Request
import java.util.concurrent.TimeUnit
/**
* Creates new inspector that allows querying remote chrome instance for debugging sessions
*/
class Inspector(
private val chromeAddress: String,
private val client: OkHttpClient,
private val mapper: FrameMapper
) {
/**
* Opens new page.
*/
fun openNewPage(url : String? = null) : Single {
return runInspectorCommand("new?url=$url").map {
mapper.deserialize(it, InspectablePage::class.java)
}
}
/**
* Returns currently opened pages and associated data (debugger connection uris)
*/
fun openedPages() : Flowable {
return this.runInspectorCommand("list").flatMapObservable {
Observable.fromArray(*mapper.deserialize(it, Array::class.java))
}.toFlowable(BackpressureStrategy.BUFFER).filter {
it.webSocketDebuggerUrl != null
}
}
/**
* Closes given page.
*/
fun close(page: InspectablePage): Single {
return runInspectorCommand("close/${page.id}")
}
/**
* Activates given page.
*/
fun activate(page: InspectablePage): Single {
return runInspectorCommand("activate/${page.id}")
}
/**
* Fetches protocol version information
*/
fun version() : Single {
return runInspectorCommand("version").map {
mapper.deserialize(it, ProtocolVersion::class.java)
}
}
/**
* Finds opened page by its' url
*/
fun findTab(tabUrl: String) : Single {
return this.openedPages().filter { it.url == tabUrl }.singleOrError()
}
/**
* Run inspector command from URI
*/
internal fun runInspectorCommand(uri : String) : Single {
return Single.fromCallable {
Request.Builder().url("http://$chromeAddress/json/$uri").build()
}.map {
client.newCall(it).execute()
}.flatMap {
if (it.isSuccessful) {
Single.just(it.body()?.string() ?: "")
} else {
Single.error(InspectorCommandFailed(it.body()?.string() ?: ""))
}
}
}
companion object {
/**
* Creates new Inspector instance by connecting to remote chrome debugger.
*/
@JvmStatic
fun connect(chromeAddress: String) : Inspector {
return Inspector(
chromeAddress,
OkHttpClient.Builder().readTimeout(0, TimeUnit.MILLISECONDS).build(),
FrameMapper()
)
}
}
}