| 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 java.util.List; | |
| 8 | |
| 9 import org.chromium.sdk.RelayOk; | |
| 10 import org.chromium.sdk.Script; | |
| 11 import org.chromium.sdk.SyncCallback; | |
| 12 import org.chromium.sdk.internal.ScriptBase; | |
| 13 import org.chromium.sdk.internal.liveeditprotocol.LiveEditProtocolParserAccess; | |
| 14 import org.chromium.sdk.internal.liveeditprotocol.LiveEditResult; | |
| 15 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException; | |
| 16 import org.chromium.sdk.internal.wip.protocol.input.debugger.CallFrameValue; | |
| 17 import org.chromium.sdk.internal.wip.protocol.input.debugger.SetScriptSourceData
; | |
| 18 import org.chromium.sdk.internal.wip.protocol.output.debugger.SetScriptSourcePar
ams; | |
| 19 import org.chromium.sdk.util.GenericCallback; | |
| 20 import org.chromium.sdk.util.RelaySyncCallback; | |
| 21 | |
| 22 /** | |
| 23 * Wip implementation of {@link Script}. | |
| 24 */ | |
| 25 class WipScriptImpl extends ScriptBase<String> { | |
| 26 private final WipScriptManager scriptManager; | |
| 27 | |
| 28 WipScriptImpl(WipScriptManager scriptManager, Descriptor<String> descriptor) { | |
| 29 super(descriptor); | |
| 30 this.scriptManager = scriptManager; | |
| 31 } | |
| 32 | |
| 33 @Override | |
| 34 public RelayOk setSourceOnRemote(String newSource, UpdateCallback callback, | |
| 35 SyncCallback syncCallback) { | |
| 36 return sendLiveEditRequest(newSource, false, callback, syncCallback); | |
| 37 } | |
| 38 | |
| 39 @Override | |
| 40 public RelayOk previewSetSource(String newSource, UpdateCallback callback, | |
| 41 SyncCallback syncCallback) { | |
| 42 return sendLiveEditRequest(newSource, true, callback, syncCallback); | |
| 43 } | |
| 44 | |
| 45 private RelayOk sendLiveEditRequest(String newSource, final boolean preview, | |
| 46 final UpdateCallback updateCallback, | |
| 47 final SyncCallback syncCallback) { | |
| 48 | |
| 49 RelaySyncCallback relay = new RelaySyncCallback(syncCallback); | |
| 50 final RelaySyncCallback.Guard guard = relay.newGuard(); | |
| 51 | |
| 52 SetScriptSourceParams params = new SetScriptSourceParams(getId(), newSource,
preview); | |
| 53 | |
| 54 GenericCallback<SetScriptSourceData> commandCallback = | |
| 55 new GenericCallback<SetScriptSourceData>() { | |
| 56 @Override | |
| 57 public void success(SetScriptSourceData value) { | |
| 58 RelayOk relayOk = | |
| 59 possiblyUpdateCallFrames(preview, value, updateCallback, guard.getRe
lay()); | |
| 60 guard.discharge(relayOk); | |
| 61 } | |
| 62 | |
| 63 @Override | |
| 64 public void failure(Exception exception) { | |
| 65 updateCallback.failure(exception.getMessage()); | |
| 66 } | |
| 67 }; | |
| 68 | |
| 69 WipCommandProcessor commandProcessor = scriptManager.getTabImpl().getCommand
Processor(); | |
| 70 return commandProcessor.send(params, commandCallback, guard.asSyncCallback()
); | |
| 71 } | |
| 72 | |
| 73 private RelayOk possiblyUpdateCallFrames(boolean preview, final SetScriptSourc
eData data, | |
| 74 final UpdateCallback updateCallback, RelaySyncCallback relay) { | |
| 75 | |
| 76 // TODO: support 'step-in recommended'. | |
| 77 | |
| 78 List<CallFrameValue> callFrames = null; | |
| 79 if (!preview) { | |
| 80 callFrames = data.callFrames(); | |
| 81 } | |
| 82 if (callFrames == null) { | |
| 83 dispatchResult(data.result(), updateCallback); | |
| 84 return relay.finish(); | |
| 85 } else { | |
| 86 GenericCallback<Void> setFramesCallback = | |
| 87 new GenericCallback<Void>() { | |
| 88 @Override public void success(Void value) { | |
| 89 dispatchResult(data.result(), updateCallback); | |
| 90 } | |
| 91 @Override public void failure(Exception exception) { | |
| 92 throw new RuntimeException(exception); | |
| 93 } | |
| 94 }; | |
| 95 WipContextBuilder contextBuilder = scriptManager.getTabImpl().getContextBu
ilder(); | |
| 96 return contextBuilder.updateStackTrace(callFrames, setFramesCallback, | |
| 97 relay.getUserSyncCallback()); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 private void dispatchResult(SetScriptSourceData.Result result, UpdateCallback
updateCallback) { | |
| 102 if (updateCallback != null) { | |
| 103 LiveEditResult liveEditResult; | |
| 104 try { | |
| 105 liveEditResult = | |
| 106 LiveEditProtocolParserAccess.get().parseLiveEditResult(result.getUnd
erlyingObject()); | |
| 107 } catch (JsonProtocolParseException e) { | |
| 108 throw new RuntimeException("Failed to parse LiveEdit response", e); | |
| 109 } | |
| 110 ChangeDescription wrappedChangeDescription = | |
| 111 UpdateResultParser.wrapChangeDescription(liveEditResult); | |
| 112 updateCallback.success(null, wrappedChangeDescription); | |
| 113 } | |
| 114 } | |
| 115 } | |
| OLD | NEW |