|
1 package org.chromium.sdk.internal.tools.v8; |
|
2 |
|
3 import java.util.HashMap; |
|
4 import java.util.Map; |
|
5 |
|
6 import org.chromium.sdk.Breakpoint; |
|
7 import org.chromium.sdk.BrowserTab; |
|
8 import org.chromium.sdk.JavascriptVm.BreakpointCallback; |
|
9 import org.chromium.sdk.internal.DebugSession; |
|
10 import org.chromium.sdk.internal.protocol.BreakpointBody; |
|
11 import org.chromium.sdk.internal.protocol.CommandResponse; |
|
12 import org.chromium.sdk.internal.protocol.SuccessCommandResponse; |
|
13 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException; |
|
14 import org.chromium.sdk.internal.tools.v8.request.DebuggerMessageFactory; |
|
15 |
|
16 public class BreakpointManager { |
|
17 /** |
|
18 * This map shall contain only breakpoints with valid IDs. |
|
19 */ |
|
20 private final Map<Long, Breakpoint> idToBreakpoint = new HashMap<Long, Breakpoint>(); |
|
21 |
|
22 private final DebugSession debugSession; |
|
23 |
|
24 public BreakpointManager(DebugSession debugSession) { |
|
25 this.debugSession = debugSession; |
|
26 } |
|
27 |
|
28 public void setBreakpoint(final Breakpoint.Type type, String target, int line, int position, |
|
29 final boolean enabled, final String condition, final int ignoreCount, |
|
30 final BrowserTab.BreakpointCallback callback) { |
|
31 debugSession.sendMessageAsync( |
|
32 DebuggerMessageFactory.setBreakpoint(type, target, toNullableInteger(line), |
|
33 toNullableInteger(position), enabled, condition, |
|
34 toNullableInteger(ignoreCount)), |
|
35 true, |
|
36 callback == null |
|
37 ? null |
|
38 : new V8CommandProcessor.V8HandlerCallback() { |
|
39 public void messageReceived(CommandResponse response) { |
|
40 SuccessCommandResponse successResponse = response.asSuccess(); |
|
41 if (successResponse != null) { |
|
42 BreakpointBody body; |
|
43 try { |
|
44 body = successResponse.getBody().asBreakpointBody(); |
|
45 } catch (JsonProtocolParseException e) { |
|
46 throw new RuntimeException(e); |
|
47 } |
|
48 long id = body.getBreakpoint(); |
|
49 |
|
50 final BreakpointImpl breakpoint = |
|
51 new BreakpointImpl(type, id, enabled, ignoreCount, |
|
52 condition, BreakpointManager.this); |
|
53 |
|
54 callback.success(breakpoint); |
|
55 idToBreakpoint.put(breakpoint.getId(), breakpoint); |
|
56 } else { |
|
57 callback.failure(response.asFailure().getMessage()); |
|
58 } |
|
59 } |
|
60 public void failure(String message) { |
|
61 if (callback != null) { |
|
62 callback.failure(message); |
|
63 } |
|
64 } |
|
65 }, |
|
66 null); |
|
67 } |
|
68 |
|
69 public Breakpoint getBreakpoint(Long id) { |
|
70 return idToBreakpoint.get(id); |
|
71 } |
|
72 |
|
73 public void clearBreakpoint( |
|
74 final BreakpointImpl breakpointImpl, final BreakpointCallback callback) { |
|
75 long id = breakpointImpl.getId(); |
|
76 if (id == Breakpoint.INVALID_ID) { |
|
77 return; |
|
78 } |
|
79 idToBreakpoint.remove(id); |
|
80 debugSession.sendMessageAsync( |
|
81 DebuggerMessageFactory.clearBreakpoint(breakpointImpl), |
|
82 true, |
|
83 new V8CommandProcessor.V8HandlerCallback() { |
|
84 public void messageReceived(CommandResponse response) { |
|
85 SuccessCommandResponse successResponse = response.asSuccess(); |
|
86 if (successResponse != null) { |
|
87 if (callback != null) { |
|
88 callback.success(null); |
|
89 } |
|
90 } else { |
|
91 if (callback != null) { |
|
92 callback.failure(response.asFailure().getMessage()); |
|
93 } |
|
94 } |
|
95 } |
|
96 public void failure(String message) { |
|
97 if (callback != null) { |
|
98 callback.failure(message); |
|
99 } |
|
100 } |
|
101 }, |
|
102 null); |
|
103 } |
|
104 |
|
105 public void changeBreakpoint(final BreakpointImpl breakpointImpl, |
|
106 final BreakpointCallback callback) { |
|
107 debugSession.sendMessageAsync( |
|
108 DebuggerMessageFactory.changeBreakpoint(breakpointImpl), |
|
109 true, |
|
110 new V8CommandProcessor.V8HandlerCallback() { |
|
111 public void messageReceived(CommandResponse response) { |
|
112 if (callback != null) { |
|
113 SuccessCommandResponse successResponse = response.asSuccess(); |
|
114 if (successResponse != null) { |
|
115 callback.success(breakpointImpl); |
|
116 } else { |
|
117 callback.failure(response.asFailure().getMessage()); |
|
118 } |
|
119 } |
|
120 } |
|
121 public void failure(String message) { |
|
122 if (callback != null) { |
|
123 callback.failure(message); |
|
124 } |
|
125 } |
|
126 }, |
|
127 null); |
|
128 } |
|
129 |
|
130 private static Integer toNullableInteger(int value) { |
|
131 return value == Breakpoint.EMPTY_VALUE |
|
132 ? null |
|
133 : value; |
|
134 } |
|
135 } |