You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavaScriptCallbackHelper.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import com.vaadin.shared.JavaScriptConnectorState;
  24. import com.vaadin.ui.AbstractJavaScriptComponent;
  25. import com.vaadin.ui.JavaScript.JavaScriptCallbackRpc;
  26. import com.vaadin.ui.JavaScriptFunction;
  27. import com.vaadin.util.ReflectTools;
  28. import elemental.json.JsonArray;
  29. import elemental.json.JsonException;
  30. /**
  31. * Internal helper class used to implement functionality common to
  32. * {@link AbstractJavaScriptComponent} and {@link AbstractJavaScriptExtension}.
  33. * Corresponding support in client-side code is in
  34. * {@link com.vaadin.client.JavaScriptConnectorHelper}.
  35. * <p>
  36. * You should most likely no use this class directly.
  37. *
  38. * @author Vaadin Ltd
  39. * @since 7.0.0
  40. */
  41. public class JavaScriptCallbackHelper implements Serializable {
  42. private static final Method CALL_METHOD = ReflectTools.findMethod(
  43. JavaScriptCallbackRpc.class, "call", String.class, JsonArray.class);
  44. private final AbstractClientConnector connector;
  45. private final Map<String, JavaScriptFunction> callbacks = new HashMap<>();
  46. private JavaScriptCallbackRpc javascriptCallbackRpc;
  47. public JavaScriptCallbackHelper(AbstractClientConnector connector) {
  48. this.connector = connector;
  49. }
  50. public void registerCallback(String functionName,
  51. JavaScriptFunction javaScriptCallback) {
  52. callbacks.put(functionName, javaScriptCallback);
  53. JavaScriptConnectorState state = getConnectorState();
  54. state.getCallbackNames().add(functionName);
  55. ensureRpc();
  56. }
  57. private JavaScriptConnectorState getConnectorState() {
  58. JavaScriptConnectorState state = (JavaScriptConnectorState) connector
  59. .getState();
  60. return state;
  61. }
  62. private void ensureRpc() {
  63. if (javascriptCallbackRpc == null) {
  64. // Note that javascriptCallbackRpc is not a lambda to make sure it
  65. // can be serialized properly
  66. javascriptCallbackRpc = new JavaScriptCallbackRpc() {
  67. @Override
  68. public void call(String name, JsonArray arguments) {
  69. JavaScriptFunction callback = callbacks.get(name);
  70. try {
  71. callback.call(arguments);
  72. } catch (JsonException e) {
  73. throw new IllegalArgumentException(e);
  74. }
  75. }
  76. };
  77. connector.registerRpc(javascriptCallbackRpc);
  78. }
  79. }
  80. public void invokeCallback(String name, Object... arguments) {
  81. if (callbacks.containsKey(name)) {
  82. throw new IllegalStateException("Can't call callback " + name
  83. + " on the client because a callback with the same name is registered on the server.");
  84. }
  85. JsonArray args = (JsonArray) JsonCodec
  86. .encode(arguments, null, Object[].class, null)
  87. .getEncodedValue();
  88. connector.addMethodInvocationToQueue(
  89. JavaScriptCallbackRpc.class.getName(), CALL_METHOD,
  90. new Object[] { name, args });
  91. }
  92. public void registerRpc(Class<?> rpcInterfaceType) {
  93. if (rpcInterfaceType == JavaScriptCallbackRpc.class) {
  94. // Ignore
  95. return;
  96. }
  97. Map<String, Set<String>> rpcInterfaces = getConnectorState()
  98. .getRpcInterfaces();
  99. String interfaceName = rpcInterfaceType.getName();
  100. if (!rpcInterfaces.containsKey(interfaceName)) {
  101. Set<String> methodNames = new HashSet<>();
  102. for (Method method : rpcInterfaceType.getMethods()) {
  103. methodNames.add(method.getName());
  104. }
  105. rpcInterfaces.put(interfaceName, methodNames);
  106. }
  107. }
  108. }