jsHandleDispatcher.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.JSHandleDispatcher = void 0;
  6. exports.parseArgument = parseArgument;
  7. exports.parseValue = parseValue;
  8. exports.serializeResult = serializeResult;
  9. var _dispatcher = require("./dispatcher");
  10. var _elementHandlerDispatcher = require("./elementHandlerDispatcher");
  11. var _serializers = require("../../protocol/serializers");
  12. /**
  13. * Copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class JSHandleDispatcher extends _dispatcher.Dispatcher {
  28. constructor(scope, jsHandle) {
  29. // Do not call this directly, use createHandle() instead.
  30. super(scope, jsHandle, jsHandle.asElement() ? 'ElementHandle' : 'JSHandle', {
  31. preview: jsHandle.toString()
  32. });
  33. this._type_JSHandle = true;
  34. jsHandle._setPreviewCallback(preview => this._dispatchEvent('previewUpdated', {
  35. preview
  36. }));
  37. }
  38. async evaluateExpression(params) {
  39. return {
  40. value: serializeResult(await this._object.evaluateExpression(params.expression, {
  41. isFunction: params.isFunction
  42. }, parseArgument(params.arg)))
  43. };
  44. }
  45. async evaluateExpressionHandle(params) {
  46. const jsHandle = await this._object.evaluateExpressionHandle(params.expression, {
  47. isFunction: params.isFunction
  48. }, parseArgument(params.arg));
  49. return {
  50. handle: _elementHandlerDispatcher.ElementHandleDispatcher.fromJSHandle(this.parentScope(), jsHandle)
  51. };
  52. }
  53. async getProperty(params) {
  54. const jsHandle = await this._object.getProperty(params.name);
  55. return {
  56. handle: _elementHandlerDispatcher.ElementHandleDispatcher.fromJSHandle(this.parentScope(), jsHandle)
  57. };
  58. }
  59. async getPropertyList() {
  60. const map = await this._object.getProperties();
  61. const properties = [];
  62. for (const [name, value] of map) properties.push({
  63. name,
  64. value: _elementHandlerDispatcher.ElementHandleDispatcher.fromJSHandle(this.parentScope(), value)
  65. });
  66. return {
  67. properties
  68. };
  69. }
  70. async jsonValue() {
  71. return {
  72. value: serializeResult(await this._object.jsonValue())
  73. };
  74. }
  75. async objectCount(params) {
  76. return {
  77. count: await this._object.objectCount()
  78. };
  79. }
  80. async dispose(_, metadata) {
  81. metadata.potentiallyClosesScope = true;
  82. this._object.dispose();
  83. this._dispose();
  84. }
  85. }
  86. // Generic channel parser converts guids to JSHandleDispatchers,
  87. // and this function takes care of coverting them into underlying JSHandles.
  88. exports.JSHandleDispatcher = JSHandleDispatcher;
  89. function parseArgument(arg) {
  90. return (0, _serializers.parseSerializedValue)(arg.value, arg.handles.map(a => a._object));
  91. }
  92. function parseValue(v) {
  93. return (0, _serializers.parseSerializedValue)(v, []);
  94. }
  95. function serializeResult(arg) {
  96. return (0, _serializers.serializeValue)(arg, value => ({
  97. fallThrough: value
  98. }));
  99. }