2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#include "Instance.h"
|
|
|
|
|
|
|
|
#include "Executor.h"
|
|
|
|
#include "MethodCall.h"
|
2017-01-10 15:04:52 +00:00
|
|
|
#include "RecoverableError.h"
|
2016-05-14 00:15:06 +00:00
|
|
|
#include "SystraceSection.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include <folly/json.h>
|
|
|
|
#include <folly/Memory.h>
|
2016-05-18 19:46:01 +00:00
|
|
|
#include <folly/MoveWrapper.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2017-01-10 15:04:52 +00:00
|
|
|
using namespace detail;
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
Instance::~Instance() {
|
2016-05-18 19:46:01 +00:00
|
|
|
if (nativeToJsBridge_) {
|
|
|
|
nativeToJsBridge_->destroy();
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::initializeBridge(
|
|
|
|
std::unique_ptr<InstanceCallback> callback,
|
|
|
|
std::shared_ptr<JSExecutorFactory> jsef,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue,
|
|
|
|
std::unique_ptr<MessageQueueThread> nativeQueue,
|
2016-05-11 18:38:47 +00:00
|
|
|
std::shared_ptr<ModuleRegistry> moduleRegistry) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_ = std::move(callback);
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
jsQueue->runOnQueueSync(
|
|
|
|
[this, &jsef, moduleRegistry, jsQueue,
|
|
|
|
nativeQueue=folly::makeMoveWrapper(std::move(nativeQueue))] () mutable {
|
|
|
|
nativeToJsBridge_ = folly::make_unique<NativeToJsBridge>(
|
|
|
|
jsef.get(), moduleRegistry, jsQueue, nativeQueue.move(), callback_);
|
2016-09-26 23:01:42 +00:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_syncMutex);
|
|
|
|
m_syncReady = true;
|
|
|
|
m_syncCV.notify_all();
|
2016-05-18 19:46:01 +00:00
|
|
|
});
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
CHECK(nativeToJsBridge_);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
void Instance::loadScriptFromString(std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("reactbridge_xplat_loadScriptFromString",
|
|
|
|
"sourceURL", sourceURL);
|
2016-09-26 23:01:38 +00:00
|
|
|
nativeToJsBridge_->loadApplication(nullptr, std::move(string), std::move(sourceURL));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 23:01:42 +00:00
|
|
|
void Instance::loadScriptFromStringSync(std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL) {
|
|
|
|
std::unique_lock<std::mutex> lock(m_syncMutex);
|
|
|
|
m_syncCV.wait(lock, [this] { return m_syncReady; });
|
|
|
|
|
|
|
|
nativeToJsBridge_->loadApplicationSync(nullptr, std::move(string), std::move(sourceURL));
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
void Instance::loadScriptFromFile(const std::string& filename,
|
|
|
|
const std::string& sourceURL) {
|
2017-01-10 15:04:52 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
|
|
|
SystraceSection s("reactbridge_xplat_loadScriptFromFile",
|
|
|
|
"fileName", filename);
|
|
|
|
|
|
|
|
std::unique_ptr<const JSBigFileString> script;
|
2017-01-13 00:21:31 +00:00
|
|
|
|
|
|
|
// This function can be called in order to change the Bridge's Source URL.
|
|
|
|
// In that case, the filename will be empty, and we should not attempt to
|
|
|
|
// load it.
|
|
|
|
if (!filename.empty()) {
|
|
|
|
RecoverableError::runRethrowingAsRecoverable<std::system_error>(
|
|
|
|
[&filename, &script]() {
|
|
|
|
script = JSBigFileString::fromPath(filename);
|
|
|
|
});
|
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-01-10 15:04:52 +00:00
|
|
|
nativeToJsBridge_->loadApplication(nullptr, std::move(script), sourceURL);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 22:23:53 +00:00
|
|
|
void Instance::loadScriptFromOptimizedBundle(std::string bundlePath,
|
|
|
|
std::string sourceURL,
|
|
|
|
int flags) {
|
|
|
|
SystraceSection s("reactbridge_xplat_loadScriptFromOptimizedBundle",
|
|
|
|
"bundlePath", bundlePath);
|
|
|
|
nativeToJsBridge_->loadOptimizedApplicationScript(std::move(bundlePath),
|
|
|
|
std::move(sourceURL),
|
|
|
|
flags);
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
void Instance::loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2016-09-26 23:01:38 +00:00
|
|
|
nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(startupScript),
|
|
|
|
std::move(startupScriptSourceURL));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 23:01:42 +00:00
|
|
|
void Instance::loadUnbundleSync(std::unique_ptr<JSModulesUnbundle> unbundle,
|
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL) {
|
|
|
|
std::unique_lock<std::mutex> lock(m_syncMutex);
|
|
|
|
m_syncCV.wait(lock, [this] { return m_syncReady; });
|
|
|
|
|
|
|
|
SystraceSection s("reactbridge_xplat_loadApplicationSync");
|
|
|
|
nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(startupScript),
|
|
|
|
std::move(startupScriptSourceURL));
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
bool Instance::supportsProfiling() {
|
2016-05-18 19:46:01 +00:00
|
|
|
return nativeToJsBridge_->supportsProfiling();
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::startProfiler(const std::string& title) {
|
2016-05-18 19:46:01 +00:00
|
|
|
return nativeToJsBridge_->startProfiler(title);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::stopProfiler(const std::string& title, const std::string& filename) {
|
2016-05-18 19:46:01 +00:00
|
|
|
return nativeToJsBridge_->stopProfiler(title, filename);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
void Instance::setGlobalVariable(std::string propName,
|
|
|
|
std::unique_ptr<const JSBigString> jsonValue) {
|
2016-05-18 19:46:01 +00:00
|
|
|
nativeToJsBridge_->setGlobalVariable(std::move(propName), std::move(jsonValue));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:43:05 +00:00
|
|
|
void *Instance::getJavaScriptContext() {
|
|
|
|
return nativeToJsBridge_->getJavaScriptContext();
|
|
|
|
}
|
|
|
|
|
2016-07-07 15:44:01 +00:00
|
|
|
void Instance::callJSFunction(ExecutorToken token, std::string&& module, std::string&& method,
|
2016-07-07 15:43:56 +00:00
|
|
|
folly::dynamic&& params) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2016-07-07 15:44:01 +00:00
|
|
|
nativeToJsBridge_->callFunction(token, std::move(module), std::move(method), std::move(params));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::callJSCallback(ExecutorToken token, uint64_t callbackId, folly::dynamic&& params) {
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("<callback>");
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2016-05-18 19:46:01 +00:00
|
|
|
nativeToJsBridge_->invokeCallback(token, (double) callbackId, std::move(params));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExecutorToken Instance::getMainExecutorToken() {
|
2016-05-18 19:46:01 +00:00
|
|
|
return nativeToJsBridge_->getMainExecutorToken();
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:07:41 +00:00
|
|
|
void Instance::handleMemoryPressureUiHidden() {
|
|
|
|
nativeToJsBridge_->handleMemoryPressureUiHidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::handleMemoryPressureModerate() {
|
|
|
|
nativeToJsBridge_->handleMemoryPressureModerate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::handleMemoryPressureCritical() {
|
|
|
|
nativeToJsBridge_->handleMemoryPressureCritical();
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|