Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: plugins/org.chromium.sdk.wipbackend.dev/src/org/chromium/sdk/internal/wip/WipValueBuilder.java

Issue 12287017: Support set variable value for WebKit protocol (Closed) Base URL: https://chromedevtools.googlecode.com/svn/trunk
Patch Set: fcr Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.sdk.internal.wip; 5 package org.chromium.sdk.internal.wip;
6 6
7 import static org.chromium.sdk.util.BasicUtil.getSafe; 7 import static org.chromium.sdk.util.BasicUtil.getSafe;
8 8
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.Collection; 10 import java.util.Collection;
(...skipping 15 matching lines...) Expand all
26 import org.chromium.sdk.JsScope; 26 import org.chromium.sdk.JsScope;
27 import org.chromium.sdk.JsValue; 27 import org.chromium.sdk.JsValue;
28 import org.chromium.sdk.JsValue.Type; 28 import org.chromium.sdk.JsValue.Type;
29 import org.chromium.sdk.JsVariable; 29 import org.chromium.sdk.JsVariable;
30 import org.chromium.sdk.RelayOk; 30 import org.chromium.sdk.RelayOk;
31 import org.chromium.sdk.Script; 31 import org.chromium.sdk.Script;
32 import org.chromium.sdk.SyncCallback; 32 import org.chromium.sdk.SyncCallback;
33 import org.chromium.sdk.TextStreamPosition; 33 import org.chromium.sdk.TextStreamPosition;
34 import org.chromium.sdk.internal.wip.WipValueLoader.Getter; 34 import org.chromium.sdk.internal.wip.WipValueLoader.Getter;
35 import org.chromium.sdk.internal.wip.WipValueLoader.ObjectProperties; 35 import org.chromium.sdk.internal.wip.WipValueLoader.ObjectProperties;
36 import org.chromium.sdk.internal.wip.protocol.input.WipCommandResponse.Success;
36 import org.chromium.sdk.internal.wip.protocol.input.debugger.FunctionDetailsValu e; 37 import org.chromium.sdk.internal.wip.protocol.input.debugger.FunctionDetailsValu e;
37 import org.chromium.sdk.internal.wip.protocol.input.debugger.LocationValue; 38 import org.chromium.sdk.internal.wip.protocol.input.debugger.LocationValue;
38 import org.chromium.sdk.internal.wip.protocol.input.debugger.ScopeValue; 39 import org.chromium.sdk.internal.wip.protocol.input.debugger.ScopeValue;
39 import org.chromium.sdk.internal.wip.protocol.input.runtime.PropertyDescriptorVa lue; 40 import org.chromium.sdk.internal.wip.protocol.input.runtime.PropertyDescriptorVa lue;
40 import org.chromium.sdk.internal.wip.protocol.input.runtime.RemoteObjectValue; 41 import org.chromium.sdk.internal.wip.protocol.input.runtime.RemoteObjectValue;
42 import org.chromium.sdk.internal.wip.protocol.output.debugger.SetVariableValuePa rams;
41 import org.chromium.sdk.internal.wip.protocol.output.runtime.CallArgumentParam; 43 import org.chromium.sdk.internal.wip.protocol.output.runtime.CallArgumentParam;
42 import org.chromium.sdk.util.AsyncFutureRef; 44 import org.chromium.sdk.util.AsyncFutureRef;
45 import org.chromium.sdk.util.GenericCallback;
43 import org.chromium.sdk.util.JavaScriptExpressionBuilder; 46 import org.chromium.sdk.util.JavaScriptExpressionBuilder;
44 import org.chromium.sdk.util.MethodIsBlockingException; 47 import org.chromium.sdk.util.MethodIsBlockingException;
45 48
46 /** 49 /**
47 * A builder for implementations of {@link JsValue} and {@link JsVariable}. 50 * A builder for implementations of {@link JsValue} and {@link JsVariable}.
48 * It works in pair with {@link WipValueLoader}. 51 * It works in pair with {@link WipValueLoader}.
49 */ 52 */
50 class WipValueBuilder { 53 class WipValueBuilder {
51 private static final Logger LOGGER = Logger.getLogger(WipValueBuilder.class.ge tName()); 54 private static final Logger LOGGER = Logger.getLogger(WipValueBuilder.class.ge tName());
52 55
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 throw new IllegalArgumentException("Incorrect argument type " + value.ge tClass()); 91 throw new IllegalArgumentException("Incorrect argument type " + value.ge tClass());
89 } 92 }
90 return (JsValueBase) value; 93 return (JsValueBase) value;
91 } 94 }
92 } 95 }
93 96
94 public JsObjectProperty createObjectProperty(final PropertyDescriptorValue pro pertyDescriptor, 97 public JsObjectProperty createObjectProperty(final PropertyDescriptorValue pro pertyDescriptor,
95 final String hostObjectRefId, String name) { 98 final String hostObjectRefId, String name) {
96 JsValue jsValue = wrap(propertyDescriptor.value()); 99 JsValue jsValue = wrap(propertyDescriptor.value());
97 100
98 final JsValue getter = wrapPropertyDescriptorFunction(propertyDescriptor.get (), "getter"); 101 final JsValue getter = wrap(propertyDescriptor.get());
99 102
100 final JsValue setter = wrapPropertyDescriptorFunction(propertyDescriptor.set (), "setter"); 103 final JsValue setter = wrap(propertyDescriptor.set());
101 104
102 return new ObjectPropertyBase(jsValue, name) { 105 return new ObjectPropertyBase(jsValue, name) {
106
107 @Override public boolean isMutable() {
108 return false;
109 }
103 @Override public boolean isWritable() { 110 @Override public boolean isWritable() {
104 return propertyDescriptor.writable(); 111 return propertyDescriptor.writable();
105 } 112 }
106 @Override public JsValue getGetter() { 113 @Override public JsValue getGetter() {
107 return getter; 114 return getter;
108 } 115 }
109 @Override public JsValue getSetter() { 116 @Override public JsValue getSetter() {
110 return setter; 117 return setter;
111 } 118 }
112 @Override public boolean isConfigurable() { 119 @Override public boolean isConfigurable() {
113 return propertyDescriptor.configurable(); 120 return propertyDescriptor.configurable();
114 } 121 }
115 @Override public boolean isEnumerable() { 122 @Override public boolean isEnumerable() {
116 return propertyDescriptor.enumerable(); 123 return propertyDescriptor.enumerable();
117 } 124 }
118 125
119 @Override 126 @Override
120 public JsFunction getGetterAsFunction() { 127 public JsFunction getGetterAsFunction() {
121 JsObject getterObject = getter.asObject(); 128 JsObject getterObject = getter.asObject();
122 if (getterObject == null) { 129 if (getterObject == null) {
123 return null; 130 return null;
124 } 131 }
125 return getterObject.asFunction(); 132 return getterObject.asFunction();
126 } 133 }
127 134
128 @Override 135 @Override
136 public RelayOk setValue(JsValue newValue, SetValueCallback callback,
137 SyncCallback syncCallback) throws UnsupportedOperationException {
138 throw new UnsupportedOperationException();
139 }
140
141 @Override
129 public RelayOk evaluateGet(EvaluateCallback callback, SyncCallback syncCal lback) { 142 public RelayOk evaluateGet(EvaluateCallback callback, SyncCallback syncCal lback) {
130 WipContextBuilder.GlobalEvaluateContext evaluateContext = 143 WipContextBuilder.GlobalEvaluateContext evaluateContext =
131 new WipContextBuilder.GlobalEvaluateContext(valueLoader); 144 new WipContextBuilder.GlobalEvaluateContext(valueLoader);
132 145
133 JsFunction getterFunction = getGetterAsFunction(); 146 JsFunction getterFunction = getGetterAsFunction();
134 if (getterFunction == null) { 147 if (getterFunction == null) {
135 throw new RuntimeException("Getter is not a function"); 148 throw new RuntimeException("Getter is not a function");
136 } 149 }
137 150
138 Map<String, SerializableValue> context = new HashMap<String, Serializabl eValue>(2); 151 Map<String, SerializableValue> context = new HashMap<String, Serializabl eValue>(2);
139 context.put(GETTER_VAR_NAME, (SerializableValue) getterFunction); 152 context.put(GETTER_VAR_NAME, (SerializableValue) getterFunction);
140 context.put(OBJECT_VAR_NAME, SerializableValue.Util.wrapRefId(hostObject RefId)); 153 context.put(OBJECT_VAR_NAME, SerializableValue.Util.wrapRefId(hostObject RefId));
141 154
142 return evaluateContext.evaluateAsyncImpl(EVALUATE_EXPRESSION, 155 return evaluateContext.evaluateAsyncImpl(EVALUATE_EXPRESSION,
143 context, valueLoader, callback, syncCallback); 156 context, valueLoader, callback, syncCallback);
144 } 157 }
145 private static final String GETTER_VAR_NAME = "gttr"; 158 private static final String GETTER_VAR_NAME = "gttr";
146 private static final String OBJECT_VAR_NAME = "obj"; 159 private static final String OBJECT_VAR_NAME = "obj";
147 private static final String EVALUATE_EXPRESSION = 160 private static final String EVALUATE_EXPRESSION =
148 GETTER_VAR_NAME + ".call(" + OBJECT_VAR_NAME + ")"; 161 GETTER_VAR_NAME + ".call(" + OBJECT_VAR_NAME + ")";
149 }; 162 };
150 } 163 }
151 164
152 private JsValue wrapPropertyDescriptorFunction(RemoteObjectValue value, String symbolicName) { 165 public JsVariable createVariable(RemoteObjectValue valueData, String name,
153 return wrap(value); 166 final WipContextBuilder.ScopeParams scopeParams) {
154 } 167 JsValue jsValue = wrap(valueData);
155 168
156 public JsVariable createVariable(RemoteObjectValue valueData, String name) { 169 VariableValueChanger valueChanger;
157 JsValue jsValue = wrap(valueData); 170 if (scopeParams == null) {
158 return createVariable(jsValue, name); 171 valueChanger = null;
172 } else {
173 valueChanger = new VariableValueChanger() {
174 @Override
175 RelayOk setValue(String variableName, JsValue newValue,
176 final GenericCallback<JsValue> callback, SyncCallback syncCallback)
177 throws UnsupportedOperationException {
178 final JsValueBase jsValueBase = JsValueBase.cast(newValue);
179 SetVariableValueParams params = new SetVariableValueParams(scopeParams .getScopeNumber(),
180 variableName, jsValueBase.createCallArgumentParam(), scopeParams.g etCallFrameId(),
181 scopeParams.getFunctionId());
182 WipCommandCallback rawCallback = new WipCommandCallback.Default() {
183 @Override protected void onSuccess(Success success) {
184 callback.success(jsValueBase);
185 }
186 @Override protected void onError(String message) {
187 callback.failure(new Exception(message));
188 }
189 };
190 return valueLoader.getTabImpl().getCommandProcessor().send(params,
191 rawCallback, syncCallback);
192 }
193 };
194 }
195
196 return new VariableImpl(name, jsValue, valueChanger);
159 } 197 }
160 198
161 public JsValue wrap(RemoteObjectValue valueData) { 199 public JsValue wrap(RemoteObjectValue valueData) {
162 if (valueData == null) { 200 if (valueData == null) {
163 return null; 201 return null;
164 } 202 }
165 return getValueType(valueData).build(valueData, valueLoader); 203 return getValueType(valueData).build(valueData, valueLoader);
166 } 204 }
167 205
168 public static JsVariable createVariable(JsValue jsValue, String name) {
169 return new VariableImpl(jsValue, name);
170 }
171
172 private static ValueType getValueType(RemoteObjectValue valueData) { 206 private static ValueType getValueType(RemoteObjectValue valueData) {
173 RemoteObjectValue.Type protocolType = valueData.type(); 207 RemoteObjectValue.Type protocolType = valueData.type();
174 ValueType result = getSafe(PROTOCOL_TYPE_TO_VALUE_TYPE, protocolType); 208 ValueType result = getSafe(PROTOCOL_TYPE_TO_VALUE_TYPE, protocolType);
175 209
176 if (result == null) { 210 if (result == null) {
177 LOGGER.severe("Unexpected value type: " + protocolType); 211 LOGGER.severe("Unexpected value type: " + protocolType);
178 result = DEFAULT_VALUE_TYPE; 212 result = DEFAULT_VALUE_TYPE;
179 } 213 }
180 return result; 214 return result;
181 } 215 }
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 }; 600 };
567 } 601 }
568 602
569 @Override 603 @Override
570 public List<? extends JsScope> getScopes() { 604 public List<? extends JsScope> getScopes() {
571 List<ScopeValue> data = getFunctionDetails().scopeChain(); 605 List<ScopeValue> data = getFunctionDetails().scopeChain();
572 if (data == null) { 606 if (data == null) {
573 return Collections.emptyList(); 607 return Collections.emptyList();
574 } 608 }
575 List<JsScope> result = new ArrayList<JsScope>(data.size()); 609 List<JsScope> result = new ArrayList<JsScope>(data.size());
576 for (ScopeValue scopeValue : data) { 610 WipContextBuilder.ScopeHolderParams holderParams =
577 result.add(WipContextBuilder.createScope(scopeValue, getRemoteValueMap ping())); 611 new WipContextBuilder.ScopeHolderParams(null, getRefId());
612 for (int i = 0; i < data.size(); i++) {
613 ScopeValue scopeValue = data.get(i);
614 result.add(WipContextBuilder.createScope(scopeValue, getRemoteValueMap ping(),
615 holderParams, i));
578 } 616 }
579 return result; 617 return result;
580 } 618 }
581 619
582 private FunctionDetailsValue getFunctionDetails() throws MethodIsBlockingE xception { 620 private FunctionDetailsValue getFunctionDetails() throws MethodIsBlockingE xception {
583 if (!loadedPositionRef.isInitialized()) { 621 if (!loadedPositionRef.isInitialized()) {
584 getRemoteValueMapping().loadFunctionLocationInFuture(getValueData().ob jectId(), 622 getRemoteValueMapping().loadFunctionLocationInFuture(getValueData().ob jectId(),
585 loadedPositionRef); 623 loadedPositionRef);
586 } 624 }
587 return loadedPositionRef.getSync().get(); 625 return loadedPositionRef.getSync().get();
588 } 626 }
589 } 627 }
590 } 628 }
591 629
592 private interface FunctionScopeAccess { 630 private interface FunctionScopeAccess {
593 List<? extends JsScope> getScopes(); 631 List<? extends JsScope> getScopes();
594 } 632 }
595 633
596 static final FunctionScopeExtension FUNCTION_SCOPE_EXTENSION = new FunctionSco peExtension() { 634 static final FunctionScopeExtension FUNCTION_SCOPE_EXTENSION = new FunctionSco peExtension() {
597 @Override 635 @Override
598 public List<? extends JsScope> getScopes(JsFunction function) 636 public List<? extends JsScope> getScopes(JsFunction function)
599 throws MethodIsBlockingException { 637 throws MethodIsBlockingException {
600 FunctionScopeAccess functionScopeAccess = (FunctionScopeAccess) function; 638 FunctionScopeAccess functionScopeAccess = (FunctionScopeAccess) function;
601 return functionScopeAccess.getScopes(); 639 return functionScopeAccess.getScopes();
602 } 640 }
603 }; 641 };
604 642
605 private static abstract class VariableBase implements JsVariable { 643 private static abstract class VariableBase implements JsVariable {
606 private final JsValue jsValue;
607 private final String name; 644 private final String name;
608 645
609 VariableBase(JsValue jsValue, String name) { 646 VariableBase(String name) {
610 this.jsValue = jsValue;
611 this.name = name; 647 this.name = name;
612 } 648 }
613 649
614 @Override 650 @Override
615 public boolean isReadable() { 651 public boolean isReadable() {
616 return true; 652 return true;
617 } 653 }
618 654
619 @Override 655 @Override
620 public JsValue getValue() {
621 return jsValue;
622 }
623
624 @Override
625 public String getName() { 656 public String getName() {
626 return name; 657 return name;
627 } 658 }
628
629 @Override
630 public boolean isMutable() {
631 return false;
632 }
633
634 @Override
635 public RelayOk setValue(JsValue newValue, SetValueCallback callback,
636 SyncCallback syncCallback) throws UnsupportedOperationException {
637 throw new UnsupportedOperationException();
638 }
639 } 659 }
640 660
641 private static class VariableImpl extends VariableBase { 661 private static class VariableImpl extends VariableBase {
642 VariableImpl(JsValue jsValue, String name) { 662 private final VariableValueChanger valueChanger;
643 super(jsValue, name); 663 private volatile JsValue jsValue;
664
665 VariableImpl(String name, JsValue jsValue, VariableValueChanger valueChanger ) {
666 super(name);
667 this.jsValue = jsValue;
668 this.valueChanger = valueChanger;
644 } 669 }
645 670
646 @Override public JsObjectProperty asObjectProperty() { 671 @Override public JsObjectProperty asObjectProperty() {
647 return null; 672 return null;
648 } 673 }
674 @Override public boolean isMutable() {
675 return valueChanger != null;
676 }
677 @Override public JsValue getValue() {
678 return jsValue;
679 }
680
681 @Override
682 public RelayOk setValue(JsValue newValue, final SetValueCallback callback,
683 SyncCallback syncCallback) throws UnsupportedOperationException {
684 if (valueChanger == null) {
685 throw new UnsupportedOperationException();
686 }
687 GenericCallback<JsValue> rawCallback = new GenericCallback<JsValue>() {
688 @Override
689 public void success(JsValue value) {
690 VariableImpl.this.jsValue = value;
691 if (callback != null) {
692 callback.success();
693 }
694 }
695
696 @Override
697 public void failure(Exception exception) {
698 if (callback != null) {
699 callback.failure(exception);
700 }
701 }
702 };
703 return valueChanger.setValue(getName(), newValue, rawCallback, syncCallbac k);
704 }
705 }
706
707 static abstract class VariableValueChanger {
708 abstract RelayOk setValue(String variableName, JsValue newValue,
709 GenericCallback<JsValue> callback, SyncCallback syncCallback)
710 throws UnsupportedOperationException;
649 } 711 }
650 712
651 private static abstract class ObjectPropertyBase 713 private static abstract class ObjectPropertyBase
652 extends VariableBase implements JsObjectProperty { 714 extends VariableBase implements JsObjectProperty {
715 private final JsValue jsValue;
716
653 ObjectPropertyBase(JsValue jsValue, String name) { 717 ObjectPropertyBase(JsValue jsValue, String name) {
654 super(jsValue, name); 718 super(name);
719 this.jsValue = jsValue;
655 } 720 }
656 721
722 @Override public JsValue getValue() {
723 return jsValue;
724 }
657 @Override public JsObjectProperty asObjectProperty() { 725 @Override public JsObjectProperty asObjectProperty() {
658 return this; 726 return this;
659 } 727 }
660 } 728 }
661 729
662 private static class ObjectType extends ValueType { 730 private static class ObjectType extends ValueType {
663 @Override 731 @Override
664 JsValue build(RemoteObjectValue valueData, WipValueLoader valueLoader) { 732 JsValue build(RemoteObjectValue valueData, WipValueLoader valueLoader) {
665 ValueType secondLevelValueType = 733 ValueType secondLevelValueType =
666 getSafe(PROTOCOL_SUBTYPE_TO_VALUE_TYPE, valueData.subtype()); 734 getSafe(PROTOCOL_SUBTYPE_TO_VALUE_TYPE, valueData.subtype());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 new SingletonPrimitiveType(JsValue.Type.TYPE_UNDEFINED, "undefined")); 775 new SingletonPrimitiveType(JsValue.Type.TYPE_UNDEFINED, "undefined"));
708 776
709 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.OBJECT, new ObjectTyp e()); 777 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.OBJECT, new ObjectTyp e());
710 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.FUNCTION, new Functio nType()); 778 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.FUNCTION, new Functio nType());
711 779
712 assert PROTOCOL_TYPE_TO_VALUE_TYPE.size() == RemoteObjectValue.Type.values() .length; 780 assert PROTOCOL_TYPE_TO_VALUE_TYPE.size() == RemoteObjectValue.Type.values() .length;
713 } 781 }
714 782
715 private static final ValueType DEFAULT_VALUE_TYPE = new ObjectSubtype(JsValue. Type.TYPE_OBJECT); 783 private static final ValueType DEFAULT_VALUE_TYPE = new ObjectSubtype(JsValue. Type.TYPE_OBJECT);
716 } 784 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698