|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.bridge |
| 9 | + |
| 10 | +import com.facebook.common.logging.FLog |
| 11 | +import com.facebook.jni.HybridData |
| 12 | +import com.facebook.proguard.annotations.DoNotStrip |
| 13 | +import com.facebook.react.common.ReactConstants |
| 14 | + |
| 15 | +@DoNotStrip |
| 16 | +public class Inspector |
| 17 | +private constructor(@Suppress("NoHungarianNotation") private val mHybridData: HybridData) { |
| 18 | + |
| 19 | + private external fun getPagesNative(): Array<Page> |
| 20 | + |
| 21 | + private external fun connectNative(pageId: Int, remote: RemoteConnection): LocalConnection? |
| 22 | + |
| 23 | + @DoNotStrip |
| 24 | + public class Page |
| 25 | + private constructor(private val id: Int, private val title: String, private val vm: String) { |
| 26 | + public fun getId(): Int = id |
| 27 | + |
| 28 | + public fun getTitle(): String = title |
| 29 | + |
| 30 | + public fun getVM(): String = vm |
| 31 | + |
| 32 | + override fun toString(): String = "Page{id=$id, title='$title'}" |
| 33 | + } |
| 34 | + |
| 35 | + @DoNotStrip |
| 36 | + public interface RemoteConnection { |
| 37 | + @DoNotStrip public fun onMessage(message: String) |
| 38 | + |
| 39 | + @DoNotStrip public fun onDisconnect() |
| 40 | + } |
| 41 | + |
| 42 | + @DoNotStrip |
| 43 | + public class LocalConnection |
| 44 | + private constructor(@Suppress("NoHungarianNotation") private val mHybridData: HybridData) { |
| 45 | + public external fun sendMessage(message: String) |
| 46 | + |
| 47 | + public external fun disconnect() |
| 48 | + } |
| 49 | + |
| 50 | + public companion object { |
| 51 | + init { |
| 52 | + BridgeSoLoader.staticInit() |
| 53 | + } |
| 54 | + |
| 55 | + @JvmStatic |
| 56 | + public fun getPages(): List<Page> { |
| 57 | + return try { |
| 58 | + instance().getPagesNative().toList() |
| 59 | + } catch (e: UnsatisfiedLinkError) { |
| 60 | + FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e) |
| 61 | + emptyList() |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @JvmStatic |
| 66 | + public fun connect(pageId: Int, remote: RemoteConnection): LocalConnection { |
| 67 | + return try { |
| 68 | + instance().connectNative(pageId, remote) |
| 69 | + ?: throw IllegalStateException("Can't open failed connection") |
| 70 | + } catch (e: UnsatisfiedLinkError) { |
| 71 | + FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e) |
| 72 | + throw RuntimeException(e) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @JvmStatic private external fun instance(): Inspector |
| 77 | + } |
| 78 | +} |
0 commit comments