react-native/ReactCommon/inspector/LegacyDispatcher.cpp
Alexander Blom 0ac7bf29af Add Page, Runtime, Debugger agents
Summary: Runtime and Debugger agents are shipped with JSC so we reuse them. Messages are routed to them through the `LegacyDispatcher` which also handles translating their events. The Page agent emits the `Page.getResourceTree` method that the Chrome inspector expects.

Reviewed By: michalgr

Differential Revision: D4021499

fbshipit-source-id: a93d0add01cee732401f8e8db1d43205bfbd4cd4
2016-11-02 12:29:14 -07:00

55 lines
1.7 KiB
C++

// Copyright 2004-present Facebook. All Rights Reserved.
#include "LegacyDispatcher.h"
#include "Util.h"
#include <wtf/text/CString.h>
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSLock.h>
namespace facebook {
namespace react {
using namespace Inspector;
LegacyDispatcher::FrontendChannel::FrontendChannel(std::shared_ptr<Channel> channel)
: channel_(channel) {}
bool LegacyDispatcher::FrontendChannel::sendMessageToFrontend(const WTF::String& message) {
channel_->sendMessage(toStdString(message));
return true;
}
LegacyDispatcher::LegacyDispatcher(JSC::JSGlobalObject& globalObject)
: globalObject_(globalObject) {}
void LegacyDispatcher::addAgent(std::string domain, std::unique_ptr<InspectorAgentBase> agent) {
domains_.emplace_back(std::move(domain));
agents_.append(std::move(agent));
}
void LegacyDispatcher::onConnect(std::shared_ptr<Channel> channel) {
// TODO: Should perhaps only create this once and then connect each time instead
frontendChannel_ = std::make_unique<FrontendChannel>(channel);
dispatcher_.reset(InspectorBackendDispatcher::create(frontendChannel_.get()).leakRef());
auto messageHandler = [this](std::string message, int, const std::string&, folly::dynamic) {
JSC::JSLockHolder lock(globalObject_.globalExec());
dispatcher_->dispatch(message.c_str());
};
for (auto& domain : domains_) {
channel->registerDomain(domain, messageHandler);
}
agents_.didCreateFrontendAndBackend(frontendChannel_.get(), dispatcher_.get());
}
void LegacyDispatcher::onDisconnect() {
// TODO: Perhaps support InspectedTargetDestroyed
agents_.willDestroyFrontendAndBackend(InspectorDisconnectReason::InspectorDestroyed);
}
}
}