mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
452cb9a78a
Summary: This call is used to continue execution when the app has just been started in a "wait for debugger" mode. This is the only case in which it has an effect. Notably, it should do nothing in the following cases, which a layperson may be tempted to classify as "WaitingForDebugger": * The app was running detached and hit a 'debugger;' statement * The app is paused because of a breakpoint or hitting the Pause button * The app stopped on an instrumentation breakpoint, and expects the debugger to collect data and potentially auto-resume. Changelog: [Internal] Add Hermes support for Debugger.runIfWaitingForDebugger Reviewed By: mhorowitz Differential Revision: D21557446 fbshipit-source-id: 790cec7444ddc61908d2ef9d92e4649b535d678f
136 lines
3.0 KiB
C++
136 lines
3.0 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "SyncConnection.h"
|
|
|
|
#include <functional>
|
|
#include <stdexcept>
|
|
|
|
#include <folly/json.h>
|
|
#include <glog/logging.h>
|
|
#include <hermes/inspector/RuntimeAdapter.h>
|
|
#include <jsinspector/InspectorInterfaces.h>
|
|
|
|
namespace facebook {
|
|
namespace hermes {
|
|
namespace inspector {
|
|
namespace chrome {
|
|
|
|
using namespace std::placeholders;
|
|
using ::facebook::react::IRemoteConnection;
|
|
|
|
namespace {
|
|
|
|
std::string prettify(const std::string &str) {
|
|
try {
|
|
folly::dynamic obj = folly::parseJson(str);
|
|
return folly::toPrettyJson(obj);
|
|
} catch (...) {
|
|
// pass
|
|
}
|
|
|
|
return str;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
class SyncConnection::RemoteConnnection : public IRemoteConnection {
|
|
public:
|
|
RemoteConnnection(SyncConnection &conn) : conn_(conn) {}
|
|
|
|
void onMessage(std::string message) override {
|
|
conn_.onReply(message);
|
|
}
|
|
|
|
void onDisconnect() override {}
|
|
|
|
private:
|
|
SyncConnection &conn_;
|
|
};
|
|
|
|
SyncConnection::SyncConnection(
|
|
std::shared_ptr<HermesRuntime> runtime,
|
|
bool waitForDebugger)
|
|
: connection_(
|
|
std::make_unique<SharedRuntimeAdapter>(
|
|
runtime,
|
|
runtime->getDebugger()),
|
|
"testConn",
|
|
waitForDebugger) {
|
|
connection_.connect(std::make_unique<RemoteConnnection>(*this));
|
|
}
|
|
|
|
void SyncConnection::send(const std::string &str) {
|
|
LOG(INFO) << "SyncConnection::send sending " << str;
|
|
|
|
connection_.sendMessage(str);
|
|
}
|
|
|
|
void SyncConnection::waitForResponse(
|
|
folly::Function<void(const std::string &)> handler,
|
|
std::chrono::milliseconds timeout) {
|
|
std::string reply;
|
|
|
|
{
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
bool success = hasReply_.wait_for(
|
|
lock, timeout, [this]() -> bool { return !replies_.empty(); });
|
|
|
|
if (!success) {
|
|
throw std::runtime_error("timed out waiting for reply");
|
|
}
|
|
|
|
reply = std::move(replies_.front());
|
|
replies_.pop();
|
|
}
|
|
|
|
handler(reply);
|
|
}
|
|
|
|
void SyncConnection::waitForNotification(
|
|
folly::Function<void(const std::string &)> handler,
|
|
std::chrono::milliseconds timeout) {
|
|
std::string notification;
|
|
|
|
{
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
bool success = hasNotification_.wait_for(
|
|
lock, timeout, [this]() -> bool { return !notifications_.empty(); });
|
|
|
|
if (!success) {
|
|
throw std::runtime_error("timed out waiting for notification");
|
|
}
|
|
|
|
notification = std::move(notifications_.front());
|
|
notifications_.pop();
|
|
}
|
|
|
|
handler(notification);
|
|
}
|
|
|
|
void SyncConnection::onReply(const std::string &message) {
|
|
LOG(INFO) << "SyncConnection::onReply got message: " << prettify(message);
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
folly::dynamic obj = folly::parseJson(message);
|
|
if (obj.count("id")) {
|
|
replies_.push(message);
|
|
hasReply_.notify_one();
|
|
} else {
|
|
notifications_.push(message);
|
|
hasNotification_.notify_one();
|
|
}
|
|
}
|
|
|
|
} // namespace chrome
|
|
} // namespace inspector
|
|
} // namespace hermes
|
|
} // namespace facebook
|