1+ package com .tns ;
2+
3+ import android .content .Context ;
4+ import android .os .Handler ;
5+ import android .util .Log ;
6+
7+ import java .io .IOException ;
8+ import java .util .concurrent .ConcurrentLinkedQueue ;
9+ import java .util .concurrent .LinkedBlockingQueue ;
10+ import java .util .concurrent .atomic .AtomicBoolean ;
11+
12+ import fi .iki .elonen .NanoHTTPD ;
13+ import fi .iki .elonen .NanoWSD ;
14+
15+ public class AndroidJsV8Inspector
16+ {
17+ public static final String DISCONNECT_MESSAGE = "nativescript-inspector-disconnect" ;
18+ private JsV8InspectorServer server ;
19+ private Logger logger ;
20+ private Context context ;
21+
22+ protected native final void init ();
23+
24+ protected native final void connect (Object connection );
25+
26+ protected static native final void disconnect ();
27+
28+ protected native final void dispatchMessage (String message );
29+ protected Handler mainHandler ;
30+ private LinkedBlockingQueue <String > inspectorMessages = new LinkedBlockingQueue <String >();
31+
32+ public AndroidJsV8Inspector (Context context , Logger logger )
33+ {
34+ this .context = context ;
35+ this .logger = logger ;
36+ }
37+
38+ public void start () throws IOException
39+ {
40+ if (this .server == null )
41+ {
42+ Runtime currentRuntime = Runtime .getCurrentRuntime ();
43+
44+ mainHandler = currentRuntime .getHandler ();
45+
46+ this .server = new JsV8InspectorServer (context .getPackageName () + "-inspectorServer" );
47+ this .server .start (-1 );
48+
49+ //Log.d("V8Inspector", "init ThreadId:" + Thread.currentThread().getId());
50+
51+ init ();
52+ }
53+ }
54+
55+ private static void send (Object connection , String payload ) throws IOException
56+ {
57+ ((JsV8InspectorWebSocket ) connection ).send (payload );
58+ }
59+
60+ private static String getInspectorMessage (Object connection )
61+ {
62+ return ((JsV8InspectorWebSocket ) connection ).getInspectorMessage ();
63+ }
64+
65+ class JsV8InspectorServer extends NanoWSD
66+ {
67+ public JsV8InspectorServer (String name )
68+ {
69+ super (name );
70+ }
71+
72+ @ Override
73+ protected Response serveHttp (IHTTPSession session )
74+ {
75+ //Log.d("{N}.v8-inspector", "http request for " + session.getUri());
76+ return super .serveHttp (session );
77+ }
78+
79+ @ Override
80+ protected WebSocket openWebSocket (IHTTPSession handshake )
81+ {
82+ return new JsV8InspectorWebSocket (handshake );
83+ }
84+ }
85+
86+ class JsV8InspectorWebSocket extends NanoWSD .WebSocket
87+ {
88+
89+ public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest )
90+ {
91+ super (handshakeRequest );
92+ }
93+
94+ @ Override
95+ protected void onOpen ()
96+ {
97+ //Log.d("V8Inspector", "onOpen: ThreadID: " + Thread.currentThread().getId());
98+
99+ final Object waitObject = new Object ();
100+
101+ mainHandler .post (new Runnable ()
102+ {
103+ @ Override
104+ public void run ()
105+ {
106+ try
107+ {
108+ //Log.d("V8Inspector", "onOpen: runnable ThreadID : " + Thread.currentThread().getId());
109+ connect (JsV8InspectorWebSocket .this );
110+ }
111+ finally
112+ {
113+ synchronized (waitObject )
114+ {
115+ waitObject .notify ();
116+ }
117+ }
118+ }
119+ });
120+
121+ try
122+ {
123+ synchronized (waitObject )
124+ {
125+ waitObject .wait ();
126+ }
127+ }
128+ catch (InterruptedException e )
129+ {
130+ e .printStackTrace ();
131+ }
132+ }
133+
134+ @ Override
135+ protected void onClose (NanoWSD .WebSocketFrame .CloseCode code , String reason , boolean initiatedByRemote )
136+ {
137+ //Log.d("V8Inspector", "onClose");
138+ mainHandler .post (new Runnable ()
139+ {
140+ @ Override
141+ public void run ()
142+ {
143+ disconnect ();
144+ }
145+ });
146+
147+ inspectorMessages .offer (DISCONNECT_MESSAGE );
148+ }
149+
150+ @ Override
151+ protected void onMessage (final NanoWSD .WebSocketFrame message )
152+ {
153+ //Log.d("V8Inspector", "onMessage");
154+ //Log.d("V8Inspector", "onMessage TextPayload" + message.getTextPayload() + " ThreadId:" + Thread.currentThread().getId());
155+ inspectorMessages .offer (message .getTextPayload ());
156+
157+ mainHandler .post (new Runnable ()
158+ {
159+ @ Override
160+ public void run ()
161+ {
162+ String nextMessage = inspectorMessages .poll ();
163+ while (nextMessage != null )
164+ {
165+ dispatchMessage (nextMessage );
166+ nextMessage = inspectorMessages .poll ();
167+ }
168+ }
169+ });
170+ }
171+
172+ @ Override
173+ public void send (String payload ) throws IOException
174+ {
175+ //Log.d("V8Inspector", "send " + payload);
176+ super .send (payload );
177+ }
178+
179+ public String getInspectorMessage ()
180+ {
181+ try
182+ {
183+ String message = inspectorMessages .take ();
184+
185+ if (message != null && message .equalsIgnoreCase (DISCONNECT_MESSAGE ))
186+ {
187+ disconnect ();
188+ return null ;
189+ }
190+
191+ return message ;
192+ }
193+ catch (InterruptedException e )
194+ {
195+ e .printStackTrace ();
196+ }
197+
198+ return null ;
199+ }
200+
201+
202+ @ Override
203+ protected void onPong (NanoWSD .WebSocketFrame pong )
204+ {
205+ //Log.d("V8Inspector", "onPong");
206+ }
207+
208+ @ Override
209+ protected void onException (IOException exception )
210+ {
211+ exception .printStackTrace ();
212+ disconnect ();
213+ }
214+ }
215+ }
0 commit comments