|
1 package org.chromium.sdk.internal.tools.v8; |
|
2 |
|
3 import java.util.HashMap; |
|
4 import java.util.Map; |
|
5 import java.util.logging.Level; |
|
6 import java.util.logging.Logger; |
|
7 |
|
8 import org.chromium.sdk.internal.DebugSession; |
|
9 import org.chromium.sdk.internal.protocol.EventNotification; |
|
10 import org.chromium.sdk.internal.protocol.IncomingMessage; |
|
11 import org.chromium.sdk.internal.tools.v8.processor.AfterCompileProcessor; |
|
12 import org.chromium.sdk.internal.tools.v8.processor.BreakpointProcessor; |
|
13 import org.chromium.sdk.internal.tools.v8.processor.V8EventProcessor; |
|
14 |
|
15 public class DefaultResponseHandler { |
|
16 |
|
17 /** The class logger. */ |
|
18 private static final Logger LOGGER = Logger.getLogger(DefaultResponseHandler.class.getName()); |
|
19 |
|
20 /** The breakpoint processor. */ |
|
21 private final BreakpointProcessor bpp; |
|
22 |
|
23 /** The "afterCompile" event processor. */ |
|
24 private final AfterCompileProcessor afterCompileProcessor; |
|
25 |
|
26 public DefaultResponseHandler(DebugSession debugSession) { |
|
27 this.bpp = new BreakpointProcessor(debugSession); |
|
28 this.afterCompileProcessor = new AfterCompileProcessor(debugSession); |
|
29 } |
|
30 |
|
31 public BreakpointProcessor getBreakpointProcessor() { |
|
32 return bpp; |
|
33 } |
|
34 |
|
35 /** |
|
36 * @param type response type ("response" or "event") |
|
37 * @param response from the V8 VM debugger |
|
38 */ |
|
39 public void handleResponseWithHandler(IncomingMessage response) { |
|
40 EventNotification eventResponse = response.asEventNotification(); |
|
41 if (eventResponse == null) { |
|
42 // Currently only events are supported. |
|
43 return; |
|
44 } |
|
45 String commandString = eventResponse.getEvent(); |
|
46 DebuggerCommand command = DebuggerCommand.forString(commandString); |
|
47 if (command == null) { |
|
48 LOGGER.log(Level.WARNING, |
|
49 "Unknown command in V8 debugger reply JSON: {0}", commandString); |
|
50 return; |
|
51 } |
|
52 final ProcessorGetter handlerGetter = command2EventProcessorGetter.get(command); |
|
53 if (handlerGetter == null) { |
|
54 return; |
|
55 } |
|
56 handlerGetter.get(this).messageReceived(eventResponse); |
|
57 } |
|
58 |
|
59 private static abstract class ProcessorGetter { |
|
60 abstract V8EventProcessor get(DefaultResponseHandler instance); |
|
61 } |
|
62 |
|
63 /** |
|
64 * The handlers that should be invoked when certain command responses arrive. |
|
65 */ |
|
66 private static final Map<DebuggerCommand, ProcessorGetter> command2EventProcessorGetter; |
|
67 static { |
|
68 command2EventProcessorGetter = new HashMap<DebuggerCommand, ProcessorGetter>(); |
|
69 ProcessorGetter bppGetter = new ProcessorGetter() { |
|
70 @Override |
|
71 BreakpointProcessor get(DefaultResponseHandler instance) { |
|
72 return instance.bpp; |
|
73 } |
|
74 }; |
|
75 command2EventProcessorGetter.put(DebuggerCommand.BREAK /* event */, bppGetter); |
|
76 command2EventProcessorGetter.put(DebuggerCommand.EXCEPTION /* event */, bppGetter); |
|
77 |
|
78 command2EventProcessorGetter.put(DebuggerCommand.AFTER_COMPILE /* event */, |
|
79 new ProcessorGetter() { |
|
80 @Override |
|
81 AfterCompileProcessor get(DefaultResponseHandler instance) { |
|
82 return instance.afterCompileProcessor; |
|
83 } |
|
84 }); |
|
85 } |
|
86 } |