| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.sdk.internal.wip; | |
| 6 | |
| 7 import static org.chromium.sdk.util.BasicUtil.getSafe; | |
| 8 | |
| 9 import java.util.ArrayList; | |
| 10 import java.util.Collection; | |
| 11 import java.util.Collections; | |
| 12 import java.util.HashMap; | |
| 13 import java.util.HashSet; | |
| 14 import java.util.List; | |
| 15 import java.util.Map; | |
| 16 import java.util.Set; | |
| 17 import java.util.concurrent.atomic.AtomicReference; | |
| 18 import java.util.logging.Level; | |
| 19 import java.util.logging.Logger; | |
| 20 | |
| 21 import org.chromium.sdk.Breakpoint; | |
| 22 import org.chromium.sdk.CallFrame; | |
| 23 import org.chromium.sdk.DebugContext; | |
| 24 import org.chromium.sdk.DebugContext.StepAction; | |
| 25 import org.chromium.sdk.ExceptionData; | |
| 26 import org.chromium.sdk.JavascriptVm; | |
| 27 import org.chromium.sdk.JsEvaluateContext; | |
| 28 import org.chromium.sdk.JsObject; | |
| 29 import org.chromium.sdk.JsScope; | |
| 30 import org.chromium.sdk.JsValue; | |
| 31 import org.chromium.sdk.JsVariable; | |
| 32 import org.chromium.sdk.RelayOk; | |
| 33 import org.chromium.sdk.RemoteValueMapping; | |
| 34 import org.chromium.sdk.RestartFrameExtension; | |
| 35 import org.chromium.sdk.SyncCallback; | |
| 36 import org.chromium.sdk.TextStreamPosition; | |
| 37 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException; | |
| 38 import org.chromium.sdk.internal.wip.WipExpressionBuilder.ValueNameBuilder; | |
| 39 import org.chromium.sdk.internal.wip.WipValueLoader.Getter; | |
| 40 import org.chromium.sdk.internal.wip.protocol.WipParserAccess; | |
| 41 import org.chromium.sdk.internal.wip.protocol.input.WipCommandResponse; | |
| 42 import org.chromium.sdk.internal.wip.protocol.input.debugger.CallFrameValue; | |
| 43 import org.chromium.sdk.internal.wip.protocol.input.debugger.EvaluateOnCallFrame
Data; | |
| 44 import org.chromium.sdk.internal.wip.protocol.input.debugger.PausedEventData; | |
| 45 import org.chromium.sdk.internal.wip.protocol.input.debugger.RestartFrameData; | |
| 46 import org.chromium.sdk.internal.wip.protocol.input.debugger.ResumedEventData; | |
| 47 import org.chromium.sdk.internal.wip.protocol.input.debugger.ScopeValue; | |
| 48 import org.chromium.sdk.internal.wip.protocol.input.runtime.EvaluateData; | |
| 49 import org.chromium.sdk.internal.wip.protocol.input.runtime.PropertyDescriptorVa
lue; | |
| 50 import org.chromium.sdk.internal.wip.protocol.input.runtime.RemoteObjectValue; | |
| 51 import org.chromium.sdk.internal.wip.protocol.output.WipParams; | |
| 52 import org.chromium.sdk.internal.wip.protocol.output.WipParamsWithResponse; | |
| 53 import org.chromium.sdk.internal.wip.protocol.output.debugger.EvaluateOnCallFram
eParams; | |
| 54 import org.chromium.sdk.internal.wip.protocol.output.debugger.RestartFrameParams
; | |
| 55 import org.chromium.sdk.internal.wip.protocol.output.debugger.ResumeParams; | |
| 56 import org.chromium.sdk.internal.wip.protocol.output.debugger.StepIntoParams; | |
| 57 import org.chromium.sdk.internal.wip.protocol.output.debugger.StepOutParams; | |
| 58 import org.chromium.sdk.internal.wip.protocol.output.debugger.StepOverParams; | |
| 59 import org.chromium.sdk.internal.wip.protocol.output.runtime.EvaluateParams; | |
| 60 import org.chromium.sdk.util.AsyncFutureRef; | |
| 61 import org.chromium.sdk.util.GenericCallback; | |
| 62 import org.chromium.sdk.util.LazyConstructable; | |
| 63 import org.chromium.sdk.util.MethodIsBlockingException; | |
| 64 import org.chromium.sdk.util.RelaySyncCallback; | |
| 65 import org.json.simple.JSONObject; | |
| 66 | |
| 67 /** | |
| 68 * Builder for {@link DebugContext} that works with Wip protocol. | |
| 69 */ | |
| 70 class WipContextBuilder { | |
| 71 private static final Logger LOGGER = Logger.getLogger(WipContextBuilder.class.
getName()); | |
| 72 | |
| 73 private final WipTabImpl tabImpl; | |
| 74 private final EvaluateHack evaluateHack; | |
| 75 private WipDebugContextImpl currentContext = null; | |
| 76 | |
| 77 WipContextBuilder(WipTabImpl tabImpl) { | |
| 78 this.tabImpl = tabImpl; | |
| 79 this.evaluateHack = new EvaluateHack(tabImpl); | |
| 80 } | |
| 81 | |
| 82 // Called from Dispatch Thread. | |
| 83 RelayOk updateStackTrace(List<CallFrameValue> callFrames, | |
| 84 GenericCallback<Void> callback, final SyncCallback syncCallback) { | |
| 85 if (currentContext == null) { | |
| 86 if (callback != null) { | |
| 87 callback.success(null); | |
| 88 } | |
| 89 return RelaySyncCallback.finish(syncCallback); | |
| 90 } else { | |
| 91 return currentContext.setFrames(callFrames, callback, syncCallback); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void createContext(PausedEventData data) { | |
| 96 if (currentContext != null) { | |
| 97 LOGGER.severe("Context is already created"); | |
| 98 currentContext = null; | |
| 99 } | |
| 100 | |
| 101 final WipDebugContextImpl context = new WipDebugContextImpl(data); | |
| 102 currentContext = context; | |
| 103 | |
| 104 GenericCallback<Void> callback = new GenericCallback<Void>() { | |
| 105 @Override | |
| 106 public void success(Void value) { | |
| 107 tabImpl.getDebugListener().getDebugEventListener().suspended(context); | |
| 108 } | |
| 109 | |
| 110 @Override | |
| 111 public void failure(Exception exception) { | |
| 112 throw new RuntimeException(exception); | |
| 113 } | |
| 114 }; | |
| 115 | |
| 116 context.setFrames(data.callFrames(), callback, null); | |
| 117 } | |
| 118 | |
| 119 EvaluateHack getEvaluateHack() { | |
| 120 return evaluateHack; | |
| 121 } | |
| 122 | |
| 123 void onResumeReportedFromRemote(ResumedEventData event) { | |
| 124 if (currentContext == null) { | |
| 125 throw new IllegalStateException(); | |
| 126 } | |
| 127 WipDebugContextImpl context = currentContext; | |
| 128 currentContext = null; | |
| 129 this.tabImpl.getDebugListener().getDebugEventListener().resumed(); | |
| 130 context.reportClosed(); | |
| 131 } | |
| 132 | |
| 133 class WipDebugContextImpl implements DebugContext { | |
| 134 private volatile List<CallFrameImpl> frames = null; | |
| 135 private final ExceptionData exceptionData; | |
| 136 private final AtomicReference<CloseRequest> closeRequest = | |
| 137 new AtomicReference<CloseRequest>(null); | |
| 138 private final JsEvaluateContext globalContext; | |
| 139 | |
| 140 public WipDebugContextImpl(PausedEventData data) { | |
| 141 PausedEventData.Data additionalData = data.data(); | |
| 142 if (data.reason() == PausedEventData.Reason.EXCEPTION && additionalData !=
null) { | |
| 143 RemoteObjectValue exceptionRemoteObject; | |
| 144 try { | |
| 145 JSONObject additionalDataObject = additionalData.getUnderlyingObject()
; | |
| 146 exceptionRemoteObject = | |
| 147 WipParserAccess.get().parseRemoteObjectValue(additionalDataObject)
; | |
| 148 } catch (JsonProtocolParseException e) { | |
| 149 throw new RuntimeException("Failed to parse exception data", e); | |
| 150 } | |
| 151 JsValue exceptionValue = | |
| 152 valueLoader.getValueBuilder().wrap(exceptionRemoteObject, null); | |
| 153 exceptionData = new ExceptionDataImpl(exceptionValue); | |
| 154 } else { | |
| 155 exceptionData = null; | |
| 156 } | |
| 157 globalContext = new GlobalEvaluateContext(getValueLoader()); | |
| 158 } | |
| 159 | |
| 160 RelayOk setFrames(List<CallFrameValue> frameDataList, | |
| 161 final GenericCallback<Void> callback, final SyncCallback syncCallback) { | |
| 162 frames = new ArrayList<CallFrameImpl>(frameDataList.size()); | |
| 163 for (CallFrameValue frameData : frameDataList) { | |
| 164 frames.add(new CallFrameImpl(frameData)); | |
| 165 } | |
| 166 | |
| 167 return tabImpl.getScriptManager().loadScriptSourcesAsync(getScriptIds(), | |
| 168 new WipScriptManager.ScriptSourceLoadCallback() { | |
| 169 @Override | |
| 170 public void done(Map<String, WipScriptImpl> loadedScripts) { | |
| 171 setScripts(loadedScripts); | |
| 172 | |
| 173 if (callback != null) { | |
| 174 callback.success(null); | |
| 175 } | |
| 176 } | |
| 177 }, | |
| 178 syncCallback); | |
| 179 } | |
| 180 | |
| 181 void resetFrames(List<CallFrameValue> frameDataList) { | |
| 182 List<CallFrameImpl> newFrames = new ArrayList<CallFrameImpl>(frameDataList
.size()); | |
| 183 for (int i = 0; i < frameDataList.size(); i++) { | |
| 184 CallFrameImpl callFrameImpl = new CallFrameImpl(frameDataList.get(i)); | |
| 185 callFrameImpl.setScript(frames.get(i).getScript()); | |
| 186 newFrames.add(callFrameImpl); | |
| 187 } | |
| 188 frames = newFrames; | |
| 189 } | |
| 190 | |
| 191 WipValueLoader getValueLoader() { | |
| 192 return valueLoader; | |
| 193 } | |
| 194 | |
| 195 void reportClosed() { | |
| 196 CloseRequest request = this.closeRequest.get(); | |
| 197 if (request != null && request.callback != null) { | |
| 198 request.callback.success(); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 Set<String> getScriptIds() { | |
| 203 Set<String> scriptIds = new HashSet<String>(); | |
| 204 for (CallFrameImpl frame : frames) { | |
| 205 String sourceId = frame.getSourceId(); | |
| 206 if (sourceId != null) { | |
| 207 scriptIds.add(sourceId); | |
| 208 } | |
| 209 } | |
| 210 return scriptIds; | |
| 211 } | |
| 212 | |
| 213 void setScripts(Map<String, WipScriptImpl> loadedScripts) { | |
| 214 for (CallFrameImpl frame : frames) { | |
| 215 String sourceId = frame.getSourceId(); | |
| 216 if (sourceId != null) { | |
| 217 WipScriptImpl script = getSafe(loadedScripts, sourceId); | |
| 218 // Script can be null. | |
| 219 frame.setScript(script); | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 @Override | |
| 225 public State getState() { | |
| 226 if (exceptionData == null) { | |
| 227 return State.NORMAL; | |
| 228 } else { | |
| 229 return State.EXCEPTION; | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 @Override | |
| 234 public ExceptionData getExceptionData() { | |
| 235 return exceptionData; | |
| 236 } | |
| 237 | |
| 238 @Override | |
| 239 public Collection<? extends Breakpoint> getBreakpointsHit() { | |
| 240 if (frames.isEmpty()) { | |
| 241 return Collections.emptyList(); | |
| 242 } | |
| 243 CallFrameImpl topFrame = frames.get(0); | |
| 244 return tabImpl.getBreakpointManager().findRelatedBreakpoints(topFrame); | |
| 245 } | |
| 246 | |
| 247 @Override | |
| 248 public List<? extends CallFrame> getCallFrames() { | |
| 249 return frames; | |
| 250 } | |
| 251 | |
| 252 @Override | |
| 253 public void continueVm(StepAction stepAction, int stepCount, | |
| 254 ContinueCallback callback) { | |
| 255 continueVm(stepAction, stepCount, callback, null); | |
| 256 } | |
| 257 | |
| 258 @Override | |
| 259 public RelayOk continueVm(StepAction stepAction, int stepCount, | |
| 260 final ContinueCallback callback, SyncCallback syncCallback) { | |
| 261 { | |
| 262 boolean updated = closeRequest.compareAndSet(null, new CloseRequest(call
back)); | |
| 263 if (!updated) { | |
| 264 throw new IllegalStateException("Continue already requested"); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 WipParams params = sdkStepToProtocolStep(stepAction); | |
| 269 | |
| 270 WipCommandCallback commandCallback; | |
| 271 if (callback == null) { | |
| 272 commandCallback = null; | |
| 273 } else { | |
| 274 commandCallback = new WipCommandCallback() { | |
| 275 @Override public void messageReceived(WipCommandResponse response) { | |
| 276 callback.success(); | |
| 277 } | |
| 278 @Override public void failure(String message) { | |
| 279 callback.failure(message); | |
| 280 } | |
| 281 }; | |
| 282 } | |
| 283 return tabImpl.getCommandProcessor().send(params, commandCallback, syncCal
lback); | |
| 284 } | |
| 285 | |
| 286 @Override | |
| 287 public JsEvaluateContext getGlobalEvaluateContext() { | |
| 288 return globalContext; | |
| 289 } | |
| 290 | |
| 291 @Override | |
| 292 public RemoteValueMapping getDefaultRemoteValueMapping() { | |
| 293 return valueLoader; | |
| 294 } | |
| 295 | |
| 296 public WipTabImpl getTab() { | |
| 297 return tabImpl; | |
| 298 } | |
| 299 | |
| 300 public WipCommandProcessor getCommandProcessor() { | |
| 301 return tabImpl.getCommandProcessor(); | |
| 302 } | |
| 303 | |
| 304 private class CloseRequest { | |
| 305 final ContinueCallback callback; | |
| 306 | |
| 307 CloseRequest(ContinueCallback callback) { | |
| 308 this.callback = callback; | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 class CallFrameImpl implements CallFrame { | |
| 313 private final String functionName; | |
| 314 private final String id; | |
| 315 private final LazyConstructable<List<JsScope>> scopeData; | |
| 316 private final JsVariable thisObject; | |
| 317 private final TextStreamPosition streamPosition; | |
| 318 private final String sourceId; | |
| 319 private WipScriptImpl scriptImpl; | |
| 320 | |
| 321 public CallFrameImpl(CallFrameValue frameData) { | |
| 322 functionName = frameData.functionName(); | |
| 323 id = frameData.callFrameId(); | |
| 324 sourceId = frameData.location().scriptId(); | |
| 325 final List<ScopeValue> scopeDataList = frameData.scopeChain(); | |
| 326 | |
| 327 scopeData = LazyConstructable.create(new LazyConstructable.Factory<List<
JsScope>>() { | |
| 328 @Override | |
| 329 public List<JsScope> construct() { | |
| 330 final List<JsScope> scopes = new ArrayList<JsScope>(scopeDataList.si
ze()); | |
| 331 | |
| 332 for (int i = 0; i < scopeDataList.size(); i++) { | |
| 333 ScopeValue scopeData = scopeDataList.get(i); | |
| 334 scopes.add(createScope(scopeData, valueLoader)); | |
| 335 } | |
| 336 return scopes; | |
| 337 } | |
| 338 }); | |
| 339 | |
| 340 RemoteObjectValue thisObjectData = frameData.getThis(); | |
| 341 if (thisObjectData == null) { | |
| 342 LOGGER.log(Level.SEVERE, "Missing local scope", new Exception()); | |
| 343 thisObject = null; | |
| 344 } else { | |
| 345 thisObject = createSimpleNameVariable("this", thisObjectData); | |
| 346 } | |
| 347 | |
| 348 // 0-based. | |
| 349 final int line = (int) frameData.location().lineNumber(); | |
| 350 | |
| 351 // 0-based. | |
| 352 // TODO: check documentation, whether it's 0-based | |
| 353 Long columnObject = frameData.location().columnNumber(); | |
| 354 final int column; | |
| 355 if (columnObject == null) { | |
| 356 column = 0; | |
| 357 } else { | |
| 358 column = columnObject.intValue(); | |
| 359 } | |
| 360 streamPosition = new TextStreamPosition() { | |
| 361 @Override public int getOffset() { | |
| 362 return WipBrowserImpl.throwUnsupported(); | |
| 363 } | |
| 364 @Override public int getLine() { | |
| 365 return line; | |
| 366 } | |
| 367 | |
| 368 @Override public int getColumn() { | |
| 369 return column; | |
| 370 } | |
| 371 }; | |
| 372 } | |
| 373 | |
| 374 String getSourceId() { | |
| 375 return sourceId; | |
| 376 } | |
| 377 | |
| 378 void setScript(WipScriptImpl scriptImpl) { | |
| 379 this.scriptImpl = scriptImpl; | |
| 380 } | |
| 381 | |
| 382 @Override | |
| 383 public List<? extends JsScope> getVariableScopes() { | |
| 384 return scopeData.get(); | |
| 385 } | |
| 386 | |
| 387 @Override | |
| 388 public JsVariable getReceiverVariable() { | |
| 389 return thisObject; | |
| 390 } | |
| 391 | |
| 392 @Override | |
| 393 public WipScriptImpl getScript() { | |
| 394 return scriptImpl; | |
| 395 } | |
| 396 | |
| 397 @Override | |
| 398 public TextStreamPosition getStatementStartPosition() { | |
| 399 return streamPosition; | |
| 400 } | |
| 401 | |
| 402 @Override | |
| 403 public String getFunctionName() { | |
| 404 return functionName; | |
| 405 } | |
| 406 | |
| 407 @Override | |
| 408 public JsEvaluateContext getEvaluateContext() { | |
| 409 return evaluateContext; | |
| 410 } | |
| 411 | |
| 412 private JsVariable createSimpleNameVariable(final String name, | |
| 413 RemoteObjectValue thisObjectData) { | |
| 414 ValueNameBuilder valueNameBuidler = WipExpressionBuilder.createRootName(
name, false); | |
| 415 return valueLoader.getValueBuilder().createVariable(thisObjectData, valu
eNameBuidler); | |
| 416 } | |
| 417 | |
| 418 private final WipEvaluateContextBase<?> evaluateContext = | |
| 419 new WipEvaluateContextBase<EvaluateOnCallFrameData>(getValueLoader())
{ | |
| 420 @Override | |
| 421 protected WipParamsWithResponse<EvaluateOnCallFrameData> createRequestPa
rams( | |
| 422 String expression, WipValueLoader destinationValueLoader) { | |
| 423 return new EvaluateOnCallFrameParams(id, expression, | |
| 424 destinationValueLoader.getObjectGroupId(), false, null, false); | |
| 425 } | |
| 426 | |
| 427 @Override protected RemoteObjectValue getRemoteObjectValue(EvaluateOnCal
lFrameData data) { | |
| 428 return data.result(); | |
| 429 } | |
| 430 | |
| 431 @Override protected Boolean getWasThrown(EvaluateOnCallFrameData data) { | |
| 432 return data.wasThrown(); | |
| 433 } | |
| 434 }; | |
| 435 | |
| 436 RelayOk restart(final GenericCallback<Boolean> callback, | |
| 437 SyncCallback syncCallback) { | |
| 438 | |
| 439 RelaySyncCallback relaySyncCallback = new RelaySyncCallback(syncCallback
); | |
| 440 | |
| 441 final RelaySyncCallback.Guard guard = relaySyncCallback.newGuard(); | |
| 442 | |
| 443 RestartFrameParams params = new RestartFrameParams(id); | |
| 444 WipCommandProcessor commandProcessor = valueLoader.getTabImpl().getComma
ndProcessor(); | |
| 445 GenericCallback<RestartFrameData> commandCallback = | |
| 446 new GenericCallback<RestartFrameData>() { | |
| 447 @Override | |
| 448 public void success(RestartFrameData value) { | |
| 449 RelayOk relayOk = handleRestartFrameData(value, callback, guard.getR
elay()); | |
| 450 guard.discharge(relayOk); | |
| 451 } | |
| 452 | |
| 453 @Override | |
| 454 public void failure(Exception exception) { | |
| 455 if (callback != null) { | |
| 456 callback.failure(exception); | |
| 457 } | |
| 458 } | |
| 459 }; | |
| 460 return commandProcessor.send(params, commandCallback, guard.asSyncCallba
ck()); | |
| 461 } | |
| 462 | |
| 463 private RelayOk handleRestartFrameData(RestartFrameData data, | |
| 464 final GenericCallback<Boolean> callback, RelaySyncCallback relay) { | |
| 465 // We are in Dispatch thread. | |
| 466 if (currentContext != WipDebugContextImpl.this) { | |
| 467 return finishSuccessfulRestart(false, callback, relay); | |
| 468 } | |
| 469 if (data.result().getUnderlyingObject().get("stack_update_needs_step_in"
) == | |
| 470 Boolean.TRUE) { | |
| 471 final RelaySyncCallback.Guard guard = relay.newGuard(); | |
| 472 final ContinueCallback continueCallback = new ContinueCallback() { | |
| 473 @Override | |
| 474 public void success() { | |
| 475 RelayOk relayOk = finishSuccessfulRestart(true, callback, guard.ge
tRelay()); | |
| 476 guard.discharge(relayOk); | |
| 477 } | |
| 478 | |
| 479 @Override | |
| 480 public void failure(String errorMessage) { | |
| 481 if (callback != null) { | |
| 482 callback.failure(new Exception(errorMessage)); | |
| 483 } | |
| 484 } | |
| 485 }; | |
| 486 return currentContext.continueVm(StepAction.IN, 1, | |
| 487 continueCallback, guard.asSyncCallback()); | |
| 488 } else { | |
| 489 resetFrames(data.callFrames()); | |
| 490 return finishSuccessfulRestart(false, callback, relay); | |
| 491 } | |
| 492 } | |
| 493 | |
| 494 private RelayOk finishSuccessfulRestart(boolean vmResumed, | |
| 495 GenericCallback<Boolean> callback, RelaySyncCallback relay) { | |
| 496 if (callback != null) { | |
| 497 callback.success(vmResumed); | |
| 498 } | |
| 499 return relay.finish(); | |
| 500 } | |
| 501 } | |
| 502 | |
| 503 private class ExceptionDataImpl implements ExceptionData { | |
| 504 private final JsValue exceptionValue; | |
| 505 | |
| 506 ExceptionDataImpl(JsValue exceptionValue) { | |
| 507 this.exceptionValue = exceptionValue; | |
| 508 } | |
| 509 | |
| 510 @Override | |
| 511 public JsValue getExceptionValue() { | |
| 512 return exceptionValue; | |
| 513 } | |
| 514 | |
| 515 @Override | |
| 516 public boolean isUncaught() { | |
| 517 // TODO: implement. | |
| 518 return false; | |
| 519 } | |
| 520 | |
| 521 @Override | |
| 522 public String getSourceText() { | |
| 523 // Not supported. | |
| 524 return null; | |
| 525 } | |
| 526 | |
| 527 @Override | |
| 528 public String getExceptionMessage() { | |
| 529 // TODO: implement. | |
| 530 return exceptionValue.getValueString(); | |
| 531 } | |
| 532 } | |
| 533 | |
| 534 @Override | |
| 535 public JavascriptVm getJavascriptVm() { | |
| 536 return tabImpl; | |
| 537 } | |
| 538 | |
| 539 private final WipValueLoader valueLoader = new WipValueLoader(tabImpl) { | |
| 540 @Override | |
| 541 String getObjectGroupId() { | |
| 542 return null; | |
| 543 } | |
| 544 }; | |
| 545 } | |
| 546 | |
| 547 /** | |
| 548 * Implements restart frame operation as chain of VM calls. After the main 're
start' command | |
| 549 * it either calls 'step in' request or reloads backtrace. {@link RelaySyncCal
lback} is used | |
| 550 * to guarantee final sync callback invocation. | |
| 551 */ | |
| 552 public static final RestartFrameExtension RESTART_FRAME_EXTENSION = new Restar
tFrameExtension() { | |
| 553 @Override | |
| 554 public RelayOk restartFrame(CallFrame callFrame, | |
| 555 GenericCallback<Boolean> callback, SyncCallback syncCallback) { | |
| 556 WipDebugContextImpl.CallFrameImpl callFrameImpl = | |
| 557 (WipDebugContextImpl.CallFrameImpl) callFrame; | |
| 558 return callFrameImpl.restart(callback, syncCallback); | |
| 559 } | |
| 560 | |
| 561 @Override | |
| 562 public boolean canRestartFrame(CallFrame callFrame) { | |
| 563 return callFrame.getScript() != null; | |
| 564 } | |
| 565 }; | |
| 566 | |
| 567 | |
| 568 static JsScope createScope(ScopeValue scopeData, WipValueLoader valueLoader) { | |
| 569 JsScope.Type type = WIP_TO_SDK_SCOPE_TYPE.get(scopeData.type()); | |
| 570 if (type == null) { | |
| 571 type = JsScope.Type.UNKNOWN; | |
| 572 } | |
| 573 if (type == JsScope.Type.WITH || type == JsScope.Type.GLOBAL) { | |
| 574 return new ObjectScopeImpl(scopeData, type, valueLoader); | |
| 575 } else { | |
| 576 return new DeclarativeScopeImpl(scopeData, type, valueLoader); | |
| 577 } | |
| 578 } | |
| 579 | |
| 580 private static class DeclarativeScopeImpl implements JsScope.Declarative { | |
| 581 private final AsyncFutureRef<Getter<ScopeVariables>> propertiesRef = | |
| 582 new AsyncFutureRef<Getter<ScopeVariables>>(); | |
| 583 private final String objectId; | |
| 584 private final Type type; | |
| 585 private final WipValueLoader valueLoader; | |
| 586 | |
| 587 public DeclarativeScopeImpl(ScopeValue scopeData, Type type, WipValueLoader
valueLoader) { | |
| 588 this.type = type; | |
| 589 this.objectId = scopeData.object().objectId(); | |
| 590 this.valueLoader = valueLoader; | |
| 591 } | |
| 592 | |
| 593 @Override public Type getType() { | |
| 594 return type; | |
| 595 } | |
| 596 | |
| 597 @Override public Declarative asDeclarativeScope() { | |
| 598 return this; | |
| 599 } | |
| 600 | |
| 601 @Override public ObjectBased asObjectBased() { | |
| 602 return null; | |
| 603 } | |
| 604 | |
| 605 @Override public <R> R accept(Visitor<R> visitor) { | |
| 606 return visitor.visitDeclarative(this); | |
| 607 } | |
| 608 | |
| 609 @Override | |
| 610 public List<? extends JsVariable> getVariables() throws MethodIsBlockingExce
ption { | |
| 611 int currentCacheState = valueLoader.getCacheState(); | |
| 612 if (propertiesRef.isInitialized()) { | |
| 613 ScopeVariables result = propertiesRef.getSync().get(); | |
| 614 if (result.cacheState == currentCacheState) { | |
| 615 return result.variables; | |
| 616 } | |
| 617 startLoadOperation(true, currentCacheState); | |
| 618 } else { | |
| 619 startLoadOperation(false, currentCacheState); | |
| 620 } | |
| 621 | |
| 622 // This is blocking. | |
| 623 return propertiesRef.getSync().get().variables; | |
| 624 } | |
| 625 | |
| 626 /** | |
| 627 * Starts load operation that works synchronously, i.e. it may block the cal
ling method. | |
| 628 * This is done because some thread must take post-processing anyway and it
shouldn't | |
| 629 * be the Dispatch thread. | |
| 630 * The method may not be blocking, if another thread is already doing the sa
me operation. | |
| 631 */ | |
| 632 private void startLoadOperation(boolean reload, int currentCacheState) | |
| 633 throws MethodIsBlockingException { | |
| 634 WipValueLoader.LoadPostprocessor<Getter<ScopeVariables>> processor = | |
| 635 new WipValueLoader.LoadPostprocessor<Getter<ScopeVariables>>() { | |
| 636 @Override | |
| 637 public Getter<ScopeVariables> process( | |
| 638 List<? extends PropertyDescriptorValue> propertyList, int currentCac
heState) { | |
| 639 final List<JsVariable> properties = new ArrayList<JsVariable>(property
List.size()); | |
| 640 | |
| 641 WipValueBuilder valueBuilder = valueLoader.getValueBuilder(); | |
| 642 for (PropertyDescriptorValue property : propertyList) { | |
| 643 final String name = property.name(); | |
| 644 | |
| 645 ValueNameBuilder valueNameBuilder = | |
| 646 WipExpressionBuilder.createRootName(name, false); | |
| 647 | |
| 648 JsVariable variable; | |
| 649 if (objectId == null) { | |
| 650 variable = valueBuilder.createVariable(property.value(), valueName
Builder); | |
| 651 } else { | |
| 652 variable = valueBuilder.createObjectProperty(property, objectId, v
alueNameBuilder); | |
| 653 } | |
| 654 properties.add(variable); | |
| 655 } | |
| 656 final ScopeVariables scopeVariables = new ScopeVariables(properties, c
urrentCacheState); | |
| 657 return new Getter<ScopeVariables>() { | |
| 658 @Override | |
| 659 ScopeVariables get() { | |
| 660 return scopeVariables; | |
| 661 } | |
| 662 }; | |
| 663 } | |
| 664 | |
| 665 @Override | |
| 666 public Getter<ScopeVariables> getEmptyResult() { | |
| 667 return EMPTY_SCOPE_VARIABLES_OPTIONAL; | |
| 668 } | |
| 669 | |
| 670 @Override | |
| 671 public Getter<ScopeVariables> forException(Exception exception) { | |
| 672 return WipValueLoader.Getter.newFailure(exception); | |
| 673 } | |
| 674 }; | |
| 675 // This is blocking. | |
| 676 valueLoader.loadPropertiesInFuture(objectId, processor, reload, currentCac
heState, | |
| 677 propertiesRef); | |
| 678 } | |
| 679 } | |
| 680 | |
| 681 private static class ObjectScopeImpl implements JsScope.ObjectBased { | |
| 682 private final JsValue jsValue; | |
| 683 private final JsScope.Type type; | |
| 684 | |
| 685 ObjectScopeImpl(ScopeValue scopeData, JsScope.Type type, WipValueLoader valu
eLoader) { | |
| 686 jsValue = valueLoader.getValueBuilder().wrap(scopeData.object(), null); | |
| 687 this.type = type; | |
| 688 } | |
| 689 | |
| 690 @Override public Type getType() { | |
| 691 return type; | |
| 692 } | |
| 693 | |
| 694 @Override public Declarative asDeclarativeScope() { | |
| 695 return null; | |
| 696 } | |
| 697 | |
| 698 @Override public ObjectBased asObjectBased() { | |
| 699 return this; | |
| 700 } | |
| 701 | |
| 702 @Override public <R> R accept(Visitor<R> visitor) { | |
| 703 return visitor.visitObject(this); | |
| 704 } | |
| 705 | |
| 706 @Override | |
| 707 public JsObject getScopeObject() throws MethodIsBlockingException { | |
| 708 JsObject jsObject = jsValue.asObject(); | |
| 709 if (jsObject == null) { | |
| 710 throw new RuntimeException("Received scope object value is not an object
"); | |
| 711 } | |
| 712 return jsObject; | |
| 713 } | |
| 714 } | |
| 715 | |
| 716 static final class GlobalEvaluateContext extends WipEvaluateContextBase<Evalua
teData> { | |
| 717 | |
| 718 GlobalEvaluateContext(WipValueLoader valueLoader) { | |
| 719 super(valueLoader); | |
| 720 } | |
| 721 | |
| 722 @Override protected WipParamsWithResponse<EvaluateData> createRequestParams(
String expression, | |
| 723 WipValueLoader destinationValueLoader) { | |
| 724 boolean doNotPauseOnExceptions = true; | |
| 725 return new EvaluateParams(expression, destinationValueLoader.getObjectGrou
pId(), | |
| 726 false, doNotPauseOnExceptions, null, false); | |
| 727 } | |
| 728 | |
| 729 @Override protected RemoteObjectValue getRemoteObjectValue(EvaluateData data
) { | |
| 730 return data.result(); | |
| 731 } | |
| 732 | |
| 733 @Override protected Boolean getWasThrown(EvaluateData data) { | |
| 734 return data.wasThrown(); | |
| 735 } | |
| 736 } | |
| 737 | |
| 738 private static final Map<ScopeValue.Type, JsScope.Type> WIP_TO_SDK_SCOPE_TYPE; | |
| 739 static { | |
| 740 WIP_TO_SDK_SCOPE_TYPE = new HashMap<ScopeValue.Type, JsScope.Type>(); | |
| 741 | |
| 742 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.GLOBAL, JsScope.Type.GLOBAL); | |
| 743 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.LOCAL, JsScope.Type.LOCAL); | |
| 744 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.WITH, JsScope.Type.WITH); | |
| 745 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.CLOSURE, JsScope.Type.CLOSURE); | |
| 746 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.CATCH, JsScope.Type.CATCH); | |
| 747 | |
| 748 assert WIP_TO_SDK_SCOPE_TYPE.size() == ScopeValue.Type.values().length; | |
| 749 } | |
| 750 | |
| 751 private static class ScopeVariables { | |
| 752 final List<JsVariable> variables; | |
| 753 final int cacheState; | |
| 754 | |
| 755 ScopeVariables(List<JsVariable> variables, int cacheState) { | |
| 756 this.variables = variables; | |
| 757 this.cacheState = cacheState; | |
| 758 } | |
| 759 } | |
| 760 | |
| 761 private static final Getter<ScopeVariables> EMPTY_SCOPE_VARIABLES_OPTIONAL = | |
| 762 new Getter<ScopeVariables>() { | |
| 763 private final ScopeVariables value = | |
| 764 new ScopeVariables(Collections.<JsVariable>emptyList(), Integer.MAX_VA
LUE); | |
| 765 | |
| 766 @Override ScopeVariables get() { | |
| 767 return value; | |
| 768 } | |
| 769 }; | |
| 770 | |
| 771 | |
| 772 private WipParams sdkStepToProtocolStep(StepAction stepAction) { | |
| 773 switch (stepAction) { | |
| 774 case CONTINUE: | |
| 775 return RESUME_PARAMS; | |
| 776 case IN: | |
| 777 return STEP_INTO_PARAMS; | |
| 778 case OUT: | |
| 779 return STEP_OUT_PARAMS; | |
| 780 case OVER: | |
| 781 return STEP_OVER_PARAMS; | |
| 782 default: | |
| 783 throw new RuntimeException(); | |
| 784 } | |
| 785 } | |
| 786 | |
| 787 private static final ResumeParams RESUME_PARAMS = new ResumeParams(); | |
| 788 private static final StepIntoParams STEP_INTO_PARAMS = new StepIntoParams(); | |
| 789 private static final StepOutParams STEP_OUT_PARAMS = new StepOutParams(); | |
| 790 private static final StepOverParams STEP_OVER_PARAMS = new StepOverParams(); | |
| 791 } | |
| OLD | NEW |