From 25789825fc94ce2f5de0598a1a433959c1fae388 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 30 Jul 2014 21:51:21 +0400 Subject: [PATCH] Refactoring --- .../ChromeRDPContainer.java | 2 +- .../ChromeRDPDebugger.java} | 430 ++++++++++-------- .../chromerdp/ChromeRDPDebuggerEndpoint.java | 76 ++++ .../teavm/chromerdp/ChromeRDPExchange.java | 13 + .../chromerdp/ChromeRDPExchangeConsumer.java | 9 + .../chromerdp/ChromeRDPExchangeListener.java | 11 + .../ChromeRDPServer.java | 21 +- .../RDPBreakpoint.java | 2 +- .../RDPCallFrame.java | 2 +- .../data/CallFrameDTO.java | 2 +- .../data/LocationDTO.java | 2 +- .../data/Message.java | 2 +- .../data/Response.java | 2 +- .../messages/ContinueToLocationCommand.java | 4 +- .../messages/RemoveBreakpointCommand.java | 2 +- .../messages/ScriptParsedNotification.java | 2 +- .../messages/SetBreakpointCommand.java | 4 +- .../messages/SetBreakpointResponse.java | 4 +- .../messages/SuspendedNotification.java | 4 +- .../teavm/chromerpd/ChromeRDPDebugger.java | 149 ------ .../java/org/teavm/debugging/Breakpoint.java | 2 +- .../java/org/teavm/debugging/Debugger.java | 20 +- .../teavm/debugging/JavaScriptDebugger.java | 2 - 23 files changed, 377 insertions(+), 390 deletions(-) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/ChromeRDPContainer.java (96%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd/ChromeRDPDebuggerEndpoint.java => chromerdp/ChromeRDPDebugger.java} (50%) create mode 100644 teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java create mode 100644 teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchange.java create mode 100644 teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeConsumer.java create mode 100644 teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeListener.java rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/ChromeRDPServer.java (86%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/RDPBreakpoint.java (98%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/RDPCallFrame.java (97%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/data/CallFrameDTO.java (97%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/data/LocationDTO.java (97%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/data/Message.java (97%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/data/Response.java (97%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/messages/ContinueToLocationCommand.java (92%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/messages/RemoveBreakpointCommand.java (96%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/messages/ScriptParsedNotification.java (96%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/messages/SetBreakpointCommand.java (92%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/messages/SetBreakpointResponse.java (93%) rename teavm-chrome-rdp/src/main/java/org/teavm/{chromerpd => chromerdp}/messages/SuspendedNotification.java (94%) delete mode 100644 teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebugger.java diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPContainer.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPContainer.java similarity index 96% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPContainer.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPContainer.java index df09d9107..25e59d7b4 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPContainer.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPContainer.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd; +package org.teavm.chromerdp; /** * diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebuggerEndpoint.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebugger.java similarity index 50% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebuggerEndpoint.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebugger.java index 11c7f0848..de52d754f 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebuggerEndpoint.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebugger.java @@ -1,46 +1,24 @@ -/* - * Copyright 2014 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.chromerpd; +package org.teavm.chromerdp; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import javax.websocket.OnClose; -import javax.websocket.OnMessage; -import javax.websocket.OnOpen; -import javax.websocket.Session; -import javax.websocket.server.ServerEndpoint; +import java.util.*; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; -import org.teavm.chromerpd.data.CallFrameDTO; -import org.teavm.chromerpd.data.LocationDTO; -import org.teavm.chromerpd.data.Message; -import org.teavm.chromerpd.data.Response; -import org.teavm.chromerpd.messages.*; -import org.teavm.debugging.JavaScriptBreakpoint; -import org.teavm.debugging.JavaScriptCallFrame; -import org.teavm.debugging.JavaScriptLocation; +import org.teavm.chromerdp.data.CallFrameDTO; +import org.teavm.chromerdp.data.LocationDTO; +import org.teavm.chromerdp.data.Message; +import org.teavm.chromerdp.data.Response; +import org.teavm.chromerdp.messages.*; +import org.teavm.debugging.*; /** * - * @author Alexey Andreev + * @author Alexey Andreev */ -@ServerEndpoint("/") -public class ChromeRDPDebuggerEndpoint { - private Session session; +public class ChromeRDPDebugger implements JavaScriptDebugger, ChromeRDPExchangeConsumer { + private ChromeRDPExchange exchange; + private List listeners = new ArrayList<>(); + private Set breakpoints = new HashSet<>(); private RDPCallFrame[] callStack = new RDPCallFrame[0]; private Map scripts = new HashMap<>(); private Map scriptIds = new HashMap<>(); @@ -48,53 +26,57 @@ public class ChromeRDPDebuggerEndpoint { private ObjectMapper mapper = new ObjectMapper(); private Map responseHandlers = new HashMap<>(); private int messageIdGenerator; - boolean closed; - private ChromeRDPDebugger debugger; - @OnOpen - public void open(Session session) { - this.session = session; - Object debugger = session.getUserProperties().get("chrome.rdp"); - if (debugger instanceof ChromeRDPDebugger) { - this.debugger = (ChromeRDPDebugger)debugger; - this.debugger.setEndpoint(this); + @Override + public void setExchange(ChromeRDPExchange exchange) { + if (this.exchange == exchange) { + return; } - } - - @OnClose - public void close() { - closed = true; - if (this.debugger != null) { - this.debugger.setEndpoint(null); - this.debugger = null; + if (this.exchange != null) { + this.exchange.removeListener(exchangeListener); } - } - - @OnMessage - public void receive(String messageText) throws IOException { - JsonNode jsonMessage = mapper.readTree(messageText); - if (jsonMessage.has("result")) { - Response response = mapper.reader(Response.class).readValue(jsonMessage); - responseHandlers.remove(response.getId()).received(response.getResult()); + this.exchange = exchange; + if (exchange != null) { + for (RDPBreakpoint breakpoint : breakpoints) { + updateBreakpoint(breakpoint); + } + for (JavaScriptDebuggerListener listener : listeners) { + listener.attached(); + } } else { - Message message = mapper.reader(Message.class).readValue(messageText); - switch (message.getMethod()) { - case "Debugger.paused": - firePaused(parseJson(SuspendedNotification.class, message.getParams())); - break; - case "Debugger.resumed": - fireResumed(); - break; - case "Debugger.scriptParsed": - scriptParsed(parseJson(ScriptParsedNotification.class, message.getParams())); - break; + suspended = false; + for (JavaScriptDebuggerListener listener : listeners) { + listener.detached(); } } + if (this.exchange != null) { + this.exchange.addListener(exchangeListener); + } } - private T parseJson(Class type, JsonNode node) throws IOException { - return mapper.reader(type).readValue(node); - } + private ChromeRDPExchangeListener exchangeListener = new ChromeRDPExchangeListener() { + @Override public void received(String messageText) throws IOException { + JsonNode jsonMessage = mapper.readTree(messageText); + if (jsonMessage.has("result")) { + Response response = mapper.reader(Response.class).readValue(jsonMessage); + responseHandlers.remove(response.getId()).received(response.getResult()); + } else { + Message message = mapper.reader(Message.class).readValue(messageText); + switch (message.getMethod()) { + case "Debugger.paused": + firePaused(parseJson(SuspendedNotification.class, message.getParams())); + break; + case "Debugger.resumed": + fireResumed(); + break; + case "Debugger.scriptParsed": + scriptParsed(parseJson(ScriptParsedNotification.class, message.getParams())); + break; + } + } + } + }; + private synchronized void firePaused(SuspendedNotification params) { suspended = true; @@ -104,7 +86,183 @@ public class ChromeRDPDebuggerEndpoint { callStack[i] = map(callFrameDTOs[i]); } this.callStack = callStack; - debugger.firePaused(); + for (JavaScriptDebuggerListener listener : listeners) { + listener.paused(); + } + } + + private synchronized void fireResumed() { + suspended = false; + callStack = null; + for (JavaScriptDebuggerListener listener : listeners) { + listener.resumed(); + } + } + + private synchronized void scriptParsed(ScriptParsedNotification params) { + if (scripts.containsKey(params.getScriptId())) { + return; + } + scripts.put(params.getScriptId(), params.getUrl()); + scriptIds.put(params.getUrl(), params.getScriptId()); + for (JavaScriptDebuggerListener listener : listeners) { + listener.scriptAdded(params.getUrl()); + } + } + + + @Override + public void addListener(JavaScriptDebuggerListener listener) { + listeners.add(listener); + } + + @Override + public void removeListener(JavaScriptDebuggerListener listener) { + listeners.remove(listener); + } + + @Override + public void suspend() { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setMethod("Debugger.pause"); + sendMessage(message); + } + + @Override + public void resume() { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setMethod("Debugger.resume"); + sendMessage(message); + } + + @Override + public void stepInto() { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setMethod("Debugger.stepInto"); + sendMessage(message); + } + + @Override + public void stepOut() { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setMethod("Debugger.stepOut"); + sendMessage(message); + } + + @Override + public void stepOver() { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setMethod("Debugger.stepOver"); + sendMessage(message); + } + + @Override + public void continueToLocation(JavaScriptLocation location) { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setMethod("Debugger.continueToLocation"); + ContinueToLocationCommand params = new ContinueToLocationCommand(); + params.setLocation(unmap(location)); + message.setParams(mapper.valueToTree(params)); + sendMessage(message); + } + + @Override + public boolean isSuspended() { + return exchange != null && suspended; + } + + @Override + public boolean isAttached() { + return exchange != null; + } + + @Override + public JavaScriptCallFrame[] getCallStack() { + if (exchange == null) { + return null; + } + return callStack != null ? callStack.clone() : null; + } + + @Override + public JavaScriptBreakpoint createBreakpoint(JavaScriptLocation location) { + RDPBreakpoint breakpoint = new RDPBreakpoint(this, location); + breakpoints.add(breakpoint); + updateBreakpoint(breakpoint); + return breakpoint; + } + + void destroyBreakpoint(RDPBreakpoint breakpoint) { + if (breakpoint.chromeId != null) { + Message message = new Message(); + message.setMethod("Debugger.removeBreakpoint"); + RemoveBreakpointCommand params = new RemoveBreakpointCommand(); + params.setBreakpointId(breakpoint.chromeId); + message.setParams(mapper.valueToTree(params)); + sendMessage(message); + } + } + + void fireScriptAdded(String script) { + for (JavaScriptDebuggerListener listener : listeners) { + listener.scriptAdded(script); + } + } + + void updateBreakpoint(final RDPBreakpoint breakpoint) { + if (exchange == null) { + return; + } + Message message = new Message(); + message.setId(++messageIdGenerator); + message.setMethod("Debugger.setBreakpoint"); + SetBreakpointCommand params = new SetBreakpointCommand(); + params.setLocation(unmap(breakpoint.getLocation())); + message.setParams(mapper.valueToTree(params)); + ResponseHandler handler = new ResponseHandler() { + @Override public void received(JsonNode node) throws IOException { + SetBreakpointResponse response = mapper.reader(SetBreakpointResponse.class).readValue(node); + breakpoint.chromeId = response.getBreakpointId(); + for (JavaScriptDebuggerListener listener : listeners) { + listener.breakpointChanged(breakpoint); + } + } + }; + responseHandlers.put(message.getId(), handler); + sendMessage(message); + } + + private T parseJson(Class type, JsonNode node) throws IOException { + return mapper.reader(type).readValue(node); + } + + private void sendMessage(Message message) { + if (exchange == null) { + return; + } + try { + exchange.send(mapper.writer().writeValueAsString(message)); + } catch (IOException e) { + throw new RuntimeException(e); + } } RDPCallFrame map(CallFrameDTO dto) { @@ -123,128 +281,6 @@ public class ChromeRDPDebuggerEndpoint { return dto; } - private synchronized void fireResumed() { - suspended = false; - callStack = null; - debugger.fireResumed(); - } - - private synchronized void scriptParsed(ScriptParsedNotification params) { - if (scripts.containsKey(params.getScriptId())) { - return; - } - scripts.put(params.getScriptId(), params.getUrl()); - scriptIds.put(params.getUrl(), params.getScriptId()); - debugger.fireScriptAdded(params.getUrl()); - } - - private void sendMessage(Message message) { - if (closed) { - return; - } - try { - String messageText = mapper.writer().writeValueAsString(message); - session.getAsyncRemote().sendText(messageText); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public void suspend() { - if (closed) { - return; - } - Message message = new Message(); - message.setMethod("Debugger.pause"); - sendMessage(message); - } - - public void resume() { - if (closed) { - return; - } - Message message = new Message(); - message.setMethod("Debugger.resume"); - sendMessage(message); - } - - public void stepInto() { - if (closed) { - return; - } - Message message = new Message(); - message.setMethod("Debugger.stepInto"); - sendMessage(message); - } - - public void stepOut() { - if (closed) { - return; - } - Message message = new Message(); - message.setMethod("Debugger.stepOut"); - sendMessage(message); - } - - public void stepOver() { - if (closed) { - return; - } - Message message = new Message(); - message.setMethod("Debugger.stepOver"); - sendMessage(message); - } - - public synchronized void continueToLocation(JavaScriptLocation location) { - Message message = new Message(); - message.setMethod("Debugger.continueToLocation"); - ContinueToLocationCommand params = new ContinueToLocationCommand(); - params.setLocation(unmap(location)); - message.setParams(mapper.valueToTree(params)); - sendMessage(message); - } - - public synchronized boolean isSuspended() { - return suspended; - } - - public synchronized JavaScriptCallFrame[] getCallStack() { - return callStack; - } - - public synchronized JavaScriptBreakpoint getCurrentBreakpoint() { - return null; - } - - public synchronized void updateBreakpoint(final RDPBreakpoint breakpoint) { - Message message = new Message(); - message.setId(++messageIdGenerator); - message.setMethod("Debugger.setBreakpoint"); - SetBreakpointCommand params = new SetBreakpointCommand(); - params.setLocation(unmap(breakpoint.getLocation())); - message.setParams(mapper.valueToTree(params)); - ResponseHandler handler = new ResponseHandler() { - @Override public void received(JsonNode node) throws IOException { - SetBreakpointResponse response = mapper.reader(SetBreakpointResponse.class).readValue(node); - breakpoint.chromeId = response.getBreakpointId(); - debugger.fireBreakpointStatusChanged(breakpoint); - } - }; - responseHandlers.put(message.getId(), handler); - sendMessage(message); - } - - void destroyBreakpoint(RDPBreakpoint breakpoint) { - if (breakpoint.chromeId != null) { - Message message = new Message(); - message.setMethod("Debugger.removeBreakpoint"); - RemoveBreakpointCommand params = new RemoveBreakpointCommand(); - params.setBreakpointId(breakpoint.chromeId); - message.setParams(mapper.valueToTree(params)); - sendMessage(message); - } - } - interface ResponseHandler { void received(JsonNode node) throws IOException; } diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java new file mode 100644 index 000000000..be6636877 --- /dev/null +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java @@ -0,0 +1,76 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.chromerdp; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import javax.websocket.OnClose; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +/** + * + * @author Alexey Andreev + */ +@ServerEndpoint("/") +public class ChromeRDPDebuggerEndpoint implements ChromeRDPExchange { + private Session session; + private ChromeRDPExchangeConsumer debugger; + private List listeners = new ArrayList<>(); + + @OnOpen + public void open(Session session) { + this.session = session; + Object debugger = session.getUserProperties().get("chrome.rdp"); + if (debugger instanceof ChromeRDPExchangeConsumer) { + this.debugger = (ChromeRDPExchangeConsumer)debugger; + this.debugger.setExchange(this); + } + } + + @OnClose + public void close() { + if (this.debugger != null) { + this.debugger.setExchange(null); + this.debugger = null; + } + } + + @OnMessage + public void receive(String message) throws IOException { + for (ChromeRDPExchangeListener listener : listeners) { + listener.received(message); + } + } + + @Override + public void send(String message) { + session.getAsyncRemote().sendText(message); + } + + @Override + public void addListener(ChromeRDPExchangeListener listener) { + listeners.add(listener); + } + + @Override + public void removeListener(ChromeRDPExchangeListener listener) { + listeners.remove(listener); + } +} diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchange.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchange.java new file mode 100644 index 000000000..d3a8404bc --- /dev/null +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchange.java @@ -0,0 +1,13 @@ +package org.teavm.chromerdp; + +/** + * + * @author Alexey Andreev + */ +public interface ChromeRDPExchange { + void send(String message); + + void addListener(ChromeRDPExchangeListener listener); + + void removeListener(ChromeRDPExchangeListener listener); +} diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeConsumer.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeConsumer.java new file mode 100644 index 000000000..43d571175 --- /dev/null +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeConsumer.java @@ -0,0 +1,9 @@ +package org.teavm.chromerdp; + +/** + * + * @author Alexey Andreev + */ +public interface ChromeRDPExchangeConsumer { + void setExchange(ChromeRDPExchange exchange); +} diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeListener.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeListener.java new file mode 100644 index 000000000..d3e79b65b --- /dev/null +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPExchangeListener.java @@ -0,0 +1,11 @@ +package org.teavm.chromerdp; + +import java.io.IOException; + +/** + * + * @author Alexey Andreev + */ +public interface ChromeRDPExchangeListener { + void received(String message) throws IOException; +} diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPServer.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPServer.java similarity index 86% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPServer.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPServer.java index 8b312abc3..f368db5a5 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPServer.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPServer.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd; +package org.teavm.chromerdp; import java.util.*; import javax.websocket.Decoder; @@ -25,7 +25,6 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; -import org.teavm.debugging.JavaScriptDebugger; /** * @@ -33,8 +32,7 @@ import org.teavm.debugging.JavaScriptDebugger; */ public class ChromeRDPServer { private int port = 2357; - private Appendable output = System.err; - private ChromeRDPDebugger debugger = new ChromeRDPDebugger(); + private ChromeRDPExchangeConsumer exchangeConsumer; public int getPort() { return port; @@ -44,12 +42,12 @@ public class ChromeRDPServer { this.port = port; } - public Appendable getOutput() { - return output; + public ChromeRDPExchangeConsumer getExchangeConsumer() { + return exchangeConsumer; } - public void setOutput(Appendable output) { - this.output = output; + public void setExchangeConsumer(ChromeRDPExchangeConsumer exchangeConsumer) { + this.exchangeConsumer = exchangeConsumer; } public void start() { @@ -67,7 +65,6 @@ public class ChromeRDPServer { try { wscontainer.addEndpoint(new RPDEndpointConfig()); server.start(); - server.dump(output); server.join(); } catch (Exception e) { throw new RuntimeException(e); @@ -78,7 +75,7 @@ public class ChromeRDPServer { private Map userProperties = new HashMap<>(); public RPDEndpointConfig() { - userProperties.put("chrome.rdp", debugger); + userProperties.put("chrome.rdp", exchangeConsumer); } @Override @@ -121,8 +118,4 @@ public class ChromeRDPServer { return Collections.emptyList(); } } - - public JavaScriptDebugger getDebugger() { - return debugger; - } } diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/RDPBreakpoint.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/RDPBreakpoint.java similarity index 98% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/RDPBreakpoint.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/RDPBreakpoint.java index bd9670eff..1333992ee 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/RDPBreakpoint.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/RDPBreakpoint.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd; +package org.teavm.chromerdp; import org.teavm.debugging.JavaScriptBreakpoint; import org.teavm.debugging.JavaScriptLocation; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/RDPCallFrame.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/RDPCallFrame.java similarity index 97% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/RDPCallFrame.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/RDPCallFrame.java index 32cf1f7a1..d89ef718d 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/RDPCallFrame.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/RDPCallFrame.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd; +package org.teavm.chromerdp; import org.teavm.debugging.JavaScriptCallFrame; import org.teavm.debugging.JavaScriptLocation; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/CallFrameDTO.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/CallFrameDTO.java similarity index 97% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/CallFrameDTO.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/CallFrameDTO.java index 4c57ef0f8..584a50c99 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/CallFrameDTO.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/CallFrameDTO.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.data; +package org.teavm.chromerdp.data; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/LocationDTO.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/LocationDTO.java similarity index 97% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/LocationDTO.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/LocationDTO.java index 13a17bf83..4a4d601e5 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/LocationDTO.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/LocationDTO.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.data; +package org.teavm.chromerdp.data; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/Message.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/Message.java similarity index 97% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/Message.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/Message.java index 0a4fec1c1..a47a1977d 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/Message.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/Message.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.data; +package org.teavm.chromerdp.data; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/Response.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/Response.java similarity index 97% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/Response.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/Response.java index 9699f9b30..682259fa0 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/data/Response.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/data/Response.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.data; +package org.teavm.chromerdp.data; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/ContinueToLocationCommand.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/ContinueToLocationCommand.java similarity index 92% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/ContinueToLocationCommand.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/ContinueToLocationCommand.java index 52c1241f1..3c54a65c7 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/ContinueToLocationCommand.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/ContinueToLocationCommand.java @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.messages; +package org.teavm.chromerdp.messages; import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.teavm.chromerpd.data.LocationDTO; +import org.teavm.chromerdp.data.LocationDTO; /** * diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/RemoveBreakpointCommand.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/RemoveBreakpointCommand.java similarity index 96% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/RemoveBreakpointCommand.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/RemoveBreakpointCommand.java index ba109fc2d..ca2432850 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/RemoveBreakpointCommand.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/RemoveBreakpointCommand.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.messages; +package org.teavm.chromerdp.messages; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/ScriptParsedNotification.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/ScriptParsedNotification.java similarity index 96% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/ScriptParsedNotification.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/ScriptParsedNotification.java index 808447a1e..a7fb115b2 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/ScriptParsedNotification.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/ScriptParsedNotification.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.messages; +package org.teavm.chromerdp.messages; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SetBreakpointCommand.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SetBreakpointCommand.java similarity index 92% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SetBreakpointCommand.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SetBreakpointCommand.java index 1dd971032..3cdcab541 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SetBreakpointCommand.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SetBreakpointCommand.java @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.messages; +package org.teavm.chromerdp.messages; import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.teavm.chromerpd.data.LocationDTO; +import org.teavm.chromerdp.data.LocationDTO; /** * diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SetBreakpointResponse.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SetBreakpointResponse.java similarity index 93% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SetBreakpointResponse.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SetBreakpointResponse.java index ea355bd9a..5a11377d5 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SetBreakpointResponse.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SetBreakpointResponse.java @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.messages; +package org.teavm.chromerdp.messages; import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.teavm.chromerpd.data.LocationDTO; +import org.teavm.chromerdp.data.LocationDTO; /** * diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SuspendedNotification.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SuspendedNotification.java similarity index 94% rename from teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SuspendedNotification.java rename to teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SuspendedNotification.java index 8f86739b6..b51028a17 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/messages/SuspendedNotification.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/messages/SuspendedNotification.java @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.chromerpd.messages; +package org.teavm.chromerdp.messages; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.teavm.chromerpd.data.CallFrameDTO; +import org.teavm.chromerdp.data.CallFrameDTO; /** * diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebugger.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebugger.java deleted file mode 100644 index 47f274812..000000000 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerpd/ChromeRDPDebugger.java +++ /dev/null @@ -1,149 +0,0 @@ -package org.teavm.chromerpd; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.teavm.debugging.*; - -/** - * - * @author Alexey Andreev - */ -public class ChromeRDPDebugger implements JavaScriptDebugger { - private ChromeRDPDebuggerEndpoint endpoint; - private List listeners = new ArrayList<>(); - private Set breakpoints = new HashSet<>(); - - void setEndpoint(ChromeRDPDebuggerEndpoint endpoint) { - if (this.endpoint == endpoint) { - return; - } - this.endpoint = endpoint; - if (endpoint != null) { - for (RDPBreakpoint breakpoint : breakpoints) { - endpoint.updateBreakpoint(breakpoint); - } - for (JavaScriptDebuggerListener listener : listeners) { - listener.attached(); - } - } else { - for (JavaScriptDebuggerListener listener : listeners) { - listener.detached(); - } - } - } - - @Override - public void addListener(JavaScriptDebuggerListener listener) { - listeners.add(listener); - } - - @Override - public void removeListener(JavaScriptDebuggerListener listener) { - listeners.remove(listener); - } - - @Override - public void suspend() { - if (endpoint != null) { - endpoint.suspend(); - } - } - - @Override - public void resume() { - if (endpoint != null) { - endpoint.resume(); - } - } - - @Override - public void stepInto() { - if (endpoint != null) { - endpoint.stepInto(); - } - } - - @Override - public void stepOut() { - if (endpoint != null) { - endpoint.stepOut(); - } - } - - @Override - public void stepOver() { - if (endpoint != null) { - endpoint.stepOver(); - } - } - - @Override - public void continueToLocation(JavaScriptLocation location) { - if (endpoint != null) { - endpoint.continueToLocation(location); - } - } - - @Override - public boolean isSuspended() { - return endpoint != null && endpoint.isSuspended(); - } - - @Override - public boolean isAttached() { - return endpoint != null; - } - - @Override - public JavaScriptCallFrame[] getCallStack() { - return endpoint != null ? endpoint.getCallStack() : null; - } - - @Override - public JavaScriptBreakpoint getCurrentBreakpoint() { - return endpoint != null ? endpoint.getCurrentBreakpoint() : null; - } - - @Override - public synchronized JavaScriptBreakpoint createBreakpoint(JavaScriptLocation location) { - RDPBreakpoint breakpoint = new RDPBreakpoint(this, location); - breakpoints.add(breakpoint); - if (endpoint != null) { - endpoint.updateBreakpoint(breakpoint); - } - return breakpoint; - } - - synchronized void destroyBreakpoint(RDPBreakpoint breakpoint) { - breakpoints.remove(breakpoint); - if (endpoint != null) { - endpoint.destroyBreakpoint(breakpoint); - } - } - - void fireResumed() { - for (JavaScriptDebuggerListener listener : listeners) { - listener.resumed(); - } - } - - void firePaused() { - for (JavaScriptDebuggerListener listener : listeners) { - listener.paused(); - } - } - - void fireScriptAdded(String script) { - for (JavaScriptDebuggerListener listener : listeners) { - listener.scriptAdded(script); - } - } - - void fireBreakpointStatusChanged(RDPBreakpoint breakpoint) { - for (JavaScriptDebuggerListener listener : listeners) { - listener.breakpointChanged(breakpoint); - } - } -} diff --git a/teavm-core/src/main/java/org/teavm/debugging/Breakpoint.java b/teavm-core/src/main/java/org/teavm/debugging/Breakpoint.java index 30120d81c..649c3b211 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/Breakpoint.java +++ b/teavm-core/src/main/java/org/teavm/debugging/Breakpoint.java @@ -37,7 +37,7 @@ public class Breakpoint { return location; } - public synchronized void destroy() { + public void destroy() { debugger.destroyBreakpoint(this); debugger = null; } diff --git a/teavm-core/src/main/java/org/teavm/debugging/Debugger.java b/teavm-core/src/main/java/org/teavm/debugging/Debugger.java index 1b03e2d77..5aac9ac0e 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/Debugger.java +++ b/teavm-core/src/main/java/org/teavm/debugging/Debugger.java @@ -79,7 +79,7 @@ public class Debugger { continueToLocation(location.getFileName(), location.getLine()); } - public synchronized void continueToLocation(String fileName, int line) { + public void continueToLocation(String fileName, int line) { if (!javaScriptDebugger.isSuspended()) { return; } @@ -105,7 +105,7 @@ public class Debugger { return createBreakpoint(new SourceLocation(file, line)); } - public synchronized Breakpoint createBreakpoint(SourceLocation location) { + public Breakpoint createBreakpoint(SourceLocation location) { Breakpoint breakpoint = new Breakpoint(this, location); breakpoints.add(breakpoint); updateInternalBreakpoints(breakpoint); @@ -113,7 +113,7 @@ public class Debugger { return breakpoint; } - public synchronized Set getBreakpoints() { + public Set getBreakpoints() { return new HashSet<>(breakpoints); } @@ -153,7 +153,7 @@ public class Debugger { } } - public synchronized CallFrame[] getCallStack() { + public CallFrame[] getCallStack() { if (!isSuspended()) { return null; } @@ -184,7 +184,7 @@ public class Debugger { return callStack.clone(); } - private synchronized void addScript(String name) { + private void addScript(String name) { if (debugInformationMap.containsKey(name)) { return; } @@ -212,7 +212,7 @@ public class Debugger { return javaScriptDebugger.isAttached(); } - synchronized void destroyBreakpoint(Breakpoint breakpoint) { + void destroyBreakpoint(Breakpoint breakpoint) { for (JavaScriptBreakpoint jsBreakpoint : breakpoint.jsBreakpoints) { jsBreakpoint.destroy(); breakpointMap.remove(jsBreakpoint); @@ -221,7 +221,7 @@ public class Debugger { breakpoints.remove(this); } - private synchronized void fireResumed() { + private void fireResumed() { for (JavaScriptBreakpoint jsBreakpoint : temporaryJsBreakpoints) { jsBreakpoint.destroy(); } @@ -231,7 +231,7 @@ public class Debugger { } } - private synchronized void fireAttached() { + private void fireAttached() { for (Breakpoint breakpoint : breakpoints) { updateInternalBreakpoints(breakpoint); updateBreakpointStatus(breakpoint, false); @@ -241,7 +241,7 @@ public class Debugger { } } - private synchronized void fireDetached() { + private void fireDetached() { for (Breakpoint breakpoint : breakpoints) { updateBreakpointStatus(breakpoint, false); } @@ -250,7 +250,7 @@ public class Debugger { } } - private synchronized void fireBreakpointChanged(JavaScriptBreakpoint jsBreakpoint) { + private void fireBreakpointChanged(JavaScriptBreakpoint jsBreakpoint) { Breakpoint breakpoint = breakpointMap.get(jsBreakpoint); if (breakpoint != null) { updateBreakpointStatus(breakpoint, true); diff --git a/teavm-core/src/main/java/org/teavm/debugging/JavaScriptDebugger.java b/teavm-core/src/main/java/org/teavm/debugging/JavaScriptDebugger.java index a5ae97b65..42fcdec38 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/JavaScriptDebugger.java +++ b/teavm-core/src/main/java/org/teavm/debugging/JavaScriptDebugger.java @@ -42,7 +42,5 @@ public interface JavaScriptDebugger { JavaScriptCallFrame[] getCallStack(); - JavaScriptBreakpoint getCurrentBreakpoint(); - JavaScriptBreakpoint createBreakpoint(JavaScriptLocation location); }