Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; |
| 11 import java.util.Collections; | 11 import java.util.Collections; |
| 12 import java.util.HashMap; | 12 import java.util.HashMap; |
| 13 import java.util.List; | 13 import java.util.List; |
| 14 import java.util.Map; | 14 import java.util.Map; |
| 15 import java.util.SortedMap; | 15 import java.util.SortedMap; |
| 16 import java.util.TreeMap; | 16 import java.util.TreeMap; |
| 17 import java.util.concurrent.atomic.AtomicReference; | 17 import java.util.concurrent.atomic.AtomicReference; |
| 18 import java.util.logging.Logger; | 18 import java.util.logging.Logger; |
| 19 | 19 |
| 20 import org.chromium.sdk.FunctionScopeExtension; | 20 import org.chromium.sdk.FunctionScopeExtension; |
| 21 import org.chromium.sdk.JsArray; | 21 import org.chromium.sdk.JsArray; |
| 22 import org.chromium.sdk.JsDeclarativeVariable; | |
| 22 import org.chromium.sdk.JsEvaluateContext.EvaluateCallback; | 23 import org.chromium.sdk.JsEvaluateContext.EvaluateCallback; |
| 23 import org.chromium.sdk.JsFunction; | 24 import org.chromium.sdk.JsFunction; |
| 24 import org.chromium.sdk.JsObject; | 25 import org.chromium.sdk.JsObject; |
| 25 import org.chromium.sdk.JsObjectProperty; | 26 import org.chromium.sdk.JsObjectProperty; |
| 26 import org.chromium.sdk.JsScope; | 27 import org.chromium.sdk.JsScope; |
| 27 import org.chromium.sdk.JsValue; | 28 import org.chromium.sdk.JsValue; |
| 28 import org.chromium.sdk.JsValue.Type; | 29 import org.chromium.sdk.JsValue.Type; |
| 29 import org.chromium.sdk.JsVariable; | 30 import org.chromium.sdk.JsVariable; |
| 30 import org.chromium.sdk.RelayOk; | 31 import org.chromium.sdk.RelayOk; |
| 31 import org.chromium.sdk.Script; | 32 import org.chromium.sdk.Script; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 97 |
| 97 public JsObjectProperty createObjectProperty(final PropertyDescriptorValue pro pertyDescriptor, | 98 public JsObjectProperty createObjectProperty(final PropertyDescriptorValue pro pertyDescriptor, |
| 98 final String hostObjectRefId, String name) { | 99 final String hostObjectRefId, String name) { |
| 99 JsValue jsValue = wrap(propertyDescriptor.value()); | 100 JsValue jsValue = wrap(propertyDescriptor.value()); |
| 100 | 101 |
| 101 final JsValue getter = wrap(propertyDescriptor.get()); | 102 final JsValue getter = wrap(propertyDescriptor.get()); |
| 102 | 103 |
| 103 final JsValue setter = wrap(propertyDescriptor.set()); | 104 final JsValue setter = wrap(propertyDescriptor.set()); |
| 104 | 105 |
| 105 return new ObjectPropertyBase(jsValue, name) { | 106 return new ObjectPropertyBase(jsValue, name) { |
| 106 | |
| 107 @Override public boolean isMutable() { | |
| 108 return false; | |
| 109 } | |
| 110 @Override public boolean isWritable() { | 107 @Override public boolean isWritable() { |
| 111 return propertyDescriptor.writable(); | 108 return propertyDescriptor.writable(); |
| 112 } | 109 } |
| 113 @Override public JsValue getGetter() { | 110 @Override public JsValue getGetter() { |
| 114 return getter; | 111 return getter; |
| 115 } | 112 } |
| 116 @Override public JsValue getSetter() { | 113 @Override public JsValue getSetter() { |
| 117 return setter; | 114 return setter; |
| 118 } | 115 } |
| 119 @Override public boolean isConfigurable() { | 116 @Override public boolean isConfigurable() { |
| 120 return propertyDescriptor.configurable(); | 117 return propertyDescriptor.configurable(); |
| 121 } | 118 } |
| 122 @Override public boolean isEnumerable() { | 119 @Override public boolean isEnumerable() { |
| 123 return propertyDescriptor.enumerable(); | 120 return propertyDescriptor.enumerable(); |
| 124 } | 121 } |
| 125 | 122 |
| 126 @Override | 123 @Override |
| 127 public JsFunction getGetterAsFunction() { | 124 public JsFunction getGetterAsFunction() { |
| 128 JsObject getterObject = getter.asObject(); | 125 JsObject getterObject = getter.asObject(); |
| 129 if (getterObject == null) { | 126 if (getterObject == null) { |
| 130 return null; | 127 return null; |
| 131 } | 128 } |
| 132 return getterObject.asFunction(); | 129 return getterObject.asFunction(); |
| 133 } | 130 } |
| 134 | 131 |
| 135 @Override | 132 @Override |
| 136 public RelayOk setValue(JsValue newValue, SetValueCallback callback, | |
| 137 SyncCallback syncCallback) throws UnsupportedOperationException { | |
| 138 throw new UnsupportedOperationException(); | |
| 139 } | |
| 140 | |
| 141 @Override | |
| 142 public RelayOk evaluateGet(EvaluateCallback callback, SyncCallback syncCal lback) { | 133 public RelayOk evaluateGet(EvaluateCallback callback, SyncCallback syncCal lback) { |
| 143 WipContextBuilder.GlobalEvaluateContext evaluateContext = | 134 WipContextBuilder.GlobalEvaluateContext evaluateContext = |
| 144 new WipContextBuilder.GlobalEvaluateContext(valueLoader); | 135 new WipContextBuilder.GlobalEvaluateContext(valueLoader); |
| 145 | 136 |
| 146 JsFunction getterFunction = getGetterAsFunction(); | 137 JsFunction getterFunction = getGetterAsFunction(); |
| 147 if (getterFunction == null) { | 138 if (getterFunction == null) { |
| 148 throw new RuntimeException("Getter is not a function"); | 139 throw new RuntimeException("Getter is not a function"); |
| 149 } | 140 } |
| 150 | 141 |
| 151 Map<String, SerializableValue> context = new HashMap<String, Serializabl eValue>(2); | 142 Map<String, SerializableValue> context = new HashMap<String, Serializabl eValue>(2); |
| 152 context.put(GETTER_VAR_NAME, (SerializableValue) getterFunction); | 143 context.put(GETTER_VAR_NAME, (SerializableValue) getterFunction); |
| 153 context.put(OBJECT_VAR_NAME, SerializableValue.Util.wrapRefId(hostObject RefId)); | 144 context.put(OBJECT_VAR_NAME, SerializableValue.Util.wrapRefId(hostObject RefId)); |
| 154 | 145 |
| 155 return evaluateContext.evaluateAsyncImpl(EVALUATE_EXPRESSION, | 146 return evaluateContext.evaluateAsyncImpl(EVALUATE_EXPRESSION, |
| 156 context, valueLoader, callback, syncCallback); | 147 context, valueLoader, callback, syncCallback); |
| 157 } | 148 } |
| 158 private static final String GETTER_VAR_NAME = "gttr"; | 149 private static final String GETTER_VAR_NAME = "gttr"; |
| 159 private static final String OBJECT_VAR_NAME = "obj"; | 150 private static final String OBJECT_VAR_NAME = "obj"; |
| 160 private static final String EVALUATE_EXPRESSION = | 151 private static final String EVALUATE_EXPRESSION = |
| 161 GETTER_VAR_NAME + ".call(" + OBJECT_VAR_NAME + ")"; | 152 GETTER_VAR_NAME + ".call(" + OBJECT_VAR_NAME + ")"; |
| 162 }; | 153 }; |
| 163 } | 154 } |
| 164 | 155 |
| 165 public JsVariable createVariable(RemoteObjectValue valueData, String name, | 156 public JsVariable createVariable(RemoteObjectValue valueData, String name) { |
| 157 JsValue jsValue = wrap(valueData); | |
|
apavlov
2013/02/20 09:02:02
is it necessary?
Peter Rybin
2013/02/21 19:08:48
Done.
| |
| 158 return new VariableImpl(name, jsValue); | |
| 159 } | |
| 160 | |
| 161 public JsDeclarativeVariable createDeclarativeVariable(RemoteObjectValue value Data, String name, | |
| 166 final WipContextBuilder.ScopeParams scopeParams) { | 162 final WipContextBuilder.ScopeParams scopeParams) { |
| 167 JsValue jsValue = wrap(valueData); | 163 JsValue jsValue = wrap(valueData); |
| 168 | 164 VariableValueChanger valueChanger = new VariableValueChanger() { |
| 169 VariableValueChanger valueChanger; | 165 @Override |
| 170 if (scopeParams == null) { | 166 RelayOk setValue(String variableName, JsValue newValue, |
| 171 valueChanger = null; | 167 final GenericCallback<JsValue> callback, SyncCallback syncCallback ) |
| 172 } else { | 168 throws UnsupportedOperationException { |
| 173 valueChanger = new VariableValueChanger() { | 169 final JsValueBase jsValueBase = JsValueBase.cast(newValue); |
| 174 @Override | 170 SetVariableValueParams params = new SetVariableValueParams(scopePara ms.getScopeNumber(), |
| 175 RelayOk setValue(String variableName, JsValue newValue, | 171 variableName, jsValueBase.createCallArgumentParam(), scopeParams .getCallFrameId(), |
| 176 final GenericCallback<JsValue> callback, SyncCallback syncCallback) | 172 scopeParams.getFunctionId()); |
| 177 throws UnsupportedOperationException { | 173 WipCommandCallback rawCallback = new WipCommandCallback.Default() { |
| 178 final JsValueBase jsValueBase = JsValueBase.cast(newValue); | 174 @Override protected void onSuccess(Success success) { |
| 179 SetVariableValueParams params = new SetVariableValueParams(scopeParams .getScopeNumber(), | 175 callback.success(jsValueBase); |
| 180 variableName, jsValueBase.createCallArgumentParam(), scopeParams.g etCallFrameId(), | 176 } |
| 181 scopeParams.getFunctionId()); | 177 @Override protected void onError(String message) { |
| 182 WipCommandCallback rawCallback = new WipCommandCallback.Default() { | 178 callback.failure(new Exception(message)); |
| 183 @Override protected void onSuccess(Success success) { | 179 } |
| 184 callback.success(jsValueBase); | 180 }; |
| 185 } | 181 return valueLoader.getTabImpl().getCommandProcessor().send(params, |
| 186 @Override protected void onError(String message) { | 182 rawCallback, syncCallback); |
| 187 callback.failure(new Exception(message)); | 183 } |
| 188 } | 184 }; |
| 189 }; | 185 return new DeclarativeVariable(name, jsValue, valueChanger); |
| 190 return valueLoader.getTabImpl().getCommandProcessor().send(params, | |
| 191 rawCallback, syncCallback); | |
| 192 } | |
| 193 }; | |
| 194 } | |
| 195 | |
| 196 return new VariableImpl(name, jsValue, valueChanger); | |
| 197 } | 186 } |
| 198 | 187 |
| 199 public JsValue wrap(RemoteObjectValue valueData) { | 188 public JsValue wrap(RemoteObjectValue valueData) { |
| 200 if (valueData == null) { | 189 if (valueData == null) { |
| 201 return null; | 190 return null; |
| 202 } | 191 } |
| 203 return getValueType(valueData).build(valueData, valueLoader); | 192 return getValueType(valueData).build(valueData, valueLoader); |
| 204 } | 193 } |
| 205 | 194 |
| 206 private static ValueType getValueType(RemoteObjectValue valueData) { | 195 private static ValueType getValueType(RemoteObjectValue valueData) { |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 640 } | 629 } |
| 641 }; | 630 }; |
| 642 | 631 |
| 643 private static abstract class VariableBase implements JsVariable { | 632 private static abstract class VariableBase implements JsVariable { |
| 644 private final String name; | 633 private final String name; |
| 645 | 634 |
| 646 VariableBase(String name) { | 635 VariableBase(String name) { |
| 647 this.name = name; | 636 this.name = name; |
| 648 } | 637 } |
| 649 | 638 |
| 650 @Override | 639 @Override public String getName() { |
| 651 public boolean isReadable() { | |
| 652 return true; | |
| 653 } | |
| 654 | |
| 655 @Override | |
| 656 public String getName() { | |
| 657 return name; | 640 return name; |
| 658 } | 641 } |
| 659 } | 642 } |
| 660 | 643 |
| 661 private static class VariableImpl extends VariableBase { | 644 private static class VariableImpl extends VariableBase { |
| 645 private final JsValue jsValue; | |
| 646 | |
| 647 VariableImpl(String name, JsValue jsValue) { | |
| 648 super(name); | |
| 649 this.jsValue = jsValue; | |
| 650 } | |
| 651 | |
| 652 @Override public JsObjectProperty asObjectProperty() { | |
| 653 return null; | |
| 654 } | |
| 655 @Override public JsDeclarativeVariable asDeclarativeVariable() { | |
| 656 return null; | |
| 657 } | |
| 658 @Override public JsValue getValue() { | |
| 659 return jsValue; | |
| 660 } | |
| 661 } | |
| 662 | |
| 663 private static class DeclarativeVariable extends VariableBase implements JsDec larativeVariable { | |
| 662 private final VariableValueChanger valueChanger; | 664 private final VariableValueChanger valueChanger; |
| 663 private volatile JsValue jsValue; | 665 private volatile JsValue jsValue; |
| 664 | 666 |
| 665 VariableImpl(String name, JsValue jsValue, VariableValueChanger valueChanger ) { | 667 DeclarativeVariable(String name, JsValue jsValue, VariableValueChanger value Changer) { |
| 666 super(name); | 668 super(name); |
| 667 this.jsValue = jsValue; | 669 this.jsValue = jsValue; |
| 668 this.valueChanger = valueChanger; | 670 this.valueChanger = valueChanger; |
| 669 } | 671 } |
| 670 | 672 |
| 671 @Override public JsObjectProperty asObjectProperty() { | 673 @Override public JsObjectProperty asObjectProperty() { |
| 672 return null; | 674 return null; |
| 673 } | 675 } |
| 676 @Override public JsDeclarativeVariable asDeclarativeVariable() { | |
| 677 return this; | |
| 678 } | |
| 674 @Override public boolean isMutable() { | 679 @Override public boolean isMutable() { |
| 675 return valueChanger != null; | 680 return valueChanger != null; |
| 676 } | 681 } |
| 677 @Override public JsValue getValue() { | 682 @Override public JsValue getValue() { |
| 678 return jsValue; | 683 return jsValue; |
| 679 } | 684 } |
| 680 | 685 |
| 681 @Override | 686 @Override |
| 682 public RelayOk setValue(JsValue newValue, final SetValueCallback callback, | 687 public RelayOk setValue(JsValue newValue, final SetValueCallback callback, |
| 683 SyncCallback syncCallback) throws UnsupportedOperationException { | 688 SyncCallback syncCallback) throws UnsupportedOperationException { |
| 684 if (valueChanger == null) { | 689 if (valueChanger == null) { |
| 685 throw new UnsupportedOperationException(); | 690 throw new UnsupportedOperationException(); |
| 686 } | 691 } |
| 687 GenericCallback<JsValue> rawCallback = new GenericCallback<JsValue>() { | 692 GenericCallback<JsValue> rawCallback = new GenericCallback<JsValue>() { |
| 688 @Override | 693 @Override |
| 689 public void success(JsValue value) { | 694 public void success(JsValue value) { |
| 690 VariableImpl.this.jsValue = value; | 695 DeclarativeVariable.this.jsValue = value; |
| 691 if (callback != null) { | 696 if (callback != null) { |
| 692 callback.success(); | 697 callback.success(); |
| 693 } | 698 } |
| 694 } | 699 } |
| 695 | 700 |
| 696 @Override | 701 @Override |
| 697 public void failure(Exception exception) { | 702 public void failure(Exception exception) { |
| 698 if (callback != null) { | 703 if (callback != null) { |
| 699 callback.failure(exception); | 704 callback.failure(exception); |
| 700 } | 705 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 718 super(name); | 723 super(name); |
| 719 this.jsValue = jsValue; | 724 this.jsValue = jsValue; |
| 720 } | 725 } |
| 721 | 726 |
| 722 @Override public JsValue getValue() { | 727 @Override public JsValue getValue() { |
| 723 return jsValue; | 728 return jsValue; |
| 724 } | 729 } |
| 725 @Override public JsObjectProperty asObjectProperty() { | 730 @Override public JsObjectProperty asObjectProperty() { |
| 726 return this; | 731 return this; |
| 727 } | 732 } |
| 733 @Override public JsDeclarativeVariable asDeclarativeVariable() { | |
| 734 return null; | |
| 735 } | |
| 728 } | 736 } |
| 729 | 737 |
| 730 private static class ObjectType extends ValueType { | 738 private static class ObjectType extends ValueType { |
| 731 @Override | 739 @Override |
| 732 JsValue build(RemoteObjectValue valueData, WipValueLoader valueLoader) { | 740 JsValue build(RemoteObjectValue valueData, WipValueLoader valueLoader) { |
| 733 ValueType secondLevelValueType = | 741 ValueType secondLevelValueType = |
| 734 getSafe(PROTOCOL_SUBTYPE_TO_VALUE_TYPE, valueData.subtype()); | 742 getSafe(PROTOCOL_SUBTYPE_TO_VALUE_TYPE, valueData.subtype()); |
| 735 | 743 |
| 736 if (secondLevelValueType == null) { | 744 if (secondLevelValueType == null) { |
| 737 LOGGER.severe("Unexpected value type: " + valueData.type() + " " + value Data.subtype()); | 745 LOGGER.severe("Unexpected value type: " + valueData.type() + " " + value Data.subtype()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 775 new SingletonPrimitiveType(JsValue.Type.TYPE_UNDEFINED, "undefined")); | 783 new SingletonPrimitiveType(JsValue.Type.TYPE_UNDEFINED, "undefined")); |
| 776 | 784 |
| 777 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.OBJECT, new ObjectTyp e()); | 785 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.OBJECT, new ObjectTyp e()); |
| 778 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.FUNCTION, new Functio nType()); | 786 PROTOCOL_TYPE_TO_VALUE_TYPE.put(RemoteObjectValue.Type.FUNCTION, new Functio nType()); |
| 779 | 787 |
| 780 assert PROTOCOL_TYPE_TO_VALUE_TYPE.size() == RemoteObjectValue.Type.values() .length; | 788 assert PROTOCOL_TYPE_TO_VALUE_TYPE.size() == RemoteObjectValue.Type.values() .length; |
| 781 } | 789 } |
| 782 | 790 |
| 783 private static final ValueType DEFAULT_VALUE_TYPE = new ObjectSubtype(JsValue. Type.TYPE_OBJECT); | 791 private static final ValueType DEFAULT_VALUE_TYPE = new ObjectSubtype(JsValue. Type.TYPE_OBJECT); |
| 784 } | 792 } |
| OLD | NEW |