All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.chrome.devtools.client.core.runtime.Runtime Maven / Gradle / Ivy

There is a newer version: 94.0.4606.61
Show newest version
package com.hubspot.chrome.devtools.client.core.runtime;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.chrome.devtools.base.ChromeRequest;
import com.hubspot.chrome.devtools.base.ChromeSessionCore;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Runtime domain exposes JavaScript runtime by means of remote evaluation and mirror objects.
 * Evaluation results are returned as mirror object that expose object type, string representation
 * and unique identifier that can be used for further object reference. Original objects are
 * maintained in memory unless they are either explicitly released or are released along with the
 * other objects in their object group.
 */
public final class Runtime {
  ChromeSessionCore chromeSession;

  ObjectMapper objectMapper;

  public Runtime(ChromeSessionCore chromeSession, ObjectMapper objectMapper) {
    this.chromeSession = chromeSession;
    this.objectMapper = objectMapper;
  }

  /**
   * Add handler to promise with given promise object id.
   *
   * @param promiseObjectId  Identifier of the promise.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object that should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   */
  public AwaitPromiseResult awaitPromise(RemoteObjectId promiseObjectId, Boolean returnByValue,
      Boolean generatePreview) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.awaitPromise");
    chromeRequest
        .putParams("promiseObjectId", promiseObjectId)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Add handler to promise with given promise object id.
   *
   * @param promiseObjectId  Identifier of the promise.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object that should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   */
  public CompletableFuture awaitPromiseAsync(RemoteObjectId promiseObjectId,
      Boolean returnByValue, Boolean generatePreview) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.awaitPromise");
    chromeRequest
        .putParams("promiseObjectId", promiseObjectId)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Calls function with given declaration on the given object. Object group of the result is
   * inherited from the target object.
   *
   * @param functionDeclaration  Declaration of the function to call.
   * @param objectId [Optional] Identifier of the object to call function on. Either objectId or executionContextId should
   * be specified.
   * @param arguments [Optional] Call arguments. All call arguments must belong to the same JavaScript world as the target
   * object.
   * @param silent [Optional] In silent mode exceptions thrown during evaluation are not reported and do not pause
   * execution. Overrides `setPauseOnException` state.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object which should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   * @param userGesture [Optional] Whether execution should be treated as initiated by user in the UI.
   * @param awaitPromise [Optional] Whether execution should `await` for resulting value and return once awaited promise is
   * resolved.
   * @param executionContextId [Optional] Specifies execution context which global object will be used to call function on. Either
   * executionContextId or objectId should be specified.
   * @param objectGroup [Optional] Symbolic group name that can be used to release multiple objects. If objectGroup is not
   * specified and objectId is, objectGroup will be inherited from object.
   */
  public CallFunctionOnResult callFunctionOn(String functionDeclaration, RemoteObjectId objectId,
      List arguments, Boolean silent, Boolean returnByValue, Boolean generatePreview,
      Boolean userGesture, Boolean awaitPromise, ExecutionContextId executionContextId,
      String objectGroup) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.callFunctionOn");
    chromeRequest
        .putParams("functionDeclaration", functionDeclaration)
        .putParams("objectId", objectId)
        .putParams("arguments", arguments)
        .putParams("silent", silent)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview)
        .putParams("userGesture", userGesture)
        .putParams("awaitPromise", awaitPromise)
        .putParams("executionContextId", executionContextId)
        .putParams("objectGroup", objectGroup);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Calls function with given declaration on the given object. Object group of the result is
   * inherited from the target object.
   *
   * @param functionDeclaration  Declaration of the function to call.
   * @param objectId [Optional] Identifier of the object to call function on. Either objectId or executionContextId should
   * be specified.
   * @param arguments [Optional] Call arguments. All call arguments must belong to the same JavaScript world as the target
   * object.
   * @param silent [Optional] In silent mode exceptions thrown during evaluation are not reported and do not pause
   * execution. Overrides `setPauseOnException` state.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object which should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   * @param userGesture [Optional] Whether execution should be treated as initiated by user in the UI.
   * @param awaitPromise [Optional] Whether execution should `await` for resulting value and return once awaited promise is
   * resolved.
   * @param executionContextId [Optional] Specifies execution context which global object will be used to call function on. Either
   * executionContextId or objectId should be specified.
   * @param objectGroup [Optional] Symbolic group name that can be used to release multiple objects. If objectGroup is not
   * specified and objectId is, objectGroup will be inherited from object.
   */
  public CompletableFuture callFunctionOnAsync(String functionDeclaration,
      RemoteObjectId objectId, List arguments, Boolean silent, Boolean returnByValue,
      Boolean generatePreview, Boolean userGesture, Boolean awaitPromise,
      ExecutionContextId executionContextId, String objectGroup) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.callFunctionOn");
    chromeRequest
        .putParams("functionDeclaration", functionDeclaration)
        .putParams("objectId", objectId)
        .putParams("arguments", arguments)
        .putParams("silent", silent)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview)
        .putParams("userGesture", userGesture)
        .putParams("awaitPromise", awaitPromise)
        .putParams("executionContextId", executionContextId)
        .putParams("objectGroup", objectGroup);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Compiles expression.
   *
   * @param expression  Expression to compile.
   * @param sourceURL  Source url to be set for the script.
   * @param persistScript  Specifies whether the compiled script should be persisted.
   * @param executionContextId [Optional] Specifies in which execution context to perform script run. If the parameter is omitted the
   * evaluation will be performed in the context of the inspected page.
   */
  public CompileScriptResult compileScript(String expression, String sourceURL,
      Boolean persistScript, ExecutionContextId executionContextId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.compileScript");
    chromeRequest
        .putParams("expression", expression)
        .putParams("sourceURL", sourceURL)
        .putParams("persistScript", persistScript)
        .putParams("executionContextId", executionContextId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Compiles expression.
   *
   * @param expression  Expression to compile.
   * @param sourceURL  Source url to be set for the script.
   * @param persistScript  Specifies whether the compiled script should be persisted.
   * @param executionContextId [Optional] Specifies in which execution context to perform script run. If the parameter is omitted the
   * evaluation will be performed in the context of the inspected page.
   */
  public CompletableFuture compileScriptAsync(String expression,
      String sourceURL, Boolean persistScript, ExecutionContextId executionContextId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.compileScript");
    chromeRequest
        .putParams("expression", expression)
        .putParams("sourceURL", sourceURL)
        .putParams("persistScript", persistScript)
        .putParams("executionContextId", executionContextId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Disables reporting of execution contexts creation.
   */
  public void disable() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.disable");
    chromeSession.send(chromeRequest);
  }

  /**
   * Disables reporting of execution contexts creation.
   */
  public void disableAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.disable");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Discards collected exceptions and console API calls.
   */
  public void discardConsoleEntries() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.discardConsoleEntries");
    chromeSession.send(chromeRequest);
  }

  /**
   * Discards collected exceptions and console API calls.
   */
  public void discardConsoleEntriesAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.discardConsoleEntries");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Enables reporting of execution contexts creation by means of `executionContextCreated` event.
   * When the reporting gets enabled the event will be sent immediately for each existing execution
   * context.
   */
  public void enable() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.enable");
    chromeSession.send(chromeRequest);
  }

  /**
   * Enables reporting of execution contexts creation by means of `executionContextCreated` event.
   * When the reporting gets enabled the event will be sent immediately for each existing execution
   * context.
   */
  public void enableAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.enable");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Evaluates expression on global object.
   *
   * @param expression  Expression to evaluate.
   * @param objectGroup [Optional] Symbolic group name that can be used to release multiple objects.
   * @param includeCommandLineAPI [Optional] Determines whether Command Line API should be available during the evaluation.
   * @param silent [Optional] In silent mode exceptions thrown during evaluation are not reported and do not pause
   * execution. Overrides `setPauseOnException` state.
   * @param contextId [Optional] Specifies in which execution context to perform evaluation. If the parameter is omitted the
   * evaluation will be performed in the context of the inspected page.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object that should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   * @param userGesture [Optional] Whether execution should be treated as initiated by user in the UI.
   * @param awaitPromise [Optional] Whether execution should `await` for resulting value and return once awaited promise is
   * resolved.
   */
  public EvaluateResult evaluate(String expression, String objectGroup,
      Boolean includeCommandLineAPI, Boolean silent, ExecutionContextId contextId,
      Boolean returnByValue, Boolean generatePreview, Boolean userGesture, Boolean awaitPromise) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.evaluate");
    chromeRequest
        .putParams("expression", expression)
        .putParams("objectGroup", objectGroup)
        .putParams("includeCommandLineAPI", includeCommandLineAPI)
        .putParams("silent", silent)
        .putParams("contextId", contextId)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview)
        .putParams("userGesture", userGesture)
        .putParams("awaitPromise", awaitPromise);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Evaluates expression on global object.
   *
   * @param expression  Expression to evaluate.
   * @param objectGroup [Optional] Symbolic group name that can be used to release multiple objects.
   * @param includeCommandLineAPI [Optional] Determines whether Command Line API should be available during the evaluation.
   * @param silent [Optional] In silent mode exceptions thrown during evaluation are not reported and do not pause
   * execution. Overrides `setPauseOnException` state.
   * @param contextId [Optional] Specifies in which execution context to perform evaluation. If the parameter is omitted the
   * evaluation will be performed in the context of the inspected page.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object that should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   * @param userGesture [Optional] Whether execution should be treated as initiated by user in the UI.
   * @param awaitPromise [Optional] Whether execution should `await` for resulting value and return once awaited promise is
   * resolved.
   */
  public CompletableFuture evaluateAsync(String expression, String objectGroup,
      Boolean includeCommandLineAPI, Boolean silent, ExecutionContextId contextId,
      Boolean returnByValue, Boolean generatePreview, Boolean userGesture, Boolean awaitPromise) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.evaluate");
    chromeRequest
        .putParams("expression", expression)
        .putParams("objectGroup", objectGroup)
        .putParams("includeCommandLineAPI", includeCommandLineAPI)
        .putParams("silent", silent)
        .putParams("contextId", contextId)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview)
        .putParams("userGesture", userGesture)
        .putParams("awaitPromise", awaitPromise);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns properties of a given object. Object group of the result is inherited from the target
   * object.
   *
   * @param objectId  Identifier of the object to return properties for.
   * @param ownProperties [Optional] If true, returns properties belonging only to the element itself, not to its prototype
   * chain.
   * @param accessorPropertiesOnly [Optional] If true, returns accessor properties (with getter/setter) only; internal properties are not
   * returned either.
   * @param generatePreview [Optional] Whether preview should be generated for the results.
   */
  public GetPropertiesResult getProperties(RemoteObjectId objectId, Boolean ownProperties,
      Boolean accessorPropertiesOnly, Boolean generatePreview) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.getProperties");
    chromeRequest
        .putParams("objectId", objectId)
        .putParams("ownProperties", ownProperties)
        .putParams("accessorPropertiesOnly", accessorPropertiesOnly)
        .putParams("generatePreview", generatePreview);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns properties of a given object. Object group of the result is inherited from the target
   * object.
   *
   * @param objectId  Identifier of the object to return properties for.
   * @param ownProperties [Optional] If true, returns properties belonging only to the element itself, not to its prototype
   * chain.
   * @param accessorPropertiesOnly [Optional] If true, returns accessor properties (with getter/setter) only; internal properties are not
   * returned either.
   * @param generatePreview [Optional] Whether preview should be generated for the results.
   */
  public CompletableFuture getPropertiesAsync(RemoteObjectId objectId,
      Boolean ownProperties, Boolean accessorPropertiesOnly, Boolean generatePreview) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.getProperties");
    chromeRequest
        .putParams("objectId", objectId)
        .putParams("ownProperties", ownProperties)
        .putParams("accessorPropertiesOnly", accessorPropertiesOnly)
        .putParams("generatePreview", generatePreview);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Returns all let, const and class variables from global scope.
   *
   * @param executionContextId [Optional] Specifies in which execution context to lookup global scope variables.
   */
  public List globalLexicalScopeNames(ExecutionContextId executionContextId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.globalLexicalScopeNames");
    chromeRequest
        .putParams("executionContextId", executionContextId);
    return chromeSession.send(chromeRequest, new TypeReference>(){});
  }

  /**
   * Returns all let, const and class variables from global scope.
   *
   * @param executionContextId [Optional] Specifies in which execution context to lookup global scope variables.
   */
  public CompletableFuture> globalLexicalScopeNamesAsync(
      ExecutionContextId executionContextId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.globalLexicalScopeNames");
    chromeRequest
        .putParams("executionContextId", executionContextId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference>(){});
  }

  public RemoteObject queryObjects(RemoteObjectId prototypeObjectId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.queryObjects");
    chromeRequest
        .putParams("prototypeObjectId", prototypeObjectId);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  public CompletableFuture queryObjectsAsync(RemoteObjectId prototypeObjectId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.queryObjects");
    chromeRequest
        .putParams("prototypeObjectId", prototypeObjectId);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  /**
   * Releases remote object with given id.
   *
   * @param objectId  Identifier of the object to release.
   */
  public void releaseObject(RemoteObjectId objectId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.releaseObject");
    chromeRequest
        .putParams("objectId", objectId);
    chromeSession.send(chromeRequest);
  }

  /**
   * Releases remote object with given id.
   *
   * @param objectId  Identifier of the object to release.
   */
  public void releaseObjectAsync(RemoteObjectId objectId) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.releaseObject");
    chromeRequest
        .putParams("objectId", objectId);
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Releases all remote objects that belong to a given group.
   *
   * @param objectGroup  Symbolic object group name.
   */
  public void releaseObjectGroup(String objectGroup) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.releaseObjectGroup");
    chromeRequest
        .putParams("objectGroup", objectGroup);
    chromeSession.send(chromeRequest);
  }

  /**
   * Releases all remote objects that belong to a given group.
   *
   * @param objectGroup  Symbolic object group name.
   */
  public void releaseObjectGroupAsync(String objectGroup) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.releaseObjectGroup");
    chromeRequest
        .putParams("objectGroup", objectGroup);
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Tells inspected instance to run if it was waiting for debugger to attach.
   */
  public void runIfWaitingForDebugger() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.runIfWaitingForDebugger");
    chromeSession.send(chromeRequest);
  }

  /**
   * Tells inspected instance to run if it was waiting for debugger to attach.
   */
  public void runIfWaitingForDebuggerAsync() {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.runIfWaitingForDebugger");
    chromeSession.sendAsync(chromeRequest);
  }

  /**
   * Runs script with given id in a given context.
   *
   * @param scriptId  Id of the script to run.
   * @param executionContextId [Optional] Specifies in which execution context to perform script run. If the parameter is omitted the
   * evaluation will be performed in the context of the inspected page.
   * @param objectGroup [Optional] Symbolic group name that can be used to release multiple objects.
   * @param silent [Optional] In silent mode exceptions thrown during evaluation are not reported and do not pause
   * execution. Overrides `setPauseOnException` state.
   * @param includeCommandLineAPI [Optional] Determines whether Command Line API should be available during the evaluation.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object which should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   * @param awaitPromise [Optional] Whether execution should `await` for resulting value and return once awaited promise is
   * resolved.
   */
  public RunScriptResult runScript(ScriptId scriptId, ExecutionContextId executionContextId,
      String objectGroup, Boolean silent, Boolean includeCommandLineAPI, Boolean returnByValue,
      Boolean generatePreview, Boolean awaitPromise) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.runScript");
    chromeRequest
        .putParams("scriptId", scriptId)
        .putParams("executionContextId", executionContextId)
        .putParams("objectGroup", objectGroup)
        .putParams("silent", silent)
        .putParams("includeCommandLineAPI", includeCommandLineAPI)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview)
        .putParams("awaitPromise", awaitPromise);
    return chromeSession.send(chromeRequest, new TypeReference(){});
  }

  /**
   * Runs script with given id in a given context.
   *
   * @param scriptId  Id of the script to run.
   * @param executionContextId [Optional] Specifies in which execution context to perform script run. If the parameter is omitted the
   * evaluation will be performed in the context of the inspected page.
   * @param objectGroup [Optional] Symbolic group name that can be used to release multiple objects.
   * @param silent [Optional] In silent mode exceptions thrown during evaluation are not reported and do not pause
   * execution. Overrides `setPauseOnException` state.
   * @param includeCommandLineAPI [Optional] Determines whether Command Line API should be available during the evaluation.
   * @param returnByValue [Optional] Whether the result is expected to be a JSON object which should be sent by value.
   * @param generatePreview [Optional] Whether preview should be generated for the result.
   * @param awaitPromise [Optional] Whether execution should `await` for resulting value and return once awaited promise is
   * resolved.
   */
  public CompletableFuture runScriptAsync(ScriptId scriptId,
      ExecutionContextId executionContextId, String objectGroup, Boolean silent,
      Boolean includeCommandLineAPI, Boolean returnByValue, Boolean generatePreview,
      Boolean awaitPromise) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.runScript");
    chromeRequest
        .putParams("scriptId", scriptId)
        .putParams("executionContextId", executionContextId)
        .putParams("objectGroup", objectGroup)
        .putParams("silent", silent)
        .putParams("includeCommandLineAPI", includeCommandLineAPI)
        .putParams("returnByValue", returnByValue)
        .putParams("generatePreview", generatePreview)
        .putParams("awaitPromise", awaitPromise);
    return chromeSession.sendAsync(chromeRequest, new TypeReference(){});
  }

  public void setCustomObjectFormatterEnabled(Boolean enabled) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.setCustomObjectFormatterEnabled");
    chromeRequest
        .putParams("enabled", enabled);
    chromeSession.send(chromeRequest);
  }

  public void setCustomObjectFormatterEnabledAsync(Boolean enabled) {
    ChromeRequest chromeRequest = new ChromeRequest("Runtime.setCustomObjectFormatterEnabled");
    chromeRequest
        .putParams("enabled", enabled);
    chromeSession.sendAsync(chromeRequest);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy