2016-05-18 19:46:01 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#include "NativeToJsBridge.h"
|
|
|
|
|
|
|
|
#include <folly/json.h>
|
|
|
|
#include <folly/Memory.h>
|
|
|
|
#include <folly/MoveWrapper.h>
|
|
|
|
|
|
|
|
#include "Instance.h"
|
2017-06-23 23:49:55 +00:00
|
|
|
#include "JSBigString.h"
|
2016-05-18 19:46:01 +00:00
|
|
|
#include "SystraceSection.h"
|
2017-06-23 23:49:55 +00:00
|
|
|
#include "MethodCall.h"
|
|
|
|
#include "MessageQueueThread.h"
|
2017-09-22 16:56:50 +00:00
|
|
|
#include "RAMBundleRegistry.h"
|
2017-06-23 23:49:55 +00:00
|
|
|
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
#include <fbsystrace.h>
|
|
|
|
using fbsystrace::FbSystraceAsyncFlow;
|
|
|
|
#endif
|
2016-05-18 19:46:01 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
// This class manages calls from JS to native code.
|
|
|
|
class JsToNativeBridge : public react::ExecutorDelegate {
|
|
|
|
public:
|
2017-04-25 12:29:45 +00:00
|
|
|
JsToNativeBridge(std::shared_ptr<ModuleRegistry> registry,
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<InstanceCallback> callback)
|
2017-04-25 12:29:45 +00:00
|
|
|
: m_registry(registry)
|
2016-05-18 19:46:01 +00:00
|
|
|
, m_callback(callback) {}
|
|
|
|
|
2016-10-03 12:07:41 +00:00
|
|
|
std::shared_ptr<ModuleRegistry> getModuleRegistry() override {
|
|
|
|
return m_registry;
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void callNativeModules(
|
2016-09-19 11:43:09 +00:00
|
|
|
JSExecutor& executor, folly::dynamic&& calls, bool isEndOfBatch) override {
|
2017-03-14 22:28:53 +00:00
|
|
|
|
|
|
|
CHECK(m_registry || calls.empty()) <<
|
|
|
|
"native module calls cannot be completed with no native modules";
|
2017-03-17 13:55:37 +00:00
|
|
|
m_batchHadNativeModuleCalls = m_batchHadNativeModuleCalls || !calls.empty();
|
|
|
|
|
|
|
|
// An exception anywhere in here stops processing of the batch. This
|
|
|
|
// was the behavior of the Android bridge, and since exception handling
|
|
|
|
// terminates the whole bridge, there's not much point in continuing.
|
|
|
|
for (auto& call : parseMethodCalls(std::move(calls))) {
|
2017-04-25 12:29:45 +00:00
|
|
|
m_registry->callNativeMethod(call.moduleId, call.methodId, std::move(call.arguments), call.callId);
|
2017-03-17 13:55:37 +00:00
|
|
|
}
|
|
|
|
if (isEndOfBatch) {
|
|
|
|
// onBatchComplete will be called on the native (module) queue, but
|
|
|
|
// decrementPendingJSCalls will be called sync. Be aware that the bridge may still
|
|
|
|
// be processing native calls when the birdge idle signaler fires.
|
|
|
|
if (m_batchHadNativeModuleCalls) {
|
|
|
|
m_callback->onBatchComplete();
|
|
|
|
m_batchHadNativeModuleCalls = false;
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
2017-03-17 13:55:37 +00:00
|
|
|
m_callback->decrementPendingJSCalls();
|
|
|
|
}
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MethodCallResult callSerializableNativeHook(
|
|
|
|
JSExecutor& executor, unsigned int moduleId, unsigned int methodId,
|
|
|
|
folly::dynamic&& args) override {
|
2017-04-25 12:29:45 +00:00
|
|
|
return m_registry->callSerializableNativeHook(moduleId, methodId, std::move(args));
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// These methods are always invoked from an Executor. The NativeToJsBridge
|
2017-04-25 12:29:45 +00:00
|
|
|
// keeps a reference to the executor, and when destroy() is called, the
|
|
|
|
// executor is destroyed synchronously on its queue.
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ModuleRegistry> m_registry;
|
|
|
|
std::shared_ptr<InstanceCallback> m_callback;
|
2017-03-16 14:40:54 +00:00
|
|
|
bool m_batchHadNativeModuleCalls = false;
|
2016-05-18 19:46:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NativeToJsBridge::NativeToJsBridge(
|
|
|
|
JSExecutorFactory* jsExecutorFactory,
|
|
|
|
std::shared_ptr<ModuleRegistry> registry,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue,
|
|
|
|
std::shared_ptr<InstanceCallback> callback)
|
|
|
|
: m_destroyed(std::make_shared<bool>(false))
|
2017-04-25 12:29:45 +00:00
|
|
|
, m_delegate(std::make_shared<JsToNativeBridge>(registry, callback))
|
|
|
|
, m_executor(jsExecutorFactory->createJSExecutor(m_delegate, jsQueue))
|
|
|
|
, m_executorMessageQueueThread(std::move(jsQueue)) {}
|
2016-05-18 19:46:01 +00:00
|
|
|
|
|
|
|
// This must be called on the same thread on which the constructor was called.
|
|
|
|
NativeToJsBridge::~NativeToJsBridge() {
|
|
|
|
CHECK(*m_destroyed) <<
|
|
|
|
"NativeToJsBridge::destroy() must be called before deallocating the NativeToJsBridge!";
|
|
|
|
}
|
|
|
|
|
2016-09-26 23:01:38 +00:00
|
|
|
void NativeToJsBridge::loadApplication(
|
2017-09-22 16:56:50 +00:00
|
|
|
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
2016-05-18 19:46:01 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL) {
|
|
|
|
runOnExecutorQueue(
|
2017-09-22 16:56:50 +00:00
|
|
|
[bundleRegistryWrap=folly::makeMoveWrapper(std::move(bundleRegistry)),
|
2016-05-18 19:46:01 +00:00
|
|
|
startupScript=folly::makeMoveWrapper(std::move(startupScript)),
|
|
|
|
startupScriptSourceURL=std::move(startupScriptSourceURL)]
|
|
|
|
(JSExecutor* executor) mutable {
|
2017-09-22 16:56:50 +00:00
|
|
|
auto bundleRegistry = bundleRegistryWrap.move();
|
|
|
|
if (bundleRegistry) {
|
|
|
|
executor->setBundleRegistry(std::move(bundleRegistry));
|
2016-09-26 23:01:38 +00:00
|
|
|
}
|
2016-05-18 19:46:01 +00:00
|
|
|
executor->loadApplicationScript(std::move(*startupScript),
|
|
|
|
std::move(startupScriptSourceURL));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-26 23:01:42 +00:00
|
|
|
void NativeToJsBridge::loadApplicationSync(
|
2017-09-22 16:56:50 +00:00
|
|
|
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
2016-09-26 23:01:42 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL) {
|
2017-09-22 16:56:50 +00:00
|
|
|
if (bundleRegistry) {
|
|
|
|
m_executor->setBundleRegistry(std::move(bundleRegistry));
|
2016-09-26 23:01:42 +00:00
|
|
|
}
|
2017-04-25 12:29:45 +00:00
|
|
|
m_executor->loadApplicationScript(std::move(startupScript),
|
2016-09-26 23:01:42 +00:00
|
|
|
std::move(startupScriptSourceURL));
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
void NativeToJsBridge::callFunction(
|
2016-07-07 15:44:01 +00:00
|
|
|
std::string&& module,
|
|
|
|
std::string&& method,
|
|
|
|
folly::dynamic&& arguments) {
|
2016-05-27 23:02:50 +00:00
|
|
|
int systraceCookie = -1;
|
2016-05-18 19:46:01 +00:00
|
|
|
#ifdef WITH_FBSYSTRACE
|
2016-05-27 23:02:50 +00:00
|
|
|
systraceCookie = m_systraceCookie++;
|
2016-05-18 19:46:01 +00:00
|
|
|
FbSystraceAsyncFlow::begin(
|
|
|
|
TRACE_TAG_REACT_CXX_BRIDGE,
|
2017-05-13 00:57:12 +00:00
|
|
|
"JSCall",
|
2016-05-18 19:46:01 +00:00
|
|
|
systraceCookie);
|
|
|
|
#endif
|
|
|
|
|
2017-05-13 00:57:12 +00:00
|
|
|
runOnExecutorQueue([module = std::move(module), method = std::move(method), arguments = std::move(arguments), systraceCookie]
|
2017-04-25 12:29:45 +00:00
|
|
|
(JSExecutor* executor) {
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
FbSystraceAsyncFlow::end(
|
|
|
|
TRACE_TAG_REACT_CXX_BRIDGE,
|
2017-05-13 00:57:12 +00:00
|
|
|
"JSCall",
|
2017-04-25 12:29:45 +00:00
|
|
|
systraceCookie);
|
2017-05-13 00:57:12 +00:00
|
|
|
SystraceSection s("NativeToJsBridge::callFunction", "module", module, "method", method);
|
2017-04-25 12:29:45 +00:00
|
|
|
#endif
|
|
|
|
// This is safe because we are running on the executor's thread: it won't
|
|
|
|
// destruct until after it's been unregistered (which we check above) and
|
|
|
|
// that will happen on this thread
|
|
|
|
executor->callFunction(module, method, arguments);
|
|
|
|
});
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
void NativeToJsBridge::invokeCallback(double callbackId, folly::dynamic&& arguments) {
|
2016-05-27 23:02:50 +00:00
|
|
|
int systraceCookie = -1;
|
2016-05-18 19:46:01 +00:00
|
|
|
#ifdef WITH_FBSYSTRACE
|
2016-05-27 23:02:50 +00:00
|
|
|
systraceCookie = m_systraceCookie++;
|
2016-05-18 19:46:01 +00:00
|
|
|
FbSystraceAsyncFlow::begin(
|
|
|
|
TRACE_TAG_REACT_CXX_BRIDGE,
|
|
|
|
"<callback>",
|
|
|
|
systraceCookie);
|
|
|
|
#endif
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
runOnExecutorQueue([callbackId, arguments = std::move(arguments), systraceCookie]
|
|
|
|
(JSExecutor* executor) {
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
FbSystraceAsyncFlow::end(
|
|
|
|
TRACE_TAG_REACT_CXX_BRIDGE,
|
|
|
|
"<callback>",
|
|
|
|
systraceCookie);
|
2017-05-13 00:57:12 +00:00
|
|
|
SystraceSection s("NativeToJsBridge::invokeCallback");
|
2017-04-25 12:29:45 +00:00
|
|
|
#endif
|
|
|
|
executor->invokeCallback(callbackId, arguments);
|
|
|
|
});
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 19:55:37 +00:00
|
|
|
void NativeToJsBridge::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
|
|
|
|
runOnExecutorQueue([bundleId, bundlePath] (JSExecutor* executor) {
|
|
|
|
executor->registerBundle(bundleId, bundlePath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
void NativeToJsBridge::setGlobalVariable(std::string propName,
|
|
|
|
std::unique_ptr<const JSBigString> jsonValue) {
|
2017-04-25 12:29:45 +00:00
|
|
|
runOnExecutorQueue([propName=std::move(propName), jsonValue=folly::makeMoveWrapper(std::move(jsonValue))]
|
2016-05-18 19:46:01 +00:00
|
|
|
(JSExecutor* executor) mutable {
|
|
|
|
executor->setGlobalVariable(propName, jsonValue.move());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void* NativeToJsBridge::getJavaScriptContext() {
|
|
|
|
// TODO(cjhopman): this seems unsafe unless we require that it is only called on the main js queue.
|
2017-04-25 12:29:45 +00:00
|
|
|
return m_executor->getJavaScriptContext();
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 21:19:37 +00:00
|
|
|
bool NativeToJsBridge::isInspectable() {
|
|
|
|
return m_executor->isInspectable();
|
|
|
|
}
|
|
|
|
|
2017-06-26 12:48:40 +00:00
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
void NativeToJsBridge::handleMemoryPressure(int pressureLevel) {
|
2017-04-25 12:29:45 +00:00
|
|
|
runOnExecutorQueue([=] (JSExecutor* executor) {
|
2017-06-26 12:48:40 +00:00
|
|
|
executor->handleMemoryPressure(pressureLevel);
|
2016-05-18 19:46:01 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-26 12:48:40 +00:00
|
|
|
#endif
|
2016-05-18 19:46:01 +00:00
|
|
|
|
|
|
|
void NativeToJsBridge::destroy() {
|
2016-06-03 00:05:09 +00:00
|
|
|
// All calls made through runOnExecutorQueue have an early exit if
|
|
|
|
// m_destroyed is true. Setting this before the runOnQueueSync will cause
|
|
|
|
// pending work to be cancelled and we won't have to wait for it.
|
|
|
|
*m_destroyed = true;
|
2017-04-25 12:29:45 +00:00
|
|
|
m_executorMessageQueueThread->runOnQueueSync([this] {
|
|
|
|
m_executor->destroy();
|
|
|
|
m_executorMessageQueueThread->quitSynchronous();
|
|
|
|
m_executor = nullptr;
|
2016-05-18 19:46:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
void NativeToJsBridge::runOnExecutorQueue(std::function<void(JSExecutor*)> task) {
|
2016-05-18 19:46:01 +00:00
|
|
|
if (*m_destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<bool> isDestroyed = m_destroyed;
|
2017-04-25 12:29:45 +00:00
|
|
|
m_executorMessageQueueThread->runOnQueue([this, isDestroyed, task=std::move(task)] {
|
2016-05-18 19:46:01 +00:00
|
|
|
if (*isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The executor is guaranteed to be valid for the duration of the task because:
|
|
|
|
// 1. the executor is only destroyed after it is unregistered
|
|
|
|
// 2. the executor is unregistered on this queue
|
|
|
|
// 3. we just confirmed that the executor hasn't been unregistered above
|
2017-04-25 12:29:45 +00:00
|
|
|
task(m_executor.get());
|
2016-05-18 19:46:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} }
|