2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2017-02-15 17:49:07 +00:00
|
|
|
#include <cxxreact/ModuleRegistry.h>
|
|
|
|
#include <cxxreact/NativeModule.h>
|
|
|
|
#include <cxxreact/NativeToJsBridge.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <folly/dynamic.h>
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
class JSExecutorFactory;
|
|
|
|
|
|
|
|
struct InstanceCallback {
|
|
|
|
virtual ~InstanceCallback() {}
|
|
|
|
virtual void onBatchComplete() = 0;
|
|
|
|
virtual void incrementPendingJSCalls() = 0;
|
|
|
|
virtual void decrementPendingJSCalls() = 0;
|
|
|
|
virtual void onNativeException(const std::string& what) = 0;
|
|
|
|
virtual ExecutorToken createExecutorToken() = 0;
|
2016-05-18 19:46:01 +00:00
|
|
|
virtual void onExecutorStopped(ExecutorToken) = 0;
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Instance {
|
|
|
|
public:
|
|
|
|
~Instance();
|
|
|
|
void 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);
|
2017-01-18 16:55:46 +00:00
|
|
|
|
|
|
|
void setSourceURL(std::string sourceURL);
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
void loadScriptFromString(std::unique_ptr<const JSBigString> string, std::string sourceURL);
|
2016-09-26 23:01:42 +00:00
|
|
|
void loadScriptFromStringSync(std::unique_ptr<const JSBigString> string, std::string sourceURL);
|
2016-05-04 02:29:58 +00:00
|
|
|
void loadScriptFromFile(const std::string& filename, const std::string& sourceURL);
|
|
|
|
void loadUnbundle(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> unbundle,
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL);
|
2016-09-26 23:01:42 +00:00
|
|
|
void loadUnbundleSync(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> unbundle,
|
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL);
|
2016-05-04 02:29:58 +00:00
|
|
|
bool supportsProfiling();
|
|
|
|
void startProfiler(const std::string& title);
|
|
|
|
void stopProfiler(const std::string& title, const std::string& filename);
|
2016-05-14 00:15:06 +00:00
|
|
|
void setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue);
|
2016-10-26 10:43:05 +00:00
|
|
|
void *getJavaScriptContext();
|
2016-07-07 15:44:01 +00:00
|
|
|
void 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
|
|
|
void callJSCallback(ExecutorToken token, uint64_t callbackId, folly::dynamic&& params);
|
2016-05-18 19:46:01 +00:00
|
|
|
MethodCallResult callSerializableNativeHook(ExecutorToken token, unsigned int moduleId,
|
|
|
|
unsigned int methodId, folly::dynamic&& args);
|
2016-09-26 23:01:43 +00:00
|
|
|
// This method is experimental, and may be modified or removed.
|
|
|
|
template <typename T>
|
|
|
|
Value callFunctionSync(const std::string& module, const std::string& method, T&& args) {
|
2016-10-07 15:00:08 +00:00
|
|
|
CHECK(nativeToJsBridge_);
|
2016-09-26 23:01:43 +00:00
|
|
|
return nativeToJsBridge_->callFunctionSync(module, method, std::forward<T>(args));
|
|
|
|
}
|
2016-09-26 23:01:42 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
ExecutorToken getMainExecutorToken();
|
2016-05-26 18:07:41 +00:00
|
|
|
void handleMemoryPressureUiHidden();
|
|
|
|
void handleMemoryPressureModerate();
|
|
|
|
void handleMemoryPressureCritical();
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-19 11:43:09 +00:00
|
|
|
void callNativeModules(ExecutorToken token, folly::dynamic&& calls, bool isEndOfBatch);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<InstanceCallback> callback_;
|
|
|
|
std::unique_ptr<NativeToJsBridge> nativeToJsBridge_;
|
2016-09-26 23:01:42 +00:00
|
|
|
|
|
|
|
std::mutex m_syncMutex;
|
|
|
|
std::condition_variable m_syncCV;
|
|
|
|
bool m_syncReady = false;
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|