2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-06-23 23:49:55 +00:00
|
|
|
#include <condition_variable>
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2017-02-15 17:49:07 +00:00
|
|
|
#include <cxxreact/NativeToJsBridge.h>
|
2017-06-23 23:49:55 +00:00
|
|
|
#include <jschelpers/Value.h>
|
|
|
|
|
|
|
|
namespace folly {
|
|
|
|
struct dynamic;
|
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2017-06-23 23:49:55 +00:00
|
|
|
class JSBigString;
|
2016-05-04 02:29:58 +00:00
|
|
|
class JSExecutorFactory;
|
2017-06-23 23:49:55 +00:00
|
|
|
class JSModulesUnbundle;
|
|
|
|
class MessageQueueThread;
|
|
|
|
class ModuleRegistry;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
struct InstanceCallback {
|
|
|
|
virtual ~InstanceCallback() {}
|
|
|
|
virtual void onBatchComplete() = 0;
|
|
|
|
virtual void incrementPendingJSCalls() = 0;
|
|
|
|
virtual void decrementPendingJSCalls() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Instance {
|
|
|
|
public:
|
|
|
|
~Instance();
|
|
|
|
void initializeBridge(
|
|
|
|
std::unique_ptr<InstanceCallback> callback,
|
|
|
|
std::shared_ptr<JSExecutorFactory> jsef,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue,
|
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);
|
|
|
|
|
2017-05-24 14:27:06 +00:00
|
|
|
void loadScriptFromString(
|
|
|
|
std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL,
|
|
|
|
bool loadSynchronously);
|
2016-05-04 02:29:58 +00:00
|
|
|
void loadUnbundle(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> unbundle,
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
2017-05-24 14:27:06 +00:00
|
|
|
std::string startupScriptSourceURL,
|
|
|
|
bool loadSynchronously);
|
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();
|
2017-04-25 12:29:45 +00:00
|
|
|
void callJSFunction(std::string&& module, std::string&& method, folly::dynamic&& params);
|
|
|
|
void callJSCallback(uint64_t callbackId, folly::dynamic&& params);
|
2017-06-23 23:49:55 +00:00
|
|
|
|
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-26 18:07:41 +00:00
|
|
|
void handleMemoryPressureUiHidden();
|
|
|
|
void handleMemoryPressureModerate();
|
|
|
|
void handleMemoryPressureCritical();
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-25 12:29:45 +00:00
|
|
|
void callNativeModules(folly::dynamic&& calls, bool isEndOfBatch);
|
2017-05-24 14:27:06 +00:00
|
|
|
void loadApplication(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> unbundle,
|
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL);
|
|
|
|
void loadApplicationSync(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> unbundle,
|
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL);
|
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_;
|
2017-06-21 18:56:08 +00:00
|
|
|
std::shared_ptr<ModuleRegistry> moduleRegistry_;
|
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
|
|
|
};
|
|
|
|
|
2017-06-23 23:49:55 +00:00
|
|
|
} }
|