PHP 7.4.33
Preview: InstanceAgent.cpp Size: 4.96 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/react-native/ReactCommon/jsinspector-modern/InstanceAgent.cpp
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include <jsinspector-modern/InstanceAgent.h>
#include "CdpJson.h"
#include "RuntimeTarget.h"

namespace facebook::react::jsinspector_modern {

InstanceAgent::InstanceAgent(
    FrontendChannel frontendChannel,
    InstanceTarget& target,
    SessionState& sessionState)
    : frontendChannel_(frontendChannel),
      target_(target),
      sessionState_(sessionState) {
  (void)target_;
}

bool InstanceAgent::handleRequest(const cdp::PreparsedRequest& req) {
  if (req.method == "Runtime.enable") {
    maybeSendExecutionContextCreatedNotification();
    maybeSendPendingConsoleMessages();
    // Fall through
  }
  if (runtimeAgent_ && runtimeAgent_->handleRequest(req)) {
    return true;
  }
  return false;
}

void InstanceAgent::setCurrentRuntime(RuntimeTarget* runtimeTarget) {
  auto previousRuntimeAgent = std::move(runtimeAgent_);
  if (runtimeTarget) {
    runtimeAgent_ = runtimeTarget->createAgent(frontendChannel_, sessionState_);
  } else {
    runtimeAgent_.reset();
  }
  if (!sessionState_.isRuntimeDomainEnabled) {
    return;
  }
  if (previousRuntimeAgent != nullptr) {
    auto& previousContext =
        previousRuntimeAgent->getExecutionContextDescription();
    folly::dynamic params =
        folly::dynamic::object("executionContextId", previousContext.id);
    if (previousContext.uniqueId.has_value()) {
      params["executionContextUniqueId"] = *previousContext.uniqueId;
    }
    frontendChannel_(
        cdp::jsonNotification("Runtime.executionContextDestroyed", params));
  }
  maybeSendExecutionContextCreatedNotification();
  maybeSendPendingConsoleMessages();
}

void InstanceAgent::maybeSendExecutionContextCreatedNotification() {
  if (runtimeAgent_ != nullptr) {
    auto& newContext = runtimeAgent_->getExecutionContextDescription();
    folly::dynamic params = folly::dynamic::object(
        "context",
        folly::dynamic::object("id", newContext.id)(
            "origin", newContext.origin)("name", newContext.name));
    if (newContext.uniqueId.has_value()) {
      params["uniqueId"] = *newContext.uniqueId;
    }
    frontendChannel_(
        cdp::jsonNotification("Runtime.executionContextCreated", params));
  }
}

void InstanceAgent::sendConsoleMessage(SimpleConsoleMessage message) {
  if (runtimeAgent_ && sessionState_.isRuntimeDomainEnabled) {
    sendConsoleMessageImmediately(std::move(message));
  } else {
    sessionState_.pendingSimpleConsoleMessages.emplace_back(std::move(message));
  }
}

static std::string consoleMessageTypeName(ConsoleAPIType type) {
  switch (type) {
    case ConsoleAPIType::kLog:
      return "log";
    case ConsoleAPIType::kDebug:
      return "debug";
    case ConsoleAPIType::kInfo:
      return "info";
    case ConsoleAPIType::kError:
      return "error";
    case ConsoleAPIType::kWarning:
      return "warning";
    case ConsoleAPIType::kDir:
      return "dir";
    case ConsoleAPIType::kDirXML:
      return "dirxml";
    case ConsoleAPIType::kTable:
      return "table";
    case ConsoleAPIType::kTrace:
      return "trace";
    case ConsoleAPIType::kStartGroup:
      return "startGroup";
    case ConsoleAPIType::kStartGroupCollapsed:
      return "startGroupCollapsed";
    case ConsoleAPIType::kEndGroup:
      return "endGroup";
    case ConsoleAPIType::kClear:
      return "clear";
    case ConsoleAPIType::kAssert:
      return "assert";
    case ConsoleAPIType::kTimeEnd:
      return "timeEnd";
    case ConsoleAPIType::kCount:
      return "count";
    default:
      assert(false && "unknown console API type");
      return "error";
  }
}

void InstanceAgent::sendConsoleMessageImmediately(
    SimpleConsoleMessage message) {
  assert(runtimeAgent_ != nullptr);
  folly::dynamic argsParam = folly::dynamic::array();
  for (auto& arg : message.args) {
    argsParam.push_back(folly::dynamic::object("type", "string")("value", arg));
  }
  frontendChannel_(cdp::jsonNotification(
      "Runtime.consoleAPICalled",
      folly::dynamic::object("type", consoleMessageTypeName(message.type))(
          "timestamp", message.timestamp)("args", std::move(argsParam))(
          "executionContextId",
          runtimeAgent_->getExecutionContextDescription().id)(
          // We use the @cdp Runtime.consoleAPICalled `context` parameter to
          // mark synthetic messages generated by the backend, i.e. not
          // originating in a real `console.*` API call.
          "context",
          runtimeAgent_->getExecutionContextDescription().name +
              "#InstanceAgent")));
}

void InstanceAgent::maybeSendPendingConsoleMessages() {
  if (runtimeAgent_ != nullptr) {
    auto messages = std::move(sessionState_.pendingSimpleConsoleMessages);
    sessionState_.pendingSimpleConsoleMessages.clear();
    for (auto& message : messages) {
      sendConsoleMessageImmediately(std::move(message));
    }
  }
}

} // namespace facebook::react::jsinspector_modern

Directory Contents

Dirs: 2 × Files: 54
Name Size Perms Modified Actions
tests DIR
- drwxr-xr-x 2025-03-28 11:04:41
Edit Download
tracing DIR
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
2.54 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.03 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.49 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.02 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.26 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.13 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.17 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.81 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.01 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
500 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
512 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.10 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.28 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.49 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.31 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.27 KB lrw-r--r-- 2025-03-28 11:04:40
Edit Download
8.55 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.77 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
445 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
8.41 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
9.62 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.21 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.65 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.07 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
4.99 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
11.52 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.71 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.80 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1015 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.45 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.96 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.08 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.63 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.72 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
14.16 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
8.33 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.51 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
544 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.77 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.22 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.74 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.25 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
9.06 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
15.77 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
4.71 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.78 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.02 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
855 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.79 KB lrw-r--r-- 2025-03-28 11:04:40
Edit Download
1.01 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.40 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.25 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.03 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.97 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).