Skip to content

Commit

Permalink
Merge pull request #8429 from sgtcoolguy/TIMOB-23628-6_0_X
Browse files Browse the repository at this point in the history
[TIMOB-23628] (6_0_X) Android: Unable to debug an application with run-on-main-thread set to true
  • Loading branch information
Gary Mathews committed Sep 27, 2016
2 parents 7727ca1 + 1c90260 commit 6dee06c
Showing 1 changed file with 78 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.LinkedBlockingQueue;

import android.net.LocalSocket;
Expand Down Expand Up @@ -103,6 +104,11 @@ public void start() {

// Tell C++ side to hook up the debug message handler
nativeEnable();

// Immediately break the debugger so we can set up our breakpoints and
// options when we connect, before the app starts running.
// This allows us to hit breakpoints as early as the first line in app.js
nativeDebugBreak();
}

/**
Expand Down Expand Up @@ -193,7 +199,7 @@ public void run() {
private class V8MessageHandler implements Runnable
{
private OutputStream output;
private boolean stop;
private AtomicBoolean stop = new AtomicBoolean(false);

// Dummy message used to stop the sentinel loop if it was waiting on v8Messages.take() while stop() got called.
private static final String STOP_MESSAGE = "STOP_MESSAGE";
Expand All @@ -205,7 +211,7 @@ public V8MessageHandler(Socket socket) throws IOException

public void stop()
{
this.stop = true;
this.stop.set(true);
// put dummy message into queue to unlock on take() below.
JSDebugger.this.v8Messages.add(STOP_MESSAGE);
}
Expand All @@ -214,7 +220,7 @@ public void stop()
public void run()
{
this.sendHandshake();
while (!stop)
while (!stop.get())
{
try
{
Expand All @@ -229,7 +235,6 @@ public void run()
catch (Throwable t)
{
// ignore
t.printStackTrace();
}
}

Expand All @@ -240,7 +245,6 @@ public void run()
catch (IOException e)
{
// ignore
e.printStackTrace();
}
}

Expand All @@ -254,7 +258,6 @@ private void sendHandshake()
catch (IOException e)
{
// FIXME Stop the DebuggerMessageHandler too!
e.printStackTrace();
}
}

Expand Down Expand Up @@ -290,16 +293,10 @@ private void sendMessageToDebugger(String msg)
}
}

private enum State
{
Header, Message
}

private class DebuggerMessageHandler implements Runnable
{
private BufferedReader input;
private Scanner scanner;
private boolean stop;
private AtomicBoolean stop = new AtomicBoolean(false);

public DebuggerMessageHandler(Socket socket) throws IOException
{
Expand All @@ -308,99 +305,94 @@ public DebuggerMessageHandler(Socket socket) throws IOException

public void stop()
{
this.stop = true;
this.scanner.close();
this.stop.set(true);
try
{
this.input.close();
}
catch (IOException e1)
{
// ignore
}
}

public void run()
{
scanner = new Scanner(this.input);
scanner.useDelimiter(LINE_ENDING);

List<String> headers = new ArrayList<String>();
String line;
State state = State.Header;
int messageLength = -1;
String leftOver = null;

try
{
while (!stop && ((line = (leftOver != null) ? leftOver : scanner.nextLine()) != null))
{
switch (state)
{
case Header:
if (line.length() == 0)
{
state = State.Message;
}
else
{
headers.add(line);
if (line.startsWith("Content-Length:"))
{
String strLen = line.substring(15).trim();
messageLength = Integer.parseInt(strLen);
}
while (!stop.get()) {
int length = readHeaders();
if (length == -1) {
break; // assume we hit EOF or got told to stop
}
//Log.w(TAG, "Message length: " + length);

if (leftOver != null)
{
leftOver = null;
}
}
break;

case Message:
if ((-1 < messageLength) && (messageLength <= line.length()))
{
String message = line.substring(0, messageLength);
if (messageLength < line.length())
{
leftOver = line.substring(messageLength);
}

state = State.Header;
headers.clear();

JSDebugger.this.sendMessage(message);
}
else
{
if (leftOver == null)
{
leftOver = line;
}
else
{
leftOver += line;
}
}
String message = readMessage(length);
if (message == null) {
// we return null if told to stop, or reading didn't give us the number of characters we expected
break;
}

// send along the message to the debugger
//Log.w(TAG, "Forwarding Message: " + message);
JSDebugger.this.sendMessage(message);
}
}
catch (NoSuchElementException e)
catch (IOException e)
{
e.printStackTrace();
//e.printStackTrace();

// TODO Stop the V8MessageHandler too!
// try
// {
// responseHandlerCloseable.close();
// }
// catch (IOException e1)
// {
// e1.printStackTrace();
// }
}
finally
{
this.stop = true;
this.stop.set(true);
JSDebugger.this.sendMessage(DISCONNECT_MESSAGE);
try
{
this.input.close();
}
catch (IOException e1)
{
// ignore
}
}
}

scanner.close();
private int readHeaders() throws IOException
{
int messageLength = -1;
String line;
while (!stop.get() && ((line = this.input.readLine()) != null))
{
final int lineLength = line.length();
// empty line means end of headers
if (lineLength == 0)
{
return messageLength;
}
// if it's telling us the message length, record that
if (line.startsWith("Content-Length:"))
{
String strLen = line.substring(15).trim();
messageLength = Integer.parseInt(strLen);
}
// otherwise, ignore the other headers - BUT MAKE SURE TO CONSUME THEM!
}
return messageLength;
}
}

private String readMessage(int length) throws IOException
{
if (stop.get()) {
return null;
}
char[] buf = new char[length];
int result = this.input.read(buf, 0, length);
if (result != length) {
return null;
}
return new String(buf);
}
}
}

0 comments on commit 6dee06c

Please sign in to comment.